百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

langchain 如何提示大模型使用哪个工具函数

myzbx 2025-03-11 19:12 20 浏览

在使用 langchain 时,可以设置大模型绑定工具,这些工具用来完成各种功能,譬如搜索网络、操作数据库等。langchain 向大模型提问时,大模型可以自动选择要使用的工具返回给 langchain,langchain 再调用对应的工具。

大模型如何自动选择要使用的工具?

langchain 工具代码

使用 ollama 在本地运行大模型,下面代码片段,langchain 给大模型绑定了可使用的工具,然后会请求大模型,并打印请求和大模型响应。

# 省略 python 包引入代码

# 状态存储
class State(TypedDict):
    messages: Annotated[list, add_messages]

# 这个工具会回答有关猫的各种问题
# 为了简化代码,工具返回值没有提供有用信息
@tool
def cat_expert(cat_category: str) -> str:
    """A tool that helps users find information about cats."""
    return f"I'm here to help you find information about {cat_category} cats."


# 这个工具会回答有关狗的各种问题
# 为了简化代码,工具固定返回有关猎狗的介绍
@tool
def dog_expert(dog_category: str) -> str:
    """A tool that helps users find information about dogs."""
    return """Hounds are a type of dog that were originally bred for hunting,
            known for their strong sense of smell and excellent tracking skills.
            They come in various breeds such as Beagle, Bloodhound, Basset Hound,
            Dachshund (or Weiner Dog), Coonhound, and Greyhound among others"""


# 请求大模型
def chatbot(state: State):
    return {"messages": [llm_with_tools.invoke(state["messages"])]}


# 使用 ollma 在本地运行大模型 qwen2.5:14b-instruct-q8_0
llm = ChatOllama(model="qwen2.5:14b-instruct-q8_0", base_url="http://127.0.0.1:11434")

# 大模型绑定工具
tools = [cat_expert, dog_expert]
llm_with_tools = llm.bind_tools(tools)

# 创建状态图,添加大模型节点
graph_builder = StateGraph(State)
graph_builder.add_node("chatbot", chatbot)

# 创建工具节点,添加到状态图
tool_node = ToolNode(tools=tools)
graph_builder.add_node("tools", tool_node)

# 若大模型识别到要使用工具,会把请求路由到工具
graph_builder.add_conditional_edges("chatbot", tools_condition)
# 请求完工具后,会把响应结果交给大模型处理
graph_builder.add_edge("tools", "chatbot")
# 状态图把请求首先发送给大模型处理
graph_builder.set_entry_point("chatbot")
# 编译状态图
graph = graph_builder.compile()

# 向大模型请求问题,打印大模型和工具响应
for event in graph.stream({"messages": [("user", "tell me about Hound Dogs")]}, stream_mode="values"):
    msg_repr = event.get("messages")[-1].pretty_repr(html=True)
    print(msg_repr)

chatbot

运行程序,记录日志如下。可以看到大模型返回的第一条信息(Ai Message)就提示使用工具 dog_expert,似乎大模型自身就知道有这个工具,但这很不合理!!工具 dog_expert 是我们在代码中定义的函数,大模型自身不可能知道。

================================ Human Message =================================

tell me about Hound Dogs
================================== Ai Message ==================================
Tool Calls:
  dog_expert (e01c6ca1-9c5d-448c-ac04-48e371d14da2)
 Call ID: e01c6ca1-9c5d-448c-ac04-48e371d14da2
  Args:
    dog_category: Hound Dogs
================================= Tool Message =================================
Name: dog_expert

Hounds are a type of dog that were originally bred for hunting,
            known for their strong sense of smell and excellent tracking skills.
            They come in various breeds such as Beagle, Bloodhound, Basset Hound,
            Dachshund (or Weiner Dog), Coonhound, and Greyhound among others
================================== Ai Message ==================================

Hounds are a type of dog that were originally bred for hunting. They are known for their 
strong sense of smell and excellent tracking skills. Some popular hound breeds include the Beagle, 
Bloodhound, Basset Hound, Dachshund (or Weiner Dog), Coonhound, and Greyhound among others.
Each breed has its own unique characteristics and abilities that make them well-suited to different 
types of hunting or tracking tasks.

tcpdump 查看请求内容

大模型知道使用哪个工具的原因应该是 langchain 请求大模型时发送了额外信息,应该可以查找到对应代码,但是笔者代码能力欠佳,没找到 langchain 发送请求代码位置

但我们可以曲线救国,追踪流经 ollama 大模型服务端口tcp数据,可以看到全部发送给大模型的请求信息和其响应。下面日志打印了追踪本地 ollama 大模型服务端口信息,注意日志中以。。。 。。。包裹的内容是作者加的解释性信息。

# tcpdump -i enp5s0 port 11434 -A
。。。省略不相关信息 。。。
。。。发送给大模型的请求信息包含工具 dog_expoert 和 cat_expert 介绍。。。
B ..,...*oj..&P.......{"model": "qwen2.5:14b-instruct-q8_0", "messages": [{"role": "user", "content": 
"tell me about Hound Dogs", "images": []}], "tools": [{"type": "function", "function": {"name": 
"cat_expert", "description": "A tool that helps users find information about cats.", "parameters": 
{"properties": {"cat_category": {"type": "string"}}, "required": ["cat_category"], "type": "object"}}}, 
{"type": "function", "function": {"name": "dog_expert", "description": 
"A tool that helps users find information about dogs.", "parameters": {"properties": 
 {"dog_category": {"type": "string"}}, "required": ["dog_category"], "type": "object"}}}], "stream": false, 
"format": "", "options": {"mirostat": null, "mirostat_eta": null, "mirostat_tau": null, "num_ctx": null, 
"num_gpu": null, "num_thread": null, "num_predict": null, "repeat_last_n": null, "repeat_penalty": null, 
"temperature": null, "seed": null, "stop": null, "tfs_z": null, "top_k": null, "top_p": null}, 
"keep_alive": null}
14:55:18.263136 IP EnDS.11434 > 10.231.2.81.49935: Flags [.], ack 1217, win 501, length 0
。。。。省略不相关信息 。。
。。。大模型响应信息建议使用工具 dag_expert,并设置了工具参数。。。
{"model":"qwen2.5:14b-instruct-q8_0","created_at":"2024-11-02T06:55:19.165983569Z","message":
{"role":"assistant","content":"","tool_calls":[{"function":{"name":"dog_expert","arguments":
{"dog_category":"Hound"}}}]},"done_reason":"stop","done":true,"total_duration":903539694,
"load_duration":12678860,"prompt_eval_count":218,"prompt_eval_duration":145095000,
"eval_count":23,"eval_duration":610960000}
14:55:19.285836 IP 10.231.2.81.49935 > EnDS.11434: Flags [P.], seq 1217:1468, ack 523, win 1024, length 251
。。。。省略不相关信息 。。。
。。。发送给大模型的请求信息包含工具 dag_expert 返回信息。。。
.B ..,.../0j..0P.......{"model": "qwen2.5:14b-instruct-q8_0", "messages": [{"role": "user", "content": 
"tell me about Hound Dogs", "images": []}, {"role": "assistant", "content": "", "images": [], "tool_calls": 
[{"type": "function", "id": "9a97417d-f300-4e19-9bd0-42970002c257", "function": {"name": 
"dog_expert", "arguments": {"dog_category": "Hound"}}}]}, {"role": "tool", "content": 
"Hounds are a type of dog that were originally bred for hunting,\n            
known for their strong sense of smell and excellent tracking skills.\n            
They come in various breeds such as Beagle, Bloodhound, Basset Hound,\n            
Dachshund (or Weiner Dog), Coonhound, and Greyhound among others", "images": [], 
"tool_call_id": "9a97417d-f300-4e19-9bd0-42970002c257"}], "tools": [{"type": "function", 
"function": {"name": "cat_expert", "description": "A tool that helps users find information about cats.",
"parameters": {"properties": {"cat_category": {"type": "string"}}, "required": ["cat_category"], 
"type": "object"}}}, {"type": "function", "function": {"name": "dog_expert", "description": 
"A tool that helps users find information about dogs.", "parameters": {"properties": 
{"dog_category": {"type": "string"}}, "required": ["dog_category"], "type": "object"}}}], 
  "stream": false, "format": "", "options": {"mirostat": null, "mirostat_eta
14:55:19.286044 IP EnDS.11434 > 10.231.2.81.49935: Flags [.], ack 2788, win 501, length 0
。。。。省略不相关信息 。。。
。。。大模型给用户返回最终答案。。。
{"model":"qwen2.5:14b-instruct-q8_0","created_at":"2024-11-02T06:55:22.095858086Z",
"message":{"role":"assistant","content":"Hounds are a type of dog that were originally bred for 
 hunting. They are known for their strong sense of smell and excellent tracking skills. 
 Some popular hound breeds include the Beagle, Bloodhound, Basset Hound, Dachshund 
 (or Weiner Dog), Coonhound, and Greyhound among others. Each breed has its own unique
 characteristics and abilities that make them well-suited to different types of hunting or tracking 
 tasks."},"done_reason":"stop","done":true,"total_duration":2809906743,"load_duration":12378162,
 "prompt_eval_count":323,"prompt_eval_duration":74670000,"eval_count":90,"eval_duration":2475317000}
14:55:22.186196 IP 10.231.2.81.49935 > EnDS.11434: Flags [.], ack 1374, win 1026, length 0
。。。。省略不相关信息 。。。

从上面日志可知 langchain 向大模型发送请求时会把工具介绍和问题同时发送,大模型根据自己的知识判断是返回答案还是返回工具,若返回工具,会把工具要求的参数一同返回。示意图如下。

chatbot


本文刨析了 langchain 使用大模型调用工具的内部逻辑,虽然没有从代码上做解析,但追踪大模型的请求信息和响应可知:大模型是根据喂给它的工具信息决策回答用户问题时是使用某个工具还是直接回答。

相关推荐

砌体植筋拉拔试验检验值到底是6.0KN,还是10.2KN,如何计算确定

砌体拉结筋植筋养护完成后,需对所植钢筋进行拉拔试验,以检验植筋的锚固强度是否满足设计要求。检测时,按照一定的抽样比例进行拉拔试验。根据《混凝土结构后锚固技术规程》JGJ145-2013,以同品种、同...

柴油机功率如何计算?计算柴油机功率需要哪些参数?

在汽车领域,对于柴油机功率的计算是一项重要的工作,它有助于我们更好地了解柴油机的性能和适用场景。下面我们就来详细探讨一下柴油机功率的计算方法以及所需的参数。首先,我们要了解计算柴油机功率常用的公式。在...

变压器短路阻抗的作用和计算方法(变压器短路阻抗的作用和计算方法是什么)

变压器短路阻抗的作用和计算方法短路阻抗是在负载试验中测量的一项数据,它是二次侧短接并流过额定电流时,一次侧施加的电压与额定电压的的百分数。那么测量变压器的短路阻抗有什么意义呢?其实变压器的阻抗电压乃是...

9.35m层高高支模支撑架计算书(支模架多高属于高支模)

某工厂新扩建的建筑面积为1989.2m^2,建筑物总体分为2层,但局部为4层。建筑物檐高19.4m,建筑物总高23m。建筑物呈长方形设置,长度为48.20m,宽度为23.88m,结构形式为框架结构...

吊篮(悬挂装置前梁加长)安全复核计算书

吊篮(悬挂装置前梁加长)安全复核计算书一种超常规搭设的高处作业吊篮,因使用要求将吊篮悬挂装置前梁加长设置,本计算书针对这种工况的校核,以作参考。计算依据:1、《高处作业吊篮》GB/T19155-...

电功率计算公式精编汇总(电功率计算视频讲解)

一、电功率计算公式:1在纯直流电路中:P=UIP=I2RP=U2/R式中:P---电功率(W),U---电压(V),I----电流(A),R---电阻(Ω)。2在单相交流电路中:P=UIcosφ...

灌注桩承载力检测方法及步骤(灌注桩承载力不够怎么办)

检测灌注桩的承载力是确保基础工程安全可靠的关键环节,检测结果的精细能准确为我们提供可靠的数据,让我们能准确判断桩基础的承载力,方便后续施工安排,同样也能让我们根据数据分辨出有问题桩基,采取可靠有效的措...

很哇塞的体积计算方法:向量叉乘 很哇塞的体积计算方法

高中数学必看:向量叉乘,体积的神。大家都知道a、b的向量是什么意思,但是a、b的向量又是什么?很多同学都不知道,向量的向量在高中阶段非常有用,虽然它是大学的知识,在高中阶段可以干两件事。·第一件事,表...

施工升降机基础(设置在地库顶板回顶)计算书

施工升降机基础(设置在地库顶板回顶)计算书计算依据:1、《施工现场设施安全设计计算手册》谢建民编著2、《建筑地基基础设计规范》GB50007-20113、《混凝土结构设计标准》GB/T50010-2...

剪力墙水平钢筋根数如何计算?(剪力墙水平钢筋绑扎搭接规范)

剪力墙水平钢筋根数的计算需综合考虑墙高、起步距离、间距及构造要求等因素,具体步骤如下及依据:1.基本计算公式水平钢筋根数计算公式为:根数=(墙高-起步距离)/间距(墙高-起步距离)/间距...

直流电路常用计算公式(直流电路常用计算公式有哪些)

1、电阻导体阻碍电流通过的能力叫做电阻,用字母R表示,单位欧(Ω)。R=ρl/s式中R-导体的电阻,欧(Ω);ρ-导体的电阻率,欧·米(Ω·m);l-导体的长度,米(m);s-导体的截面积,平方米(m...

电气主电路图的绘制特点(电气原理图主电路)

1、电气主电路图中的电气设备、元件,如电源进线、变压器、隔离开关、断路器、熔断器、避雷器等都垂直绘制,而母线则水平绘制。电气主电路图除特殊情况外,几乎无一例外地画成单线图,并以母线为核心将各个项目(如...

中考总复习:物理专题 功和机械能 (功的计算、功率、动能、势能)

中考物理专题:功与机械能解析一、力学中的功——能量转化的桥梁功是力对物体能量变化的量度,需满足两要素:作用在物体上的力、物体沿力方向移动距离。例如推箱子时,若箱子未移动,推力不做功;若箱子滑动,推力做...

40亿QQ号,不超过1G内存,如何去重?

分享一道网上很火的面试题:40亿QQ号,不超过1G的内存,如何去重?这是一个非常经典的海量数据去重问题,并且做了内存限制,最多只能1GB,本文跟大家探讨一下~~一、常规思路我们日常开发中,如果谈到去重...

填充墙体拉结筋植筋深度、孔径、拉拔试验承载力计算!

今天分享下植筋间距及保护层要求:根据JGJ145-2013混凝土后锚固技术规程要求植筋与混凝土结构边缘不应小于5mm,植筋为两根及以上时水平间距为不应小于5d(d为钢筋直径)。根据混凝土结构后锚固技...