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

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

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

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

相关推荐

炫酷的计时器效果Canvas绘图与动画

-----------------------------------------华丽的分割线-----------------------------------------------------...

康托尔集合的绘制及其Python绘制(康托尔集合论的概括原则是什么)

康托尔三分集(Cantorternaryset)是数学中一个著名的分形例子,由德国数学家格奥尔格·康托尔在1883年引入。它通过不断去掉线段的中间三分之一部分,重复这个过程得到的一个分形集合。康托...

一文带你搞懂JS实现压缩图片(js 压缩图片)

作者:wuwhs转发链接:https://segmentfault.com/a/1190000023486410前言公司的移动端业务需要在用户上传图片是由前端压缩图片大小,再上传到服务器,这样可以减...

数据可视化—Echarts图表应用(数据可视化图表类型)

ECharts是一款由百度前端技术部开发的,基于Javascript的数据可视化图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表。使用JavaScript实现开源的可视化库,可以流畅的...

ThreeJS中三维世界坐标转换成二维屏幕坐标

Threejs全称是“Javascript3Dlibrary”。WebGL则是openGL在浏览器上的一个实现。Threejs对WebGL进行了封装,让前端开发人员在不需要掌握很多数学知识和绘图知...

鸿蒙开源第三方件组件——加载动画库

前言基于安卓平台的加载动画库AVLoadingIndicatorView(https://github.com/81813780/AVLoadingIndicatorView),实现了鸿蒙化迁移和重构...

canvas实现下雪背景图(canvas绘制背景图)

canvas下雪背景html+css+js实现:1.定义标签:<h1>北极光之夜。</h1><divclass="bg"></...

用canvas画简单的“我的世界”人物头像

前言:花了4天半终于看完了《HeadFirstHTML5》,这本书的学习给我最大的感受就是,自己知识的浅薄,还有非常多非常棒的技术在等着我呢。[熊本表情]扶朕起来,朕还能学!H5新增标签里面最喜欢...

Manim-基础图形之点(什么叫图形基点)

制作数学演示视频时需要用到各类的集合图形,manim中内置了一些列的图形,本篇就从最简单的点讲起。点作为manim中最简单图形,也是其他所有图形的基,所有图形的绘制都是靠点来定位。manim中的点主...

一起学 WebGL:坐标系(坐标系格式)

大家好,我是前端西瓜哥,今天我们来学习WebGL。WebGL的世界坐标系是三维的。默认使用笛卡尔坐标系的右手坐标系,满足右手定则,即x轴向右,y轴向上,z轴向着观察者,原点位于画布中心。然...

漫画 欣赏 - 聖鬥士星矢 THE.LOST.CANVAS 冥王神話 24

《圣斗士星矢THELOSTCANVAS冥王神话》改编自车田正美原作的漫画《圣斗士星矢》,由车田正美原作、手代木史织作画。其外传《圣斗士星矢THELOSTCANVAS冥王神话外传》则在《...

漫画 欣赏 - 聖鬥士星矢 THE.LOST.CANVAS 冥王神話 25 - 完结篇

《圣斗士星矢THELOSTCANVAS冥王神话》改编自车田正美原作的漫画《圣斗士星矢》,由车田正美原作、手代木史织作画。其外传《圣斗士星矢THELOSTCANVAS冥王神话外传》则在《...

Eric Fischl 名画录(eric tucker画家)

艾瑞克费舍尔(EricFischl,1948——),是美国新表现主义画家,当代国际画坛一位十分活跃的人物,在国际上享有很高的知名度。作为20世纪美国第6次经济衰退时期本土第一个伟大画家艾瑞克·费舍尔...

canvas绘画板的实现(canvas画布)

新项目有一个需求:客户需要在订单确认的时候签名。第一反应就是用html的canvas实现,同事一起商量了下,canvas有三个制约:canvas必须要用鼠标,签名会很难看;手机端webapp怎么实现...

Python程序开发之简单小程序实例(9)利用Canvas绘制图形和文字

Python程序开发之简单小程序实例(9)利用Canvas绘制图形和文字一、项目功能利用Tkinter组件中的Canvas绘制图形和文字。二、项目分析要在窗体中绘制图形和文字,需先导入Tkinter组...