为什么OpenAI有20个不同模型的20种不同代码?无论如何,我已经尽可能地模仿我在网上找到的现有代码,但总是遇到错误。我意识到有不同的端点(虽然我不知道端点是什么),你必须编写适合特定端点的代码,但我认为我已经找到了适合gpt-4模型的正确代码,但我仍然无法使代码工作:
model = 'gpt-4'prompt=f"Translate the following from Ancient Greek into English: {txt}\n",messages = [{'role':'user','content': prompt}]obj = client.chat.completions.create(model=model, messages=messages)
我也尝试过:
messages = [{'role':'system','content': prompt}]
我得到的错误信息是:
openai.BadRequestError: Error code: 400 – {‘error’: {‘message’: “Invalid type for ‘messages[0].content[0]’: expected an object, but got a string instead.”, ‘type’: ‘invalid_request_error’, ‘param’: ‘messages[0].content[0]’, ‘code’: ‘invalid_type’}}
我在chatgpt网站上看到的关于如何处理不同端点的Python代码很少。我能够使用Babbage模型使其他OpenAI代码工作,但它提供的翻译很差。
回答:
试试这个:
client = OpenAI( api_key=API_KEY)response = client.chat.completions.create( model="gpt-4", messages=[ { "role": "system", "content": "You are a helpful assistant" }, { "role": "user", "content": "Test" } ], temperature=0.5, max_tokens=64,)print(response.choices[0].message.content)
这会给我:
Hello! How can I assist you today?
并且它应该适用于所有聊天完成模型。
进一步测试你的代码后,我发现了罪魁祸首:行尾的逗号:
prompt=f"Translate the following from Ancient Greek into English: {txt}\n",
如果你删除这个,错误就会消失。