From b48b0eeb0e5cfea3a8de9f7d2f1052119f8af9ec Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Sat, 19 Jul 2025 17:52:30 +0530 Subject: [PATCH] feat(imageSearch): use XML parsing, implement few shot prompting --- src/lib/chains/imageSearchAgent.ts | 60 ++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/src/lib/chains/imageSearchAgent.ts b/src/lib/chains/imageSearchAgent.ts index 4fd684f..993cba9 100644 --- a/src/lib/chains/imageSearchAgent.ts +++ b/src/lib/chains/imageSearchAgent.ts @@ -3,32 +3,18 @@ import { RunnableMap, RunnableLambda, } from '@langchain/core/runnables'; -import { PromptTemplate } from '@langchain/core/prompts'; +import { ChatPromptTemplate, PromptTemplate } from '@langchain/core/prompts'; import formatChatHistoryAsString from '../utils/formatHistory'; import { BaseMessage } from '@langchain/core/messages'; import { StringOutputParser } from '@langchain/core/output_parsers'; import { searchSearxng } from '../searxng'; import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; +import LineOutputParser from '../outputParsers/lineOutputParser'; const imageSearchChainPrompt = ` You will be given a conversation below and a follow up question. You need to rephrase the follow-up question so it is a standalone question that can be used by the LLM to search the web for images. You need to make sure the rephrased question agrees with the conversation and is relevant to the conversation. - -Example: -1. Follow up question: What is a cat? -Rephrased: A cat - -2. Follow up question: What is a car? How does it works? -Rephrased: Car working - -3. Follow up question: How does an AC work? -Rephrased: AC working - -Conversation: -{chat_history} - -Follow up question: {query} -Rephrased question: +Output only the rephrased query wrapped in an XML element. Do not include any explanation or additional text. `; type ImageSearchChainInput = { @@ -54,12 +40,48 @@ const createImageSearchChain = (llm: BaseChatModel) => { return input.query; }, }), - PromptTemplate.fromTemplate(imageSearchChainPrompt), + ChatPromptTemplate.fromMessages([ + ['system', imageSearchChainPrompt], + [ + "user", + "\n\n\nWhat is a cat?\n" + ], + [ + "assistant", + "A cat" + ], + + [ + "user", + "\n\n\nWhat is a car? How does it work?\n" + ], + [ + "assistant", + "Car working" + ], + [ + "user", + "\n\n\nHow does an AC work?\n" + ], + [ + "assistant", + "AC working" + ], + [ + 'user', + '{chat_history}\n\n{query}\n' + ] + ]), llm, strParser, RunnableLambda.from(async (input: string) => { - input = input.replace(/.*?<\/think>/g, ''); + const queryParser = new LineOutputParser({ + key: 'query' + }) + return (await queryParser.parse(input)) + }), + RunnableLambda.from(async (input: string) => { const res = await searchSearxng(input, { engines: ['bing images', 'google images'], });