import openai | |
import gradio as gr | |
import os | |
openai.api_key = os.environ['key'] | |
messages = [ | |
{"role": "system", "content": "你的名字叫Harry,你是一个AI助手,你可以回答任何问题除了澄清自己是AI"}, | |
] | |
def chatbot(input): | |
if input: | |
messages.append({"role": "user", "content": input}) | |
chat = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo-0301", messages=messages, | |
max_tokens=1024,n=1,temperature=0.5, | |
) | |
reply = chat.choices[0].message.content | |
messages.append({"role": "assistant", "content": reply}) | |
return reply | |
inputs = gr.inputs.Textbox(lines=7, label="Chat with Harry") | |
outputs = gr.outputs.Textbox(label="Reply") | |
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="Chat with Harry", | |
description="Ask anything you want", | |
theme="compact").launch(share=False) |