LangChain-Agent自定义Tools类 ——输入参数篇(二)

这篇具有很好参考价值的文章主要介绍了LangChain-Agent自定义Tools类 ——输入参数篇(二)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

给自定义函数传入输入参数,分别有single-input 参数函数案例和multi-input 参数函数案例: 

from langchain.agents import Tool
from langchain.tools import BaseTool
from math import pi
from typing import Union
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
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from typing import Optional


class Calculate_Life(BaseTool):#这里的RUL value 我瞎编的
      name = "Calculate Life Tool"
      description = "use this tool when you need to calculate the happiness of life using the RUL value"

      def _run(self, RUL: Union[int, float]):
        return "The happiness of life is {RUL*2}"

      def _arun(self, RUL: int):
        raise NotImplementedError("This tool does not support async")
class Calculate_volumn(BaseTool):#这里的RUL value 我瞎编的
      name = "Calculate Object Volumn"
      description = (
      "use this tool when you need to calculate the the volumn of object,inputs are multi parameters,need multi input variables,like the length,width,and height."
      "To use the tool, you must provide at least three of the following parameters "
      "['length', 'width', 'height']."
      )

      def _run(
          self,
          length: Optional[Union[int, float]] = None,
          width: Optional[Union[int, float]] = None,
          height: Optional[Union[int, float]] = None
      ):
          # check for the values we have been given
          if length and width and height:
              return length*width*height
          else:
              return "Could not calculate the size of universe. Need two or more of `length`, `width`,`height`."
      def _arun(self, query: str):
          raise NotImplementedError("This tool does not support async")
os.environ["OPENAI_API_KEY"] = "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"
llm = ChatOpenAI(
    openai_api_key=os.environ["OPENAI_API_KEY"] ,
    temperature=0,
    model_name='gpt-3.5-turbo'
)
# initialize conversational memory
conversational_memory = ConversationBufferWindowMemory(
        memory_key='chat_history',
        k=5,
        return_messages=True
)

tools = [Calculate_volumn()]
 
# sys_msg = """Assistant is a large language model trained by OpenAI.

# Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.

# Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.

# Unfortunately, Assistant is terrible at maths. When provided with math questions, no matter how simple, assistant always refers to it's trusty tools and absolutely does NOT try to answer math questions by itself

# Overall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
# """
# sys_msg = "Do not try to figure math question by yourself, you might be blindly confident about your mathematic capacity,please always refers to it's trusty tools and absolutely does NOT try to answer math questions by itself"



# agent = initialize_agent(
#     agent='chat-conversational-react-description',
#     # agent='zero-shot-react-description',
#     tools=tools,
#     llm=llm,
#     verbose=True,
#     max_iterations=3,
#     early_stopping_method='generate',
#     memory=conversational_memory
# )


new_prompt = agent.agent.create_prompt(
    system_message=sys_msg,
    tools=tools
)

agent.agent.llm_chain.prompt = new_prompt
# update the agent tools
agent.tools = tools

# agent("How much is the happiness of life when RUL value is equal to 2")#单个参数提问
agent("Given [length=10000,width=2,height=3],what's the size of this box?")#多个参数提问

运行结果 

LangChain-Agent自定义Tools类 ——输入参数篇(二)

后期关注结构化输出,方便作为借口提供给其他下游应用 

相关友情链接:

为 LLM 代理构建自定义工具 |松果 (pinecone.io)文章来源地址https://www.toymoban.com/news/detail-513120.html

到了这里,关于LangChain-Agent自定义Tools类 ——输入参数篇(二)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Langchain Agent研究】SalesGPT项目介绍(五)

    【Langchain Agent研究】SalesGPT项目介绍(四)-CSDN博客                 上节课,我们分析了一下salesGPT项目里源代码的一些问题,重新写了一个运行方法,换了一个模型并修改了一些源代码开始把项目跑起来了,我们已经可以通过console和模型进行对话了。         我们

    2024年02月19日
    浏览(22)
  • 【Langchain Agent研究】SalesGPT项目介绍(四)

    【Langchain Agent研究】SalesGPT项目介绍(三)-CSDN博客   github地址:GitHub - jerry1900/SalesGPT: Context-aware AI Sales Agent to automate sales outreach.               上节课,我们主要介绍了SalesGPT的类属性和它最重要的类方法from_llm()。因为SalesGPT没有构造器方法,所以类方法from_llm()方法就

    2024年02月21日
    浏览(38)
  • 让AI做决策,学会langChain的Agent

    今天内容涉及如下: 1.initialize_agent,:执行gent工作,并把工具Tool传入 2.Tool:选取行为函数工具类 之前我们学习的都是把问题给AI,让AI模型给出答案,那么这种情况下应该怎么处理呢,我需要根据不同的问题选择不同的答案,比如我问AI我想选择一件衣服就去调用挑选衣服的

    2024年01月18日
    浏览(33)
  • 怎么和Bing一样使用ChatGPT?如何让ChapGPT与外界世界交互结合?LangChain Agent模块帮你解决问题。LangChain Agent模块的使用案例和源码详解

    ChatGPT很火,但是对于这个模型我们怎么用呢?只是和他聊聊天,回答回答问题? 如何基于这个模型进行二次开发呢?是否可以和new bing一样,可以搜索资料然后进行回复?甚至可以按照你的指令帮你操作机器人? LangChain的 Agent模块就可以帮大家做到这些,而Agent是如何使用

    2023年04月14日
    浏览(49)
  • Langchain+ElasticSearch+文心千帆 构建检索增强LLM Agent

    很早就开始做检索增强的大语言模型Agent了,通过外接知识库为LLM提供外部知识能增强它回答的准确性。这里我们使用ElasticSearch作为数据库存储相关知识,使用百度文心千帆的embedding API提供向量嵌入;借助langchain搭建LLM Agent. 需要安装的环境有: Python, ElasticSearch, langchain, q

    2024年02月04日
    浏览(33)
  • LangChain与大型语言模型(LLMs)应用基础教程:神奇的Agent

      LangChain是大型语言模型(LLM)的应用框架,LangChain可以直接与 OpenAI 的 text-davinci-003、gpt-3.5-turbo 模型以及 Hugging Face 的各种开源语言模如 Google 的 flan-t5等模型集成。通过使用LangChain可以开发出更为强大和高效的LLM的各种应用。 今天我们就来实现一个神奇的功能,如何你是一个不

    2024年02月03日
    浏览(33)
  • 从API到Agent:万字长文洞悉LangChain工程化设计

    我想做一个尝试,看看能不能用尽量清晰的逻辑,给“AI外行人士”(当然,我也是……)引入一下LangChain,试着从工程角度去理解LangChain的设计和使用。同时大家也可以将此文档作为LangChain的“10分钟快速上手”手册,本意是希望帮助需要的同学实现AI工程的Bootstrap。 文中所

    2024年03月15日
    浏览(24)
  • LangChain 67 深入理解LangChain 表达式语言30 调用tools搜索引擎 LangChain Expression Language (LCEL)

    LangChain系列文章 LangChain 50 深入理解LangChain 表达式语言十三 自定义pipeline函数 LangChain Expression Language (LCEL) LangChain 51 深入理解LangChain 表达式语言十四 自动修复配置RunnableConfig LangChain Expression Language (LCEL) LangChain 52 深入理解LangChain 表达式语言十五 Bind runtime args绑定运行时参数

    2024年01月23日
    浏览(45)
  • Observability:如何把 Elastic Agent 采集的数据输入到 Logstash 并最终写入到 Elasticsearch

    在之前的文章 “安装独立的 Elastic Agents 并采集数据 - Elastic Stack 8.0”,我们详述了如何使用 No Fleet Server 来把数据写入到 Elasticsearch 中。在今天的文章中,我们来详述如下使用 Elastic Agents 在独立(standalone)模式下来采集数据并把数据最终通过 Logstash 来写入到 Elasticsearch 中去

    2024年02月11日
    浏览(35)
  • [LangChain核心模块]模型的输入和输出->Prompts

    ⭐作者介绍:大二本科网络工程专业在读,持续学习Java,努力输出优质文章 ⭐作者主页:@逐梦苍穹 ⭐所属专栏:人工智能。 任何语言模型应用的核心元素是 模型的输入和输出 。LangChain提供了与任何语言模型进行接口交互的基本组件。 ● 提示 prompts: 将模型输入模板化、动

    2024年02月16日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包