编程技术文章分享与教程

网站首页 > 技术文章 正文

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

hmc789 2024-11-19 04:58:12 技术文章 2 ℃

在使用 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)

运行程序,记录日志如下。可以看到大模型返回的第一条信息(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 向大模型发送请求时会把工具介绍和问题同时发送,大模型根据自己的知识判断是返回答案还是返回工具,若返回工具,会把工具要求的参数一同返回。示意图如下。


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

Tags:

标签列表
最新留言