File size: 6,562 Bytes
1857da6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import openai
import tiktoken

import datetime
import json
import os

openai.api_key = os.getenv('API_KEY')
openai.request_times = 0

def ask(question, history, behavior):
    openai.request_times += 1
    print(f"request times {openai.request_times}: {datetime.datetime.now()}: {question}")
    try:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=forget_long_term(
                [
                    {"role":"system", "content":content}
                    for content in behavior
                ] + [
                    {"role":"user" if i%2==0 else "assistant", "content":content}
                    for i,content in enumerate(history + [question])
                ]
            )
        )["choices"][0]["message"]["content"]
        while response.startswith("\n"):
            response = response[1:]
    except Exception as e:
        print(e)
        response = 'Timeout! Please wait a few minutes and retry'
    history = history + [question, response]
    return history

def num_tokens_from_messages(messages, model="gpt-3.5-turbo"):
    """Returns the number of tokens used by a list of messages."""
    try:
        encoding = tiktoken.encoding_for_model(model)
    except KeyError:
        encoding = tiktoken.get_encoding("cl100k_base")
    if model == "gpt-3.5-turbo":  # note: future models may deviate from this
        num_tokens = 0
        for message in messages:
            num_tokens += 4  # every message follows <im_start>{role/name}\n{content}<im_end>\n
            for key, value in message.items():
                num_tokens += len(encoding.encode(value))
                if key == "name":  # if there's a name, the role is omitted
                    num_tokens += -1  # role is always required and always 1 token
        num_tokens += 2  # every reply is primed with <im_start>assistant
        return num_tokens
    else:
        raise NotImplementedError(f"""num_tokens_from_messages() is not presently implemented for model {model}.
See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""")

def forget_long_term(messages, max_num_tokens=4000):
    while num_tokens_from_messages(messages)>max_num_tokens:
        if messages[0]["role"]=="system" and not len(messages[0]["content"]>=max_num_tokens):
            messages = messages[:1] + messages[2:]
        else:
            messages = messages[1:]
    return messages


import gradio as gr


def to_md(content):
    is_inside_code_block = False
    output_spans = []
    for i in range(len(content)):
        if content[i]=="\n" and not is_inside_code_block:
            if len(output_spans)>0 and output_spans[-1]=="\n```\n":
                pass
            else:
                output_spans.append("<br>")
        elif content[i]=="`":
            output_spans.append(content[i])
            if len(output_spans)>=3 and all([output_spans[j]=="`" for j in [-3,-2,-1]]):
                is_inside_code_block = not is_inside_code_block
                output_spans = output_spans[:-3]
                if not output_spans[-1]=="\n":
                    output_spans.append("\n")
                output_spans.append("\n```\n")
        else:
            output_spans.append(content[i])
    return "".join(output_spans)


def predict(question, history=[], behavior=[]):
    history = ask(question, history, behavior)
    response = [(to_md(history[i]),to_md(history[i+1])) for i in range(0,len(history)-1,2)]
    return "", history, response


with gr.Blocks() as demo:
    
    examples_txt = [
        ['200字介绍一下凯旋门:'],
        ['网上购物有什么小窍门?'],
        ['补全下述对三亚的介绍:\n三亚位于海南岛的最南端,是'],
        ['将这句文言文翻译成英语:"逝者如斯夫,不舍昼夜。"'],
        ['Question: What\'s the best winter resort city? User: A 10-year professional traveler. Answer: '],
        ['How to help my child to make friends with his classmates? answer this question step by step:'],
        ['polish the following statement for a paper: In this section, we perform case study to give a more intuitive demonstration of our proposed strategies and corresponding explanation.'],
    ]
    
    examples_bhv = [
        "你现在是一个带有批判思维的导游,会对景点的优缺点进行中肯的分析。",
        "你现在是一名佛教信仰者,但同时又对世界上其它的宗教和文化保持着包容、尊重和交流的态度。",
        f"You are a helpful assistant. You will answer all the questions step-by-step.",
        f"You are a helpful assistant. Today is {datetime.date.today()}.",
    ]
    
    gr.Markdown(
        """
        朋友你好,
        
        这是我利用[gradio](https://gradio.app/creating-a-chatbot/)编写的一个小网页,用于以网页的形式给大家分享ChatGPT请求服务,希望你玩的开心。关于使用技巧或学术研讨,欢迎在[Community](https://huggingface.co/spaces/zhangjf/chatbot/discussions)中和我交流。
        
        p.s. 响应时间和聊天内容长度正相关,一般能在5秒~30秒内响应。
        """)
    
    behavior = gr.State([])
    
    with gr.Column(variant="panel"):
        with gr.Row().style(equal_height=True):
            with gr.Column(scale=0.85):
                bhv = gr.Textbox(show_label=False, placeholder="输入你想让ChatGPT扮演的人设").style(container=False)
            with gr.Column(scale=0.15, min_width=0):
                button_set = gr.Button("Set")
    bhv.submit(fn=lambda x:(x,[x]), inputs=[bhv], outputs=[bhv, behavior])
    button_set.click(fn=lambda x:(x,[x]), inputs=[bhv], outputs=[bhv, behavior])
    

    state = gr.State([])
    
    with gr.Column(variant="panel"):
        chatbot = gr.Chatbot()
        txt = gr.Textbox(show_label=False, placeholder="输入你想让ChatGPT回答的问题").style(container=False)
        with gr.Row():
            button_gen = gr.Button("Submit")
            button_clr = gr.Button("Clear")
        
    gr.Examples(examples=examples_bhv, inputs=bhv, label="Examples for setting behavior")
    gr.Examples(examples=examples_txt, inputs=txt, label="Examples for asking question")
    txt.submit(predict, [txt, state, behavior], [txt, state, chatbot])
    button_gen.click(fn=predict, inputs=[txt, state, behavior], outputs=[txt, state, chatbot])
    button_clr.click(fn=lambda :([],[]), inputs=None, outputs=[chatbot, state])

demo.launch()