acecalisto3 commited on
Commit
8c76f34
·
verified ·
1 Parent(s): 726561d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -173
app.py CHANGED
@@ -3,7 +3,7 @@ import subprocess
3
  import random
4
  from huggingface_hub import InferenceClient
5
  import gradio as gr
6
- from safe_search import safe_search
7
  from i_search import google
8
  from i_search import i_search as i_s
9
  from agent import (
@@ -18,6 +18,9 @@ from agent import (
18
  READ_PROMPT,
19
  TASK_PROMPT,
20
  UNDERSTAND_TEST_RESULTS_PROMPT,
 
 
 
21
  )
22
  from utils import parse_action, parse_file_content, read_python_module_structure
23
  from datetime import datetime
@@ -32,35 +35,44 @@ client = InferenceClient(
32
 
33
  VERBOSE = True
34
  MAX_HISTORY = 100
35
- #MODEL = "gpt-3.5-turbo" # "gpt-4"
36
 
37
- def format_prompt(message, history):
38
- prompt = "<s>"
39
- for user_prompt, bot_response in history:
40
- prompt += f"[INST] {user_prompt} [/INST]"
41
- prompt += f" {bot_response}</s> "
42
- prompt += f"[INST] {message} [/INST]"
43
- return prompt
 
 
 
 
 
 
 
 
44
 
45
  def run_gpt(
46
  prompt_template,
47
  stop_tokens,
48
  max_tokens,
49
  purpose,
 
 
 
 
50
  **prompt_kwargs,
51
  ):
52
- seed = random.randint(1,1111111111111111)
53
- print (seed)
54
  generate_kwargs = dict(
55
- temperature=1.0,
56
- max_new_tokens=2096,
57
- top_p=0.99,
58
- repetition_penalty=1.0,
59
  do_sample=True,
60
  seed=seed,
61
  )
62
 
63
-
64
  content = PREFIX.format(
65
  date_time_str=date_time_str,
66
  purpose=purpose,
@@ -68,12 +80,10 @@ def run_gpt(
68
  ) + prompt_template.format(**prompt_kwargs)
69
  if VERBOSE:
70
  print(LOG_PROMPT.format(content))
71
-
72
-
73
- #formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
74
- #formatted_prompt = format_prompt(f'{content}', history)
75
 
76
- stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
 
 
77
  resp = ""
78
  for response in stream:
79
  resp += response.token.text
@@ -90,22 +100,23 @@ def compress_history(purpose, task, history, directory):
90
  purpose=purpose,
91
  task=task,
92
  history=history,
 
 
 
 
93
  )
94
  history = "observation: {}\n".format(resp)
95
  return history
96
-
97
  def call_search(purpose, task, history, directory, action_input):
98
  print("CALLING SEARCH")
99
  try:
100
-
101
  if "http" in action_input:
102
  if "<" in action_input:
103
  action_input = action_input.strip("<")
104
  if ">" in action_input:
105
  action_input = action_input.strip(">")
106
-
107
  response = i_s(action_input)
108
- #response = google(search_return)
109
  print(response)
110
  history += "observation: search result is: {}\n".format(response)
111
  else:
@@ -117,11 +128,15 @@ def call_search(purpose, task, history, directory, action_input):
117
  def call_main(purpose, task, history, directory, action_input):
118
  resp = run_gpt(
119
  ACTION_PROMPT,
120
- stop_tokens=["observation:", "task:", "action:","thought:"],
121
  max_tokens=2096,
122
  purpose=purpose,
123
  task=task,
124
  history=history,
 
 
 
 
125
  )
126
  lines = resp.strip().strip("\n").split("\n")
127
  for line in lines:
@@ -130,11 +145,9 @@ def call_main(purpose, task, history, directory, action_input):
130
  if line.startswith("thought: "):
131
  history += "{}\n".format(line)
132
  elif line.startswith("action: "):
133
-
134
  action_name, action_input = parse_action(line)
135
- print (f'ACTION_NAME :: {action_name}')
136
- print (f'ACTION_INPUT :: {action_input}')
137
-
138
  history += "{}\n".format(line)
139
  if "COMPLETE" in action_name or "COMPLETE" in action_input:
140
  task = "END"
@@ -143,10 +156,6 @@ def call_main(purpose, task, history, directory, action_input):
143
  return action_name, action_input, history, task
144
  else:
145
  history += "{}\n".format(line)
146
- #history += "observation: the following command did not produce any useful output: '{}', I need to check the commands syntax, or use a different command\n".format(line)
147
-
148
- #return action_name, action_input, history, task
149
- #assert False, "unknown action: {}".format(line)
150
  return "MAIN", None, history, task
151
 
152
  def call_set_task(purpose, task, history, directory, action_input):
@@ -157,6 +166,10 @@ def call_set_task(purpose, task, history, directory, action_input):
157
  purpose=purpose,
158
  task=task,
159
  history=history,
 
 
 
 
160
  ).strip("\n")
161
  history += "observation: task has been updated to: {}\n".format(task)
162
  return "MAIN", None, history, task
@@ -170,46 +183,41 @@ NAME_TO_FUNC = {
170
  "UPDATE-TASK": call_set_task,
171
  "SEARCH": call_search,
172
  "COMPLETE": end_fn,
173
-
174
  }
175
 
176
  def run_action(purpose, task, history, directory, action_name, action_input):
177
- print(f'action_name::{action_name}')
178
  try:
179
  if "RESPONSE" in action_name or "COMPLETE" in action_name:
180
- action_name="COMPLETE"
181
- task="END"
182
  return action_name, "COMPLETE", history, task
183
-
184
  # compress the history when it is long
185
  if len(history.split("\n")) > MAX_HISTORY:
186
  if VERBOSE:
187
  print("COMPRESSING HISTORY")
188
  history = compress_history(purpose, task, history, directory)
189
  if not action_name in NAME_TO_FUNC:
190
- action_name="MAIN"
191
  if action_name == "" or action_name == None:
192
- action_name="MAIN"
193
  assert action_name in NAME_TO_FUNC
194
-
195
  print("RUN: ", action_name, action_input)
196
  return NAME_TO_FUNC[action_name](purpose, task, history, directory, action_input)
197
  except Exception as e:
198
  history += "observation: the previous command did not produce any useful output, I need to check the commands syntax, or use a different command\n"
199
-
200
  return "MAIN", None, history, task
201
 
202
- def run(purpose,history):
203
-
204
- #print(purpose)
205
- #print(hist)
206
- task=None
207
- directory="./"
208
  if history:
209
- history=str(history).strip("[]")
210
  if not history:
211
  history = ""
212
-
213
  action_name = "UPDATE-TASK" if task is None else "MAIN"
214
  action_input = None
215
  while True:
@@ -231,139 +239,97 @@ def run(purpose,history):
231
  action_input,
232
  )
233
  yield (history)
234
- #yield ("",[(purpose,history)])
235
  if task == "END":
236
  return (history)
237
- #return ("", [(purpose,history)])
238
 
239
- ################################################
240
-
241
- def format_prompt(message, history):
242
- prompt = "<s>"
243
- for user_prompt, bot_response in history:
244
- prompt += f"[INST] {user_prompt} [/INST]"
245
- prompt += f" {bot_response}</s> "
246
- prompt += f"[INST] {message} [/INST]"
247
- return prompt
248
- agents =[
249
- "WEB_DEV",
250
- "AI_SYSTEM_PROMPT",
251
- "PYTHON_CODE_DEV"
252
- ]
253
- def generate(
254
- prompt, history, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
255
- ):
256
- seed = random.randint(1,1111111111111111)
257
-
258
- agent=prompts.WEB_DEV
259
- if agent_name == "WEB_DEV":
260
- agent = prompts.WEB_DEV
261
- if agent_name == "AI_SYSTEM_PROMPT":
262
- agent = prompts.AI_SYSTEM_PROMPT
263
- if agent_name == "PYTHON_CODE_DEV":
264
- agent = prompts.PYTHON_CODE_DEV
265
- system_prompt=agent
266
- temperature = float(temperature)
267
- if temperature < 1e-2:
268
- temperature = 1e-2
269
- top_p = float(top_p)
270
-
271
- generate_kwargs = dict(
272
- temperature=temperature,
273
- max_new_tokens=max_new_tokens,
274
- top_p=top_p,
275
- repetition_penalty=repetition_penalty,
276
- do_sample=True,
277
- seed=seed,
278
- )
279
-
280
- formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
281
- stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
282
- output = ""
283
-
284
- for response in stream:
285
- output += response.token.text
286
- yield output
287
- return output
288
-
289
- additional_inputs=[
290
- gr.Dropdown(
291
- label="Agents",
292
- choices=[s for s in agents],
293
- value=agents[0],
294
- interactive=True,
295
- ),
296
- gr.Textbox(
297
- label="System Prompt",
298
- max_lines=1,
299
- interactive=True,
300
- ),
301
- gr.Slider(
302
- label="Temperature",
303
- value=0.9,
304
- minimum=0.0,
305
- maximum=1.0,
306
- step=0.05,
307
- interactive=True,
308
- info="Higher values produce more diverse outputs",
309
- ),
310
-
311
- gr.Slider(
312
- label="Max new tokens",
313
- value=1048*10,
314
- minimum=0,
315
- maximum=1048*10,
316
- step=64,
317
- interactive=True,
318
- info="The maximum numbers of new tokens",
319
- ),
320
- gr.Slider(
321
- label="Top-p (nucleus sampling)",
322
- value=0.90,
323
- minimum=0.0,
324
- maximum=1,
325
- step=0.05,
326
- interactive=True,
327
- info="Higher values sample more low-probability tokens",
328
- ),
329
- gr.Slider(
330
- label="Repetition penalty",
331
- value=1.2,
332
- minimum=1.0,
333
- maximum=2.0,
334
- step=0.05,
335
- interactive=True,
336
- info="Penalize repeated tokens",
337
- ),
338
-
339
- ]
340
-
341
- examples=[["What are the biggest news stories today?", None, None, None, None, None, ],
342
- ["When is the next full moon?", None, None, None, None, None, ],
343
- ["I'm planning a vacation to Japan. Can you suggest a one-week itinerary including must-visit places and local cuisines to try?", None, None, None, None, None, ],
344
- ["Can you write a short story about a time-traveling detective who solves historical mysteries?", None, None, None, None, None,],
345
- ["I'm trying to learn French. Can you provide some common phrases that would be useful for a beginner, along with their pronunciations?", None, None, None, None, None,],
346
- ["I have chicken, rice, and bell peppers in my kitchen. Can you suggest an easy recipe I can make with these ingredients?", None, None, None, None, None,],
347
- ["Can you explain how the QuickSort algorithm works and provide a Python implementation?", None, None, None, None, None,],
348
- ["What are some unique features of Rust that make it stand out compared to other systems programming languages like C++?", None, None, None, None, None,],
349
- ]
350
-
351
- def process_input(user_input, history, chatbot):
352
  """Processes user input and updates the chatbot."""
353
  purpose = "General"
354
  history = history + [(user_input, "")]
355
  chatbot.append(user_input, "")
356
 
357
- for response in run(purpose, history):
 
 
 
 
 
 
 
 
 
358
  chatbot.append("", response)
359
  yield chatbot
360
 
361
  with gr.Blocks() as iface:
362
- chatbot = gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, layout="panel")
363
- msg = gr.Textbox(label="Enter your message")
364
  with gr.Row():
365
- submit_b = gr.Button("Submit")
366
- clear = gr.ClearButton([msg, chatbot])
367
- submit_b.click(process_input, [msg, chatbot, chatbot], chatbot)
368
- msg.submit(process_input, [msg, chatbot, chatbot], chatbot)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
  iface.launch()
 
3
  import random
4
  from huggingface_hub import InferenceClient
5
  import gradio as gr
6
+ from safe_search import safe_search # You need to implement this
7
  from i_search import google
8
  from i_search import i_search as i_s
9
  from agent import (
 
18
  READ_PROMPT,
19
  TASK_PROMPT,
20
  UNDERSTAND_TEST_RESULTS_PROMPT,
21
+ WEB_DEV,
22
+ AI_SYSTEM_PROMPT,
23
+ PYTHON_CODE_DEV,
24
  )
25
  from utils import parse_action, parse_file_content, read_python_module_structure
26
  from datetime import datetime
 
35
 
36
  VERBOSE = True
37
  MAX_HISTORY = 100
 
38
 
39
+ # Default values for inputs
40
+ DEFAULT_AGENT = "WEB_DEV"
41
+ DEFAULT_SYSTEM_PROMPT = ""
42
+ DEFAULT_TEMPERATURE = 0.9
43
+ DEFAULT_MAX_TOKENS = 256
44
+ DEFAULT_TOP_P = 0.95
45
+ DEFAULT_REPETITION_PENALTY = 1.0
46
+
47
+ def format_prompt(message, history, system_prompt):
48
+ prompt = "<s>"
49
+ prompt += f"[INST] {system_prompt}, {message} [/INST]"
50
+ for user_prompt, bot_response in history:
51
+ prompt += f"[INST] {user_prompt} [/INST]"
52
+ prompt += f" {bot_response}</s> "
53
+ return prompt
54
 
55
  def run_gpt(
56
  prompt_template,
57
  stop_tokens,
58
  max_tokens,
59
  purpose,
60
+ temperature,
61
+ top_p,
62
+ repetition_penalty,
63
+ system_prompt,
64
  **prompt_kwargs,
65
  ):
66
+ seed = random.randint(1, 1111111111111111)
 
67
  generate_kwargs = dict(
68
+ temperature=temperature,
69
+ max_new_tokens=max_tokens,
70
+ top_p=top_p,
71
+ repetition_penalty=repetition_penalty,
72
  do_sample=True,
73
  seed=seed,
74
  )
75
 
 
76
  content = PREFIX.format(
77
  date_time_str=date_time_str,
78
  purpose=purpose,
 
80
  ) + prompt_template.format(**prompt_kwargs)
81
  if VERBOSE:
82
  print(LOG_PROMPT.format(content))
 
 
 
 
83
 
84
+ formatted_prompt = format_prompt(content, prompt_kwargs.get("history", []), system_prompt)
85
+
86
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
87
  resp = ""
88
  for response in stream:
89
  resp += response.token.text
 
100
  purpose=purpose,
101
  task=task,
102
  history=history,
103
+ temperature=DEFAULT_TEMPERATURE,
104
+ top_p=DEFAULT_TOP_P,
105
+ repetition_penalty=DEFAULT_REPETITION_PENALTY,
106
+ system_prompt=DEFAULT_SYSTEM_PROMPT,
107
  )
108
  history = "observation: {}\n".format(resp)
109
  return history
110
+
111
  def call_search(purpose, task, history, directory, action_input):
112
  print("CALLING SEARCH")
113
  try:
 
114
  if "http" in action_input:
115
  if "<" in action_input:
116
  action_input = action_input.strip("<")
117
  if ">" in action_input:
118
  action_input = action_input.strip(">")
 
119
  response = i_s(action_input)
 
120
  print(response)
121
  history += "observation: search result is: {}\n".format(response)
122
  else:
 
128
  def call_main(purpose, task, history, directory, action_input):
129
  resp = run_gpt(
130
  ACTION_PROMPT,
131
+ stop_tokens=["observation:", "task:", "action:", "thought:"],
132
  max_tokens=2096,
133
  purpose=purpose,
134
  task=task,
135
  history=history,
136
+ temperature=DEFAULT_TEMPERATURE,
137
+ top_p=DEFAULT_TOP_P,
138
+ repetition_penalty=DEFAULT_REPETITION_PENALTY,
139
+ system_prompt=DEFAULT_SYSTEM_PROMPT,
140
  )
141
  lines = resp.strip().strip("\n").split("\n")
142
  for line in lines:
 
145
  if line.startswith("thought: "):
146
  history += "{}\n".format(line)
147
  elif line.startswith("action: "):
 
148
  action_name, action_input = parse_action(line)
149
+ print(f"ACTION_NAME :: {action_name}")
150
+ print(f"ACTION_INPUT :: {action_input}")
 
151
  history += "{}\n".format(line)
152
  if "COMPLETE" in action_name or "COMPLETE" in action_input:
153
  task = "END"
 
156
  return action_name, action_input, history, task
157
  else:
158
  history += "{}\n".format(line)
 
 
 
 
159
  return "MAIN", None, history, task
160
 
161
  def call_set_task(purpose, task, history, directory, action_input):
 
166
  purpose=purpose,
167
  task=task,
168
  history=history,
169
+ temperature=DEFAULT_TEMPERATURE,
170
+ top_p=DEFAULT_TOP_P,
171
+ repetition_penalty=DEFAULT_REPETITION_PENALTY,
172
+ system_prompt=DEFAULT_SYSTEM_PROMPT,
173
  ).strip("\n")
174
  history += "observation: task has been updated to: {}\n".format(task)
175
  return "MAIN", None, history, task
 
183
  "UPDATE-TASK": call_set_task,
184
  "SEARCH": call_search,
185
  "COMPLETE": end_fn,
 
186
  }
187
 
188
  def run_action(purpose, task, history, directory, action_name, action_input):
189
+ print(f"action_name::{action_name}")
190
  try:
191
  if "RESPONSE" in action_name or "COMPLETE" in action_name:
192
+ action_name = "COMPLETE"
193
+ task = "END"
194
  return action_name, "COMPLETE", history, task
195
+
196
  # compress the history when it is long
197
  if len(history.split("\n")) > MAX_HISTORY:
198
  if VERBOSE:
199
  print("COMPRESSING HISTORY")
200
  history = compress_history(purpose, task, history, directory)
201
  if not action_name in NAME_TO_FUNC:
202
+ action_name = "MAIN"
203
  if action_name == "" or action_name == None:
204
+ action_name = "MAIN"
205
  assert action_name in NAME_TO_FUNC
206
+
207
  print("RUN: ", action_name, action_input)
208
  return NAME_TO_FUNC[action_name](purpose, task, history, directory, action_input)
209
  except Exception as e:
210
  history += "observation: the previous command did not produce any useful output, I need to check the commands syntax, or use a different command\n"
 
211
  return "MAIN", None, history, task
212
 
213
+ def run(purpose, history, agent_name=DEFAULT_AGENT, system_prompt=DEFAULT_SYSTEM_PROMPT, temperature=DEFAULT_TEMPERATURE, max_tokens=DEFAULT_MAX_TOKENS, top_p=DEFAULT_TOP_P, repetition_penalty=DEFAULT_REPETITION_PENALTY):
214
+ task = None
215
+ directory = "./"
 
 
 
216
  if history:
217
+ history = str(history).strip("[]")
218
  if not history:
219
  history = ""
220
+
221
  action_name = "UPDATE-TASK" if task is None else "MAIN"
222
  action_input = None
223
  while True:
 
239
  action_input,
240
  )
241
  yield (history)
 
242
  if task == "END":
243
  return (history)
 
244
 
245
+ def process_input(user_input, history, chatbot, agent_name, system_prompt, temperature, max_tokens, top_p, repetition_penalty):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  """Processes user input and updates the chatbot."""
247
  purpose = "General"
248
  history = history + [(user_input, "")]
249
  chatbot.append(user_input, "")
250
 
251
+ for response in run(
252
+ purpose,
253
+ history,
254
+ agent_name=agent_name,
255
+ system_prompt=system_prompt,
256
+ temperature=temperature,
257
+ max_tokens=max_tokens,
258
+ top_p=top_p,
259
+ repetition_penalty=repetition_penalty,
260
+ ):
261
  chatbot.append("", response)
262
  yield chatbot
263
 
264
  with gr.Blocks() as iface:
 
 
265
  with gr.Row():
266
+ chatbot = gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, layout="panel")
267
+ with gr.Column():
268
+ msg = gr.Textbox(label="Enter your message")
269
+ with gr.Row():
270
+ submit_b = gr.Button("Submit")
271
+ clear = gr.ClearButton([msg, chatbot])
272
+
273
+ # Input fields for configuration
274
+ with gr.Column():
275
+ agent_dropdown = gr.Dropdown(
276
+ label="Agent",
277
+ choices=[s for s in ["WEB_DEV", "AI_SYSTEM_PROMPT", "PYTHON_CODE_DEV"]],
278
+ value=DEFAULT_AGENT,
279
+ interactive=True,
280
+ )
281
+ system_prompt_textbox = gr.Textbox(
282
+ label="System Prompt",
283
+ value=DEFAULT_SYSTEM_PROMPT,
284
+ interactive=True,
285
+ )
286
+ temperature_slider = gr.Slider(
287
+ label="Temperature",
288
+ value=DEFAULT_TEMPERATURE,
289
+ minimum=0.0,
290
+ maximum=1.0,
291
+ step=0.05,
292
+ interactive=True,
293
+ info="Higher values produce more diverse outputs",
294
+ )
295
+ max_tokens_slider = gr.Slider(
296
+ label="Max new tokens",
297
+ value=DEFAULT_MAX_TOKENS,
298
+ minimum=0,
299
+ maximum=1048 * 10,
300
+ step=64,
301
+ interactive=True,
302
+ info="The maximum numbers of new tokens",
303
+ )
304
+ top_p_slider = gr.Slider(
305
+ label="Top-p (nucleus sampling)",
306
+ value=DEFAULT_TOP_P,
307
+ minimum=0.0,
308
+ maximum=1,
309
+ step=0.05,
310
+ interactive=True,
311
+ info="Higher values sample more low-probability tokens",
312
+ )
313
+ repetition_penalty_slider = gr.Slider(
314
+ label="Repetition penalty",
315
+ value=DEFAULT_REPETITION_PENALTY,
316
+ minimum=1.0,
317
+ maximum=2.0,
318
+ step=0.05,
319
+ interactive=True,
320
+ info="Penalize repeated tokens",
321
+ )
322
+
323
+ # Connect input fields to the processing function
324
+ submit_b.click(
325
+ process_input,
326
+ [msg, chatbot, chatbot, agent_dropdown, system_prompt_textbox, temperature_slider, max_tokens_slider, top_p_slider, repetition_penalty_slider],
327
+ chatbot,
328
+ )
329
+ msg.submit(
330
+ process_input,
331
+ [msg, chatbot, chatbot, agent_dropdown, system_prompt_textbox, temperature_slider, max_tokens_slider, top_p_slider, repetition_penalty_slider],
332
+ chatbot,
333
+ )
334
+
335
  iface.launch()