File size: 833 Bytes
ee71ca6
 
 
 
 
2b85018
 
ee71ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# import gradio as gr

# gr.load("models/meta-llama/Llama-3.1-8B-Instruct").launch()

import openai
import gradio as gr

# 设置 OpenAI API 密钥
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

# 创建 Gradio 接口
iface = gr.Interface(
    fn=respond,
    inputs=["text", "state"],
    outputs=["text", "state"],
    title="OpenAI 聊天应用",
    description="与 OpenAI 的聊天模型进行对话"
)

# 启动应用
iface.launch()