GPT实战系列-LangChain的Tools函数转换器

GPT实战系列-LangChain的Tools函数转换器

LangChain

GPT实战系列-LangChain如何构建基通义千问的多工具链

GPT实战系列-构建多参数的自定义LangChain工具

GPT实战系列-通过Basetool构建自定义LangChain工具方法

GPT实战系列-一种构建LangChain自定义Tool工具的简单方法

GPT实战系列-搭建LangChain流程简单应用

GPT实战系列-简单聊聊LangChain搭建本地知识库准备

GPT实战系列-LangChain + ChatGLM3构建天气查询助手

GPT实战系列-大模型为我所用之借用ChatGLM3构建查询助手

GPT实战系列-简单聊聊LangChain

大模型查询工具助手之股票免费查询接口

LangChain 附带许多实用程序来简化函数调用。也就是说,它带有

  • 将函数绑定到模型的语法
  • 用于将各种类型的对象格式化为预期函数模式的转换器
  • 用于从 API 响应中提取函数调用的输出解析器

函数转为可调用解析

LangChain 有一个内置的转换器,可以将 Python 函数、Pydantic 类和 LangChain Tools 转换为 OpenAI 函数格式。

import json
from langchain_core.utils.function_calling import convert_to_openai_tool
 
def multiply(a: int, b: int) -> int:
    """Multiply two integers together.
    Args:
        a: First integer
        b: Second integer
    """
    return a * b
 
print(json.dumps(convert_to_openai_tool(multiply), indent=2))

json格式的tool函数描述:

{
  "type": "function",
  "function": {
    "name": "multiply",
    "description": "Multiply two integers together.",
    "parameters": {
      "type": "object",
      "properties": {
        "a": {
          "type": "integer",
          "description": "First integer"
        },
        "b": {
          "type": "integer",
          "description": "Second integer"
        }
      },
      "required": [
        "a",
        "b"
      ]
    }
  }
} 

另一种定义方式,也是支持的。

from typing import Any, Type
from langchain_core.tools import BaseTool
 
class MultiplySchema(BaseModel):
    """Multiply tool schema."""
 
    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")
 
class Multiply(BaseTool):
    args_schema: Type[BaseModel] = MultiplySchema
    name: str = "multiply"
    description: str = "Multiply two integers together."
 
    def _run(self, a: int, b: int, **kwargs: Any) -> Any:
        return a * b
 
 
# Note: we're passing in a Multiply object not the class itself.
print(json.dumps(convert_to_openai_tool(Multiply()), indent=2))

打印输出:

{
  "type": "function",
  "function": {
    "name": "multiply",
    "description": "Multiply two integers together.",
    "parameters": {
      "type": "object",
      "properties": {
        "a": {
          "description": "First integer",
          "type": "integer"
        },
        "b": {
          "description": "Second integer",
          "type": "integer"
        }
      },
      "required": [
        "a",
        "b"
      ]
    }
  }
} 

给LLM传递工具参数

因此,有了它和Prompt,就可以调度起LLM,以合适方式设置提示的格式。

当大语言模型(LLM)支持tools参数的时候,创建一个聊天提示模板,如下所示:

from langchain_openai import ChatOpenAI
 
llm = ChatOpenAI(model="gpt-3.5-turbo")
llm.invoke("what's 5 times three", tools=[convert_to_openai_tool(multiply)])
 
AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_JvOu9oUwMrQHiDekZTbpNCHY', 'function': {'arguments': '{\n  "a": 5,\n  "b": 3\n}', 'name': 'multiply'}, 'type': 'function'}]})

其实就是做些简单的封装,提供一些灵活性,来构建您的 Chain,或者Agent 引导。

LangChain是一个Python框架,可以使用LLMs构建应用程序。它与各种模块连接,使与LLM和提示管理,一切变得简单。

觉得有用 收藏 收藏 收藏

点个赞 点个赞 点个赞

End

GPT专栏文章:

GPT实战系列-实战Qwen通义千问在Cuda 12+24G部署方案_通义千问 ptuning-CSDN博客

GPT实战系列-ChatGLM3本地部署CUDA11+1080Ti+显卡24G实战方案

GPT实战系列-Baichuan2本地化部署实战方案

GPT实战系列-让CodeGeeX2帮你写代码和注释_codegeex 中文-CSDN博客

GPT实战系列-ChatGLM3管理工具的API接口_chatglm3 api文档-CSDN博客

GPT实战系列-大话LLM大模型训练-CSDN博客

GPT实战系列-LangChain + ChatGLM3构建天气查询助手

GPT实战系列-大模型为我所用之借用ChatGLM3构建查询助手

GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(二)

GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(一)

GPT实战系列-ChatGLM2模型的微调训练参数解读

GPT实战系列-如何用自己数据微调ChatGLM2模型训练

GPT实战系列-ChatGLM2部署Ubuntu+Cuda11+显存24G实战方案

GPT实战系列-Baichuan2等大模型的计算精度与量化

GPT实战系列-GPT训练的Pretraining,SFT,Reward Modeling,RLHF

GPT实战系列-探究GPT等大模型的文本生成-CSDN博客

  • 24
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex_StarSky

你的鼓励是创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值