Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openai 中的 GPTAPI 的参数配置错误导致除非流式外的智能体无法处理通义千问模型的响应 #286

Open
wojiaoyishang opened this issue Dec 22, 2024 · 1 comment

Comments

@wojiaoyishang
Copy link
Contributor

所用版本

提交 #285,main 分支。

问题概述

使用如下代码尝试获取智能体回复时:

from lagent.agents import Agent
from lagent.schema import AgentMessage
from lagent.llms import GPTAPI

# qwen 智能体配置来自项目 MindSearch
url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation"
qwen = dict(
    type=GPTAPI,
    model_type="qwen-max-longcontext",
    key="sk-apikey",
    api_base=url,
    meta_template=[
        dict(role="system", api_role="system"),
        dict(role="user", api_role="user"),
        dict(role="assistant", api_role="assistant"),
        dict(role="environment", api_role="system"),
    ],
    top_p=0.8,
    temperature=0,
    max_new_tokens=4096,
    repetition_penalty=1.02,
    stop_words=["<|im_end|>"],
)

system_prompt = '你的回答只能从“典”、“孝”、“急”三个字中选一个。'
agent = Agent(qwen, system_prompt)

user_msg = AgentMessage(sender='user', content='今天天气情况')
bot_msg = agent(user_msg)
print(bot_msg)

终端输出:

image

经过定位来源于文件 lagent/llms/openai.py

image

原因概述

导致 JSON 无法解码的原因是 JSON 数据开头携带了

id:1
event:result
:HTTP_STATUS/200

此项内容出现的原因是由于在请求通义千问模型的时候在请求头携带了 X-DashScope-SSE: enbale 参数,根据 通义千问API文档 此参数应该仅需要在流式请求时携带即可:

image

image

image

解决方法

在非流式的请求中直接注释掉 header['X-DashScope-SSE'] = 'enable' 修改流式请求逻辑,或者在解码 JSON 时预处理,并在 return response['choices'][0]['message']['content'].strip() 中增加对 output 的选取:

image

补充说明

难点在于如果仅是对 header['X-DashScope-SSE'] = 'enable' 注释,需要考虑流式下的请求问题,需要重写部分代码,此外,在第 265 行的意外用于调试的 print 是否是忘记删除了?

image

刚开始研究此项目和 MindSearch ,部分表述不当见谅。晚些开窍了尝试提交PR (*^▽^*)

@wojiaoyishang
Copy link
Contributor Author

补充:通义千问提供两套接口,一套是类 openai 接口,另一套是 DashScope,现有代码基于的是 DashScope 接口并进行流式输出,存在BUG,属于是混用了,现在思考的解决方案是完全采用类 openai 接口书写,然后单独写 DashScope 的接口,因为按照代码目录结构来讲,lagent/llms/openai.py 中应该只存在类 openai 接口,而不是进行混用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant