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

docker配置智谱AI环境变量运行报错,已给出相应的解决方案 #2289

Open
2 tasks done
FEEHarrison opened this issue Sep 5, 2024 · 5 comments
Open
2 tasks done

Comments

@FEEHarrison
Copy link

前置确认

  • 我确认我运行的是最新版本的代码,并且安装了所需的依赖,在FAQS中也未找到类似问题。

⚠️ 搜索issues中是否已存在类似问题

  • 我已经搜索过issues和disscussions,没有跟我遇到的问题相关的issue

操作系统类型?

MacOS

运行的python版本是?

python 3.8

使用的chatgpt-on-wechat版本是?

Latest Release

运行的channel类型是?

wx(个人微信, itchat)

复现步骤 🕹

docker-compose文件配置

version: '2.0'
services:
  chatgpt-on-wechat:
    image: zhayujie/chatgpt-on-wechat
    container_name: 3a8f3204
    security_opt:
      - seccomp:unconfined
    environment:
      TZ: 'Asia/Shanghai'
      MODEL: 'GLM-4'
      PROXY: ''
      SINGLE_CHAT_PREFIX: '[""]'
      SINGLE_CHAT_REPLY_PREFIX: '[""]'
      GROUP_CHAT_PREFIX: '["小助手"]'
      GROUP_NAME_WHITE_LIST: '[""]'
      IMAGE_CREATE_PREFIX: '["画"]'
      CONVERSATION_MAX_TOKENS: '2500'
      SPEECH_RECOGNITION: 'False'
      CHARACTER_DESC: ''
      EXPIRES_IN_SECONDS: 3600
      USE_GLOBAL_PLUGIN_CONFIG: 'True'
      USE_LINKAI: 'False'
      LINKAI_API_KEY: ''
      LINKAI_APP_CODE: ''
      TEMPERATURE: '0.6'
      ZHIPU_AI_API_KEY: "xxx.xxx"
      ZHIPU_AI_API_BASE: "https://open.bigmodel.cn/api/paas/v4"

问题描述 😯

从你提供的错误信息中,可以看到两个主要的问题:

num_tokens_from_messages() is not implemented for model GLM-4 这表明在使用 GLM-4 模型时,代码逻辑尝试计算消息的 token 数量,但目前没有为 GLM-4 实现该功能。系统默认返回的是 gpt-3.5-turbo 的 token 数计算方式。这表明你的代码在处理不同模型时可能没有处理得当。

Incorrect API key provided: YOUR API KEY 错误提示 API 密钥无效。这是因为你在代码或配置中提供了错误的 API 密钥,也可能是你在调用 OpenAI 的 API 而不是 Zhipu AI 的 API。

问题原因分析

  1. 关于 num_tokens_from_messages()
    num_tokens_from_messages() 函数是用于计算消息中 token 数量的,但该函数可能只针对 OpenAI 的 GPT 模型有效。如果你使用 GLM-4,需要确保在代码中处理该模型时不调用该函数,或者为 GLM-4 模型实现相应的 token 计算逻辑。

检查文件 /mnt/data/chatgpt-on-wechat/chatgpt-on-wechat/bot/zhipuai/zhipuai_bot.py 或相关代码中的 token 计算逻辑,特别是 num_tokens_from_messages() 的调用。你可能需要为 GLM-4 实现一个自定义的 token 计算方法,或者跳过这个步骤。

  1. 关于 API Key 错误
    错误信息表明代码正在调用 OpenAI 的 API (openai.ChatCompletion.create()),并且 API Key 错误,说明配置中提供的 ZHIPU_AI_API_KEY 和 ZHIPU_AI_API_BASE 并没有在正确的地方被使用,仍在使用 OpenAI 的 API 逻辑。

你需要确保:

确认 docker-compose.yml 中的 ZHIPU_AI_API_KEY 和 ZHIPU_AI_API_BASE 已正确传递到代码中。
确认在代码中调用 Zhipu AI API 时,正确使用了 ZHIPU_AI_API_KEY 和 ZHIPU_AI_API_BASE,而不是错误地调用了 OpenAI 的 API。
修复方法

  1. 修复 num_tokens_from_messages()
    你可以通过添加模型判断条件来避免在 GLM-4 模型下调用不适用的函数。

python

if model == "GLM-4":
    # Implement or skip token calculation for GLM-4
    cur_tokens = custom_token_calculation_for_glm_4(messages)
else:
    cur_tokens = num_tokens_from_messages(messages, model)
  1. 确保 Zhipu AI API 被正确调用
    检查代码中的 API 调用逻辑,确保在调用 Zhipu AI API 时,使用的是正确的 ZHIPU_AI_API_KEY 和 ZHIPU_AI_API_BASE。特别是在 zhipuai_bot.py 中,API 的调用应如下所示:

python

response = ZhipuAI.create_completion(session.messages, **self.args)

如果仍在使用 OpenAI 的 API,请确保逻辑正确切换为 Zhipu AI 的 API。

此外,确保代码中打印日志以调试是否正确加载了 Zhipu AI 的 API Key 和 Base URL:

python

logger.info(f"Using Zhipu API Key: {conf().get('zhipu_ai_api_key')}")
logger.info(f"Using Zhipu API Base: {conf().get('zhipu_ai_api_base')}")

总结
你需要修复 num_tokens_from_messages() 函数在 GLM-4 模型下的调用问题。
确保在代码中正确使用 Zhipu AI 的 API,而不是 OpenAI 的 API。
检查环境变量是否正确传递并在代码中生效,避免使用错误的 API 密钥。

终端日志 📒

- [CHATGPT] query=你是谁 [WARNING][2024-09-06 05:09:33][chat_gpt_session.py:86] - num_tokens_from_messages() is not implemented for model GLM-4. Returning num tokens assuming gpt-3.5-turbo. [ERROR][2024-09-06 05:09:33][chat_gpt_bot.py:155] - [CHATGPT] Exception: Incorrect API key provided: YOUR API KEY. You can find your API key at https://platform.openai.com/account/api-keys. Traceback (most recent call last): File "/app/bot/chatgpt/chat_gpt_bot.py", line 123, in reply_text response = openai.ChatCompletion.create(api_key=api_key, messages=session.messages, **args) File "/usr/local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 25, in create return super().create(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 153, in create response, _, api_key = requestor.request( File "/usr/local/lib/python3.10/site-packages/openai/api_requestor.py", line 298, in request resp, got_stream = self._interpret_response(result, stream) File "/usr/local/lib/python3.10/site-packages/openai/api_requestor.py", line 700, in _interpret_response self._interpret_response_line( File "/usr/local/lib/python3.10/site-packages/openai/api_requestor.py", line 763, in _interpret_response_line raise self.handle_error_response( openai.error.AuthenticationError: Incorrect API key provided: YOUR API KEY. You can find your API key at https://platform.openai.com/account/api-keys. [INFO][2024-09-06 05:09:34][wechat_channel.py:217] - [WX] sendMsg=Reply(type=ERROR, content=[ERROR] 我现在有点累了,等会再来吧), receiver=@4383e9f13cc0ce3df448191ce9010014
@6vision
Copy link
Collaborator

6vision commented Sep 15, 2024

看你日志,应该是配置文件没生效,走的还是gpt模型

@FEEHarrison
Copy link
Author

看你日志,应该是配置文件没生效,走的还是gpt模型
我看过config.py,配置后还是有问题,使用智谱ai的标准格式除了api和base还需要配置什么吗?
这是我的报错:
00:56:29][chat_gpt_bot.py:49] - [CHATGPT] query=你好 [WARNING][2024-09-18 00:56:34][chat_gpt_session.py:86] - num_tokens_from_messages() is not implemented for model GLM-4-Flash. Returning num tokens assuming gpt-3.5-turbo. [ERROR][2024-09-18 00:56:36][chat_gpt_bot.py:155] - [CHATGPT] Exception: Incorrect API key provided: YOUR API KEY. You can find your API key at https://platform.openai.com/account/api-keys. Traceback (most recent call last): File "/app/bot/chatgpt/chat_gpt_bot.py", line 123, in reply_text response = openai.ChatCompletion.create(api_key=api_key, messages=session.messages, **args) File "/usr/local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 25, in create return super().create(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 153, in create response, _, api_key = requestor.request( File "/usr/local/lib/python3.10/site-packages/openai/api_requestor.py", line 298, in request resp, got_stream = self._interpret_response(result, stream) File "/usr/local/lib/python3.10/site-packages/openai/api_requestor.py", line 700, in _interpret_response self._interpret_response_line( File "/usr/local/lib/python3.10/site-packages/openai/api_requestor.py", line 763, in _interpret_response_line raise self.handle_error_response( openai.error.AuthenticationError: Incorrect API key provided: YOUR API KEY. You can find your API key at https://platform.openai.com/account/api-keys. [INFO][2024-09-18 00:56:36][wechat_channel.py:217] - [WX] sendMsg=Reply(type=ERROR, content=[ERROR] 我现在有点累了,等会再来吧), receiver=@6beb23c66993de6cb1d10c8dde22de20

@FEEHarrison
Copy link
Author

看你日志,应该是配置文件没生效,走的还是gpt模型

补充一下,我使用的是glm-4-flash

@Dhaizei
Copy link

Dhaizei commented Sep 18, 2024

在config.json中加入 "model": "glm-4-air", "zhipu_ai_api_key": "", 即可使用

@GitHubbeginerTuring
Copy link

我看了一下,这个里面是用的openai三方框架来请求zhipuai的,然后我也遇到跟你一样的问题。
image
需要配置bot_type,不知道为啥里面有一个方法在做这个字段的验证。然后在const.py中把
ZHIPU_AI = "glm-4"更改为
ZHIPU_AI = "glm-4-flash"
image
我是win环境中本地运行的,
成功运行到了,就是有个不能为空的提示,[zhipu_ai_session.py:11] - [ZhiPu] character_desc can not be empty。给你参考
image
8e68e6be436a33f66aecbd51eacbda40

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

No branches or pull requests

4 participants