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

背景

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

python 复制代码
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\`\`\`\\nJSON_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\`\`\`\\nJSON_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使用上的一个坑。

相关推荐
win4r2 小时前
🚀DeepSeek又放大招!这个OCR模型让文档识别效率倍增!本地部署+客观实测DeepSeek-OCR!OCR识别准确率97%,支持100+语言,每天处理3
llm·aigc·deepseek
后端小肥肠4 小时前
【n8n入门系列】3 种方法搞定 n8n 生图!最多3步,小白也能学会的自动化教程
人工智能·openai·agent
大模型教程4 小时前
一套完整的 RAG 脚手架,附完整代码,基于LangChain
程序员·langchain·llm
刘安然4 小时前
扣子工作流Ai Agent教程一站解锁扣子工作流
agent
AI大模型4 小时前
大模型 AI Agent 科研从入门到精通:完整路线图
程序员·llm·agent
yaocheng的ai分身4 小时前
瑞·达利欧的AI克隆以及我们对AI克隆的期待
llm·agent
AI大模型4 小时前
AI Agent开发路线图2025:从入门到精通,一文读懂智能体技术
程序员·llm·agent
猫头虎5 小时前
DeepSeek刚刚开源了一个3B的 OCR模型:什么是DeepSeek-OCR?单张A100-40G每天可以处理20万+页文档
人工智能·开源·whisper·prompt·aigc·ocr·gpu算力
景天科技苑5 小时前
【AI智能体开发】什么是LLM?如何在本地搭建属于自己的Ai智能体?
人工智能·llm·agent·智能体·ai智能体·ollama·智能体搭建
韩宁羽5 小时前
从0到1,LangChain+RAG全链路实战AI知识库
langchain