1 什么是Tool
AI是个超级学霸,但有个毛病——像个书呆子,只会动嘴皮子!你让它“算个996乘886”,它表面淡定,背地里CPU都快烧了,最后憋出一句:“亲,答案是…大概…100万左右吧?”(实际是882,456!)这时候你就想抽它:“要你何用?!”
Tool登场! 这就是给AI大佬配的“外挂工具箱”!直接甩给它一个计算器Tool,下次遇到数学题,AI立马翘起二郎腿,嚣张地一挥手:“这种小事,交给我的小弟计算器!” 两秒出结果,还自带装逼特效。
Tool的好处简单粗暴:
1. 让AI从嘴炮王者变成动手达人
:自己算数、自己查资料、自己翻数据库,甚至能帮你点外卖!(如果接个外卖API的Tool,它真能!)
2. 专业的事交给专业的工具
:就像让米其林大厨不用自己种菜,AI不用苦哈哈地背百科全书,缺啥直接摇人(工具)!
3. 防止AI一本正经地胡说八道
:遇到不懂的,不再硬着头皮瞎编,而是掏出Tool理直气壮:“这位施主,等我请个外援!”
说白了,Tool就是AI的“钞能力”——没有Tool的AI像条虫,有了Tool的AI直接成龙!
2 使用已经集成的第三方工具
接下来我们将使用arxiv对论文内容进行总结
from langchain.chat_models import init_chat_model
from langchain_community.tools import ArxivQueryRun
from langchain_core.messages import HumanMessage
# 1 构建查询工具
arxiv_tool = ArxivQueryRun()
# 2. 构建llm
API_KEY = "sk-xxx"
llm = init_chat_model(model_provider="deepseek", model="deepseek-chat", api_key=API_KEY)
llm_with_tool = llm.bind_tools([arxiv_tool])
# 3. 尝试总结论文
messages = [HumanMessage("1606.04080这篇论文的标题是什么?")]
response = llm_with_tool.invoke("1606.04080这篇论文的标题是什么?")
messages.append(response)
# 4. 调用工具,并将工具的返回message加入列表
for tool_call in response.tool_calls:
tool_msg = arxiv_tool.invoke(tool_call)
messages.append(tool_msg)
# 5. 由模型给出最终答案
print(llm.invoke(messages).content)
- 学术小精灵(Arxiv工具)就像外卖跑腿,帮你把论文从学术殿堂"外卖"过来
- 语言模型是烧烤师傅,把生肉论文撒上"注意力调料"做成美味小串
- 整个过程就像后厨流水线:你点单→跑腿取货→师傅加工→直接上菜
输出内容如下:
1. 以下为模型入参message
[HumanMessage(content='1606.04080这篇论文的标题是什么?', additional_kwargs={}, response_metadata={}), AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_0_9cc8fd41-5cba-4817-b272-21f79f91bece', 'function': {'arguments': '{"query":"1606.04080"}', 'name': 'arxiv'}, 'type': 'function', 'index': 0}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 21, 'prompt_tokens': 163, 'total_tokens': 184, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 128}, 'prompt_cache_hit_tokens': 128, 'prompt_cache_miss_tokens': 35}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_3a5770e1b4', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-1e675698-d1b9-4b76-b031-3bfa1869a12c-0', tool_calls=[{'name': 'arxiv', 'args': {'query': '1606.04080'}, 'id': 'call_0_9cc8fd41-5cba-4817-b272-21f79f91bece', 'type': 'tool_call'}], usage_metadata={'input_tokens': 163, 'output_tokens': 21, 'total_tokens': 184, 'input_token_details': {'cache_read': 128}, 'output_token_details': {}}), ToolMessage(content='Published: 2017-12-29\nTitle: Matching Networks for One Shot Learning\nAuthors: Oriol Vinyals, Charles Blundell, Timothy Lillicrap, Koray Kavukcuoglu, Daan Wierstra\nSummary: Learning from a few examples remains a key challenge in machine learning.\nDespite recent advances in important domains such as vision and language, the\nstandard supervised deep learning paradigm does not offer a satisfactory\nsolution for learning new concepts rapidly from little data. In this work, we\nemploy ideas from metric learning based on deep neural features and from recent\nadvances that augment neural networks with external memories. Our framework\nlearns a network that maps a small labelled support set and an unlabelled\nexample to its label, obviating the need for fine-tuning to adapt to new class\ntypes. We then define one-shot learning problems on vision (using Omniglot,\nImageNet) and language tasks. Our algorithm improves one-shot accuracy on\nImageNet from 87.6% to 93.2% and from 88.0% to 93.8% on Omniglot compared to\ncompeting approaches. We also demonstrate the usefulness of the same model on\nlanguage modeling by introducing a one-shot task on the Penn Treebank.', name='arxiv', tool_call_id='call_0_9cc8fd41-5cba-4817-b272-21f79f91bece')]
2. 以下为模型回复
论文标题是 **"Matching Networks for One Shot Learning"**。这篇论文由 Oriol Vinyals、Charles Blundell、Timothy Lillicrap、Koray Kavukcuoglu 和 Daan Wierstra 撰写,发表于 2017 年 12 月 29 日。论文提出了一种基于深度神经特征和外部记忆增强的框架,用于解决小样本学习问题,并在视觉和语言任务上展示了其有效性。
由输出内容可知,如果需要模型基于tool的回复进行回答,需要将tool的返回再次传递给LLM,如果需要自动调用 tool 及 组织消息体,需要用到Agent类似的组件。langchain最新版中已经废弃Agent相关内容,转移到LangGraph中实现,此处不再赘述。
实例中使用的论文地址:https://arxiv.org/pdf/1606.04080
更多工具请参考官网:Tools | 🦜️🔗 LangChain
3 自定义工具
接下来,我们自定义一个加法工具,由AI进行调用
from langchain.chat_models import init_chat_model
from langchain.tools import tool
from langchain_core.messages import HumanMessage
# 加法器
@tool
def add_tool(a: int, b: int) -> int:
"""add two numbers."""
return a + b
# 组装工具
API_KEY = "sk-xxx"
llm = init_chat_model(model_provider="deepseek", model="deepseek-chat", api_key=API_KEY)
llm_with_tool = llm.bind_tools([add_tool])
# 组装调用工具内容
messages = [HumanMessage("请告诉我,99加1等于几?使用工具计算后请回复我")]
invoke_tool_response = llm_with_tool.invoke(messages)
messages.append(invoke_tool_response)
for tool_call in invoke_tool_response.tool_calls:
selected_tool = {"add_tool": add_tool}[tool_call["name"].lower()]
tool_msg = selected_tool.invoke(tool_call)
messages.append(tool_msg)
# 实战演示
print(llm.invoke(messages).content)
输出内容如下:
99 加 1 等于 **100**。