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  │
└─────────┘

相关推荐
艾醒(AiXing-w)7 小时前
LangChain 1.0 入门(三):稳定性双核心——重试机制+速率限速器参数详解与实战
开发语言·langchain·php
未若君雅裁8 小时前
从函数到工具,LangChain Tools 与 Tool Calling 完整入门
langchain
未若君雅裁8 小时前
工具描述决定调用质量,LangChain 工具 Schema 与参数校验
langchain
艾醒(AiXing-w)10 小时前
LangChain 1.0 入门(二):LangChain 全模型标准化接入最佳实践(小白参数详解版)
前端·javascript·langchain
invicinble14 小时前
langchain --rage了解(周末版本)
langchain
qy2016skq14 小时前
OpenClaw 源码解读——入门与破局9 双插件协同:Quota Guard 负责“停“,Model Router 负责“绕“
langchain·prompt·aigc·embedding·ai编程·llama·agi
这就是佬们吗15 小时前
治幻觉,先治检索:RAG 系统防幻觉的完整工程指南
python·langchain·embedding
爱奥尼欧15 小时前
14.输出解析器-Pydantic与JSON
人工智能·学习·langchain·json
qy2016skq15 小时前
OpenClaw 源码解读——入门与破局8 从“报错不切换“到“秒级自动切换“:模型路由插件在 2026.4.14 上的五次迭代实录
langchain·prompt·aigc·embedding·ai编程·ai-native
AI_小站1 天前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain