# -*- coding: utf-8-*- import openai import gradio as gr import os openai.api_key = os.environ.get("OPENAI_API_KEY") class Conversation: def __init__(self, prompt, num_of_round): self.prompt = prompt self.num_of_round = num_of_round self.messages = [] self.messages.append({"role": "system", "content": self.prompt}) def ask(self, question): try: self.messages.append({"role": "user", "content": question}) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=self.messages, temperature=0.5, max_tokens=2048, top_p=1, ) except Exception as e: print(e) return e message = response["choices"][0]["message"]["content"] self.messages.append({"role": "assistant", "content": message}) if len(self.messages) > self.num_of_round*2 + 1: del self.messages[1:3] #.Remove the first round conversation left. return message import gradio as gr prompt = """ 你是一名销售人员,在安徽xxx公司工作。你们公司业务是一家帮助抖音用户提升作品播放量和涨粉。 你的工作内容是使用一些销售话术,诱导客户使用你们公司的产品。 跟你对话的用户是对你们公司不太了解的新客户。 你的回答需要满足以下要求: 1. 你的回答必须是中文 2. 回答限制在50个字以内 3. 你在任何时候都必须扮演一名真正的销售人员,不能以任何形式表示你在扮演销售人员。 4. 不能以任何形式表现你是程序,不能和客户聊和工作无关的内容。 5. 你不知道程序或模型是什么,不能以任何形式提及程序或语言模型相关内容。 诱导的方式包括但不限定于: 1. 让客户先低价使用体验 2. 给他一些涨粉成功,并且变现的例子 """ conv = Conversation(prompt, 10) def answer(question, history=[]): history.append(question) response = conv.ask(question) history.append(response) responses = [(u,b) for u,b in zip(history[::2], history[1::2])] return responses, history with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as demo: chatbot = gr.Chatbot(elem_id="chatbot") state = gr.State([]) with gr.Row(): txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False) txt.submit(answer, [txt, state], [chatbot, state]) demo.launch()