Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Websearch] change context schema #944

Merged
merged 2 commits into from Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/lib/server/preprocessMessages.ts
Expand Up @@ -11,8 +11,14 @@ export async function preprocessMessages(
): Promise<Message[]> {
return await Promise.all(
structuredClone(messages).map(async (message, idx) => {
const webSearchContext = webSearch?.contextSources
.map(({ context }) => context)
.flat()
.sort((a, b) => a.idx - b.idx)
.map(({ text }) => text)
.join(" ");
// start by adding websearch to the last message
if (idx === messages.length - 1 && webSearch && webSearch.context) {
if (idx === messages.length - 1 && webSearch && webSearchContext?.trim()) {
const lastQuestion = messages.findLast((el) => el.from === "user")?.content ?? "";
const previousQuestions = messages
.filter((el) => el.from === "user")
Expand All @@ -23,7 +29,7 @@ export async function preprocessMessages(
message.content = `I searched the web using the query: ${webSearch.searchQuery}.
Today is ${currentDate} and here are the results:
=====================
${webSearch.context}
${webSearchContext}
=====================
${previousQuestions.length > 0 ? `Previous questions: \n- ${previousQuestions.join("\n- ")}` : ""}
Answer the question: ${lastQuestion}`;
Expand Down
12 changes: 6 additions & 6 deletions src/lib/server/websearch/runWebSearch.ts
Expand Up @@ -36,7 +36,6 @@ export async function runWebSearch(
prompt,
searchQuery: "",
results: [],
context: "",
contextSources: [],
createdAt: new Date(),
updatedAt: new Date(),
Expand Down Expand Up @@ -153,14 +152,15 @@ export async function runWebSearch(
const indices = await findSimilarSentences(embeddingModel, prompt, texts, {
topK: topKClosestParagraphs,
});
webSearch.context = indices.map((idx) => texts[idx]).join("");

const usedSources = new Set<string>();
for (const idx of indices) {
const { source } = paragraphChunks[idx];
if (!usedSources.has(source.link)) {
usedSources.add(source.link);
webSearch.contextSources.push(source);
const contextWithId = { idx, text: texts[idx] };
const usedSource = webSearch.contextSources.find((cSource) => cSource.link === source.link);
if (usedSource) {
usedSource.context.push(contextWithId);
} else {
webSearch.contextSources.push({ ...source, context: [contextWithId] });
}
}
updatePad({
Expand Down
7 changes: 5 additions & 2 deletions src/lib/types/WebSearch.ts
Expand Up @@ -10,8 +10,7 @@ export interface WebSearch extends Timestamps {

searchQuery: string;
results: WebSearchSource[];
context: string;
contextSources: WebSearchSource[];
contextSources: WebSearchUsedSource[];
}

export interface WebSearchSource {
Expand All @@ -21,6 +20,10 @@ export interface WebSearchSource {
text?: string; // You.com provides text of webpage right away
}

export interface WebSearchUsedSource extends WebSearchSource {
context: { idx: number; text: string }[];
}

export type WebSearchMessageSources = {
type: "sources";
sources: WebSearchSource[];
Expand Down