LangChain(6)构建用户自己的Agent

news2024/12/28 19:12:23

构建用户自己的Agent

    • 编写简单的计算工具
    • 编写有多个参数的工具
    • 其它更高级的工具

LangChain 中有一些可用的Agent内置工具,但在实际应用中我们可能需要编写自己的Agent。

编写简单的计算工具

!pip install -qU langchain openai transformers
from langchain.tools import BaseTool
from math import pi
from typing import Union
class CircumferenceTool(BaseTool):
	# tool名称
    name = "Circumference calculator"
    # 描述此工具能做什么,当LLM语义匹配到该description时,就会执行此tool
    description = "use this tool when you need to calculate a circumference using the radius of a circle"
	# 调用run 时执行此函数
    def _run(self, radius: Union[int, float]):
        return float(radius)*2.0*pi
    # 异步用
    def _arun(self, radius: Union[int, float]):
        raise NotImplementedError("This tool does not support async")


import os
from langchain.chat_models import ChatOpenAI
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY') or 'OPENAI_API_KEY'
# initialize LLM (we use ChatOpenAI because we'll later define a `chat` agent)
llm = ChatOpenAI(
    openai_api_key=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
)

from langchain.agents import initialize_agent
tools = [CircumferenceTool()]
# initialize agent with tools
agent = initialize_agent(
	'''
	chat-conversational-react-description名称解释
	chat:使用chat模型,如 gpt-4 and gpt-3.5-turbo
	conversational:含缓存conversation memory
	react:模型自推理
	description:LLM模型决定使用哪个工具
	'''
    agent='chat-conversational-react-description',
    tools=tools,
    llm=llm,
    verbose=True,
    max_iterations=3,
    early_stopping_method='generate',
    memory=conversational_memory
)
agent("can you calculate the circumference of a circle that has a radius of 7.81mm")

>>>  {'input': 'can you calculate the circumference of a circle that has a radius of 7.81mm',
 'chat_history': [],
 'output': 'The circumference of a circle with a radius of 7.81mm is approximately 49.03mm.'}

输出的答案为 49.03,是个错误答案,实际上为 49.07=(7.81 * 2) * pi
可见模型并没有使用我们定义的 Circumference calculator 进行计算,而是LLM模型自己进行了推理,但LLM不善于数据计算,所以最后的结果虽然接近但是是错误的。

# 打印提示词 existing prompt
print(agent.agent.llm_chain.prompt.messages[0].prompt.template)

>>> '''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.

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 = """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.
"""
# 更新提示词 prompt
new_prompt = agent.agent.create_prompt(
    system_message=sys_msg,
    tools=tools
)
agent.agent.llm_chain.prompt = new_prompt

agent("can you calculate the circumference of a circle that has a radius of 7.81mm")

# 现在能得到正确答案
>>>{'input': 'can you calculate the circumference of a circle that has a radius of 7.81mm',
 'chat_history': [HumanMessage(content='can you calculate the circumference of a circle that has a radius of 7.81mm', additional_kwargs={}),
  AIMessage(content='The circumference of a circle with a radius of 7.81mm is approximately 49.03mm.', additional_kwargs={})],
 'output': 'The circumference of a circle with a radius of 7.81mm is approximately 49.07mm.'}

编写有多个参数的工具

一个用于计算三角形斜边的工具

from typing import Optional
from math import sqrt, cos, sin

desc = (
    "use this tool when you need to calculate the length of an hypotenuse "
    "given one or two sides of a triangle and/or an angle (in degrees). "
    "To use the tool you must provide at least two of the following parameters "
    "['adjacent_side', 'opposite_side', 'angle']."
)

class PythagorasTool(BaseTool):
    name = "Hypotenuse calculator"
    description = desc

    def _run(
        self,
        adjacent_side: Optional[Union[int, float]] = None,
        opposite_side: Optional[Union[int, float]] = None,
        angle: Optional[Union[int, float]] = None
    ):
        # check for the values we have been given
        if adjacent_side and opposite_side:
            return sqrt(float(adjacent_side)**2 + float(opposite_side)**2)
        elif adjacent_side and angle:
            return adjacent_side / cos(float(angle))
        elif opposite_side and angle:
            return opposite_side / sin(float(angle))
        else:
            return "Could not calculate the hypotenuse of the triangle. Need two or more of `adjacent_side`, `opposite_side`, or `angle`."
    
    def _arun(self, query: str):
        raise NotImplementedError("This tool does not support async")

tools = [PythagorasTool()]

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("If I have a triangle with two sides of length 51cm and 34cm, what is the length of the hypotenuse?")

其它更高级的工具

一个描述图像的工具

import torch
from transformers import BlipProcessor, BlipForConditionalGeneration
# 描述图像的模型名称
hf_model = "Salesforce/blip-image-captioning-large"
device = 'cuda' if torch.cuda.is_available() else 'cpu'

processor = BlipProcessor.from_pretrained(hf_model)
model = BlipForConditionalGeneration.from_pretrained(hf_model).to(device)

# 下载图片
import requests
from PIL import Image
img_url = 'https://images.unsplash.com/photo-1616128417859-3a984dd35f02?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2372&q=80' 
image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
image.show() # image

# unconditional image captioning
inputs = processor(image, return_tensors="pt").to(device)

out = model.generate(**inputs, max_new_tokens=20)
print(processor.decode(out[0], skip_special_tokens=True))

>>>there is a monkey that is sitting in a tree

在这里插入图片描述
构建 agent 工具

desc = (
    "use this tool when given the URL of an image that you'd like to be "
    "described. It will return a simple caption describing the image."
)

class ImageCaptionTool(BaseTool):
    name = "Image captioner"
    description = desc

    def _run(self, url: str):
        # download the image and convert to PIL object
        image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
        # preprocess the image
        inputs = processor(image, return_tensors="pt").to(device)
        # generate the caption
        out = model.generate(**inputs, max_new_tokens=20)
        # get the caption
        caption = processor.decode(out[0], skip_special_tokens=True)
        return caption
    
    def _arun(self, query: str):
        raise NotImplementedError("This tool does not support async")

tools = [ImageCaptionTool()]

# 新的提示词
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.

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.
"""

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(f"What does this image show?\n{img_url}")

参考
Building Tools
Building Custom Tools for LLM Agents

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Spring-Interceptor拦截器

使用步骤 申明拦截器bean,并实现HandlerInterceptor接口 true为放行,false为拦截 2.定义配置类,继承WebMvcConfigurationSupport,实现addInterceptors方法,该方法调用具体的拦截器进行拦截 也可以在配子类通过实现W…

HTPP入门教程||HTTP 状态码||HTTP content-type

HTTP 状态码 当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求。当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含 HTTP 状态码的信息头(server header)用以响应浏览器的请求。 HTTP 状态码的英文为…

Springboot+Flask+Neo4j+Vue2+Vuex+Uniapp+Mybatis+Echarts+Swagger综合项目学习笔记

文章目录 Neo4j教程:Neo4j高性能图数据库从入门到实战 医疗问答系统算法教程:医学知识图谱问答系统项目示例:neo4j知识图谱 Vueflask 中药中医方剂大数据可视化系统可视化技术:ECharts、D.jsflask教程:速成教程Flask w…

『分割』 分割圆柱

原始点云 直通滤波过滤后&#xff08;z:0~1.5&#xff09; 分割到的平面 分割得到的圆柱形 代码&#xff1a; #include <pcl/ModelCoefficients.h> #include <pcl/io/pcd_io.h> #include <pcl/filters/extract_indices.h> // 用于提取指定索引的数据 #inclu…

伪标签(pseudo label)(半监督学习)

使用伪标签进行半监督学习&#xff0c;在机器学习竞赛当中是一个比较容易快速上分的关键点。下面给大家来介绍一下什么是基于伪标签的半监督学习。在传统的监督学习当中&#xff0c;我们的训练集具有标签&#xff0c;同时&#xff0c;测试集也具有标签。这样我们通过训练集训练…

RS485转ETHERCAT连接西门子支持ethercat吗

我们将为大家介绍一款强大的设备——远创智控YC-ECT-RS485/232通讯网关。这是一款自主研发的ETHERCAT从站功能的网关&#xff0c;它能够将ETHERCAT网络和RS485或RS232设备无缝连接。 这款网关在ETHERCAT总线和RS485或RS232总线中均能发挥主站或从站的作用。它的最大特点就是解决…

ICCV 2023 接收结果出炉!再创历史新高!录用2160篇!(附6篇最新论文)

点击下方卡片&#xff0c;关注“CVer”公众号 AI/CV重磅干货&#xff0c;第一时间送达 点击进入—>【计算机视觉】微信交流群 2023 年 7 月 14 日13:03&#xff0c;ICCV 2023 顶会论文接收结果出炉&#xff01;这次直接放出论文 Accepted Paper ID List。这也意味着&#xf…

【大数据趋势】7月15日 汇率,美澳,恒指期货的大数据趋势概率分析。

1. 数据源头之一 : 美元汇率破位&#xff0c;下面有3-7%的空间 从程序模拟趋势来看&#xff0c;如果没有干预&#xff0c;美元这个破位后&#xff0c;下方空间是3-7%。从中期趋势来看&#xff0c;是一次破位寻找新的稳定位置&#xff0c;在RSI的底部附近&#xff0c;现在刚刚开…

6.3.1 利用Wireshark进行协议分析(一)

6.3.1 利用Wireshark进行协议分析&#xff08;一&#xff09; Wireshark是目前全球使用最广泛的抓包软件&#xff0c;也是很强大的抓包软件&#xff0c;不管从事网络测试还是软件开发工程师&#xff0c;Wireshark都是一个非常实用的好工具。单纯的学习网络理论知识很抽象&…

个人笔记:下载及配置maven详细步骤

下载maven以及配置maven 访问 maven 官网&#xff0c;这里我下载的是 3.6.1的版本&#xff0c;尽量不要下载太新的版本&#xff0c;新版本可能会不太稳定&#xff0c;遇到一些奇奇怪怪的问题。 官网地址&#xff1a; https://maven.apache.org/download.cgi点击 download&…

安全开发-PHP应用留言板功能超全局变量数据库操作第三方插件引用后台模块SessionCookieToken身份验证唯一性

文章目录 开发环境数据导入-mysql架构&库表列数据库操作-mysqli函数&增删改查数据接收输出-html混编&超全局变量第三方插件引用-js传参&函数对象调用身份验证-Cookie使用身份验证-Session使用唯一性判断-Token使用具体安全知识点&#xff1a;Cookie和Session都…

aes加密解密算法流程

有了https加密传输为何还要使用AES加密 HTTPS&#xff08;HTTP Secure&#xff09;是基于 SSL/TLS 协议的安全通信协议&#xff0c;通过对传输的数据进行加密和认证来确保通信的机密性、完整性和身份验证。它使用了公钥加密来建立安全通道&#xff0c;并使用对称密钥加密来加密…

分享一个创意滚筒按钮

先看效果&#xff1a; 再看代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>滚筒按钮</title><style>* {border: 0;box-sizing: border-box;margin: 0;padding: 0;}:ro…

COMP527数据分析:中心性度量

在数据分析中&#xff0c;中心性度量通常是指描述数据集中心位置的统计量。

gorm实现动态sql

文章目录 前言需求实现步骤 前言 书接上回&#xff0c;解决了选择性的忽略某些字段这个问题&#xff0c;新的问题出现&#xff0c;想要使用gorm完成动态sql&#xff0c;我只知道mybatis中使用标签可以完成动态sql&#xff0c;不会使用gorm完成动态sql&#xff0c;下面通过一个…

基于linux下的高并发服务器开发(第一章)- 文件属性操作函数

08 / 文件属性操作函数 1、access.c #include <unistd.h>int access(const char *pathname, int mode); 作用&#xff1a;判断某个文件是否有某个权限&#xff0c;或者判断文件是否存在 参数&#xff1a; - pathname: 判断的文件路径 - mode: …

nacos注册中心+Ribbon负载均衡+完成openfeign的调用(超详细步骤)

目录 1.注册中心 1.1.nacos注册中心 1.2. 微服务注册和拉取注册中心的内容 2.3.修改订单微服务的代码 3.负载均衡组件 3.1.什么是负载均衡 3.2.什么是Ribbon 3.3.Ribbon 的主要作用 3.4.Ribbon提供的负载均衡策略 4.openfeign完成服务调用 4.1.什么是OpenFeign 4.2…

搜索引擎elasticsearch :安装elasticsearch (包含安装组件kibana、IK分词器、部署es集群)

文章目录 安装elasticsearch1.部署单点es1.1.创建网络1.2.加载镜像1.3.运行 2.部署kibana2.1.部署2.2.DevTools2.3 分词问题(中文不友好) 3.安装IK分词器3.1.在线安装ik插件&#xff08;较慢&#xff09;3.2.离线安装ik插件&#xff08;推荐&#xff09;1&#xff09;查看数据卷…

相机图像质量研究(1)Camera成像流程介绍

系列文章目录 相机图像质量研究(1)Camera成像流程介绍 相机图像质量研究(2)ISP专用平台调优介绍 相机图像质量研究(3)图像质量测试介绍 相机图像质量研究(4)常见问题总结&#xff1a;光学结构对成像的影响--焦距 相机图像质量研究(5)常见问题总结&#xff1a;光学结构对成…

检测到目标X-XSS-Protection响应头缺失

详细描述 HTTP X-XSS-Protection 响应头是 Internet Explorer&#xff0c;Chrome 和 Safari 的一个特性&#xff0c;当检测到跨站脚本攻击 (XSS)时&#xff0c;浏览器将停止加载页面。 X-XSS-Protection响应头的缺失使得目标URL更易遭受跨站脚本攻击。 解决办法 将您的服务…