提问人:Josiah 提问时间:10/22/2023 更新时间:10/22/2023 访问量:55
使用 openAI python 库时获取 AttributeError
Getting AttributeError when using openAI python library
问:
我正在利用 openai 库构建一个新的 AI 聊天机器人,我在一个文件 (app.py) 中设置了一个 gradio UI,在另一个文件 (trainedBot.py) 中设置了一个 predict() 函数 每次我通过gradio UI发送请求时,我都会收到以下错误:
File "/home/user/app/trainedBot.py", line 48, in predict
return response.choices.message.content
AttributeError: 'list' object has no attribute 'message'
我尝试将消息和 openAI 响应都放在一个变量中,但什么也没发生。仍然有同样的错误。响应如下所示:
{
"id": "chatcmpl-8CHKgpOewruWDC2Et1R6ZFtdPmSQR",
"object": "chat.completion",
"created": 1697937254,
"model": "gpt-4-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "HIIIIIII"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 308,
"completion_tokens": 4,
"total_tokens": 312
}
}
答:
0赞
Goku - stands with Palestine
10/22/2023
#1
response = {
"id": "chatcmpl-8CHKgpOewruWDC2Et1R6ZFtdPmSQR",
"object": "chat.completion",
"created": 1697937254,
"model": "gpt-4-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "HIIIIIII"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 308,
"completion_tokens": 4,
"total_tokens": 312
}
}
你的选择是一个所以访问它,你需要把list
[0]
print(response['choices'][0]['message']['content'])
'HIIIIIII'
或者可能是:
response.get('choices')[0].get('message').get('content')
评论