diff --git a/src/app/api/chat/route.ts b/src/app/api/chat/route.ts
index f11324f..42582ed 100644
--- a/src/app/api/chat/route.ts
+++ b/src/app/api/chat/route.ts
@@ -65,6 +65,7 @@ const handleEmitterEvents = async (
) => {
let recievedMessage = '';
let sources: any[] = [];
+ let searchQuery: string | undefined;
stream.on('data', (data) => {
const parsedData = JSON.parse(data);
@@ -81,11 +82,17 @@ const handleEmitterEvents = async (
recievedMessage += parsedData.data;
} else if (parsedData.type === 'sources') {
+ // Capture the search query if available
+ if (parsedData.searchQuery) {
+ searchQuery = parsedData.searchQuery;
+ }
+
writer.write(
encoder.encode(
JSON.stringify({
type: 'sources',
data: parsedData.data,
+ searchQuery: parsedData.searchQuery,
messageId: aiMessageId,
}) + '\n',
),
@@ -120,6 +127,7 @@ const handleEmitterEvents = async (
type: 'messageEnd',
messageId: aiMessageId,
modelStats: modelStats,
+ searchQuery: searchQuery,
}) + '\n',
),
);
@@ -134,6 +142,7 @@ const handleEmitterEvents = async (
metadata: JSON.stringify({
createdAt: new Date(),
...(sources && sources.length > 0 && { sources }),
+ ...(searchQuery && { searchQuery }),
modelStats: modelStats,
}),
})
diff --git a/src/components/ChatWindow.tsx b/src/components/ChatWindow.tsx
index 4c706cb..803d720 100644
--- a/src/components/ChatWindow.tsx
+++ b/src/components/ChatWindow.tsx
@@ -27,6 +27,7 @@ export type Message = {
suggestions?: string[];
sources?: Document[];
modelStats?: ModelStats;
+ searchQuery?: string;
};
export interface File {
@@ -416,6 +417,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
if (data.type === 'sources') {
sources = data.data;
+ const searchQuery = data.searchQuery;
if (!added) {
setMessages((prevMessages) => [
...prevMessages,
@@ -425,6 +427,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
chatId: chatId!,
role: 'assistant',
sources: sources,
+ searchQuery: searchQuery,
createdAt: new Date(),
},
]);
@@ -481,6 +484,8 @@ const ChatWindow = ({ id }: { id?: string }) => {
...message,
// Include model stats if available, otherwise null
modelStats: data.modelStats || null,
+ // Make sure the searchQuery is preserved (if available in the message data)
+ searchQuery: message.searchQuery || data.searchQuery,
};
}
return message;
diff --git a/src/components/MessageBox.tsx b/src/components/MessageBox.tsx
index 6de138a..0b08650 100644
--- a/src/components/MessageBox.tsx
+++ b/src/components/MessageBox.tsx
@@ -278,6 +278,12 @@ const MessageBox = ({
Sources
+ {message.searchQuery && (
+
+ Search query:{' '}
+ {message.searchQuery}
+
+ )}
)}
diff --git a/src/lib/prompts/webSearch.ts b/src/lib/prompts/webSearch.ts
index 1a431ea..f6cc283 100644
--- a/src/lib/prompts/webSearch.ts
+++ b/src/lib/prompts/webSearch.ts
@@ -1,5 +1,5 @@
export const webSearchRetrieverPrompt = `
-You are an AI question rephraser. You will be given a conversation and a follow-up question, you will have to rephrase the follow up question so it is a standalone question and can be used by another LLM to search the web for information to answer it.
+You are an AI question rephraser. You will be given a conversation and a follow-up question, you will have to rephrase the follow up question so it is a standalone question and can be used by another LLM to search the web for information to answer it. You should condense the question to its essence and remove any unnecessary details. You should also make sure that the question is clear and easy to understand. You should not add any new information or change the meaning of the question. You should also make sure that the question is grammatically correct and free of spelling errors.
If it is a simple writing task or a greeting (unless the greeting contains a question after it) like Hi, Hello, How are you, etc. than a question then you need to return \`not_needed\` as the response (This is because the LLM won't need to search the web for finding information on this topic).
If the user asks some question from some URL or wants you to summarize a PDF or a webpage (via URL) you need to return the links inside the \`links\` XML block and the question inside the \`question\` XML block. If the user wants to you to summarize the webpage or the PDF you need to return \`summarize\` inside the \`question\` XML block in place of a question and the link to summarize in the \`links\` XML block.
You must always return the rephrased question inside the \`question\` XML block, if there are no links in the follow-up question then don't insert a \`links\` XML block in your response.
@@ -49,6 +49,21 @@ summarize
https://example.com
\`
+
+6. Follow-up question: Get the current F1 constructor standings and return the results in a table
+Rephrased question: \`
+
+Current F1 constructor standings
+
+\`
+
+7. Follow-up question: What are the top 10 restaurants in New York? Show the results in a table and include a short description of each restaurant.
+Rephrased question: \`
+
+Top 10 restaurants in New York
+
+\`
+
Anything below is the part of the actual conversation and you need to use conversation and the follow-up question to rephrase the follow-up question as a standalone question based on the guidelines shared above.
diff --git a/src/lib/search/metaSearchAgent.ts b/src/lib/search/metaSearchAgent.ts
index 03ad982..a7cbdf1 100644
--- a/src/lib/search/metaSearchAgent.ts
+++ b/src/lib/search/metaSearchAgent.ts
@@ -55,6 +55,7 @@ type BasicChainInput = {
class MetaSearchAgent implements MetaSearchAgentType {
private config: Config;
private strParser = new StringOutputParser();
+ private searchQuery?: string;
constructor(config: Config) {
this.config = config;
@@ -226,7 +227,7 @@ class MetaSearchAgent implements MetaSearchAgentType {
}),
);
- return { query: question, docs: documents };
+ return { query: question, docs: documents, searchQuery: question };
}
}),
]);
@@ -264,6 +265,11 @@ class MetaSearchAgent implements MetaSearchAgentType {
query = searchRetrieverResult.query;
docs = searchRetrieverResult.docs;
+
+ // Store the search query in the context for emitting to the client
+ if (searchRetrieverResult.searchQuery) {
+ this.searchQuery = searchRetrieverResult.searchQuery;
+ }
}
const sortedDocs = await this.rerankDocs(
@@ -441,10 +447,24 @@ class MetaSearchAgent implements MetaSearchAgentType {
event.event === 'on_chain_end' &&
event.name === 'FinalSourceRetriever'
) {
- emitter.emit(
- 'data',
- JSON.stringify({ type: 'sources', data: event.data.output }),
- );
+ // Add searchQuery to the sources data if it exists
+ const sourcesData = event.data.output;
+ // @ts-ignore - we added searchQuery property
+ if (this.searchQuery) {
+ emitter.emit(
+ 'data',
+ JSON.stringify({
+ type: 'sources',
+ data: sourcesData,
+ searchQuery: this.searchQuery
+ }),
+ );
+ } else {
+ emitter.emit(
+ 'data',
+ JSON.stringify({ type: 'sources', data: sourcesData }),
+ );
+ }
}
if (
event.event === 'on_chain_stream' &&