Error code: 404 - {'error': {'message': 'Invalid URL (POST /v1/chat/completions/...
🚨 错误信息
openai.NotFoundError: Error code: 404 - {'error': {'message': 'Invalid URL (POST /v1/chat/completions/chat/completions)', 'type': 'invalid_request_error', 'code': ''}}
🤖 AI解决方案
openai.NotFoundError: Error code: 404 - {'error': {'message': 'Invalid URL (POST /v1/chat/completions/chat/completions)', 'type': 'invalid_request_error', 'code': ''}}
# 错误翻译
"无效的URL (POST /v1/chat/completions/chat/completions)"
# 原因分析
API请求的URL路径存在重复,`/v1/chat/completions/chat/completions` 应该是 `/v1/chat/completions`。
# 解决方案
检查您的代码中调用 OpenAI API 的部分,确保发送请求的URL是正确的。修改为:pythonimport openai
假设您的API key已经设置
openai.api_key = "YOUR_API_KEY"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"}
]
)
print(response.choices[0].message['content'])
# 预防措施
1. 在发送API请求前,仔细检查URL的拼写和结构,避免重复或错误。
2. 使用OpenAI官方提供的SDK(如Python库)来调用API,SDK已封装了正确的URL,可以减少手动构建URL出错的几率。