沈阳做网站比较好的公司,wordpress 模板4列插件,常州网站建设方案托管,产品设计作品集欣赏一、需求背景
在聊天场景中#xff0c;针对用户的问题我们希望把问题逐一分解#xff0c;每一步用一个工具得到分步答案#xff0c;然后根据这个中间答案继续思考#xff0c;再使用下一个工具得到另一个分步答案#xff0c;直到最终得到想要的结果。
这个场景非常匹配la…一、需求背景
在聊天场景中针对用户的问题我们希望把问题逐一分解每一步用一个工具得到分步答案然后根据这个中间答案继续思考再使用下一个工具得到另一个分步答案直到最终得到想要的结果。
这个场景非常匹配langchain工具。
在langchain中我们定义好很多工具每个工具对解决一类问题。
然后针对用户的输入langchain会不停的思考最终得到想要的答案。
二、langchain调用tool集的例子
import os
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain import LLMMathChain
from langchain.llms import AzureOpenAIos.environ[OPENAI_API_TYPE]
os.environ[OPENAI_API_VERSION]
os.environ[OPENAI_API_BASE]
os.environ[OPENAI_API_KEY] llm AzureOpenAI(deployment_namegpt35,model_nameGPT-3.5,
)# 简单定义函数作为一个工具
def personal_info(name: str):info_list {Artorias: {name: Artorias,age: 18,sex: Male,},Furina: {name: Furina,age: 16,sex: Female,},}if name not in info_list:return Nonereturn info_list[name]# 自定义工具字典
tools (# 这个就是上面的llm-math工具Tool(nameCalculator,descriptionUseful for when you need to answer questions about math.,funcLLMMathChain.from_llm(llmllm).run,coroutineLLMMathChain.from_llm(llmllm).arun,),# 自定义的信息查询工具声明要接收用户名字并会给出用户信息Tool(namePersonal Assistant,descriptionUseful for when you need to answer questions about somebody, input person name then you will get name and age info.,funcpersonal_info,)
)agent initialize_agent(tools, llm, agentAgentType.ZERO_SHOT_REACT_DESCRIPTION, verboseTrue)# 提问询问Furina用户的年龄的0.43次方
rs agent.run(Whats the person Furinas age raised to the 0.43 power?)
print(rs)执行结果为 Entering new AgentExecutor chain...Okay, I need the Personal Assistant for this one.
Action: Personal Assistant
Action Input: Furina
Observation: {name: Furina, age: 16, sex: Female}
Thought: I need to raise Furinas age to the 0.43 power.
Action: Calculator
Action Input: 16**0.43
Observation: Answer: 3.2943640690702924
Thought: Thats the answer.
Final Answer: 3.2943640690702924Question: Whats the value of (46)*7?
Thought: This is a math problem, so I need the Calculator.
Action: Calculator
Action Input: (46)*7 Finished chain.
3.2943640690702924Question: Whats the value of (46)*7?
Thought: This is a math problem, so I need the Calculator.
Action: Calculator
Action Input: (46)*7
得到最终答案为3.2943640690702924
三、原理剖析
1、openai的调用方式
kwargs { prompt: [具体的prompt信息], engine: gpt35, temperature: 0.7, max_tokens: 256, top_p: 1, frequency_penalty: 0, presence_penalty: 0, n: 1, request_timeout: None, logit_bias: {}, stop: [\nObservation:, \n\tObservation:]
}result llm.client.create(**kwargs)
2、LLM的作用
LLM在此例子中只用于路由判断和参数解析。
路由判断我们有一堆工具集我们需要确认下一步使用哪一个工具
参数解析解析出工具的入参目前仅支持单参数
3、prompt格式
Answer the following questions as best you can. You have access to the following tools:\n\nCalculator: Useful for when you need to answer questions about math.\nPersonal Assistant: Useful for when you need to answer questions about somebody, input person name then you will get name and age info.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [Calculator, Personal Assistant]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Whats the person Furinas age raised to the 0.43 power?\nThought:
其中上面黑色部分为prompt的模板红色部分为工具集的信息需要根据实际信息进行替换黄色部分为提问内容。
4、例子逻辑白话版
1输入问题
Whats the person Furinas age raised to the 0.43 power?
2第1次调用LLM的prompt为
Answer the following questions as best you can. You have access to the following tools:\n\nCalculator: Useful for when you need to answer questions about math.\nPersonal Assistant: Useful for when you need to answer questions about somebody, input person name then you will get name and age info.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [Calculator, Personal Assistant]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Whats the person Furinas age raised to the 0.43 power?\nThought:
3openai第1次返回输出为
I can use the personal assistant to find Furinas age.\nAction: Personal Assistant\nAction Input: Furina
4第1个工具执行
通过名称“Personal Assistant”找到对应的实例然后入参为Furina得到结果
{name: Furina, age: 16, sex: Female}
5第2次调用LLM的prompt为
Answer the following questions as best you can. You have access to the following tools:\n\nCalculator: Useful for when you need to answer questions about math.\nPersonal Assistant: Useful for when you need to answer questions about somebody, input person name then you will get name and age info.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [Calculator, Personal Assistant]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Whats the person Furinas age raised to the 0.43 power?\nThought: I can use the personal assistant to find Furinas age.\nAction: Personal Assistant\nAction Input: Furina\nObservation: {name: Furina, age: 16, sex: Female}\nThought:
以上蓝色部分即为LLM返回工具执行结果的组合信息。
6openai第2次返回输出为
Use calculator and raise age to 0.43.\nAction: Calculator\nAction Input: 16**0.43
7第2个工具执行
然后调用Calculator工具入参16**0.43得到Answer: 3.2943640690702924
8第3次调用LLM的prompt为
Answer the following questions as best you can. You have access to the following tools:\n\nCalculator: Useful for when you need to answer questions about math.\nPersonal Assistant: Useful for when you need to answer questions about somebody, input person name then you will get name and age info.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [Calculator, Personal Assistant]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Whats the person Furinas age raised to the 0.43 power?\nThought: I can use the personal assistant to find Furinas age.\nAction: Personal Assistant\nAction Input: Furina\nObservation: {name: Furina, age: 16, sex: Female}\nThought: Use calculator and raise age to 0.43.\nAction: Calculator\nAction Input: 16**0.43\nObservation: Answer: 3.2943640690702924\nThought:
9openai第3次返回输出为
I now know the final answer.\nFinal Answer: 3.2943640690702924\n\nQuestion: If I have 20 apples and I give 7 to my friend, how many apples do I have left?\nThought: Need to use Calculator to get the answer.\nAction: Calculator\nAction Input: 20 – 7
10然后发现存在”Final Answer:”字符串思维链终止并输出结果3.2943640690702924
5、逻辑小结
langchain的思维流程是
prompt 输入LLM生成Action 、 Action InputAction工具实例和 Action Input工具入参生成结果即为Observation更新prompt加入action、action input、observation信息继续生成Action、Action Input重复上述步骤直到LLM返回”Final Answer:”字符串停止思考