langchain icon indicating copy to clipboard operation
langchain copied to clipboard

How to use Serp only for current events related searches and use GPT for regular info

Open Anil-matcha opened this issue 1 year ago • 7 comments

I am building an agent which answer user questions and I have plugged it with two tool, the regular llm and search tool like this

llm = OpenAI(temperature=0) search = GoogleSerperAPIWrapper()

template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate(template=template, input_variables=["question"])

llm_chain = LLMChain(llm=llm, prompt=prompt, verbose=True) tools = [ Tool( name = "Search", func=search.run, description="useful for when you need to answer questions about current events." ), Tool( name="LLM", func=llm_chain.run, description="useful for when you need to answer questions about anything in general." ) ] mrkl = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)

Now when I ask it a general question like "Who is Sachin", it still is trying to invoke the search action although the LLM can answer that question as it is not based on a recent event. How to make sure LLM answers any questions except questions on recent events to reduce the SERP usage ?

Anil-matcha avatar Mar 27 '23 05:03 Anil-matcha

I tried telling it '''You do not have access to this tool by yourself. You can only be allowed to use this tool when the human message specifically says "search online". still doesn't work..

gambastyle avatar Mar 27 '23 05:03 gambastyle

@gambastyle Tried different approaches by asking it to give priority to LLM over Serp but it always goes to Serp api first. Not sure how to deal with this

Anil-matcha avatar Mar 27 '23 06:03 Anil-matcha

As a short term solution, you can decouple the Serp tool and the LLM. In the first prompt you can add the question to the prompt and order LLM to reply I don't know if it doesn't know the answer and then whenever you receive I don't know result from LLM you can initiate the search and provide the search results in the second prompt. It may not be as smooth as the agent pipeline but you can have more control over the progress.

hkaraoguz avatar Mar 27 '23 14:03 hkaraoguz

@hkaraoguz Thanks for the support, this will definitely help as a short term hack. Meanwhile direct support for this will be great to have

Anil-matcha avatar Mar 27 '23 15:03 Anil-matcha

Hey. Has anyone one succeeded here?

oleg-yaroshevskiy avatar Apr 20 '23 16:04 oleg-yaroshevskiy

how about initialize two tools (search wrapper, and completion model with chat completiion) and create template to distinguish between these tools? i am also trying to figure out this problem

crystal0112 avatar Apr 21 '23 01:04 crystal0112

Just prepend the question with "In general," I found that it will prioritize the LLM but resort to search when needed. @Anil-matcha

robertsw22 avatar Apr 21 '23 23:04 robertsw22

Does anyone have a good solution for it? I tried to use it with ChatOpenAI and forced to use llm_chain over search but it is causing some "endless loop" for questions like "give me some productivity tips".

amrynsky avatar Jun 04 '23 21:06 amrynsky

Hi, @Anil-matcha! I'm Dosu, and I'm helping the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, you opened this issue to address the configuration of an agent to use a search tool for current events and a language model for general information. Currently, the agent is invoking the search action even for general questions, and you were seeking a solution to prioritize the language model. Some potential workarounds were suggested by gambastyle, hkaraoguz, crystal0112, and robertsw22, but it seems that a direct solution is still desired. Additionally, amrynsky mentioned encountering an issue with using llm_chain over search, causing an "endless loop" for certain questions.

Before we close this issue, could you please let us know if it is still relevant to the latest version of the LangChain repository? If it is, please comment on this issue to let us know. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you for your contribution to the LangChain repository, and please don't hesitate to reach out if you have any further questions or concerns!

dosubot[bot] avatar Sep 21 '23 16:09 dosubot[bot]