Skip to content

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

Open
@wojiaoyishang

Description

@wojiaoyishang

所用版本

提交 #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 (*^▽^*)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions