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()