mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-06-15 14:28:32 +00:00
feat(UI): Add the search query to the response.
Also, tweaked the search retriever prompt so it gives better search queries.
This commit is contained in:
@ -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,
|
||||
}),
|
||||
})
|
||||
|
@ -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;
|
||||
|
@ -278,6 +278,12 @@ const MessageBox = ({
|
||||
Sources
|
||||
</h3>
|
||||
</div>
|
||||
{message.searchQuery && (
|
||||
<div className="mb-2 text-sm bg-light-secondary dark:bg-dark-secondary rounded-lg p-3">
|
||||
<span className="font-medium text-black/70 dark:text-white/70">Search query:</span>{' '}
|
||||
<span className="text-black dark:text-white">{message.searchQuery}</span>
|
||||
</div>
|
||||
)}
|
||||
<MessageSources sources={message.sources} />
|
||||
</div>
|
||||
)}
|
||||
|
@ -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
|
||||
</links>
|
||||
\`
|
||||
|
||||
6. Follow-up question: Get the current F1 constructor standings and return the results in a table
|
||||
Rephrased question: \`
|
||||
<question>
|
||||
Current F1 constructor standings
|
||||
</question>
|
||||
\`
|
||||
|
||||
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: \`
|
||||
<question>
|
||||
Top 10 restaurants in New York
|
||||
</question>
|
||||
\`
|
||||
|
||||
</examples>
|
||||
|
||||
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.
|
||||
|
@ -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'
|
||||
) {
|
||||
// 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: event.data.output }),
|
||||
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' &&
|
||||
|
Reference in New Issue
Block a user