LangChain不是每次都使用工具

问题描述 投票:0回答:1

我正在使用OpenAI构建LangChain应用程序。

我使用了 3 个工具-

  1. 两个工具是基于 pdf 的工具,阅读 pdf 并通过用户查询查找相似度分数
  2. 一个工具是基于 Excel 的工具,读取 pdf 并通过用户查询查找相似度分数

但是当我问一个应该使用 pdf 工具回答的问题时,它给了我通用答案,而不是使用任何工具。

它进入思想并认为不使用任何工具。

> Entering new AgentExecutor chain...
Thought: Do I need to use a tool? No

然后它给出一个通用答案。并不是每个问题都会发生这种情况,但有些问题会发生。

供您参考,这就是我初始化代理的方式。

system_message = """<system message>"""

memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

agent_chain = initialize_agent(
    [pdf_tool_1, pdf_tool_1, excel_tool],
    llm,
    agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
    verbose=True,
    memory=memory,
    handle_parsing_errors=True,
    return_intermediate_steps=False,
    max_iterations=5,
    agent_kwargs={
        "system_message": system_message
    }
)

奇怪的是,如果我使用

AgentType.ZERO_SHOT_REACT_DESCRIPTION
,它会按预期工作。

但是,我不能使用

ZERO_SHOT_REACT_DESCRIPTION
,因为它必须是对话代理。

如何确保它使用该工具,即使有

CONVERSATIONAL_REACT_DESCRIPTION
并且如果没有找到任何相关内容,它不应该给出任何通用答案?

python azure openai-api langchain azure-openai
1个回答
0
投票

ZERO_SHOT_REACT_DESCRIPTION 允许代理选择要执行的工具。在我的示例中,代理将在搜索或计算器之间进行选择

  search = GoogleSerperAPIWrapper(serper_api_key=serpapi_key)
#llm = OpenAI(model="gpt-3.5-turbo-instruct",openai_api_key=key,temperature=0,max_tokens=800)
llm=ChatOpenAI(model="gpt-3.5-turbo",temperature=0, openai_api_key=key)

memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

llm_math = LLMMathChain.from_llm(llm)

tools = [
    Tool(
        name="Intermediate Answer",
        func=search.run,
        description="useful for when you need to ask with search",
    ),
     Tool(
        name="Math LLM",
        func=llm_math.run,
        description="provides answers to math-related questions",
    )
]

agent=initialize_agent(tools,llm,agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,handle_parsing_errors=True,memory=memory)

agent.agent.llm_chain.prompt.template

#agent.run("How are you today?")
#agent.run("Who is the President of the United States. What is his current age divided by 2")
agent.run("compare Cerebras' approach in two different areas: Molecular Dynamics and Sparse Large Language Model (LLM) training. convert the percentage performance increase to a whole number")
© www.soinside.com 2019 - 2024. All rights reserved.