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

相关推荐
Query*7 小时前
深入浅出LangGraph【一】_基础篇
python·ai·langchain
大连好光景7 小时前
大模型应用中,如何实现短期记忆与长期记忆
langchain·记忆
Bug收容所9 小时前
学习LangChain day1
学习·langchain·llm·agent
小此方10 小时前
LangChain/LangGraph(二)大模型接入篇一:API接入,从API Key到API请求报文,使用Apifox完成大模型接口调用
ai·langchain
一木 之林14 小时前
RAG开发学习总结:从 LangChain 入门到检索增强生成链路的全栈实战-4/6
人工智能·学习·计算机视觉·langchain
制造数据与AI践行者老蒋15 小时前
排坑笔记:LangChain 1.x API 迁移:create_react_agent 与 ChatOllama 导入错误完整解决方案
langchain·依赖管理·importerror·agent开发·ai开发环境·排坑笔记·版本兼容
桃西西呀16 小时前
LangChain 之八:流式与透传
人工智能·langchain·llm
桃西西呀16 小时前
LangChain 之九:一个能检索又会调工具的流式问答助手
人工智能·langchain·llm
10年前端老司机18 小时前
LangChain 五种工具定义方式踩坑总结
python·langchain·llm
Maiko Star20 小时前
* LangChain 短期记忆:上下文与记忆入门、持久化与记忆治理
人工智能·langchain