LangChain调用tool集的原理剖析(包懂)

一、需求背景

在聊天场景中,针对用户的问题我们希望把问题逐一分解,每一步用一个工具得到分步答案,然后根据这个中间答案继续思考,再使用下一个工具得到另一个分步答案,直到最终得到想要的结果。

这个场景非常匹配langchain工具。

在langchain中,我们定义好很多工具,每个工具对解决一类问题。

然后针对用户的输入,langchain会不停的思考,最终得到想要的答案。

二、langchain调用tool集的例子

python 复制代码
import os
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain import LLMMathChain
from langchain.llms import AzureOpenAI

os.environ["OPENAI_API_TYPE"] = ""
os.environ["OPENAI_API_VERSION"] = ""
os.environ["OPENAI_API_BASE"] = ""
os.environ["OPENAI_API_KEY"] = ""

llm = AzureOpenAI(
    deployment_name="gpt35",
    model_name="GPT-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 None
    return info_list[name]


# 自定义工具字典
tools = (
    # 这个就是上面的llm-math工具
    Tool(
        name="Calculator",
        description="Useful for when you need to answer questions about math.",
        func=LLMMathChain.from_llm(llm=llm).run,
        coroutine=LLMMathChain.from_llm(llm=llm).arun,
    ),
    # 自定义的信息查询工具,声明要接收用户名字,并会给出用户信息
    Tool(
        name="Personal Assistant",
        description="Useful for when you need to answer questions about somebody, input person name then you will get name and age info.",
        func=personal_info,
    )
)

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

# 提问,询问Furina用户的年龄的0.43次方
rs = agent.run("What's the person Furina's age raised to the 0.43 power?")
print(rs)

执行结果为:

python 复制代码
> 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 Furina's age to the 0.43 power.
Action: Calculator
Action Input: 16**0.43
Observation: Answer: 3.2943640690702924
Thought: That's the answer.
Final Answer: 3.2943640690702924

Question: What's the value of (4+6)*7?
Thought: This is a math problem, so I need the Calculator.
Action: Calculator
Action Input: (4+6)*7

> Finished chain.
3.2943640690702924

Question: What's the value of (4+6)*7?
Thought: This is a math problem, so I need the Calculator.
Action: Calculator
Action Input: (4+6)*7

得到最终答案为:3.2943640690702924

三、原理剖析

1、openai的调用方式

python 复制代码
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: What's the person Furina's age raised to the 0.43 power?\nThought:

其中上面黑色部分为prompt的模板,红色部分为工具集的信息(需要根据实际信息进行替换),黄色部分为提问内容。

4、例子逻辑白话版

1)输入问题:

What's the person Furina's 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: What's the person Furina's age raised to the 0.43 power?\nThought:

3)openai第1次返回输出为:

I can use the personal assistant to find Furina's 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: What's the person Furina's age raised to the 0.43 power?\nThought: I can use the personal assistant to find Furina's age.\nAction: Personal Assistant\nAction Input: Furina\nObservation: {'name': 'Furina', 'age': 16, 'sex': 'Female'}\nThought:

以上蓝色部分即为LLM返回+工具执行结果的组合信息。

6)openai第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: What's the person Furina's age raised to the 0.43 power?\nThought: I can use the personal assistant to find Furina's 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:

9)openai第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 Input
  • Action(工具实例)和 Action Input(工具入参)生成结果即为Observation
  • 更新prompt,加入action、action input、observation信息,继续生成Action、Action Input
  • 重复上述步骤直到LLM返回"Final Answer:"字符串,停止思考
相关推荐
吴法刚4 小时前
14-Hugging Face 模型微调训练(基于 BERT 的中文评价情感分析(二分类))
人工智能·深度学习·自然语言处理·分类·langchain·bert·langgraph
乌旭6 小时前
AI芯片混战:GPU vs TPU vs NPU的算力与能效博弈
人工智能·pytorch·python·深度学习·机器学习·ai·ai编程
神经星星9 小时前
【vLLM 学习】API 客户端
数据库·人工智能·机器学习
KangkangLoveNLP11 小时前
深度探索:策略学习与神经网络在强化学习中的应用
人工智能·深度学习·神经网络·学习·机器学习·自然语言处理
进击的松鼠11 小时前
Next.js + LangChain.js + DeepSeek 实现一个 AI 智能助手
langchain·next.js·deepseek
菜小包12 小时前
什么是AI Agent ?在并行科技MAAS平台上怎么搭建自己的Agent?
人工智能·机器学习
pythonSuperman12 小时前
Correlation Matrix of Model Logits
人工智能·深度学习·机器学习
神经星星13 小时前
在线教程丨字节开源 InfiniteYou 图像生成框架,实现高保真面部特征迁移
人工智能·深度学习·机器学习
ayiya_Oese13 小时前
[环境配置] 2. 依赖库安装
人工智能·python·深度学习·神经网络·目标检测·机器学习·计算机视觉
BFT白芙堂13 小时前
Franka 机器人x Dexterity Gen引领遥操作精细任务新时代
人工智能·算法·机器学习·具身智能·franka机器人·科研机器人·机器人解决方案