做代理记账网站,网站建设与管理 管理课程,google免费入口,做一个公司网址多少钱什么是LangChain工具#xff1f;在LangChain框架中#xff0c;工具#xff08;Tools#xff09;是让大语言模型#xff08;LLM#xff09;能够与外部系统、API或函数进行交互的核心组件。通过工具#xff0c;LLM可以执行计算、查询数据、调用外部服务等操作#xff0c;…什么是LangChain工具在LangChain框架中工具Tools是让大语言模型LLM能够与外部系统、API或函数进行交互的核心组件。通过工具LLM可以执行计算、查询数据、调用外部服务等操作从而突破纯文本生成的限制实现更强大的功能。2.1 创建工具2.1.1 使用tool装饰器创建工具tool装饰器是LangChain中创建工具的最简单方法。它通过装饰Python函数来定义工具自动从函数名、类型提示和文档字符串中提取工具的相关属性。from langchain_core.tools import tool tool def multiply(a: int, b: int) - int: Multiply two integers. Args: a: First integer b: Second integer return a * b print(multiply.invoke({a: 2, b: 3})) # 输出6 print(multiply.name) # 输出multiply print(multiply.description) # 输出Multiply two integers... print(multiply.args) # 输出{a: {title: A, type: integer}, b: {title: B, type: integer}}关键特性默认使用函数名称作为工具名称使用函数的文档字符串作为工具描述函数名、类型提示和文档字符串都是传递给工具Schema的重要组成部分什么是SchemaSchema是描述数据结构的声明格式用于自动验证数据。在JSON Schema中我们定义数据的类型、字段、格式等约束条件。示例对比// 示例1简单结构 { name: 张小红, birthday: 1732年2月22日, address: 陕西省西安市雁塔区 } // 示例2结构化结构 { surname: 王, given_name: 刚, birthday: 1732-02-22, address: { district: 萧山区, city: 杭州市, province: 浙江省, country: 中国 } }对应的JSON Schema{ type: object, properties: { surname: { type: string }, given_name: { type: string }, birthday: { type: string, format: date }, address: { type: object, properties: { district: { type: string }, city: { type: string }, province: { type: string }, country: { type: string } } } } }Google风格文档字符串Google风格是Python文档字符串的一种写作规范使用Args:、Returns:等关键字参数描述简洁明了def fetch_data(url, retries3): 从给定的URL获取数据。 Args: url (str): 要从中获取数据的URL。 retries (int, optional): 失败时重试的次数。默认为3。 Returns: dict: 从URL解析的JSON响应。 # 函数实现2.1.1.1 模式1依赖Pydantic类当函数没有提供文档字符串时可以使用Pydantic类来定义工具Schemafrom langchain_core.tools import tool from pydantic import BaseModel, Field class AddInput(BaseModel): Add two integers. a: int Field(..., descriptionFirst integer) b: int Field(..., descriptionSecond integer) class MultiplyInput(BaseModel): Multiply two integers. a: int Field(..., descriptionFirst integer) b: int Field(..., descriptionSecond integer) tool(args_schemaAddInput) def add(a: int, b: int) - int: # 未提供描述 return a b tool(args_schemaMultiplyInput) def multiply(a: int, b: int) - int: # 未提供描述 return a * b注意 除非提供默认值否则所有字段都是required。2.1.1.2 模式2依赖Annotated使用Annotated和文档字符串传递工具Schemafrom langchain_core.tools import tool from typing_extensions import Annotated tool def add( a: Annotated[int, ..., First integer], b: Annotated[int, ..., Second integer] ) - int: Add two integers. return a b tool def multiply( a: Annotated[int, ..., First integer], b: Annotated[int, ..., Second integer] ) - int: Multiply two integers. return a * b2.1.2 使用StructuredTool类创建工具StructuredTool.from_function类方法通过给定的函数来创建并返回一个工具from langchain_core.tools import StructuredTool def multiply(a: int, b: int) - int: Multiply two numbers. return a * b calculator_tool StructuredTool.from_function(funcmultiply) print(calculator_tool.invoke({a: 2, b: 3})) # 输出6关键参数说明func要设置的函数name工具名称默认为函数名称description工具描述默认为函数文档字符串args_schema工具输入参数的schemaresponse_format工具响应格式默认为content2.1.2.2 加入配置依赖Pydantic类from langchain_core.tools import StructuredTool from pydantic import BaseModel, Field class CalculatorInput(BaseModel): a: int Field(descriptionfirst number) b: int Field(descriptionsecond number) def multiply(a: int, b: int) - int: return a * b calculator_tool StructuredTool.from_function( funcmultiply, nameCalculator, description两数相乘, args_schemaCalculatorInput, ) print(calculator_tool.invoke({a: 2, b: 3})) # 输出6 print(calculator_tool.name) # 输出Calculator print(calculator_tool.description) # 输出两数相乘 print(calculator_tool.args) # 输出{a: {description: first number, title: A, type: integer}, b: {description: second number, title: B, type: integer}}2.1.2.3 加入response_format配置当需要区分消息内容content和其他工件artifact时可以使用response_formatcontent_and_artifactfrom langchain_core.tools import StructuredTool from pydantic import BaseModel, Field from typing import List, Tuple class CalculatorInput(BaseModel): a: int Field(descriptionfirst number) b: int Field(descriptionsecond number) def multiply(a: int, b: int) - Tuple[str, List[int]]: nums [a, b] content f{nums}相乘的结果是{a * b} return content, nums calculator_tool StructuredTool.from_function( funcmultiply, nameCalculator, description两数相乘, args_schemaCalculatorInput, response_formatcontent_and_artifact ) # 直接调用工具只返回content print(calculator_tool.invoke({a: 2, b: 3})) # 输出[2, 3]相乘的结果是6 # 模拟大模型调用返回ToolMessage print(calculator_tool.invoke({ name: Calculator, args: {a: 2, b: 3}, id: 123, # 必须工具调用标识符 type: tool_call, # 必须 }))输出结果content[2, 3]相乘的结果是6 nameCalculator tool_call_id123 artifact[2, 3]artifact的作用保存原始结构化数据供链中后续组件使用不适合直接塞给大模型可用于日志记录、数据分析、自定义处理等场景2.2 绑定工具使用聊天模型的.bind_tools()方法将工具绑定到模型from langchain_openai import ChatOpenAI # 定义大模型 model ChatOpenAI(modelgpt-4o-mini) # 绑定工具返回一个Runnable实例 tools [add, multiply] model_with_tools model.bind_tools(tools)bind_tools()方法参数tools绑定的工具定义列表tool_choice要求模型调用哪个工具tool_name调用指定工具auto自动选择工具none不调用工具any或required或True强制调用至少一个工具strict是否严格验证输入输出parallel_tool_calls是否允许并行工具调用2.3 工具调用使用绑定的Runnable实例调用.invoke()方法完成工具调用from langchain_openai import ChatOpenAI from langchain_core.messages import HumanMessage from langchain_core.tools import tool from typing_extensions import Annotated # 定义大模型 model ChatOpenAI(modelgpt-4o-mini) tool def add( a: Annotated[int, ..., First integer], b: Annotated[int, ..., Second integer] ) - int: Add two integers. return a b tool def multiply( a: Annotated[int, ..., First integer], b: Annotated[int, ..., Second integer] ) - int: Multiply two integers. return a * b # 绑定工具 tools [add, multiply] model_with_tools model.bind_tools(tools) # 调用工具 result model_with_tools.invoke(9乘6等于多少) print(result)输出结果AIMessagecontent additional_kwargs{tool_calls: [{id: call_mBnxNMY7vfAOrExEETcdEC6l, function: {arguments: {a:9,b:6}, name: multiply}, type: function}], refusal: None} response_metadata{token_usage: {completion_tokens: 17, prompt_tokens: 86, total_tokens: 103, completion_tokens_details: {accepted_prediction_tokens: 0, audio_tokens: 0, reasoning_tokens: 0, rejected_prediction_tokens: 0}, prompt_tokens_details: {audio_tokens: 0, cached_tokens: 0}}, model_name: gpt-4o-mini-2024-07-18, system_fingerprint: fp_560af6e559, id: chatcmpl-C6DSvbskJtwUl6YTkPILUaiDdDuEN, service_tier: default, finish_reason: tool_calls, logprobs: None} idrun--1f43e942-d56b-4d6d-8d3f-145b7e5d0036-0 tool_calls[{name: multiply, args: {a: 9, b: 6}, id: call_mBnxNMY7vfAOrExEETcdEC6l, type: tool_call}] usage_metadata{input_tokens: 86, output_tokens: 17, total_tokens: 103, input_token_details: {audio: 0, cache_read: 0}, output_token_details: {audio: 0, reasoning: 0}}关键输出字段content消息内容additional_kwargs工具调用信息tool_calls工具调用详情名称、参数、IDfinish_reason完成原因tool_calls表示调用了工具2.4 强制模型调用工具通过设置tool_choiceany强制模型调用至少一个工具model_with_tools model.bind_tools(tools, tool_choiceany) result model_with_tools.invoke(hello world!) print(result)2.5 工具属性如果调用了工具result将具有tool_calls属性model_with_tools model.bind_tools(tools, tool_choiceany) result model_with_tools.invoke(9乘6等于多少) print(result.tool_calls)输出结果[{name: multiply, args: {a: 9, b: 6}, id: call_csFbMmYD4Dmz8yMja3ZWK1QW, type: tool_call}]总结LangChain的工具系统提供了灵活的方式来扩展大语言模型的能力。通过tool装饰器、StructuredTool类以及Pydantic模型我们可以轻松定义各种工具并将其绑定到聊天模型中。工具调用机制使得LLM能够与外部系统交互实现更复杂的应用场景。核心要点工具定义需要函数名、类型提示和文档字符串可以使用Pydantic类或Annotated来定义Schema通过bind_tools()方法将工具绑定到模型工具调用返回AIMessage包含工具调用信息可以强制模型调用工具或让模型自动选择掌握这些工具调用技术将为构建更强大的AI应用奠定坚实基础。