Langchain agents

huangapple go评论72阅读模式
英文:

Langchain agents

问题

我在使用Langchain代理与Serpapi以及本地的LLM时遇到了问题。当我连接到OpenAI时,我成功地完成了相同的操作。

我的本地LLM模型可以是MPT-7B模型或30B_Lazarus,都处于文本生成模式。

我的代码如下。

from langchain.agents import load_tools
from langchain.agents import initialize_agent
import json

query = "Get Microsoft share price from the www.bloomberg.com and include the url where you got this information."

llm = HuggingFacePipeline(pipeline=generate_text)
toolkit = load_tools(["serpapi"], llm=llm, serpapi_api_key=SERPAPI_API_KEY)
agent = initialize_agent(toolkit, llm, agent="zero-shot-react-description", verbose=True, return_intermediate_steps=True)
response = agent({"input": query})

详细消息如下。

进入新的链条...

我应该在互联网上搜索信息

操作:在互联网上搜索信息

操作输入:www.bloomberg.com

观察:在互联网上搜索信息不是有效的工具,请尝试另一个工具。

思考:我应该使用搜索引擎来查找信息

操作:在搜索引擎上搜索信息

操作输入:Microsoft股价

观察:在搜索引擎上搜索信息不是有效的工具,请尝试另一个工具。

英文:

I have a problem using Langchain agent with Serpapi together with a local LLM. I have successfully do the same thing when I connect with OpenAI.

My local LLM is either the MPT-7B model and the 30B_Lazarus on text generation mode.

My codes looks like this.

from langchain.agents import load_tools
from langchain.agents import initialize_agent
import json

query="Get Microsoft share price from the www.bloomberg.com and include the url where you got this information."

llm = HuggingFacePipeline(pipeline=generate_text)
toolkit = load_tools(["serpapi"], llm=llm, serpapi_api_key=SERPAPI_API_KEY)
agent = initialize_agent(toolkit, llm, agent="zero-shot-react-description", verbose=True, return_intermediate_steps=True)
response = agent({"input":query})

The verbose message looks like this.

Entering new chain...

I should search for the information on the internet

Action: Search for the information on the internet

Action Input: www.bloomberg.com

Observation: Search for the information on the internet is not a valid tool, try another one.

Thought: I should use a search engine to find the information

Action: Search for the information on a search engine

Action Input: Microsoft share price

Observation: Search for the information on a search engine is not a valid tool, try another one.

答案1

得分: 1

这个 agent="zero-shot-react-description" 不是适用于搜索引擎的正确代理类型。你应该使用 self_ask_with_search 代理类型:

from langchain import OpenAI, SerpAPIWrapper
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType

# 你可以定义一个不同的llm
llm = OpenAI(temperature=0)
search = SerpAPIWrapper()
tools = [
    Tool(
        name="Intermediate Answer",
        func=search.run,
        description="useful for when you need to ask with search",
    )
]

self_ask_with_search = initialize_agent(
    tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True
)
self_ask_with_search.run(
    "What is the hometown of the reigning men's U.S. Open champion?"
)
英文:

this agent="zero-shot-react-description" is not the right agent type for the search engine. you should be using self_ask_with_search agent type:

from langchain import OpenAI, SerpAPIWrapper
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType

# you can define a different llm
llm = OpenAI(temperature=0)
search = SerpAPIWrapper()
tools = [
    Tool(
        name="Intermediate Answer",
        func=search.run,
        description="useful for when you need to ask with search",
    )
]

self_ask_with_search = initialize_agent(
    tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True
)
self_ask_with_search.run(
    "What is the hometown of the reigning men's U.S. Open champion?"
)

huangapple
  • 本文由 发表于 2023年6月22日 16:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定