|
|
|
|
|
|
|
|
|
import openai |
|
import gradio as gr |
|
|
|
|
|
openai.api_key = "你的_openai_api_key" |
|
|
|
def respond(message, history): |
|
|
|
response = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", |
|
messages=history + [{"role": "user", "content": message}] |
|
) |
|
reply = response['choices'][0]['message']['content'] |
|
history.append({"role": "user", "content": message}) |
|
history.append({"role": "assistant", "content": reply}) |
|
return reply, history |
|
|
|
|
|
iface = gr.Interface( |
|
fn=respond, |
|
inputs=["text", "state"], |
|
outputs=["text", "state"], |
|
title="OpenAI 聊天应用", |
|
description="与 OpenAI 的聊天模型进行对话" |
|
) |
|
|
|
|
|
iface.launch() |