File size: 2,591 Bytes
84a6c36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import gradio as gr
from loguru import logger
from V3 import call_gpt

class Conversation:
    def __init__(self, max_history_len=10):
        self.max_history_len = max_history_len

    def pop_history(self, history):
        while len(history) > self.max_history_len:
            for item in history:
                if item["role"] == "user":
                    history.remove(item)
                    break
            for item in history:
                if item["role"] == "assistant":
                    history.remove(item)
                    break
        return history

    def ask(self, history, prompt):
        history = self.pop_history(history)
        logger.info(history)
        return call_gpt(history, prompt)

conv = Conversation()

def make_history(system_prompt, qa_list):
    history = [{"role": "system", "content": system_prompt}]
    for q, a in qa_list:
        history.append({"role": "user", "content": q})
        history.append({"role": "assistant", "content": a})
    return history

def answer(system_prompt, prompt, history=[]):
    history.append(prompt)
    qa_list = [(u, b) for u, b in zip(history[::2], history[1::2])]
    message = conv.ask(make_history(system_prompt, qa_list), prompt)
    
    # 对反引号进行转义
    message = message.replace("`", "\\`")
    
    # 包裹为代码块
    message = f"```\n{message}\n```"
    
    history.append(message)

    chatbot_messages = []
    for q, a in qa_list:
        chatbot_messages.append((q, a))
    
    chatbot_messages.append((prompt, message))
    
    return "", chatbot_messages, history

def clear_history(state):
    state.clear()
    return state, []

with gr.Blocks(css="#chatbot{height:500px} .overflow-y-auto{height:500px}") as rxbot:
    with gr.Row():
        sys = gr.Textbox(show_label=False, value="You are open-o1, a helpful assistant.")
    chatbot = gr.Chatbot()
    state = gr.State([])
    
    with gr.Row():
        txt = gr.Textbox(show_label=False, placeholder="请输入你的问题", max_lines=8)
        
    with gr.Row():
        clear_button = gr.Button("🧹Clear History")
        send_button = gr.Button("🚀Send")
        
        send_button.click(
            fn=answer,
            inputs=[sys, txt, state],
            outputs=[txt, chatbot, state]
        )

        txt.submit(
            fn=answer,
            inputs=[sys, txt, state],
            outputs=[txt, chatbot, state]
        )

        clear_button.click(
            fn=clear_history,
            inputs=[state],
            outputs=[state, chatbot]
        )

rxbot.launch()