LangChain 工具创建方法总结

LangChain 工具创建方法总结


.@tool装饰器创建工具

这是什么

主要依赖库:

  • from pydantic import BaseModel, Field
  • from langchain_core.tools import tool

定义的类:

  • MultiplyInput

有什么用

通过本文件学习:

  • 相关库的导入和使用方法
  • 类的创建和使用
  • LangChain 的实际应用场景

示例代码

python 复制代码
from pydantic import BaseModel, Field
from langchain_core.tools import tool
class MultiplyInput(BaseModel):
    a: int = Field(description="第一个数字")
    b: int = Field(description="第二个数字")
@tool("multiply_tool", return_direct=True, args_schema=MultiplyInput)
def multiply(a: int, b: int) -> int:

流程图

text 复制代码
┌─────────┐
│  Input  │
└────┬────┘
     │
     ▼
┌─────────┐
│Process  │
└────┬────┘
     │
     ▼
┌─────────┐
│ Output  │
└─────────┘

.StructuredTool类方法创建工具

这是什么

主要依赖库:

  • from pydantic import BaseModel, Field
  • from langchain_core.tools import StructuredTool

定义的类:

  • MultiplyInput

有什么用

通过本文件学习:

  • 相关库的导入和使用方法
  • 类的创建和使用
  • LangChain 的实际应用场景

示例代码

python 复制代码
from pydantic import BaseModel, Field
from langchain_core.tools import StructuredTool
class MultiplyInput(BaseModel):
    a: int = Field(description="第一个数字")
    b: int = Field(description="第二个数字")
def multiply(a: int, b: int) -> int:
    return a * b
calculator = StructuredTool.from_function(
    func=multiply,
    coroutine=amultiply,
    name="multiply_tool",
    description="将传递的两个数字相乘",
    return_direct=True,
    args_schema=MultiplyInput,
print("名称: ", calculator.name)
print("描述: ", calculator.description)
print("参数: ", calculator.args)
print("直接返回: ", calculator.return_direct)
print(calculator.invoke({"a": 2, "b": 8}))

流程图

text 复制代码
┌─────────┐
│  Input  │
└────┬────┘
     │
     ▼
┌─────────┐
│Process  │
└────┬────┘
     │
     ▼
┌─────────┐
│ Output  │
└─────────┘

.BaseTool子类创建工具

这是什么

主要依赖库:

  • from typing import Any, Type
  • from pydantic import BaseModel, Field
  • from langchain_core.tools import BaseTool

定义的类:

  • MultiplyInput
  • MultiplyTool

有什么用

通过本文件学习:

  • 相关库的导入和使用方法
  • 类的创建和使用
  • LangChain 的实际应用场景

示例代码

python 复制代码
from typing import Any, Type
from pydantic import BaseModel, Field
from langchain_core.tools import BaseTool
class MultiplyInput(BaseModel):
    a: int = Field(description="第一个数字")
    b: int = Field(description="第二个数字")
class MultiplyTool(BaseTool):
        return kwargs.get("a") * kwargs.get("b")
calculator = MultiplyTool()
print("名称: ", calculator.name)
print("描述: ", calculator.description)
print("参数: ", calculator.args)
print("直接返回: ", calculator.return_direct)
print(calculator.invoke({"a": 2, "b": 8}))

流程图

text 复制代码
┌─────────┐
│  Input  │
└────┬────┘
     │
     ▼
┌─────────┐
│Process  │
└────┬────┘
     │
     ▼
┌─────────┐
│ Output  │
└─────────┘

相关推荐
蓝悦无人机13 小时前
LangChain v1.0 系列教程——第2章 工具系统
langchain·json·装饰器模式·pydantic
艾醒(AiXing-w)13 小时前
LangChain 1.0 入门(九):Agent 核心概念与技术架构
人工智能·chatgpt·langchain
多学一分钟14 小时前
讲清 Agent 记忆与上下文管理:长短期记忆、多轮对话、上下文压缩
langchain·agent
聪明蛋子哟15 小时前
从LangChain到LangGraph:Python与Java双栈Agent开发实战对比
java·ai·langchain
小叶肥辉19 小时前
LangChain链和LangGraph图的学习笔记【四】——(1)调用方法:invoke(2)提示语模板:PromptTemplate
python·langchain·prompt
梦在远山后2 天前
从需求到架构:我的企业研发 Agent 整体设计
langchain·agent
Warson_L2 天前
Python的TypedDict
python·langchain·llm
OKkankan2 天前
LangChain能力详解!:从工具调用到 LangSmith——让聊天模型具备实时交互能力!
开发语言·python·langchain
ctlover3 天前
LangChain 概述
python·langchain
玉宇夕落3 天前
llm模块二 结构化输出 LangChain 结构化输出完全指南:从 JSON 解析到 withStructuredOutput
langchain·llm