gpt4 book ai didi

python - 使用 openai.ChatCompletion API 和 GPT-3.5-turbo 时,Gradio 输出字典的键而不是字符串

转载 作者:行者123 更新时间:2023-12-02 22:48:29 26 4
gpt4 key购买 nike

我一直在尝试创建一个带有 Gradio 界面的 GPT-3.5-turbo 聊天机器人,该聊天机器人在命令行中工作得很好,但当我用 Gradio 实现它时却不行。我能够发送我的输入并接收响应。然而,响应随后被返回,并且 Gradio 无法正确显示结果。它使用“角色”和“内容”字典键而不是聊天字符串进行回复。我的目标是能够与网络界面中记录的历史记录进行简单的聊天对话。

我尝试过返回字符串、字典的各种不同部分,但我完全不知所措。之前,当我在预测函数中返回一个字符串时,它会提示它想要枚举一些东西。然后我发送了一个字符串列表,但也没有运气。当我返回整个字典时,它不会抛出错误,但会显示键而不是值。

这是 Gradio Interface 中发生的错误的图像

这是我当前的代码:

import openai
import gradio as gr

openai.api_key = "XXXXXXX"

history = []
system_msg = input("What type of chatbot would you like to create? ")
history.append({"role": "system", "content": system_msg})

with open("chatbot.txt", "w") as f:
f.write("System: "+system_msg)

print("Say hello to your new assistant!")

def predict(input, history):
if len(history) > 10:
history.pop(1)
history.pop(2)
history.pop(3)
history.append({"role": "user", "content": input})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=history)
reply = response["choices"][0]["message"]["content"]
history.append({"role": "assistant", "content": reply})
return history, history

with gr.Blocks() as demo:
chatbot = gr.Chatbot()
state = gr.State([])

with gr.Row():
txt = gr.Textbox(show_label=False, placeholder="What kind of chatbot would you like to create? ").style(container=False)

txt.submit(predict, [txt, state], [chatbot, state])

demo.launch()

最佳答案

好的,解决了。问题是历史是一本字典。提交的输出需要一个列表的列表。我最终通过创建一个单独的列表解决了这个问题。问题和回复的列表,然后最上面列表的下一项是另一个问题和回复的列表。

字典是我的死神。这是固定代码。

chat_history = []

def gpt_reply(chat_history):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=chat_history)
reply = response["choices"][0]["message"]["content"]
return reply


def predict(input, history = []):
if len(history) == 0:
system_msg = "You are an expert code completionist."
history.append({"role": "system", "content": system_msg})
with open("chatbot.txt", "w") as f:
f.write("System: "+system_msg)
if len(history) > 10:
history.pop(1)
history.pop(2)
history.pop(3)

message = input
with open("chatbot.txt", "a") as f:
f.write("\nUser: " + message + "\n")
history.append({"role": "user", "content": message})
reply = gpt_reply(history)
history.append({"role": "assistant", "content": reply})
with open("chatbot.json", "w") as f:
json.dump(history, f, indent=4, ensure_ascii=False)
# create new list with only the user and bot responses. Each response is a string of one item of a list
chat_history.append([message, reply])
with open("user_responses.json", "w") as f:
json.dump(chat_history, f, indent=4, ensure_ascii=False)
return chat_history, history

with gr.Blocks() as demo:
chatbot = gr.Chatbot()
state = gr.State([])

with gr.Row():
txt = gr.Textbox(show_label=False, placeholder="What kind of chatbot would you like to create? ").style(container=False)

txt.submit(predict, [txt, state], [chatbot, state])

demo.launch()

关于python - 使用 openai.ChatCompletion API 和 GPT-3.5-turbo 时,Gradio 输出字典的键而不是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75630847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com