当前位置:   article > 正文

LLM大语言模型(十四):LangChain中Tool的不同定义方式,对prompt的影响_reminder to always respond with a valid json blob

reminder to always respond with a valid json blob

背景

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

  1. from langchain_core.tools import tool
  2. @tool
  3. def calculator(calculation:str)->str:
  4.     "Useful for when you need to calculate math problems"
  5.     calculation = calculation.replace("^", "**")
  6.     if "sqrt" in calculation:
  7.         calculation = calculation.replace("sqrt", "math.sqrt")
  8.     elif "log" in calculation:
  9.         calculation = calculation.replace("log", "math.log")
  10.     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

  1. import os
  2. import requests
  3. from typing import Type, Any
  4. from langchain.tools import BaseTool
  5. from pydantic import BaseModel, Field
  6. class WeatherInput(BaseModel):
  7. location: str = Field(description="the location need to check the weather")
  8. class Weather(BaseTool):
  9. name = "weather"
  10. description = "Use for searching weather at a specific location"
  11. args_schema: Type[BaseModel] = WeatherInput
  12. def __init__(self):
  13. super().__init__()
  14. def _run(self, location: str) -> dict[str, Any]:
  15. weather = {
  16. "temperature": "20度",
  17. "description": "温度适中",
  18. }
  19. 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使用上的一个坑。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号