无需微调,仅用提示词工程就能让 LLM 获得 tool calling 的功能

无需微调,仅用提示词工程就能让 LLM 获得 tool calling 的功能

无需微调,仅用提示词工程就能让 LLM 获得 tool calling 的功能 无需微调,仅用提示词工程就能让 LLM 获得 tool calling 的功能 Modified August 1, 2024 当使用提示词工程时,图中 is tools in sys prompt 属性为 enable,gemma2 9b 模型给出了正确的实时天气信息。 无代码开发 1、Comfyui 下载 如果你是一个 AI 绘图工作者,你一定对 Comfyui 并不陌生。如果你从来没有接触过 Comfyui,你需要先到 GitHub 上拉取 Comfyui 这个项目,或者你可以在 B 站下载整合包,例如:秋叶大佬的绘世整合包或者只剩一瓶辣椒酱的铁锅炖整合包。 https://github.com/comfyanonymous/ComfyUI GitHub comfyanonymous/ComfyUI: The most powerful and modular stable diffusion GUI, api and backend The most powerful and modular stable diffusion GUI, api and backend with a graph/nodes interface. comfyanonymous/ComfyUI 2、Comfyui LLM party 下载 Comfyui LLM party 是一个以 Comfyui 作为前端的节点库,用于 LLM 智能体以及 LLM 工作流的无代码开发。功能类似于 coze、dify、flowise 等。但由于可以在 Comfyui 中直接使用,与 Comfyui 生态下的绝大部分节点都相辅相成,有着无缝接入 SD 图像流的特色。今天所介绍的麦洛薇,就是用这套节点库开发完成的。项目地址如下: https://github.com/heshengtao/comfyui LLM party.git GitHub heshengtao/comfyui LLM party: Dify in comfyui is compatible with Omost,ChatTTS, and adapts Dify in comfyui is compatible with Omost,ChatTTS, and adapts to all models with similar openai interfaces, such as ollama, qwen, GLM, deepseek, moonshot,doubao. Adapted to local models such as llam... 使用以下方法之一安装: • 方法一: 1. 在 comfyui 管理器中搜索comfyui LLM party,一键安装 2. 重启 comfyui • 方法二: 1. 导航到 ComfyUI 根文件夹中下的custom nodes子文件夹 2. 使用克隆此存储库。git clone https://github.com/heshengtao/comfyui LLM party.git 3、工作流搭建 打开 Comfyui 界面后,右键点击界面,即可找到 Comfyui LLM party 的目录,你既可以如下图一样,学习如何手动连接这些节点,从而实现一个最简单的 AI 女友工作流,也可以直接将工具调用对比实验工作流文件拖拽到 Comfyui 界面中一键复刻我的提示词工程实验。 4、启动 ollama 从 ollama 的 github 仓库找到对应版本并下载: https://github.com/ollama/ollama GitHub ollama/ollama: Get up and running with Llama 3.1, Mistral, Gemma 2, and other large languag Get up and running with Llama 3.1, Mistral, Gemma 2, and other large language models. ollama/ollama 启动 ollama 后,在 cmd 中输入ollama run gemma2将自动下载gemma2模型到本地并启动。将 ollama 的默认base URL=http://127.0.0.1:11434/v1/以及api key=ollama填入 LLM 加载器节点即可调用 ollama 中的模型进行实验。 5、常见报错及解决方案 1. 如果 ollama 连接不上,很可能是代理服务器的问题,请将你的127.0.0.1:11434添加到不使用代理服务器的列表中。 实现原理 本文采用的提示词工程主要有两部分代码组成:提示词注入和工具结果回传。提示词注入用于将工具信息以及使用工具的提示词添加到系统提示中。工具结果回传则是解析 tool calling 的输出,并将工具返回的内容再次嵌入 LLM。 1、提示词注入阶段 Code block Python if tools is not None: tools dis = json.loads(tools) for tool dis in tools dis: tools list.append(tool dis["function"]) tools instructions = "" tools instruction list = [] for tool in tools list: tools instruction list.append(tool["name"]) tools instructions += ( str(tool["name"]) + ":" + "Call this tool to interact with the " + str(tool["name"]) + " API. What is the " + str(tool["name"]) + " API useful for? " + str(tool["description"]) + ". Parameters:" + str(tool["parameters"]) + "Required parameters:" + str(tool["parameters"]["required"]) + "\n" ) TOOL EAXMPLE = "You will receive a JSON string containing a list of callable tools. Please parse this JSON string and return a JSON object containing the tool name and tool parameters. Here is an example of the tool list:\n\n{\"tools\": [{\"name\": \"plus one\", \"description\": \"Add one to a number\", \"parameters\": {\"type\": \"object\",\"properties\": {\"number\": {\"type\": \"string\",\"description\": \"The number that needs to be changed, for example: 1\",\"default\": \"1\",}},\"required\": [\"number\"]}},{\"name\": \"minus one\", \"description\": \"Minus one to a number\", \"parameters\": {\"type\": \"object\",\"properties\": {\"number\": {\"type\": \"string\",\"description\": \"The number that needs to be changed, for example: 1\",\"default\": \"1\",}},\"required\": [\"number\"]}}]}\n\nBased on this tool list, generate a JSON object to call a tool. For example, if you need to add one to number 77, return:\n\n{\"tool\": \"plus one\", \"parameters\": {\"number\": \"77\"}}\n\nPlease note that the above is just an example and does not mean that the plus one and minus one tools are currently available." REUTRN FORMAT="{\"tool\": \"tool name\", \"parameters\": {\"parameter name\": \"parameter value\"}}" INSTRUCTION = f""" {TOOL EAXMPLE} Answer the following questions as best you can. You have access to the following APIs: {tools instructions} Use the following format: Please choose the appropriate tool according to the user's question. If you don't need to call it, please reply directly to the user's question. When the user communicates with you in a language other than English, you need to communicate with the user in the same language. When you have enough information from the tool results, respond directly to the user with a text message without having to call the tool again. """ system prompt=INSTRUCTION INSTRUCTION为最后注入到系统提示中的字符串,他又包含了TOOL EAXMPLE、tools instructions、REUTRN FORMAT三个部分。TOOL EAXMPLE用于提示 LLM 如何理解 tool 以及如何使用 tool。在编写TOOL EAXMPLE时,请注意用一些无关紧要的工具作为示例,例如本文使用的将数字加一和数字减一的工具,从而避免 LLM 混淆真正可以使用的工具与示例工具。tools instructions是由目前通用的工具字典转换成 LLM 可读的工具列表。实际使用 LLM 时,可以通过输入不同的工具来动态调整tools instructions,让 LLM 得知目前可用的工具有哪些以及如何使用。REUTRN FORMAT定义了调用 API 的格式。 2、工具结果回传阶段 利用正则表达式抓取输出中的"tool"和"parameters"参数。对于 interpreter 工具,使用了另一种正则表达式来提取 LLM 输出的代码,提高 LLM 使用 interpreter 工具的成功率。本文使用代码如下: Code block Python history.append({ "role": "user", "content": user prompt.strip() }) response= model.create chat completion( messages = history, max tokens=max length, temperature=temperature, ) response content=response['choices'][0]['message']['content'] pattern = r'\{\s "tool":\s "(. ?)",\s "parameters":\s \{(. ?)\}\s \}' while re.search(pattern, response content, re.DOTALL)!=None: match=re.search(pattern, response content, re.DOTALL) tool = match.group(1) parameters = match.group(2) json str = '{"tool": "' + tool + '", "parameters": {' + parameters + '}}' parameters = json.loads('{' +parameters+ '}') results = dispatch tool(tool, parameters) print(results) history.append({"role":"assistant", "content": json str}) history.append({"role": "observation", "content": results}) response= model.create chat completion( messages = history, max tokens=max length, temperature=temperature, ) response content = response.choices[0].message.content pattern = r" " while re.search(pattern, response, re.DOTALL) !=None: matches = re.search(pattern, response, re.DOTALL) code = matches.group(1) results = interpreter(code) print(results) json str = '{"tool": "interpreter", "parameters": '+code+'}' history.append({"role":"assistant", "content": json str}) history.append({"role": "observation", "content": results}) response= model.create chat completion( messages = history, max tokens=max length, temperature=temperature, ) response content = response.choices[0].message.content 通过识别 LLM 返回的调用工具的字典,提取出对应的值,再传入相应的工具函数,最后将工具返回的结果以observation的角色返回给 LLM。对于一些不接受observation、tool、function角色的 LLM 接口,可以改为回传给 user 角色,例如: Code block Python history.append( { "role": "user", "content": "Call" + tool + "The result returned by the tool is:" + results + ". Please continue to answer my previous question based on the result returned by the tool." } ) 通过以上提示词工程,可以避免微调,让完全没有 tool calling 能力的 LLM 获得稳定的 tool calling 能力。 写到最后 1. 如果你使用 comfyui LLM party 时出现错误,或你有一些新的想法,欢迎加 QQ 群:931057213 2. github 项目地址: https://github.com/heshengtao/comfyui LLM party GitHub heshengtao/comfyui LLM party: Dify in comfyui is compatible with Omost,ChatTTS, and adapts Dify in comfyui is compatible with Omost,ChatTTS, and adapts to all models with similar openai interfaces, such as ollama, qwen, GLM, deepseek, moonshot,doubao. Adapted to local models such as llam... 3. 视频教程: https://www.bilibili.com/video/BV1JZ421v7Tw/?spm id from=333.999.0.0&vd source=f229e378448918b84afab7c430c6a75b 【ComfyUI×LLM】手把手教你如何搭建积木化智能体(超简单!) 哔哩哔哩 bilibili 好久不见!哪个男孩可以拒绝拼高达的快乐呢,哪个女孩可以拒绝挑穿搭的快乐呢,为了让更多的人可以用搭积木的方式搭建属于自己的LLM专属机器人,我就想要开发这样一款comfyui插件:我在GitHub上开源的项目,叫作comfyui LLM party,该项目希望基于comfyui开发一套完整的用于LLM工作流搭建的节点库。可以让用户更便捷快速地搭建自己的LLM工作流,并且更方便地接入自己的SD工作流中 comfyui 管理器 工具调用对比实验 当使用提示词工程时,图中 is tools in sys prompt 属性为 enable,gemma2 9b 模型给出了正确的实时天气信息。 无代码开发 1、Comfyui 下载 如果你是一个 AI 绘图工作者,你一定对 Comfyui 并不陌生。如果你从来没有接触过 Comfyui,你需要先到 GitHub 上拉取 Comfyui 这个项目,或者你可以在 B 站下载整合包,例如:秋叶大佬的绘世整合包或者只剩一瓶辣椒酱的铁锅炖整合包。 https://github.com/comfyanonymous/ComfyUI GitHub comfyanonymous/ComfyUI: The most powerful and modular stable diffusion GUI, api and backend The most powerful and modular stable diffusion GUI, api and backend with a graph/nodes interface. comfyanonymous/ComfyUI 2、Comfyui LLM party 下载 Comfyui LLM party 是一个以 Comfyui 作为前端的节点库,用于 LLM 智能体以及 LLM 工作流的无代码开发。功能类似于 coze、dify、flowise 等。但由于可以在 Comfyui 中直接使用,与 Comfyui 生态下的绝大部分节点都相辅相成,有着无缝接入 SD 图像流的特色。今天所介绍的麦洛薇,就是用这套节点库开发完成的。项目地址如下: https://github.com/heshengtao/comfyui LLM party.git GitHub heshengtao/comfyui LLM party: Dify in comfyui is compatible with Omost,ChatTTS, and adapts Dify in comfyui is compatible with Omost,ChatTTS, and adapts to all models with similar openai interfaces, such as ollama, qwen, GLM, deepseek, moonshot,doubao. Adapted to local models such as llam... 使用以下方法之一安装: • 方法一: 1. 在 comfyui 管理器中搜索comfyui LLM party,一键安装 comfyui 管理器 2. 重启 comfyui • 方法二: 1. 导航到 ComfyUI 根文件夹中下的custom nodes子文件夹 2. 使用克隆此存储库。git clone https://github.com/heshengtao/comfyui LLM party.git 3、工作流搭建 打开 Comfyui 界面后,右键点击界面,即可找到 Comfyui LLM party 的目录,你既可以如下图一样,学习如何手动连接这些节点,从而实现一个最简单的 AI 女友工作流,也可以直接将工具调用对比实验工作流文件拖拽到 Comfyui 界面中一键复刻我的提示词工程实验。 工具调用对比实验 4、启动 ollama 从 ollama 的 github 仓库找到对应版本并下载: https://github.com/ollama/ollama GitHub ollama/ollama: Get up and running with Llama 3.1, Mistral, Gemma 2, and other large languag Get up and running with Llama 3.1, Mistral, Gemma 2, and other large language models. ollama/ollama 启动 ollama 后,在 cmd 中输入ollama run gemma2将自动下载gemma2模型到本地并启动。将 ollama 的默认base URL=http://127.0.0.1:11434/v1/以及api key=ollama填入 LLM 加载器节点即可调用 ollama 中的模型进行实验。 5、常见报错及解决方案 1. 如果 ollama 连接不上,很可能是代理服务器的问题,请将你的127.0.0.1:11434添加到不使用代理服务器的列表中。 实现原理 本文采用的提示词工程主要有两部分代码组成:提示词注入和工具结果回传。提示词注入用于将工具信息以及使用工具的提示词添加到系统提示中。工具结果回传则是解析 tool calling 的输出,并将工具返回的内容再次嵌入 LLM。 1、提示词注入阶段 tool json {REUTRN FORMAT} INSTRUCTION为最后注入到系统提示中的字符串,他又包含了TOOL EAXMPLE、tools instructions、REUTRN FORMAT三个部分。TOOL EAXMPLE用于提示 LLM 如何理解 tool 以及如何使用 tool。在编写TOOL EAXMPLE时,请注意用一些无关紧要的工具作为示例,例如本文使用的将数字加一和数字减一的工具,从而避免 LLM 混淆真正可以使用的工具与示例工具。tools instructions是由目前通用的工具字典转换成 LLM 可读的工具列表。实际使用 LLM 时,可以通过输入不同的工具来动态调整tools instructions,让 LLM 得知目前可用的工具有哪些以及如何使用。REUTRN FORMAT定义了调用 API 的格式。 2、工具结果回传阶段 利用正则表达式抓取输出中的"tool"和"parameters"参数。对于 interpreter 工具,使用了另一种正则表达式来提取 LLM 输出的代码,提高 LLM 使用 interpreter 工具的成功率。本文使用代码如下: python\n(. ?)\n 通过识别 LLM 返回的调用工具的字典,提取出对应的值,再传入相应的工具函数,最后将工具返回的结果以observation的角色返回给 LLM。对于一些不接受observation、tool、function角色的 LLM 接口,可以改为回传给 user 角色,例如: 通过以上提示词工程,可以避免微调,让完全没有 tool calling 能力的 LLM 获得稳定的 tool calling 能力。 写到最后 1. 如果你使用 comfyui LLM party 时出现错误,或你有一些新的想法,欢迎加 QQ 群:931057213 2. github 项目地址: https://github.com/heshengtao/comfyui LLM party GitHub heshengtao/comfyui LLM party: Dify in comfyui is compatible with Omost,ChatTTS, and adapts Dify in comfyui is compatible with Omost,ChatTTS, and adapts to all models with similar openai interfaces, such as ollama, qwen, GLM, deepseek, moonshot,doubao. Adapted to local models such as llam... 3. 视频教程: https://www.bilibili.com/video/BV1JZ421v7Tw/?spm id from=333.999.0.0&vd source=f229e378448918b84afab7c430c6a75b 【ComfyUI×LLM】手把手教你如何搭建积木化智能体(超简单!) 哔哩哔哩 bilibili 好久不见!哪个男孩可以拒绝拼高达的快乐呢,哪个女孩可以拒绝挑穿搭的快乐呢,为了让更多的人可以用搭积木的方式搭建属于自己的LLM专属机器人,我就想要开发这样一款comfyui插件:我在GitHub上开源的项目,叫作comfyui LLM party,该项目希望基于comfyui开发一套完整的用于LLM工作流搭建的节点库。可以让用户更便捷快速地搭建自己的LLM工作流,并且更方便地接入自己的SD工作流中 大家好,我是一个开源项目作者 ailm。目前,绝大多数小型本地开源大语言模型以及部分商用大模型接口都不支持稳定的 tool calling 功能。而现有的解决方案是微调 LLM,这导致大量的时间和算力被浪费。本文提出一种仅使用提示词工程和一些精巧的代码设计,即可让 LLM 获得稳定的 tool calling 能力。本文使用多个不具备 tool calling 能力的 LLM 作为测试模型,在多个工具调用任务上进行实验,成功率为 100%。本文所有工作是基于 comfyui 开发,适合完全没有代码基础的小伙伴们复现并且按自己的想法修改我的工作。 效果展示 1、实验任务 本文以目前主流的小型开源模型 llama3 8b、gemma2 9b、qwen2 7b、mistral 7b 的 ollama 量化版本作为测试模型,将如下调用工具作为测试任务,并以不同的提问方式测试 10 次: 1. 查询不同时区的实时时间; 2. 查询不同地点的实时天气; 3. 谷歌搜索相关信息后回答最近发生的事情; 4. 使用 python 解释器计算数学问题; 5. 搜索本地文件信息回答问题; 6. 查询 arxiv 上的相关论文; 7. 搜索本地知识图谱回答问题。 2、实验设备及平台 测试使用的设备为 nvidia 的 GeForce RTX 4080,搭建智能体的平台为笔者 ailm 自己开发的开源项目: https://github.com/heshengtao/comfyui LLM party GitHub heshengtao/co

在 小宇宙note 阅读完整内容