| import gradio as gr |
| import os |
| import openai |
| import requests |
| import json |
|
|
| openai.api_key = os.environ.get("OPENAI_API_KEY") |
|
|
| prompt = "你现在同时扮演AI和人类,针对以下主题进行中文辩论,AI要尽量理性,人类要尽量情绪化,每句话前面标识是谁说的,不少于50句话。" |
| model_engine = "gpt-3.5-turbo" |
| temperature = 0.5 |
| max_tokens = 1024 |
| stop_sequence = "\n\n" |
|
|
| def generate_debate_content(topic): |
| prompt_with_topic = prompt + [topic] |
| response = openai.ChatCompletion.create( |
| model=model_engine, |
| messages=prompt_with_topic, |
| temperature=temperature, |
| max_tokens=max_tokens, |
| stop=stop_sequence |
| ) |
| return response.choices[0].text |
|
|
| def chatbot(topic): |
| debate_content = generate_debate_content(topic) |
| conversation = debate_content.split("\n\n") |
| result = [] |
| for i in range(0, len(conversation), 2): |
| result.append("User 1: " + conversation[i]) |
| if i+1 < len(conversation): |
| result.append("User 2: " + conversation[i+1]) |
| return "\n".join(result) |
|
|
| iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Debate Chatbot", description="Enter a topic to start a debate!") |
| iface.launch() |