import os import openai import gradio as gr from dotenv import load_dotenv load_dotenv() openai.api_key = os.environ['OPENAI_API_KEY'] messages = [ {"role": "system", "content": "你是算塔罗牌的专家,帮助人们算塔罗牌."}, ] def chat(message, history): if message: messages.append({"role": "user", "content": message}) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages ) reply = response.choices[0].message.content # messages.append({"role": "assistant", "content": reply}) return reply # def chat(user_input): # if user_input: # messages.append({"role": "user", "content": user_input}) # response = openai.ChatCompletion.create( # model="gpt-3.5-turbo", messages=messages # ) # reply = response.choices[0].message.content # messages.append({"role": "assistant", "content": reply}) # return reply inputs = gr.inputs.Textbox(label="User input") outputs = gr.outputs.Textbox(label="Response") chatInterface = gr.ChatInterface( fn=chat, # inputs=inputs, # outputs=outputs, title="AI 塔罗", ) chatInterface.launch(share=True)