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

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

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

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

相关推荐

油猴脚本:净化微博界面,聚焦核心内容

在信息过载的社交场景中,微博原生界面的推荐流、视频入口、游戏标签及无障碍图标,常分散用户注意力,影响内容浏览效率。【移除微博推荐、视频、游戏标签和无障碍图标】油猴脚本,以精准界面优化能力,为用户打造...

一个月快速学习前端开发入门与学习计划,技能也能变成钱

快速学习前端开发(HTML/CSS/JavaScript),核心是“先搭框架、再填细节、边学边练”,按以下3步走,能高效入门:“基础→实战→进阶”为逻辑,每天学习+练习时长建议2-3小时,重点围绕“...

HTML5 header标签的定义与规定_html中header标签的作用

提示:点击上方"蓝色字体"↑可以订阅噢!<header>标签定义文档的页面组合,通常是一些引导和导航信息(DOM接口、可设置属性)。<header>标签定义文档的页眉(介绍信...

CSS 电梯:纯 CSS 实现的状态机与楼层导航

点击关注公众号,“技术干货”及时达!作为一个对状态机痴迷的开发者,我常常会被一些文章点燃灵感,比如那篇《用HTML复选框和CSS打造完整状态机》。纯CSS驱动的状态机...

Vue.js源码全方位深入解析,快人一步进名企

Vue.js源码全方位深入解析,快人一步进名企来百度APP畅享高清图片//下栽のke:chaoxingit.com/512/Vue.js源码全方位深入解析,快人一步进名企随着互联网技术的不断发展,前端...

你真的会用setState吗?_setstate用法

setState函数是什么?1.将需要处理的变化塞入组建的state对象中2.告诉该组件及其子组件需要用更新的状态来重新渲染3.响应事件处理和服务端响应更新用户界面的主要方式setState经典...

React 事件机制原理_react案例

相关问题React合成事件与原生DOM事件的区别React如何注册和触发事件React事件如何解决浏览器兼容问题回答关键点React的事件处理机制可以分为两个阶段:初始化渲染时在root...

Vue 侦听器(watch 与 watchEffect)全解析1

在Vue组合式API中,当我们需要在响应式状态变化时执行“副作用”(如操作DOM、发起异步请求、修改其他状态等),watch和watchEffect是核心工具。它们能帮我们精准捕获状态...

Github 45.9K,一款助你用 HTML 实现现代Web交互神器,开发效率飙升

在前端技术日新月异的今天,React、Vue、Angular等大型框架几乎成为Web开发的标配。你是否曾经因为这些复杂的工具链、繁琐的配置和“JavaScript疲劳”而感到力不从心?有没有想...

Wijmo5 Flexgrid基础教程:动态加载右键菜单

WijmoEnterprise下载>在上文中我们介绍了使用wijmo3的menu给flexgrid做右键菜单。本文我们就在这个基础上,介绍如何动态的给flexgrid添加右键菜单。本文的右键菜...

实战 | 基于Vue语言的企业级前端开发框架Hui的应用研究

文/华夏银行乌鲁木齐分行信息科技部张文涛随着前端技术的迅速发展,开发模式也在不断演进。早期的Web页面由服务器端生成,浏览器负责展现,前后端高度耦合,导致业务逻辑与展现逻辑混杂在一起,代码可维护...

Vue渲染器解析_vue渲染函数实战

渲染器是Vue与浏览器之间的「翻译官」。它拿到一份用JavaScript对象描述的UI(虚拟DOM),然后精准地创建、更新、销毁真实DOM,同时把响应式数据和渲染函数绑定成一条自动刷新的...

如何实现 Vue 自定义组件中 hover 事件以及 v-model

在CSS中,很容易在鼠标hover时进行更改,只需:.item{background:blue;}.item:hover{background:green;}在Vue中,它...

Pydoll:更流畅可靠的浏览器自动化

无论是数据抓取,还是自动化AI助手,或是网页测试,浏览器自动化技术都是能在其中发挥关键作用的一环。然而,传统的浏览器自动化工具往往依赖于复杂的WebDriver配置,这不仅增加了使用的难度,还...

web前端tips:js的事件循环(Event Loop)

一、介绍1.什么是js的事件循环JavaScript事件循环是一种处理异步事件和回调函数的机制,它是JavaScript实现异步编程的核心。它在浏览器或Node.js环境中运行,用于管理任务队列和调...