File size: 642 Bytes
c20b058
 
 
 
 
 
 
 
 
 
 
97995d8
c20b058
 
 
 
 
 
 
97995d8
c20b058
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import gradio as gr
from openai import OpenAI

def ask_gpt(api_key, prompt):
    client=OpenAI(api_key=api_key) 
    chat_completion=client.chat.completions.create(
        messages=[{'role': 'user', 'content': prompt}],
        model='gpt-3.5-turbo'
        ) 
    return chat_completion.choices[0].message.content

api_key=gr.Textbox(label='輸入 OpenAI 金鑰', type='password') 
prompt=gr.Textbox(label='您的詢問: ')
reply=gr.Textbox(label='OpenAI 回答: ')
iface=gr.Interface(
    fn=ask_gpt,
    inputs=[api_key, prompt],  
    outputs=reply,
    title='OpenAI API 聊天機器人',
    flagging_mode='never',
    )
iface.launch()