dragonSwing commited on
Commit
5cf20bf
·
1 Parent(s): 4430327

Reformat code

Browse files
Files changed (1) hide show
  1. langchain_app.py +117 -39
langchain_app.py CHANGED
@@ -16,7 +16,7 @@ prompt_templates = {
16
  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.
17
 
18
  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.
19
- """
20
  }
21
 
22
  # TODO: Add system prompt input when langchain support multiple inputs for ConversationalChain
@@ -34,15 +34,16 @@ Human: {input}
34
  Assistant:"""
35
 
36
  chat_prompt = PromptTemplate(
37
- input_variables=["system_prompt", "history", "input"],
38
- template=chat_template
39
  )
40
 
41
  memory = ConversationBufferWindowMemory(k=2)
42
 
 
43
  def get_empty_state():
44
  return {"total_tokens": 0, "messages": []}
45
 
 
46
  def download_prompt_templates():
47
  url = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"
48
  try:
@@ -63,45 +64,52 @@ def download_prompt_templates():
63
  choices = choices[:1] + sorted(choices[1:])
64
  return gr.update(value=choices[0], choices=choices)
65
 
 
66
  def on_token_change(user_token):
67
  os.environ["OPENAI_API_KEY"] = user_token
68
 
 
69
  def on_prompt_template_change(prompt_template):
70
- if not isinstance(prompt_template, str): return
 
71
  return prompt_templates[prompt_template]
72
 
73
- def submit_message(chat_history, prompt, prompt_template, temperature, max_tokens, context_length):
 
 
 
74
  memory.k = context_length
75
  chatgpt_chain = ConversationChain(
76
- llm=ChatOpenAI(temperature=temperature, max_tokens=max_tokens),
77
- prompt=chat_prompt,
78
- verbose=False,
79
  memory=memory,
80
  )
81
 
82
  if not prompt:
83
- return gr.update(value=''), chat_history, f""
84
-
85
  system_prompt = prompt_templates[prompt_template]
86
 
87
  if not os.environ["OPENAI_API_KEY"]:
88
- return '', chat_history.append((prompt, "Error: OpenAI API Key is not set.")), f"Total tokens used: 0"
89
-
90
- try:
91
- response = chatgpt_chain.predict(
92
- system_prompt=system_prompt,
93
- input=prompt
94
  )
95
 
 
 
 
96
  chat_history.append((prompt, response))
97
 
98
-
99
  except Exception as e:
100
  chat_history.append((prompt, f"Error: {e}"))
101
 
102
  total_tokens_used_msg = f""
103
 
104
- return '', chat_history, total_tokens_used_msg
 
105
 
106
  def clear_conversation():
107
  memory.clear()
@@ -119,44 +127,114 @@ css = """
119
  """
120
 
121
  with gr.Blocks(css=css) as demo:
122
-
123
  state = gr.State(get_empty_state())
124
 
125
-
126
  with gr.Column(elem_id="col-container"):
127
- gr.Markdown("""## OpenAI ChatGPT Demo
 
128
  Using the ofiicial API (gpt-3.5-turbo model)
129
  Prompt templates from [awesome-chatgpt-prompts](https://github.com/f/awesome-chatgpt-prompts).""",
130
- elem_id="header")
 
131
 
132
  with gr.Row():
133
  with gr.Column(scale=0.3):
134
- gr.Markdown("Enter your OpenAI API Key. You can get one [here](https://platform.openai.com/account/api-keys).", elem_id="label")
135
- user_token = gr.Textbox(value='', placeholder="OpenAI API Key", type="password", show_label=False)
136
- prompt_template = gr.Dropdown(label="Set a custom insruction for the chatbot:", choices=list(prompt_templates.keys()))
137
- prompt_template_preview = gr.Markdown(elem_id="prompt_template_preview", value=prompt_templates["Default ChatGPT"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  with gr.Accordion("Advanced parameters", open=False):
139
- temperature = gr.Slider(minimum=0, maximum=2.0, value=0.7, step=0.1, label="Temperature", info="Higher = more creative/chaotic")
140
- max_tokens = gr.Slider(minimum=100, maximum=4096, value=1000, step=1, label="Max tokens per response")
141
- 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.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  with gr.Column(scale=0.7):
143
  chatbot = gr.Chatbot(elem_id="chatbox")
144
- input_message = gr.Textbox(show_label=False, placeholder="Enter text and press enter", visible=True).style(container=False)
 
 
 
 
145
  btn_submit = gr.Button("Submit")
146
  total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
147
  btn_clear_conversation = gr.Button("🔃 Start New Conversation")
148
 
149
- gr.HTML('''<br><br><br><center>You can duplicate this Space to skip the queue:<a href="https://huggingface.co/spaces/dragonSwing/chatgpt-grad?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a><br>
150
- <p><img src="https://visitor-badge.glitch.me/badge?page_id=dragonswing.chatgpt_api_grad_hf" alt="visitors"></p></center>''')
 
 
151
 
152
- btn_submit.click(submit_message, [chatbot, input_message, prompt_template, temperature, max_tokens, context_length], [input_message, chatbot, total_tokens_str])
153
- input_message.submit(submit_message, [chatbot, input_message, prompt_template, temperature, max_tokens, context_length], [input_message, chatbot, total_tokens_str])
154
- btn_clear_conversation.click(clear_conversation, [], [input_message, chatbot, total_tokens_str])
155
- prompt_template.change(on_prompt_template_change, inputs=[prompt_template], outputs=[prompt_template_preview])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  user_token.change(on_token_change, inputs=[user_token], outputs=[])
157
-
158
- demo.load(download_prompt_templates, inputs=None, outputs=[prompt_template], queue=False)
 
 
159
 
160
 
161
  demo.queue(concurrency_count=10)
162
- demo.launch(height='800px')
 
16
  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.
17
 
18
  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.
19
+ """,
20
  }
21
 
22
  # TODO: Add system prompt input when langchain support multiple inputs for ConversationalChain
 
34
  Assistant:"""
35
 
36
  chat_prompt = PromptTemplate(
37
+ input_variables=["system_prompt", "history", "input"], template=chat_template
 
38
  )
39
 
40
  memory = ConversationBufferWindowMemory(k=2)
41
 
42
+
43
  def get_empty_state():
44
  return {"total_tokens": 0, "messages": []}
45
 
46
+
47
  def download_prompt_templates():
48
  url = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"
49
  try:
 
64
  choices = choices[:1] + sorted(choices[1:])
65
  return gr.update(value=choices[0], choices=choices)
66
 
67
+
68
  def on_token_change(user_token):
69
  os.environ["OPENAI_API_KEY"] = user_token
70
 
71
+
72
  def on_prompt_template_change(prompt_template):
73
+ if not isinstance(prompt_template, str):
74
+ return
75
  return prompt_templates[prompt_template]
76
 
77
+
78
+ def submit_message(
79
+ chat_history, prompt, prompt_template, temperature, max_tokens, context_length
80
+ ):
81
  memory.k = context_length
82
  chatgpt_chain = ConversationChain(
83
+ llm=ChatOpenAI(temperature=temperature, max_tokens=max_tokens),
84
+ prompt=chat_prompt,
85
+ verbose=False,
86
  memory=memory,
87
  )
88
 
89
  if not prompt:
90
+ return gr.update(value=""), chat_history, f""
91
+
92
  system_prompt = prompt_templates[prompt_template]
93
 
94
  if not os.environ["OPENAI_API_KEY"]:
95
+ return (
96
+ "",
97
+ chat_history.append((prompt, "Error: OpenAI API Key is not set.")),
98
+ f"Total tokens used: 0",
 
 
99
  )
100
 
101
+ try:
102
+ response = chatgpt_chain.predict(system_prompt=system_prompt, input=prompt)
103
+
104
  chat_history.append((prompt, response))
105
 
 
106
  except Exception as e:
107
  chat_history.append((prompt, f"Error: {e}"))
108
 
109
  total_tokens_used_msg = f""
110
 
111
+ return "", chat_history, total_tokens_used_msg
112
+
113
 
114
  def clear_conversation():
115
  memory.clear()
 
127
  """
128
 
129
  with gr.Blocks(css=css) as demo:
 
130
  state = gr.State(get_empty_state())
131
 
 
132
  with gr.Column(elem_id="col-container"):
133
+ gr.Markdown(
134
+ """## OpenAI ChatGPT Demo
135
  Using the ofiicial API (gpt-3.5-turbo model)
136
  Prompt templates from [awesome-chatgpt-prompts](https://github.com/f/awesome-chatgpt-prompts).""",
137
+ elem_id="header",
138
+ )
139
 
140
  with gr.Row():
141
  with gr.Column(scale=0.3):
142
+ gr.Markdown(
143
+ "Enter your OpenAI API Key. You can get one [here](https://platform.openai.com/account/api-keys).",
144
+ elem_id="label",
145
+ )
146
+ user_token = gr.Textbox(
147
+ value="",
148
+ placeholder="OpenAI API Key",
149
+ type="password",
150
+ show_label=False,
151
+ )
152
+ prompt_template = gr.Dropdown(
153
+ label="Set a custom insruction for the chatbot:",
154
+ choices=list(prompt_templates.keys()),
155
+ )
156
+ prompt_template_preview = gr.Markdown(
157
+ elem_id="prompt_template_preview",
158
+ value=prompt_templates["Default ChatGPT"],
159
+ )
160
  with gr.Accordion("Advanced parameters", open=False):
161
+ temperature = gr.Slider(
162
+ minimum=0,
163
+ maximum=2.0,
164
+ value=0.7,
165
+ step=0.1,
166
+ label="Temperature",
167
+ info="Higher = more creative/chaotic",
168
+ )
169
+ max_tokens = gr.Slider(
170
+ minimum=100,
171
+ maximum=4096,
172
+ value=1000,
173
+ step=1,
174
+ label="Max tokens per response",
175
+ )
176
+ context_length = gr.Slider(
177
+ minimum=1,
178
+ maximum=10,
179
+ value=2,
180
+ step=1,
181
+ label="Context length",
182
+ info="Number of previous messages to send to the chatbot. Be careful with high values, it can blow up the token budget quickly.",
183
+ )
184
  with gr.Column(scale=0.7):
185
  chatbot = gr.Chatbot(elem_id="chatbox")
186
+ input_message = gr.Textbox(
187
+ show_label=False,
188
+ placeholder="Enter text and press enter",
189
+ visible=True,
190
+ ).style(container=False)
191
  btn_submit = gr.Button("Submit")
192
  total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
193
  btn_clear_conversation = gr.Button("🔃 Start New Conversation")
194
 
195
+ gr.HTML(
196
+ """<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>
197
+ <p><img src="https://visitor-badge.glitch.me/badge?page_id=dragonswing.chatgpt-app" alt="visitors"></p></center>"""
198
+ )
199
 
200
+ btn_submit.click(
201
+ submit_message,
202
+ [
203
+ chatbot,
204
+ input_message,
205
+ prompt_template,
206
+ temperature,
207
+ max_tokens,
208
+ context_length,
209
+ ],
210
+ [input_message, chatbot, total_tokens_str],
211
+ )
212
+ input_message.submit(
213
+ submit_message,
214
+ [
215
+ chatbot,
216
+ input_message,
217
+ prompt_template,
218
+ temperature,
219
+ max_tokens,
220
+ context_length,
221
+ ],
222
+ [input_message, chatbot, total_tokens_str],
223
+ )
224
+ btn_clear_conversation.click(
225
+ clear_conversation, [], [input_message, chatbot, total_tokens_str]
226
+ )
227
+ prompt_template.change(
228
+ on_prompt_template_change,
229
+ inputs=[prompt_template],
230
+ outputs=[prompt_template_preview],
231
+ )
232
  user_token.change(on_token_change, inputs=[user_token], outputs=[])
233
+
234
+ demo.load(
235
+ download_prompt_templates, inputs=None, outputs=[prompt_template], queue=False
236
+ )
237
 
238
 
239
  demo.queue(concurrency_count=10)
240
+ demo.launch(height="800px")