idootop/mi-gpt

使用KIMI的接口报 LLM 响应异常401 Invalid Authentication,测试API KEY是正常的

Closed this issue · 5 comments

.env内容:
OPENAI_MODEL=moonshot-v1-8k
OPENAI_API_KEY=sk-xxx
OPENAI_BASE_URL=https://api.moonshot.cn/v1

package.json内容:
"scripts": {
"start": "node --env-file=.env app.js",

报错日志:
2024/06/09 10:20:49 Speaker ✅ 服务已启动...
2024/06/09 10:20:55 Speaker 🔥 召唤小小
2024/06/09 10:20:56 Speaker 🔊 你好,我是小小,很高兴认识你噢
2024/06/09 10:21:04 Speaker 🔥 介绍一下你自己
2024/06/09 10:21:05 Speaker 🔊 请稍等
2024/06/09 10:21:09 Open AI ❌ LLM 响应异常 AuthenticationError: 401 Invalid Authentication
at APIError.generate (/Users/amon/Downloads/mi-gpt-main/node_modules/.pnpm/openai@4.47.3/node_modules/openai/error.js:48:20)
at OpenAI.makeStatusError (/Users/amon/Downloads/mi-gpt-main/node_modules/.pnpm/openai@4.47.3/node_modules/openai/core.js:263:33)
at OpenAI.makeRequest (/Users/amon/Downloads/mi-gpt-main/node_modules/.pnpm/openai@4.47.3/node_modules/openai/core.js:306:30)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async OpenAIClient.chatStream (/Users/amon/Downloads/mi-gpt-main/dist/index.cjs:1147:20) {
status: 401,
headers: {
connection: 'keep-alive',
'content-length': '84',
'content-type': 'application/json; charset=utf-8',
date: 'Sun, 09 Jun 2024 02:21:09 GMT',
'msh-cache': 'miss',
server: 'nginx',
'server-timing': 'inner; dur=18',
'strict-transport-security': 'max-age=15724800; includeSubDomains',
vary: 'Origin'
},
request_id: undefined,
error: {
message: 'Invalid Authentication',
type: 'invalid_authentication_error'
},
code: undefined,
param: undefined,
type: 'invalid_authentication_error'
}

单独测试API KEY:
curl https://api.moonshot.cn/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer sk-xxxx" \ -d '{ "model": "moonshot-v1-8k", "messages": [ {"role": "system", "content": "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色 暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。"}, {"role": "user", "content": "你好,我叫李雷,1+1等于多少?"} ], "temperature": 0.3 }'

返回显示正常:
{"id":"chatcmpl-","object":"chat.completion","created":1717899981,"model":"moonshot-v1-8k","choices":[{"index":0,"message":{"role":"assistant","content":"你好,李雷!1+1 等于 2。这是一个基本的数学加法问题。如果你有其他问题或需要帮助,请随时告诉我。"},"finish_reason":"stop"}],"usage":{"prompt_tokens":89,"completion_tokens":33,"total_tokens":122}}%

.env
配一下魔法:HTTP_PROXY=http://192.168.*.*:7890

401 错误基本上都是因为你设置的key有问题,请检查key是否有效,以及env环境变量配置是否生效。

@xumeng 你是clone这个仓库在本地运行的吗?感觉像是你的env配置没生效,你可以在vs code 里打断点看下环境变量有没有更新。

hello 是的,clone本地运行的。
仔细看了下错误,在代码中打印了env中的配置,发现原来是用了本地的 bashrc 中配置的OPENAI_API_KEY(已过期)。
暂时解决了这个问题。

有点疑问,
.env和bashrc中的配置如何处理不冲突呢,我在启动脚本中设置了 "start": "node --env-file=.env app.js",

没了解过,感兴趣的话你可以研究研究 node env 配置项的覆盖规则