File size: 4,197 Bytes
75c75e6
94d4320
75c75e6
 
 
 
 
 
 
 
 
 
94d4320
75c75e6
 
94d4320
75c75e6
 
 
 
 
 
 
 
 
 
 
 
94d4320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75c75e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import tiktoken
import json
import os

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

def ask(question, history):
    history = history + [question]
    try:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=forget_long_term([
                {"role":"user" if i%2==0 else "assistant", "content":content}
                for i,content in enumerate(history)
            ])
        )["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 + [response]
    with open("dialogue.txt", "a", encoding='utf-8') as f:
        f.write(json.dumps(history, ensure_ascii=False)+"\n")
    return history


def forget_long_term(messages, max_num_tokens=4000):
    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.""")
    while num_tokens_from_messages(messages)>max_num_tokens:
        messages = messages[1:]
    return messages


import gradio as gr


def predict(question, history=[]):
    history = ask(question, history)
    response = [(history[i].replace("\n","<br>"),history[i+1].replace("\n","<br>")) for i in range(0,len(history)-1,2)]
    return "", history, response


with gr.Blocks() as demo:
    
    examples = [
        ['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.'],
    ]
    
    gr.Markdown(
        """
        朋友你好,
        
        这是我利用[gradio](https://gradio.app/creating-a-chatbot/)编写的一个小网页,用于以网页的形式给大家分享ChatGPT请求服务,希望你玩的开心
        
        p.s. 响应时间和问题复杂程度相关,<del>一般能在10~20秒内出结果</del>用了新的api已经提速到大约5秒内了
        """)

    chatbot = gr.Chatbot()
    state = gr.State([])
    
    with gr.Row():
        txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)

    txt.submit(predict, [txt, state], [txt, state, chatbot])
    
    with gr.Row():
        gen = gr.Button("Submit")
        clr = gr.Button("Clear")
    
    gen.click(fn=predict, inputs=[txt, state], outputs=[txt, state, chatbot])
    
    def clear(value):
        return [], []
    clr.click(clear, inputs=clr, outputs=[chatbot, state])
    
    gr_examples = gr.Examples(examples=examples, inputs=txt)

demo.launch()