LangChain-Agent自定义Tools类 ——基础篇(一)

from langchain.agents import Tool
from langchain.tools import BaseTool
from math import pi
from typing import Union
from langchain.agents import initialize_agent
from langchain.agents import AgentType
import os
from langchain.chat_models import ChatOpenAI

os.environ["OPENAI_API_KEY"] = "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
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
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"
llm = ChatOpenAI(
    openai_api_key=os.environ["OPENAI_API_KEY"] ,
    temperature=0,
    model_name='gpt-3.5-turbo'
)
class CustomTool(BaseTool):
    name = "Temperature Detector"
    description = "This is a custom tool for my temperature detection use case"
 
    def _run(self, input: str) -> str:
        # Your logic here
        return "temperature is not bad,huh,20 celceius"
 
    def _arun(self, query: str):
        raise NotImplementedError("This tool does not support async")
class Robotic_Control(BaseTool):
    name = "Robotic Arm Control"
    description = "This is a custom tool for my Robotic Arm Control"
 
    def _run(self, input: str) -> str:
        # Your logic here
        return "Working "
 
    def _arun(self, query: str):
        raise NotImplementedError("This tool does not support async")
tools = [CustomTool(),Robotic_Control()]
 
 
 
# agent = initialize_agent(tools, agent=AgentType.DEFAULT)
agent = initialize_agent(tools, 
                         llm, 
                         agent="zero-shot-react-description", 
                         verbose=True)
agent("How's the temperature and Robotic Arm Control")

LangChain-Agent自定义Tools类 ——基础篇(一)_第1张图片

其他友情链接:

为 LLM 代理构建自定义工具 |松果 (pinecone.io)

Create Custom Tools for Chatbots in LangChain — LangChain #8 - YouTube

//油管这个小哥讲的比较好 

Tools怎么使用?可以定义自己的tool么? - General - LangChain中文社区

你可能感兴趣的:(LangChain,langchain,自定义工具)