LLM大语言模型(十四):LangChain中Tool的不同定义方式,对prompt的影响

news2024/10/5 15:23:27

背景

ChatGLM3-6B的函数调用功能,和LangChain的Tool调用,在prompt上并没有对齐。

参考:LLM大语言模型(十二):关于ChatGLM3-6B不兼容Langchain 的Function Call_error: valueerror: caught exception: unknown forma-CSDN博客

因此在LangChain的Agent中调用ChatGLM3-6B时,单独对prompt进行了转换。

参考:LLM大语言模型(十三):ChatGLM3-6B兼容Langchain的Function Call的一步一步的详细转换过程记录_langchain+chatglm3-CSDN博客

今日发现,使用LangChain的不同方式定义的tool,之前的prompt转换失效了。

LangChain中不同Tool定义方式,生成的prompt不同

方式一:@tool

from langchain_core.tools import tool

@tool

def calculator(calculation:str)->str:

    "Useful for when you need to calculate math problems"

    calculation = calculation.replace("^", "**")

    if "sqrt" in calculation:

        calculation = calculation.replace("sqrt", "math.sqrt")

    elif "log" in calculation:

        calculation = calculation.replace("log", "math.log")

    return eval(calculation)

在Agent执行时生成的prompt如下,关注红色部分:

'System: Respond to the human as helpfully and accurately as possible. You have access to the following tools:\n\n

calculator: calculator(calculation: str) -> str - Useful for when you need to calculate math problems, args: {\'calculation\': {\'title\': \'Calculation\', \'type\': \'string\'}}\n\n

Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).\n\nValid "action" values: "Final Answer" or calculator\n\nProvide only ONE action per $JSON_BLOB, as shown:\n\n```\n{\n  "action": $TOOL_NAME,\n  "action_input": $INPUT\n}\n```\n\nFollow this format:\n\nQuestion: input question to answer\nThought: consider previous and subsequent steps\nAction:\n```\n$JSON_BLOB\n```\nObservation: action result\n... (repeat Thought/Action/Observation N times)\nThought: I know what to respond\nAction:\n```\n{\n  "action": "Final Answer",\n  "action_input": "Final response to human"\n}\n\nBegin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation\nHuman: 34 * 34\n\n\n (reminder to respond in a JSON blob no matter what)'


 

方式二:继承BaseTool

import os
import requests

from typing import Type, Any
from langchain.tools import BaseTool
from pydantic import BaseModel, Field

class WeatherInput(BaseModel):
    location: str = Field(description="the location need to check the weather")


class Weather(BaseTool):
    name = "weather"
    description = "Use for searching weather at a specific location"
    args_schema: Type[BaseModel] = WeatherInput

    def __init__(self):
        super().__init__()

    def _run(self, location: str) -> dict[str, Any]:
        weather = {
            "temperature": "20度",
            "description": "温度适中",
        }
        return weather

在Agent执行时生成的prompt如下,关注红色部分:

System: Respond to the human as helpfully and accurately as possible. You have access to the following tools:\n\n

Calculator: Useful for when you need to calculate math problems, args: {\'calculation\': {\'description\': \'calculation to perform\', \'title\': \'Calculation\', \'type\': \'string\'}}\n\n

Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).\n\nValid "action" values: "Final Answer" or Calculator\n\nProvide only ONE action per $JSON_BLOB, as shown:\n\n```\n{\n  "action": $TOOL_NAME,\n  "action_input": $INPUT\n}\n```\n\nFollow this format:\n\nQuestion: input question to answer\nThought: consider previous and subsequent steps\nAction:\n```\n$JSON_BLOB\n```\nObservation: action result\n... (repeat Thought/Action/Observation N times)\nThought: I know what to respond\nAction:\n```\n{\n  "action": "Final Answer",\n  "action_input": "Final response to human"\n}\n\nBegin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation\nHuman: 34 * 34\n\n\n (reminder to respond in a JSON blob no matter what)

因为生成的prompt不同了,所以之前的prompt转换也就失效了。

LangChain使用上的一个坑。

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

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

相关文章

AI编码工具-通义灵码功能实测(二)

AI编码工具-通义灵码功能实测(二) 通义灵码智能问答 在上一篇文章中:https://blog.csdn.net/csdn565973850/article/details/138563670?spm1001.2014.3001.5501 讲述了通义灵码的7大应用场景,这里在使用过程中遇到了一些问题&…

如何向Linux内核提交开源补丁?

2021年,我曾经在openEuler社区上看到一项改进Linux内核工具的需求,因此参与过Linux内核社区的开源贡献。贡献开源社区的流程都可以在内核社区文档中找到,但是,单独学习需要一个较长的过程,新手难以入门,因此…

springboot+vue+mybatis警情高发智能灯箱+PPT+论文+讲解+售后

时代在飞速进步,每个行业都在努力发展现在先进技术,通过这些先进的技术来提高自己的水平和优势,警情高发智能灯箱当然不能排除在外。警情高发智能灯箱是在实际应用和软件工程的开发原理之上,运用微信开发者、java语言以及SpringBo…

超标量处理器设计:寄存器重命名(1)介绍

★继续学习体系结构的知识。 指令之间的相关性 1.数据相关性 具体分为三类: (1) Output Dependence (WAW):当两条指令尝试向同一个寄存器写入数据时发生。后面的写操作必须等待前面的写操作完成,因为它们争夺同一资源。 (2) Anti-Depende…

大厂面试sql手撕题目总结

文章目录 1. 常用函数1. 日期函数 2. 行转列(转置)1. 行转列 连续N天登录1. 查询出连续三天登录的人员姓名 N日留存率1. 求用户当天,次日,七日留存率 分组内topN1. 求出每个部门工资最高的前三名员工,并计算这些员工的…

AWS Cli Windows安装配置

1. 安装 下载地址:AWS 命令行界面(CLI)_管理AWS服务的统一工具-AWS云服务 检验安装: > aws --version aws-cli/2.15.44 Python/3.11.8 Windows/10 exe/AMD64 prompt/off 2. 创建IAM用户 1) 创建组 选择IAM 点击创建组 填写用户组名,…

YOLOv5改进 | 独家创新篇 | 利用MobileNetV4的UIB模块二次创新C3(全网独家首发)

一、本文介绍 本文给大家带来的改进机制是利用MobileNetV4的UIB模块二次创新C3,其中UIB模块来自2024.5月发布的MobileNetV4网络,其是一种高度优化的神经网络架构,专为移动设备设计。它最新的改动总结主要有两点,采用了通用反向瓶…

synchronized关键字和ReentrantLock锁区别

synchronized关键字和ReentrantLock锁是Java中用于同步的两个重要机制,它们在很多方面有所不同: 1. **锁定范围**: synchronized关键字只能在方法的执行过程中提供锁定,而ReentrantLock可以锁定任何对象,包括方法、代码块和对象。…

django项目结构介绍

小白的django学习笔记 五一前的某天 文章目录 django项目结构介绍项目的基本配置templates项目模块manage.pyExternal Libraries django项目结构介绍 项目的基本配置 在这里配置,跟工程名是一样的 templates 放网页、js、css的地方 django 项目模块 项目开发时&…

Android Studio高版本安卓模拟器抓取https包

Android Studio avd 设置 证书生成 *.cer格式证书​ openssl x509 -inform DER -subject\_hash\_old -in charles-ssl-proxying-certificate.cer​ *.pem格式证书​ openssl x509 -inform PEM -subject\_hash\_old -in charles-ssl-proxying-certificate.pem会输出 2cb30a9e …

QX------mini51单片机学习------(5)数码管的静态与动态显示

目录 1数码管应用场景 2数码管显示原理 3静态与动态显示 474HC573锁存器工作原理 5上拉电阻的作用 6原理图分析 7实践 1数码管应用场景 2数码管显示原理 图(b)左边是共阴极,右边是共阳极 GND是公共极,可以用万用表测&am…

【JavaEE初阶系列】——理解Cookie 和 Session以及相关API

目录 🚩Cookie 🚩Session 🚩Servlet API 🎈HttpServletRequest 🎈HttpSession 🚩Cookie http请求header中一个属性,浏览器持久化存储数据的一种机制,网页无法访问主机的文件系…

技术速递|Python in Visual Studio Code 2024年4月发布

排版:Alan Wang 我们很高兴地宣布 Visual Studio Code 的 Python 和 Jupyter 扩展 2024 年 4 月发布! 此版本包括以下公告: 改进了 Flask 和 Django 的调试配置流程Jupyter Run Dependent Cells with Pylance 的模块和导入分析Hatch 环境发…

【Leetcode每日一题】 综合练习 - 逆波兰表达式求值(难度⭐⭐)(73)

1. 题目解析 题目链接&#xff1a;150. 逆波兰表达式求值 这个问题的理解其实相当简单&#xff0c;只需看一下示例&#xff0c;基本就能明白其含义了。 2.算法原理 数据结构选择&#xff1a; 使用栈&#xff08;stack<int>&#xff09;来存储操作数&#xff0c;以便进…

OpenHarmony 3.2 Release版本实战开发——Codec HDI适配过程

简介 OpenHarmony Codec HDI&#xff08;Hardware Device Interface&#xff09;驱动框架基于 OpenMax 实现了视屏硬件编解码驱动&#xff0c;提供 Codec 基础能力接口供上层媒体服务调用&#xff0c;包括获取组件编解码能力、创建组件、参数设置、数据的轮转和控制、以及销毁…

OpenMVS学习笔记(一):WSL编译安装测试

1.CUDA和CUDNN安装 [1] WSL版本cuda安装&#xff1a; >> wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin >> sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600 >> wg…

Burp Suite (bp)启动激活报错问题

一、自查 1、从哪里下载程序 【大部分同学的问题&#xff0c;都是重新下载解决的&#xff0c;简单快速直接】 一律使用“常用软件”中提供的Burp Suite软件包&#xff0c;方便排查问题 自带JDK的版本 Burp 2024.3&#xff0c;自带JDK21&#xff0c;双击bat即可启动&#xff…

YOLOv8网络结构介绍

将按照YOLOv8目标检测任务、实例分割任务、关键点检测任务以及旋转目标检测任务的顺序来介绍&#xff0c;主要内容也是在目标检测任务中介绍&#xff0c;其他任务也只是Head层不相同。 1.YOLOv8_det网络结构 首先&#xff0c;YOLOv8网络分成了三部分&#xff0c;分别是主干网络…

Java | Spring框架| 集合与复杂类型的注入:处理多值依赖

集合与复杂类型的注入&#xff1a;处理多值依赖 在Spring框架中&#xff0c;除了可以注入简单的依赖类型&#xff0c;如字符串、整数等&#xff0c;还可以注入集合类型和复杂类型的数据。集合类型包括List、Set、Map和数组&#xff0c;而复杂类型可以是自定义的对象、配置类等…

【JavaEE网络】用Form与Ajax构建HTTP请求

目录 通过 form 表单构造 HTTP 请求form 发送 GET 请求form 发送 POST 请求 通过 ajax 构造 HTTP 请求发送 GET 请求发送 POST 请求发送 application/json 数据封装 ajax 方法 通过 form 表单构造 HTTP 请求 form (表单) 是 HTML 中的一个常用标签. 可以用于给服务器发送 GET …