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

相关推荐
吃饱了得干活7 小时前
别只会传 temperature!一文挖透 LangChain 模型配置:Profile、全参数与运行时动态覆盖
python·langchain
龙腾亚太8 小时前
基础差/零基础人员具身智能学习路线
langchain·qlora·大模型培训·智能体培训·具身智能培训·llm 实战教程
xywww16814 小时前
大模型 API 选型实战:GPT、Gemini、Claude 接入时该看哪些指标?
运维·服务器·人工智能·python·gpt·langchain
糖果店的幽灵1 天前
从 GPT Researcher 学习 LangChain
gpt·学习·langchain
吃饱了得干活1 天前
LangChain 模型调用方案:invoke、stream、batch 的同步与异步
langchain·llm
吃饱了得干活1 天前
LangChain 多对话模型统一接入工程实践
langchain
一只小bit1 天前
LangChain 核型组件与消息管理及提示词模版
windows·microsoft·langchain
齐 飞1 天前
LangGraph快速入门-03节点与边
python·langchain
知行合一。。。1 天前
LangChain--06--结构化输出(Structured Output)
android·java·langchain
飞猪~2 天前
LangChain python 版本 第一集
开发语言·python·langchain