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 小时前
第四章:模型集成生态 —— Partner 包架构与 init_chat_model 统一入口
人工智能·langchain
老王熬夜敲代码15 小时前
引入RAG
langchain
疯狂成瘾者21 小时前
LangChain Middleware 技术解析:从“插槽机制”到 Agent 运行时控制
数据库·python·langchain
Where-21 小时前
LangChain、LangGraph入门
python·langchain·langgraph
秦jh_1 天前
【LangChain】LangChain 与 LangGraph 介绍
人工智能·langchain
星轨zb1 天前
从“乐高积木”到“建筑设计图”:LangChain与LangGraph框架初探
langchain
chaors1 天前
LangGraph 入门到精通0x02:基础 API (二)
langchain·llm·agent
杨艺韬1 天前
LangGraph设计与实现-第15章-Store 与长期记忆
langchain·agent
杨艺韬1 天前
LangGraph设计与实现-第17章-多 Agent 模式实战
langchain·agent
杨艺韬1 天前
LangGraph设计与实现-第13章-流式输出与调试
langchain·agent