| import gradio as gr | |
| import os | |
| import openai | |
| openai.api_key = os.environ.get("OPENAI_API_KEY") | |
| content = os.environ.get("CHAT_TYPE") | |
| system_prompt = [{"role": "system", | |
| "content": content}] | |
| model_engine = "gpt-3.5-turbo" | |
| temperature = 0.7 | |
| max_tokens = 1024 | |
| def generate_debate_content(topic): | |
| prompt_msg = {"role": "user", "content": topic} | |
| prompt_with_topic = system_prompt + [prompt_msg] | |
| response = openai.ChatCompletion.create( | |
| model=model_engine, | |
| messages=prompt_with_topic, | |
| temperature=temperature, | |
| max_tokens=max_tokens | |
| ) | |
| return response.choices[0].message.content | |
| def chatbot(topic): | |
| debate_content = generate_debate_content(topic) | |
| conversation = debate_content.split("|") | |
| result = [] | |
| for i in range(0, len(conversation) - 1, 2): | |
| result.append(conversation[i]) | |
| if i + 1 < len(conversation): | |
| result.append(conversation[i + 1]) | |
| return "\n".join(result) | |
| iface = gr.Interface(fn=chatbot, inputs=gr.inputs.Textbox(label="请输入话题"), | |
| outputs=gr.outputs.Textbox(label="聊天内容"), title="GPT聊天室(人类不得入内)", | |
| layout="vertical") | |
| iface.launch() |