-
Notifications
You must be signed in to change notification settings - Fork 1
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
访问api返回”Role must be user or assistant and Content length must be greater than 0“ #11
Comments
深入阅读代码后我还有个疑问: nonebot-plugin-with-ai-agents/llm/llms/dashscope.py Lines 60 to 70 in 1c1b96d
nonebot-plugin-with-ai-agents/llm/llms/glm.py Lines 52 to 62 in 1c1b96d
这俩段代码相似度很高,可以考虑一下整一个函数出来进行代码复用? async def api_request(url, headers, body, timeout):
async with httpx.AsyncClient() as client:
try:
result = await client.post(url, headers=headers, json=body, timeout=timeout)
except httpx.ReadTimeout as e:
logger.critical(f"访问大模型超时, {e}")
result = None
except Exception as e:
logger.critical(r.text + str(e))
raise e
return result
class GLMModel(BaseLLMModel):
...
async def ask_model(
self,
question: str,
system_prompt: str = None,
message_history: list = (),
temperature: float = 0.01
):
"""向 ChatGLM 提问
- question:用户问题
- system_prompt:系统级提示词
- message_history: 消息历史列表
"""
url = self.get_api_url()
headers = self.get_headers()
body = self.get_body_template(temperature)
if message_history:
body["messages"] = message_history
if system_prompt:
sys_msg = {"role": "system", "content": system_prompt}
body["messages"].insert(0, sys_msg)
user_message = {"role": "user", "content": question}
body["messages"].append(user_message)
if result := api_request(url, headers, body, self.timeout):
#这下面可以依据不同的更改不同的逻辑
ans = result.json()["choices"][0]["message"]["content"]
return ans 没有代码复用,这里出现了一个奇怪的try: nonebot-plugin-with-ai-agents/llm/llms/openai.py Lines 54 to 65 in 1c1b96d
把访问大模型放在代码放在 try 外面,可能不是你的本意 |
想给你发一个 pr 了(跃跃欲试 |
|
其实说白了就是有许多问题都是临时解决方案,从这个插件发布第一版的时候就有了,细节的东西比较多需要考虑的,一口气写不过来。所以有些东西会尽量写的简单,为以后的修改留有余地。 |
但是还是觉得直接回复“有错误,自己看日志,可能过期了“有些不太妥,用 Bot 的不止 Bot 的主人,非开发者看见了可能一脸懵。其实只要说明出错了可以了,比如说明”出现错误,请稍后在试或者联系 Bot 主人。“,希望后面能改改,自定义一下。 |
这里的问题已经有部分得到解决,详见 0.1.15;标题中的主要的问题待解决 |
问题
经过一段时间的聊天,Bot 开始回复消息”有错误,自己看日志,可能过期了“。
观察日志发现:
插件版本
0.1.14
Nonebot版本
2.3.0
python 版本
3.12.3
复现过程
只要启动 Bot ,聊一段时间就会出现这样的问题。
日志上下文:
截图
文字
排查
已确定不是由网络问题引发的
nonebot-plugin-with-ai-agents/llm/llms/dashscope.py
Lines 60 to 70 in 1c1b96d
通过观察源码发现这个错误是直接通过错误基类被捕获的,然后以 str 的形式与 r.text 拼接,在这里这个异常的str是'output',然后 bot 回复 ”有错误,自己看日志,可能过期了“。这是不是有点太草率了。
把 body 的内容打印出来以后,发现 role 的内容也是正常的。
The text was updated successfully, but these errors were encountered: