File size: 10,200 Bytes
fa0debb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cf20bf
fa0debb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cf20bf
fa0debb
 
 
 
5cf20bf
fa0debb
 
 
5cf20bf
fa0debb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cf20bf
fa0debb
 
 
5cf20bf
fa0debb
5cf20bf
 
fa0debb
 
5cf20bf
 
 
 
fa0debb
 
5cf20bf
 
 
fa0debb
 
 
 
5cf20bf
 
fa0debb
 
 
5cf20bf
 
 
 
fa0debb
 
5cf20bf
 
 
fa0debb
 
 
 
 
 
 
5cf20bf
 
fa0debb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cf20bf
 
fa0debb
 
5cf20bf
 
fa0debb
 
 
5cf20bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa0debb
5cf20bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa0debb
 
5cf20bf
 
 
 
 
fa0debb
 
 
 
5cf20bf
 
 
 
fa0debb
5cf20bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa0debb
5cf20bf
 
 
 
fa0debb
 
 
5cf20bf
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import gradio as gr
import requests
import csv
import os
from langchain import ConversationChain, PromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferWindowMemory

prompt_templates = {
    "Default ChatGPT": "",
    "Helpful Asistant": """

    Assistant is a large language model trained by OpenAI.



    Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.



    Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.



    Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.

    """,
}

# TODO: Add system prompt input when langchain support multiple inputs for ConversationalChain
chat_template = """

Assistant is a large language model trained by OpenAI.



Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.



Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.



Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.



{history}

Human: {input}

Assistant:"""

chat_prompt = PromptTemplate(
    input_variables=["system_prompt", "history", "input"], template=chat_template
)

memory = ConversationBufferWindowMemory(k=2)


def get_empty_state():
    return {"total_tokens": 0, "messages": []}


def download_prompt_templates():
    url = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"
    try:
        response = requests.get(url)
        reader = csv.reader(response.text.splitlines())
        next(reader)  # skip the header row
        for row in reader:
            if len(row) >= 2:
                act = row[0].strip('"')
                prompt = row[1].strip('"')
                prompt_templates[act] = prompt

    except requests.exceptions.RequestException as e:
        print(f"An error occurred while downloading prompt templates: {e}")
        return

    choices = list(prompt_templates.keys())
    choices = choices[:1] + sorted(choices[1:])
    return gr.update(value=choices[0], choices=choices)


def on_token_change(user_token):
    os.environ["OPENAI_API_KEY"] = user_token


def on_prompt_template_change(prompt_template):
    if not isinstance(prompt_template, str):
        return
    return prompt_templates[prompt_template]


def submit_message(

    chat_history, prompt, prompt_template, temperature, max_tokens, context_length

):
    memory.k = context_length
    chatgpt_chain = ConversationChain(
        llm=ChatOpenAI(temperature=temperature, max_tokens=max_tokens),
        prompt=chat_prompt,
        verbose=False,
        memory=memory,
    )

    if not prompt:
        return gr.update(value=""), chat_history, f""

    system_prompt = prompt_templates[prompt_template]

    if not os.environ["OPENAI_API_KEY"]:
        return (
            "",
            chat_history.append((prompt, "Error: OpenAI API Key is not set.")),
            f"Total tokens used: 0",
        )

    try:
        response = chatgpt_chain.predict(system_prompt=system_prompt, input=prompt)

        chat_history.append((prompt, response))

    except Exception as e:
        chat_history.append((prompt, f"Error: {e}"))

    total_tokens_used_msg = f""

    return "", chat_history, total_tokens_used_msg


def clear_conversation():
    memory.clear()
    return gr.update(value=None, visible=True), None, ""


css = """

      #col-container {max-width: 90%; margin-left: auto; margin-right: auto;}

      #chatbox {min-height: 400px;}

      #header {text-align: center;}

      #prompt_template_preview {padding: 1rem; border-width: 1px; border-style: solid; border-color: #e0e0e0; border-radius: 4px;}

      #total_tokens_str {text-align: right; font-size: 0.8rem; color: #666;}

      #label {font-size: 0.8rem; padding: 0.5em; margin: 0;}

      .message { font-size: 1.2rem; }

      """

with gr.Blocks(css=css) as demo:
    state = gr.State(get_empty_state())

    with gr.Column(elem_id="col-container"):
        gr.Markdown(
            """## OpenAI ChatGPT Demo

                    Using the ofiicial API (gpt-3.5-turbo model)

                    Prompt templates from [awesome-chatgpt-prompts](https://github.com/f/awesome-chatgpt-prompts).""",
            elem_id="header",
        )

        with gr.Row():
            with gr.Column(scale=0.3):
                gr.Markdown(
                    "Enter your OpenAI API Key. You can get one [here](https://platform.openai.com/account/api-keys).",
                    elem_id="label",
                )
                user_token = gr.Textbox(
                    value="",
                    placeholder="OpenAI API Key",
                    type="password",
                    show_label=False,
                )
                prompt_template = gr.Dropdown(
                    label="Set a custom insruction for the chatbot:",
                    choices=list(prompt_templates.keys()),
                )
                prompt_template_preview = gr.Markdown(
                    elem_id="prompt_template_preview",
                    value=prompt_templates["Default ChatGPT"],
                )
                with gr.Accordion("Advanced parameters", open=False):
                    temperature = gr.Slider(
                        minimum=0,
                        maximum=2.0,
                        value=0.7,
                        step=0.1,
                        label="Temperature",
                        info="Higher = more creative/chaotic",
                    )
                    max_tokens = gr.Slider(
                        minimum=100,
                        maximum=4096,
                        value=1000,
                        step=1,
                        label="Max tokens per response",
                    )
                    context_length = gr.Slider(
                        minimum=1,
                        maximum=10,
                        value=2,
                        step=1,
                        label="Context length",
                        info="Number of previous messages to send to the chatbot. Be careful with high values, it can blow up the token budget quickly.",
                    )
            with gr.Column(scale=0.7):
                chatbot = gr.Chatbot(elem_id="chatbox")
                input_message = gr.Textbox(
                    show_label=False,
                    placeholder="Enter text and press enter",
                    visible=True,
                ).style(container=False)
                btn_submit = gr.Button("Submit")
                total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
                btn_clear_conversation = gr.Button("🔃 Start New Conversation")

    gr.HTML(
        """<br><br><br><center>You can duplicate this Space to skip the queue:<a href="https://huggingface.co/spaces/dragonSwing/chatgpt-app?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a><br>

            <p><img src="https://visitor-badge.glitch.me/badge?page_id=dragonswing.chatgpt-app" alt="visitors"></p></center>"""
    )

    btn_submit.click(
        submit_message,
        [
            chatbot,
            input_message,
            prompt_template,
            temperature,
            max_tokens,
            context_length,
        ],
        [input_message, chatbot, total_tokens_str],
    )
    input_message.submit(
        submit_message,
        [
            chatbot,
            input_message,
            prompt_template,
            temperature,
            max_tokens,
            context_length,
        ],
        [input_message, chatbot, total_tokens_str],
    )
    btn_clear_conversation.click(
        clear_conversation, [], [input_message, chatbot, total_tokens_str]
    )
    prompt_template.change(
        on_prompt_template_change,
        inputs=[prompt_template],
        outputs=[prompt_template_preview],
    )
    user_token.change(on_token_change, inputs=[user_token], outputs=[])

    demo.load(
        download_prompt_templates, inputs=None, outputs=[prompt_template], queue=False
    )


demo.queue(concurrency_count=10)
demo.launch(height="800px")