| | import openai |
| | import os |
| | import gradio as gr |
| |
|
| | openai.api_key = os.environ.get("OPENAI_API_KEY") |
| | |
| | MAX_PROMPT_LENGTH = 1024 |
| | |
| | prompt = """你是一个AI个人助手。你的回答需要满足以下要求: |
| | 1. 你的回答必须是中文 |
| | 2. 回答限制在1000个字以内""" |
| |
|
| |
|
| | |
| | def check_prompt_length(question): |
| | text_length = len(prompt) + len(question) |
| | if text_length > MAX_PROMPT_LENGTH: |
| | return False |
| | return True |
| |
|
| |
|
| | 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): |
| | if not check_prompt_length(question): |
| | return "你的输入太长了,请将文本长度限制在900以内,请重新输入" |
| | 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] |
| | return message |
| |
|
| |
|
| | conv = Conversation(prompt, 0) |
| |
|
| |
|
| | def answer(question, history=[]): |
| | if question.lower() in ["bye", "goodbye", "exit"]: |
| | print("Goodbye!") |
| | exit(0) |
| | 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() |
| |
|