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 Optionalclass 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 givenif length and width and height:return length*width*heightelse: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?")#多个参数提问

运行结果 

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

相关友情链接:

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/4938.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

金蝶云星空财务软件被locked勒索病毒攻击后如何更快解密数据库数据?

金蝶云星空财务软件是一款广泛应用于企业财务管理领域的软件,然而,近期很多企业的金蝶云星空财务软件遭受到了locked勒索病毒的攻击,导致数据库数据被加密。而这次的locked勒索病毒采用了新的加密形式,它不仅能够扫描出各种软件系…

LLM 应用参考架构:ArchGuard Co-mate 实践示例

随着,对于 LLM 应用于架构领域探索的进一步深入,以及 ArchGuard Co-mate 开发进入深入区,我们发现越来越多的通用模式。 在先前的文章里,我们总结了一系列的设计原则,在这篇文章里,我们将介绍 ArchGuard Co…

数字信号的载波传输

从信号传输质量来看,数字系统优于模拟系统。 由于数字基带信号的频谱包含低频成分,而许多重要的通信信道是带通型的,比如无线信道和许多有线信道,这时需要调制成数字频带信号。 数字调制与模拟调制原理基本相似,有调幅…

STM32单片机(三)第二节:GPIO输出练习3(蜂鸣器)

❤️ 专栏简介:本专栏记录了从零学习单片机的过程,其中包括51单片机和STM32单片机两部分;建议先学习51单片机,其是STM32等高级单片机的基础;这样再学习STM32时才能融会贯通。 ☀️ 专栏适用人群 :适用于想要…

UWB超宽带定位技术的原理及定位方法

uwb定位技术即超宽带技术,它是一种无载波通信技术,利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。传统的定位技术是根据信号强弱来判别物体位置,信号强弱受外界 影响较大,因此定位出的物体位置与实际…

centos8.x系统安装K8S,kubernetes集群v1.23.9,docker支持的最后一个版本

1. 部署环境主机(条件说明) 卸载podman,centos默认安装了podman容器(不管有没有,执行下总没错),可能与docker存在冲突 #环境准备 master 192.168.186.128 CentOS Linux release 8.5 (Core) n…

CentOS 挂载ntfs格式U盘

1、查看磁盘信息 fdisk -l 如上图所示,可以看到插入的U盘/dev/sdc 2、查看磁盘块信息 lsblk 如上图所示,sdc即是插入的U盘,sdc1为磁盘块,挂载的时候使用sdc1 3、查看磁盘文件系统类型 parted -l 如上图所示,U盘是WD…

Electron + ts + vue3 + vite 项目搭建

Electron 是一个基于 Chromium 和 Node.js 的桌面应用程序开发框架,而 Vue3 则是一种流行的前端框架。将两者结合使用可以快速地打造出跨平台的桌面应用程序。在这种组合中,Electron 提供了强大的桌面应用开发能力,而 Vue3 则提供了易用的 UI…

4.28 白噪声功率谱密度的估计

前一节有下面结论: 什么仿真的白噪声波形是均匀的,均匀的特点是相关函数在0时刻是相关的(自己和自己相关的)0以外时刻都互相不相关

微信小程序——二维码推广海报

&#x1f60a;博主&#xff1a;小猫娃来啦 &#x1f60a;本文核心&#xff1a;本地图片和接口返回二维码制作一体化canvas海报 目录 创建一个画布通过canvas API绘制图像细节1 长按保存细节2 创建一个画布 在微信小程序中&#xff0c;我们使用<canvas>标签来创建一个画布…

Multi-sensor KIT 多传感器开发板

1.前言 经过一段时间的思考后&#xff0c;我决定设计一款能够兼容多个传感器的开发板。这个开发板由核心底板和扩展板组成&#xff0c;其中核心底板预留了多路的I2C、SPI、UART、ADC等接口&#xff0c;而扩展板则兼容了QMI8658A-EVB和CH101/201-EVB的支持。这个设计可以让我更…

java小技能:分布式任务调度平台

文章目录 引言I 报表数据生成II 注意事项2.1 任务创建2.2 pom.xml 添加到maven项目 see also 引言 任务调度的应用场景&#xff1a; 生成日报、月报、定时处理任务&#xff08;定期清理文件、处理数据&#xff09; I 报表数据生成 https://kunnan.blog.csdn.net/article/deta…