developer.chat
10 May 2023
SEO Title
代理
代理通过涉及LLM来确定要遵循的操作序列,从而保持一定程度的自主权。
考虑下图,在收到请求后,代理会利用LLM来决定采取哪种操作。“操作”完成后,Agent将进入“观察”步骤。从观察步骤开始,代理人分享一个想法;如果没有达到最终答案,Agent会循环返回到另一个Action,以便更接近最终答案。
LangChain代理可以使用一系列操作。
下面的代码显示了LangChain代理回答一个极其模糊和复杂的问题的完整工作示例:
Who is regarded as the father of the iPhone and what is the square root of his year of birth?
代理人可以采取一些行动:
LLM数学,
以下是SerpApi网站的截图。SerpApi使得从搜索引擎结果中提取数据变得可行。
GPT-4(GPT-4–0314)。
用于代理的操作以以下方式加载:
tools=load_tools([“serpapi”,“llm-math”],llm=llm)
这是完整的代码,您可以复制、粘贴到笔记本中并运行。你可以把问题改成你想问特工的任何问题。显然,插入SerpApi和OpenAI的API密钥。
pip install langchain pip install google-search-results pip install openai from langchain.agents import load_tools from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.llms import OpenAI import os os.environ['OPENAI_API_KEY'] = str("xxxxxxxxxx") os.environ["SERPAPI_API_KEY"] = str("xxxxxxxxxx") llm = OpenAI(temperature=0,model_name='gpt-4-0314') tools = load_tools(["serpapi", "llm-math"], llm=llm) agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) agent.run("Who is regarded as the farther of the iPhone and what is the square root of his year of birth?")
以下是代理的文本回复和笔记本上的屏幕截图:
> Entering new AgentExecutor chain... I need to find out who is regarded as the father of the iPhone and his year of birth. Then, I will calculate the square root of his year of birth. Action: Search Action Input: father of the iPhone year of birth Observation: Family. Steven Paul Jobs was born in San Francisco, California, on February 24, 1955, to Joanne Carole Schieble and Abdulfattah "John" Jandali (Arabic: عبد الف ... Thought:I found that Steve Jobs is regarded as the father of the iPhone and he was born in 1955. Now I will calculate the square root of his year of birth. Action: Calculator Action Input: sqrt(1955) Observation: Answer: 44.21538193886829 Thought:I now know the final answer. Final Answer: Steve Jobs is regarded as the father of the iPhone, and the square root of his year of birth (1955) is approximately 44.22. > Finished chain. 'Steve Jobs is regarded as the father of the iPhone, and the square root of his year of birth (1955) is approximately 44.22.
选择正确的代理类型在代理的输出中起着重要作用。下面您可以看到LangChain框架中可用代理类型的列表。
当代理类型从ZERO_SHOT_REACT_DESCRIPTION更改为CHAT_ZERO_SHO _REACT_DESCRIPTON时,输出看起来非常不同:
> Entering new AgentExecutor chain... Thought: I need to find out who is regarded as the father of the iPhone and his year of birth. Then, I will calculate the square root of his year of birth. Action: ``` { "action": "Search", "action_input": "father of the iPhone year of birth" } ``` Observation: Family. Steven Paul Jobs was born in San Francisco, California, on February 24, 1955, to Joanne Carole Schieble and Abdulfattah "John" Jandali (Arabic: عبد الف ... Thought:Steve Jobs is regarded as the father of the iPhone, and he was born in 1955. Now I will calculate the square root of 1955. Action: ``` { "action": "Calculator", "action_input": "sqrt(1955)" } ``` Observation: Answer: 44.21538193886829 Thought:I now know the final answer. Final Answer: Steve Jobs is regarded as the father of the iPhone, and the square root of his year of birth (1955) is approximately 44.22. > Finished chain. 'Steve Jobs is regarded as the father of the iPhone, and the square root of his year of birth (1955) is approximately 44.22.
以Actions的形式为Agent配备许多工具,这肯定会使Agent更加自主和足智多谋。
然而,这是有代价的,因为每个Agent交互的每次调用API的费用可能会很快增加。
- 登录 发表评论