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

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

LangChain系列

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

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

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

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

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

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

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

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

GPT实战系列-简单聊聊LangChain

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

随着OpenAI的GPT-4这样的大型语言模型(LLMs)已经风靡全球,现在让它们自动执行各种任务,如回答问题、翻译语言、分析文本等。LLMs是在交互上真正体验到像"人工智能"。

如何管理这些模块呢?

LangChain在这方面发挥重要作用。LangChain使构建由LLMs驱动的应用程序变得简单,使用LangChain,可以在统一的界面中轻松与不同类型的LLMs进行交互,管理模型版本,管理对话版本,并将LLMs连接在一起。

python 复制代码
# 引入需要的模块
from langchain.pydantic_v1 import BaseModel, Field
from langchain.tools import BaseTool, StructuredTool, tool

from typing import Optional, Type

from langchain.callbacks.manager import (
    AsyncCallbackManagerForToolRun,
    CallbackManagerForToolRun,
)

当需要定义多个参数的自定义tools,怎么构造呢?

用@Tool装饰器自定义

LangChain可以连接到自己定义的工具,也可以连接到内嵌的tool提供商。通过@Tool构造多参数。

例子自定义乘法器:

python 复制代码
@tool
def multiply(a: int, b: int) -> int:
    """Multiply two numbers."""
    return a * b

查看相关的参数:

python 复制代码
print(multiply.name)
print(multiply.description)
print(multiply.args)

可以看到 两个int 参数:

复制代码
multiply
multiply(a: int, b: int) -> int - Multiply two numbers.
{'a': {'title': 'A', 'type': 'integer'}, 'b': {'title': 'B', 'type': 'integer'}}

用BaseTool构建多参数tool

除了tool装饰器,还有是BaseTool的方法,定义工具的参数说明。

仍然定义乘法器。

python 复制代码
# 定义参数说明
class CalculatorInput(BaseModel):
    a: int = Field(description="first number")
    b: int = Field(description="second number")

自定义多参数工具类,实现功能:

python 复制代码
class CustomCalculatorTool(BaseTool):
    name = "Calculator"
    description = "useful for when you need to answer questions about math"
    args_schema: Type[BaseModel] = CalculatorInput
    return_direct: bool = True

    def _run(
        self, a: int, b: int, run_manager: Optional[CallbackManagerForToolRun] = None
    ) -> str:
        """Use the tool."""
        return a * b

    async def _arun(
        self,
        a: int,
        b: int,
        run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
    ) -> str:
        """Use the tool asynchronously."""
        raise NotImplementedError("Calculator does not support async")

实例化,查看参数情况:

python 复制代码
multiply = CustomCalculatorTool()
print(multiply.name)
print(multiply.description)
print(multiply.args)
print(multiply.return_direct)

得到类似的输出:

复制代码
Calculator
useful for when you need to answer questions about math
{'a': {'title': 'A', 'description': 'first number', 'type': 'integer'}, 'b': {'title': 'B', 'description': 'second number', 'type': 'integer'}}
True

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博客

相关推荐
AI大模型12 小时前
GitHub 狂飙 72k Star,这本大模型书凭啥能圈粉无数?
程序员·llm·agent
AI大模型17 小时前
一篇文章看懂RAG + 实战,看不懂来揍我
程序员·llm·agent
叫我詹躲躲20 小时前
n8n 自动化工作流平台完整部署
前端·langchain·领域驱动设计
安思派Anspire1 天前
创建完整的评估生命周期以构建高(二)
aigc·openai·agent
字节跳动数据平台1 天前
一句话让AI帮你搞营销?火山引擎Data Agent说:这事儿可以的~
agent
杨杨杨大侠1 天前
打开 JVM 黑匣子——走进 Java 字节码(一)
java·jvm·agent
大模型教程2 天前
8GB显存笔记本能跑多大AI模型?这个计算公式90%的人都不知道!
程序员·llm·agent
大模型教程2 天前
大模型应用开发到底有多赚钱?看完这5个真实案例,你会惊掉下巴
程序员·llm·agent
量子位2 天前
GPT-5编程专用版发布!独立连续编程7小时,简单任务提速10倍,VS Code就能用
gpt·chatgpt
AI大模型2 天前
别乱装!Ollama×DeepSeek×AnythingLLM一键本地AI知识库,快人10倍
程序员·llm·agent