API请求失败(小白
xiaomingzzzzz opened this issue · comments
import requests
使用你的API密钥
api_key = "---" # 请确保替换为你的实际API密钥
prompt = "今天天气怎么样?"
def ask_gpt(prompt, api_key):
url = 'https://api.chatanywhere.tech/v1/completions'
headers = {
"Authorization": f"Bearer {api_key}"
}
data = {
"model": "gpt-3.5-turbo", # 调整为使用免费API支持的模型
"prompt": prompt,
"temperature": 0.5,
"max_tokens": 100
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
response_json = response.json()
answers = response_json.get('choices', [{}])
answer = answers[0].get('text', 'No answer received.').strip()
else:
answer = f"Request failed with status {response.status_code}: {response.text}"
return answer
print(ask_gpt(prompt, api_key))
这个代码怎么没办法访问呢
D:\pythonProject1\venv\Scripts\python.exe D:\pythonProject1\main.py
Request failed with status 403: {"error":{"message":"免费API限制使用gpt-3.5-turbo,gpt-4 和 embeddings模型,如有更多需求,请访问 https://buyca.top 购买付费API。The free account is limited to use gpt-3.5-turbo, gpt-4 and embeddings. If you have additional requirements, please visit https://buyca.top to purchase a premium key.","type":"chatanywhere_error","param":null,"code":"403 FORBIDDEN"}}
进程已结束,退出代码0
api隐藏了 实际上正确输入了
url = 'https://api.chatanywhere.tech/v1/completions'
这个url调用的是completion模型,gpt应该请求chat模型的url,如下:
url = 'https://api.chatanywhere.tech/v1/chat/completions'