josStorer / RWKV-Runner

A RWKV management and startup tool, full automation, only 8MB. And provides an interface compatible with the OpenAI API. RWKV is a large language model that is fully open source and available for commercial use.
https://www.rwkv.com
MIT License
5.05k stars 481 forks source link

feat(python backend): function call upgrade #368

Closed EliwiiKeeya closed 3 weeks ago

EliwiiKeeya commented 1 month ago

This is a draft pull request to showcase the progress of my work on the function calling.

josStorer commented 1 month ago

364

josStorer commented 1 month ago

eb89837 做了一些调整,现在function_call测试脚本能够跑通 image

josStorer commented 1 month ago

考虑到现在 tool_choice 暂时仅开发 "auto" , 因此令服务端检测请求 body 中 tools 属性是否为 None, 将 tools 属性不为 None 视作开始输出 tool call 的标志, 此时产生一个 role 为 assistant, content 为 None 的响应 开始推理并持续检查推理结果, 直到推理结果中出现 <函数名>\n```<(缺省)编程语言>\ntool_call( 的字段, 中间不产生任何响应

参考 https://github.com/josStorer/RWKV-Runner/pull/368/commits/eb898374dfde1e2a439d910f3921072701155121#diff-4e4a05876ad4fc0cfe0ed3cbdc219a3859cb0518161d9e959d52820520c87227R452, 只有当匹配到tool_call( 开始,确认AI进行了函数调用时,才返回null并开始tool_calls响应,同时stream发送函数名称。若ai回复出现了4个及以上的换行符,或累计字符串长度超过了40,或累计字符串长度超过30且没有出现过```三个点,则均视为非tool_calls响应,正常产生流式回复

先进行后处理, 例如出现的双引号(“) 替换为反斜杠+双引号 (\")以及如果是最外层括号, 那么外面加上花括号等。

以及等于号替换为冒号

总体而言设想的stream流程没什么问题,只是不用立即返回一个content: null,而是确认找到函数名时再一同返回

EliwiiKeeya commented 1 month ago

我最新的 Commit https://github.com/josStorer/RWKV-Runner/pull/368/commits/c4b9c4e3776d7b355556f495b3ea8dd83212f1c3 尝试实现了 stream 模式下处理并响应包含 tools 的用户请求 其中简单测试通过了部分判断并产生正常流式回复的部分 (tests/function_call_stream.py)

我本地难以测试这些情况, 做调试及修 bug 的工作辛苦了。 或者, 如果有可能的远程资源提供的话, 可以由我可以进一步调试功能并修复潜在的漏洞。

josStorer commented 1 month ago

远程资源没有,我这边也是自己的电脑跑的😂

josStorer commented 1 month ago

我得后天再进行测试,明天周末休息一下吧😆

josStorer commented 1 month ago

测试下来有一些问题, stream模式跑不通, 我在做调整

josStorer commented 1 month ago

做了一些调整: 现在stream模式的function call已经能够跑通: https://github.com/josStorer/RWKV-Runner/pull/368/commits/3df4302b2362b337c4237333478a545d8c3107f4

不过简单粗暴的等于号替换为冒号存在问题, 需要改为栈的方式处理

EliwiiKeeya commented 4 weeks ago

调整 stream 模式辛苦了。 现在 function call 还有需要调整的部分么? 如果没有, 我将转换为正式PR并提交 😄

josStorer commented 4 weeks ago

先转为正式PR提交吧,还有一些小细节我改一下

josStorer commented 4 weeks ago

非常感谢这么多天的工作!