当前位置: 首页 > news >正文

昆明网站排名优化价格上海森琦阳网络科技有限公司

昆明网站排名优化价格,上海森琦阳网络科技有限公司,动态链接做网站外链图,上海网站建站建设服务文章目录 一 LangChain 自定义工具概述二创建自定义工具的三种方法2.1 方法一#xff1a;tool 装饰器2.1.1 同步方法案例2.1.2 工具描述方式1#xff1a;传参2.1.3 工具描述方式2#xff1a;文档字符串 2.2 方法二#xff1a;StructuredTool类2.2.1 StructuredTool创建自定… 文章目录 一 LangChain 自定义工具概述二创建自定义工具的三种方法2.1 方法一tool 装饰器2.1.1 同步方法案例2.1.2 工具描述方式1传参2.1.3 工具描述方式2文档字符串 2.2 方法二StructuredTool类2.2.1 StructuredTool创建自定义工具2.2.2 StructuredTool配置信息 2.3 方法三BaseTool类 三 异步工具的实现四 工具错误处理策略 一 LangChain 自定义工具概述 LangChain提供了一套工具、组件和接口可简化创建由大型语言模型(LLM)和聊天模型提供支持的应用程序的过程。在构建智能代理时为其提供合适的工具是确保其强大功能的关键LangChain作为一个支持创建和管理工具的框架能让开发者以模块化方式扩展代理的能力。 当构建代理时需要为其提供一组工具列表代理可以使用这些工具。除了调用的实际函数之外工具由几个组件组成 name(str)是必需的并且在提供给代理的工具集中必须是唯一的description(str)是可选的但建议的因为代理使用它来确定工具的使用方式return_direct(bool)默认为Falseargs_schema(PydanticBaseModel)是可选的但建议的可以用于提供更多信息或用于验证预期参数。 二创建自定义工具的三种方法 LangChain为开发人员提供了不同的方式来创建工具这些工具可以被智能代理或语言模型调用。 第一种方法是使用tool装饰器(定义自定义工具的最简单方法)第二种方法是使用StructuredTool类允许更细粒度的配置而无需过多的代码第三种方法是基于BaseTool类构造子类显式自定义工具工具定义提供最大限度的把控但需要做更多的工作。 2.1 方法一tool 装饰器 使用tool修饰符是自定义工具最简单的方法。默认情况下修饰符用函数的名字作为工具名称但传字符串作为第一个参数可以重命名工具名称。例如可以创建一个总是返回字符串LangChain的虚构搜索函数或者将两个数字相乘的乘法函数。这些函数最大的区别是第一个函数只有一个输入第二个函数需要多个输入。 2.1.1 同步方法案例 from langchain_core.tools import tool tool def multiply(a:int,b:int)-int:Multiply two numbersreturn a*bprint(multiply.name) # multiply print(multiply.description) # Multiply two numbers print(multiply.args) # {a: {title: A, type: integer}, b: {title: B, type: integer}}2.1.2 工具描述方式1传参 通过将工具名称和 JSON 参数传递给工具装饰器进行自定义 from langchain_core.tools import tool from pydantic import BaseModel, Fieldclass CalculatorInput(BaseModel):a: int Field(descriptionThe first number)b: int Field(descriptionThe second number)tool(multiplication-tool, args_schemaCalculatorInput, return_directTrue) def multiply(a: int, b: int) - int:Multiply two numbersreturn a * bprint(multiply.name) # multiplication-tool print(multiply.description) # Multiply two numbers print(multiply.args) # {a: {title: A, type: integer}, b: {title: B, type: integer}} print(multiply.return_direct) # True2.1.3 工具描述方式2文档字符串 tool 可以选择性地解析 Google 风格文档字符串并将文档字符串组件如参数描述与工具模式的相关部分关联起来。 tool(parse_docstringTrue) def foo(bar: str, baz: int) - str:The foo.Args:bar: The bar.baz: The baz.return bar foo.args_schema.schema(){description: The foo.,properties: {bar: {description: The bar.,title: Bar,type: string},baz: {description: The baz., title: Baz, type: integer}},required: [bar, baz],title: fooSchema,type: object}2.2 方法二StructuredTool类 使用StructuredTool类允许更细粒度的配置而无需过多的代码 2.2.1 StructuredTool创建自定义工具 使用StructuredTool的from_function可以创建自定义工具包含两个参数func,coroutine分别指定同步方法和异步方法。 import asyncio from langchain_core.tools import StructuredTooldef multiply(a:int,b:int)-int:Multiply two numbersreturn a*b async def a_multiply(a:int,b:int)-int:Multiply two numbersreturn a*basync def main():calculatorStructuredTool.from_function(funcmultiply,coroutinea_multiply)print(calculator.run({a:2, b:3}))print(calculator.invoke({a:2, b:3}))print(await calculator.ainvoke({a:2, b:5}))asyncio.run(main())2.2.2 StructuredTool配置信息 import asyncio from langchain_core.tools import StructuredTool from pydantic import Field, BaseModelclass CalculatorInput(BaseModel):a: int Field(descriptionThe first number)b: int Field(descriptionThe second number)#创建同步、异步包装器函数 def multiply(a:int,b:int)-int:Multiply two numbersreturn a*b async def async_multiply(a:int,b:int)-int:Multiply two numbersreturn a*basync def main():calculatorStructuredTool.from_function(funcmultiply,namecalculator,descriptionMultiply two numbers,args_schemaCalculatorInput,coroutineasync_multiply,return_directTrue)print(calculator.name)print(calculator.description)print(calculator.args)asyncio.run(main())2.3 方法三BaseTool类 接受字符串或 dict 输入的 LangChain Runnables 可以使用 as_tool 方法转换为工具该方法允许为参数指定名称、描述和其他模式信息。 # 导入所需的模块 from langchain_core.language_models import GenericFakeChatModel # 导入一个假的聊天模型用于测试 from langchain_core.output_parsers import StrOutputParser # 导入字符串输出解析器 from langchain_core.prompts import ChatPromptTemplate # 导入聊天提示模板# 创建一个聊天提示模板包含一个占位符 {answer_style} prompt ChatPromptTemplate.from_messages([(human, Hello. Please respond in the style of {answer_style}.)] )# 创建一个假的聊天模型实例并提供一个固定的响应 llm GenericFakeChatModel(messagesiter([hello matey]))# 构建一个链式调用先应用提示模板然后调用 LLM最后解析输出为字符串 chain prompt | llm | StrOutputParser()# 将 chain 转换为一个工具Tool并指定名称和描述 as_tool chain.as_tool(nameStyle responder, descriptionDescription of when to use tool. )# 打印工具的参数信息 print(as_tool.args) # {answer_style: {title: Answer Style, type: string}}三 异步工具的实现 LangChain的工具可以实现异步版本以节省计算资源和提高效率。在定义异步工具时需要使用async/await语法并确保函数和工具名准确反映功能提供详细的文档字符串。对于涉及外部API调用的工具建议使用API代理服务来提高访问的稳定性并确保异步工具充分利用并发优势避免阻塞操作。 from langchain_core.tools import tooltool async def multiply(a:int,b:int)-int:Multiply two numbersreturn a*bprint(multiply.name) # multiply print(multiply.description) # Multiply two numbers print(multiply.args) # {a: {title: A, type: integer}, b: {title: B, type: integer}}四 工具错误处理策略 为了让代理能从错误中恢复并继续执行需要特定的错误处理策略。可以抛出ToolException并定义错误处理函数设置handle_tool_error属性来定义工具错误的应对策略可以是字符串、布尔值或函数。需要注意的是仅引发ToolException是无效的需要先设置该工具的handle_tool_error因为它的默认值为False。 from langchain_core.tools import StructuredTool from langchain_core.tools import ToolExceptiondef get_weather(city: str) - str:获取给定城市的天气。raise ToolException(f错误没有名为{city}的城市。)get_weather_tool StructuredTool.from_function(funcget_weather,# 默认情况下如果函数抛出TooLException则将TooLException的message作为响应。# 如果设置为True则将返回TooLException异常文本False将会抛出TooLExceptionhandle_tool_errorTrue, )response get_weather_tool.invoke({city:天京}) print(response) # 错误没有名为天京的城市。定义异常处理 from langchain_core.tools import StructuredTool from langchain_core.tools import ToolExceptiondef get_weather(city: str) - str:获取给定城市的天气。raise ToolException(f错误没有名为{city}的城市。)get_weather_tool StructuredTool.from_function(funcget_weather,handle_tool_error没有找到这个城市, )response get_weather_tool.invoke({city:天京}) print(response) # 没有找到这个城市异常个性化处理 from langchain_core.tools import StructuredTool from langchain_core.tools import ToolExceptiondef get_weather(city: str) - str:获取给定城市的天气。raise ToolException(f错误没有名为{city}的城市。)def _handle_tool_error(error: ToolException) - str:处理工具错误。return f工具执行期间发生以下错误{error.args[0]}get_weather_tool StructuredTool.from_function(funcget_weather,handle_tool_error_handle_tool_error )response get_weather_tool.invoke({city:天京}) print(response) # 工具执行期间发生以下错误错误没有名为天京的城市。
http://www.zqtcl.cn/news/571688/

相关文章:

  • 企业网站免费源码装修公司需要多少钱
  • 沈阳建设网站费用群晖wordpress打不开
  • jsp网站开发 pdf建设植绒衣架网站
  • 做网站接单的网站做外贸网站价位
  • 金融商城快捷申请网站模板下载汕头网站建设和运营
  • 网站建设网站备案所需资料请兼职做企业网站设计怎么收费
  • 电脑配件经营网站的建设论文邯郸市环保局网站建设项目环境
  • 那些网站可以做反链免费游戏不用登录直接玩
  • 安徽网站建设的基本步骤接外贸订单的平台
  • 那些网站可以找得到做货代的广东企业微信网站开发
  • 海宁市建设局官方网站6哔哩哔哩网页版官网在线观看
  • 泉州网站建设轩奇网讯韩国美容网站模板
  • 培训好吗网站建设wordpress手游
  • 元典科技网站建设网页设计制作图片页面
  • 网站设置什么意思无代码搭建平台
  • 织梦做的网站后台登录做网站购买域名
  • 哈尔滨网站关键词优化排名合江做网站
  • 手机网站自动适配旅游网络营销方案
  • 敦化网站开发黔东南购物网站开发设计
  • 建设一个网站 需要提供什么如何免费推广自己的网站
  • 佛山企业网站制作公司中国互联网企业100强榜单
  • 买了域名就可以做网站怎么创造游戏软件
  • 广东广州电脑个人建站徐州网站排名公司
  • 网站优化 流量做网站对企业有什么好处
  • 建设机械网站制作人工智能工程师月薪多少
  • wordpress 百度站长沈阳app开发公司哪家好
  • 做网站平台公司网站建设硬件环境
  • 可视化编辑建站平台新密市城乡建设局网站
  • 电子商务的网站的建设内容wordpress主题 微软
  • 什么软件可以做动画视频网站网站的按钮怎么做 视频