Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,17 +3,24 @@ 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 datetime import datetime
|
10 |
-
import logging
|
11 |
-
import json
|
12 |
-
import nltk # Import nltk for the generate_text_chunked function
|
13 |
-
from transformers import pipeline # Import pipeline from transformers
|
14 |
-
|
15 |
-
nltk.download('punkt') # Download the punkt tokenizer if you haven't already
|
16 |
-
|
17 |
now = datetime.now()
|
18 |
date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
|
19 |
|
@@ -21,77 +28,24 @@ client = InferenceClient(
|
|
21 |
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
22 |
)
|
23 |
|
24 |
-
# --- Set up logging ---
|
25 |
-
logging.basicConfig(
|
26 |
-
filename="app.log", # Name of the log file
|
27 |
-
level=logging.INFO, # Set the logging level (INFO, DEBUG, etc.)
|
28 |
-
format="%(asctime)s - %(levelname)s - %(message)s",
|
29 |
-
)
|
30 |
-
|
31 |
-
agents = [
|
32 |
-
"WEB_DEV",
|
33 |
-
"AI_SYSTEM_PROMPT",
|
34 |
-
"PYTHON_CODE_DEV"
|
35 |
-
]
|
36 |
############################################
|
37 |
|
|
|
38 |
VERBOSE = True
|
39 |
-
MAX_HISTORY =
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
Purpose: {purpose}
|
45 |
-
Safe Search: {safe_search}
|
46 |
-
"""
|
47 |
-
|
48 |
-
LOG_PROMPT = """
|
49 |
-
PROMPT: {content}
|
50 |
-
"""
|
51 |
-
|
52 |
-
LOG_RESPONSE = """
|
53 |
-
RESPONSE: {resp}
|
54 |
-
"""
|
55 |
-
|
56 |
-
COMPRESS_HISTORY_PROMPT = """
|
57 |
-
You are a helpful AI assistant. Your task is to compress the following history into a summary that is no longer than 512 tokens.
|
58 |
-
History:
|
59 |
-
{history}
|
60 |
-
"""
|
61 |
-
|
62 |
-
ACTION_PROMPT = """
|
63 |
-
You are a helpful AI assistant. You are working on the task: {task}
|
64 |
-
Your current history is:
|
65 |
-
{history}
|
66 |
-
What is your next thought?
|
67 |
-
thought:
|
68 |
-
What is your next action?
|
69 |
-
action:
|
70 |
-
"""
|
71 |
-
|
72 |
-
TASK_PROMPT = """
|
73 |
-
You are a helpful AI assistant. Your current history is:
|
74 |
-
{history}
|
75 |
-
What is the next task?
|
76 |
-
task:
|
77 |
-
"""
|
78 |
-
|
79 |
-
UNDERSTAND_TEST_RESULTS_PROMPT = """
|
80 |
-
You are a helpful AI assistant. The test results are:
|
81 |
-
{test_results}
|
82 |
-
What do you want to know about the test results?
|
83 |
-
thought:
|
84 |
-
"""
|
85 |
-
|
86 |
-
def format_prompt(message, history, max_history_turns=2):
|
87 |
prompt = "<s>"
|
88 |
-
|
89 |
-
for user_prompt, bot_response in history[-max_history_turns:]:
|
90 |
prompt += f"[INST] {user_prompt} [/INST]"
|
91 |
prompt += f" {bot_response}</s> "
|
92 |
prompt += f"[INST] {message} [/INST]"
|
93 |
return prompt
|
94 |
|
|
|
|
|
95 |
def run_gpt(
|
96 |
prompt_template,
|
97 |
stop_tokens,
|
@@ -100,7 +54,16 @@ def run_gpt(
|
|
100 |
**prompt_kwargs,
|
101 |
):
|
102 |
seed = random.randint(1,1111111111111111)
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
content = PREFIX.format(
|
106 |
date_time_str=date_time_str,
|
@@ -108,59 +71,21 @@ def run_gpt(
|
|
108 |
safe_search=safe_search,
|
109 |
) + prompt_template.format(**prompt_kwargs)
|
110 |
if VERBOSE:
|
111 |
-
|
112 |
-
resp = client.text_generation(content, max_new_tokens=max_new_tokens, stop_sequences=stop_tokens, temperature=0.7, top_p=0.8, repetition_penalty=1.5)
|
113 |
-
if VERBOSE:
|
114 |
-
logging.info(LOG_RESPONSE.format(resp=resp)) # Log the response
|
115 |
-
return resp
|
116 |
-
|
117 |
-
def generate(
|
118 |
-
prompt, history, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0, model="mistralai/Mixtral-8x7B-Instruct-v0.1"
|
119 |
-
):
|
120 |
-
seed = random.randint(1, 1111111111111111)
|
121 |
-
logging.info(f"Seed: {seed}") # Log the seed
|
122 |
-
|
123 |
-
# Set the agent prompt based on agent_name
|
124 |
-
agent = "You are a helpful AI assistant."
|
125 |
-
if agent_name == "WEB_DEV":
|
126 |
-
agent += " You are a web developer."
|
127 |
-
elif agent_name == "AI_SYSTEM_PROMPT":
|
128 |
-
agent += " You are an AI system."
|
129 |
-
elif agent_name == "PYTHON_CODE_DEV":
|
130 |
-
agent += " You are a Python code developer."
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
top_p = float(top_p)
|
136 |
-
|
137 |
-
# Add the system prompt to the beginning of the prompt
|
138 |
-
formatted_prompt = f"{system_prompt} {prompt}"
|
139 |
-
|
140 |
-
# Use 'prompt' here instead of 'message'
|
141 |
-
formatted_prompt = format_prompt(formatted_prompt, history, max_history_turns=5) # Truncated history
|
142 |
-
logging.info(f"Formatted Prompt: {formatted_prompt}")
|
143 |
-
|
144 |
-
# Conditionally create client
|
145 |
-
this_client = InferenceClient(model) if model != "mistralai/Mixtral-8x7B-Instruct-v0.1" else client
|
146 |
|
147 |
-
stream =
|
148 |
-
formatted_prompt,
|
149 |
-
temperature=temperature,
|
150 |
-
max_new_tokens=max_new_tokens,
|
151 |
-
top_p=top_p,
|
152 |
-
repetition_penalty=repetition_penalty,
|
153 |
-
stream=True,
|
154 |
-
details=True,
|
155 |
-
return_full_text=False
|
156 |
-
)
|
157 |
resp = ""
|
158 |
for response in stream:
|
159 |
resp += response.token.text
|
160 |
-
yield resp # This allows for streaming the response
|
161 |
|
162 |
if VERBOSE:
|
163 |
-
|
|
|
|
|
164 |
|
165 |
def compress_history(purpose, task, history, directory):
|
166 |
resp = run_gpt(
|
@@ -175,7 +100,7 @@ def compress_history(purpose, task, history, directory):
|
|
175 |
return history
|
176 |
|
177 |
def call_search(purpose, task, history, directory, action_input):
|
178 |
-
|
179 |
try:
|
180 |
|
181 |
if "http" in action_input:
|
@@ -186,7 +111,7 @@ def call_search(purpose, task, history, directory, action_input):
|
|
186 |
|
187 |
response = i_s(action_input)
|
188 |
#response = google(search_return)
|
189 |
-
|
190 |
history += "observation: search result is: {}\n".format(response)
|
191 |
else:
|
192 |
history += "observation: I need to provide a valid URL to 'action: SEARCH action_input=https://URL'\n"
|
@@ -195,11 +120,10 @@ def call_search(purpose, task, history, directory, action_input):
|
|
195 |
return "MAIN", None, history, task
|
196 |
|
197 |
def call_main(purpose, task, history, directory, action_input):
|
198 |
-
logging.info(f"CALLING MAIN: {action_input}")
|
199 |
resp = run_gpt(
|
200 |
ACTION_PROMPT,
|
201 |
stop_tokens=["observation:", "task:", "action:","thought:"],
|
202 |
-
max_tokens=
|
203 |
purpose=purpose,
|
204 |
task=task,
|
205 |
history=history,
|
@@ -210,11 +134,12 @@ def call_main(purpose, task, history, directory, action_input):
|
|
210 |
continue
|
211 |
if line.startswith("thought: "):
|
212 |
history += "{}\n".format(line)
|
213 |
-
logging.info(f"Thought: {line}")
|
214 |
elif line.startswith("action: "):
|
215 |
|
216 |
action_name, action_input = parse_action(line)
|
217 |
-
|
|
|
|
|
218 |
history += "{}\n".format(line)
|
219 |
if "COMPLETE" in action_name or "COMPLETE" in action_input:
|
220 |
task = "END"
|
@@ -223,7 +148,6 @@ def call_main(purpose, task, history, directory, action_input):
|
|
223 |
return action_name, action_input, history, task
|
224 |
else:
|
225 |
history += "{}\n".format(line)
|
226 |
-
logging.info(f"Other Output: {line}")
|
227 |
#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)
|
228 |
|
229 |
#return action_name, action_input, history, task
|
@@ -232,7 +156,6 @@ def call_main(purpose, task, history, directory, action_input):
|
|
232 |
|
233 |
|
234 |
def call_set_task(purpose, task, history, directory, action_input):
|
235 |
-
logging.info(f"CALLING SET_TASK: {action_input}")
|
236 |
task = run_gpt(
|
237 |
TASK_PROMPT,
|
238 |
stop_tokens=[],
|
@@ -245,7 +168,6 @@ def call_set_task(purpose, task, history, directory, action_input):
|
|
245 |
return "MAIN", None, history, task
|
246 |
|
247 |
def end_fn(purpose, task, history, directory, action_input):
|
248 |
-
logging.info(f"CALLING END_FN: {action_input}")
|
249 |
task = "END"
|
250 |
return "COMPLETE", "COMPLETE", history, task
|
251 |
|
@@ -258,7 +180,7 @@ NAME_TO_FUNC = {
|
|
258 |
}
|
259 |
|
260 |
def run_action(purpose, task, history, directory, action_name, action_input):
|
261 |
-
|
262 |
try:
|
263 |
if "RESPONSE" in action_name or "COMPLETE" in action_name:
|
264 |
action_name="COMPLETE"
|
@@ -267,7 +189,8 @@ def run_action(purpose, task, history, directory, action_name, action_input):
|
|
267 |
|
268 |
# compress the history when it is long
|
269 |
if len(history.split("\n")) > MAX_HISTORY:
|
270 |
-
|
|
|
271 |
history = compress_history(purpose, task, history, directory)
|
272 |
if not action_name in NAME_TO_FUNC:
|
273 |
action_name="MAIN"
|
@@ -275,14 +198,15 @@ def run_action(purpose, task, history, directory, action_name, action_input):
|
|
275 |
action_name="MAIN"
|
276 |
assert action_name in NAME_TO_FUNC
|
277 |
|
278 |
-
|
279 |
return NAME_TO_FUNC[action_name](purpose, task, history, directory, action_input)
|
280 |
except Exception as e:
|
281 |
history += "observation: the previous command did not produce any useful output, I need to check the commands syntax, or use a different command\n"
|
282 |
-
|
283 |
return "MAIN", None, history, task
|
284 |
|
285 |
-
def run(purpose,history):
|
|
|
286 |
#print(purpose)
|
287 |
#print(hist)
|
288 |
task=None
|
@@ -295,12 +219,14 @@ def run(purpose,history):
|
|
295 |
action_name = "UPDATE-TASK" if task is None else "MAIN"
|
296 |
action_input = None
|
297 |
while True:
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
|
|
|
|
304 |
|
305 |
action_name, action_input, history, task = run_action(
|
306 |
purpose,
|
@@ -320,91 +246,55 @@ def run(purpose,history):
|
|
320 |
|
321 |
################################################
|
322 |
|
323 |
-
def format_prompt(message, history
|
324 |
prompt = "<s>"
|
325 |
-
|
326 |
-
for user_prompt, bot_response in history[-max_history_turns:]:
|
327 |
prompt += f"[INST] {user_prompt} [/INST]"
|
328 |
prompt += f" {bot_response}</s> "
|
329 |
prompt += f"[INST] {message} [/INST]"
|
330 |
return prompt
|
331 |
-
|
332 |
agents =[
|
333 |
"WEB_DEV",
|
334 |
"AI_SYSTEM_PROMPT",
|
335 |
"PYTHON_CODE_DEV"
|
336 |
]
|
337 |
-
|
338 |
def generate(
|
339 |
-
|
340 |
):
|
341 |
seed = random.randint(1,1111111111111111)
|
342 |
-
logging.info(f"Seed: {seed}") # Log the seed
|
343 |
|
344 |
-
|
345 |
-
agent = "You are a helpful AI assistant."
|
346 |
if agent_name == "WEB_DEV":
|
347 |
-
agent
|
348 |
-
|
349 |
-
agent
|
350 |
-
|
351 |
-
agent
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
top_p = float(top_p)
|
357 |
|
358 |
-
|
359 |
-
formatted_prompt = f"{system_prompt} {prompt}"
|
360 |
-
|
361 |
-
# Use 'prompt' here instead of 'message'
|
362 |
-
formatted_prompt = format_prompt(formatted_prompt, history, max_history_turns=5) # Truncated history
|
363 |
-
logging.info(f"Formatted Prompt: {formatted_prompt}")
|
364 |
-
|
365 |
-
# Conditionally create client
|
366 |
-
this_client = InferenceClient(model) if model != "mistralai/Mixtral-8x7B-Instruct-v0.1" else client
|
367 |
-
|
368 |
-
stream = this_client.text_generation(
|
369 |
-
formatted_prompt,
|
370 |
temperature=temperature,
|
371 |
max_new_tokens=max_new_tokens,
|
372 |
top_p=top_p,
|
373 |
repetition_penalty=repetition_penalty,
|
374 |
-
|
375 |
-
|
376 |
-
return_full_text=False
|
377 |
)
|
378 |
-
resp = ""
|
379 |
-
for response in stream:
|
380 |
-
resp += response.token.text
|
381 |
-
yield resp # This allows for streaming the response
|
382 |
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
generator = pipeline('text-generation', model=model)
|
392 |
-
|
393 |
-
for sentence in sentences:
|
394 |
-
# Tokenize the sentence and check if it's within the limit
|
395 |
-
tokens = generator.tokenizer(sentence).input_ids
|
396 |
-
if len(tokens) + max_tokens_to_generate <= 32768:
|
397 |
-
# Generate text for this chunk
|
398 |
-
response = generator(sentence, max_length=max_tokens_to_generate, **generation_parameters)
|
399 |
-
generated_text.append(response[0]['generated_text'])
|
400 |
-
else:
|
401 |
-
# Handle cases where the sentence is too long
|
402 |
-
# You could split the sentence further or skip it
|
403 |
-
print(f"Sentence too long: {sentence}")
|
404 |
|
405 |
-
return ''.join(generated_text)
|
406 |
|
407 |
-
|
408 |
additional_inputs=[
|
409 |
gr.Dropdown(
|
410 |
label="Agents",
|
@@ -458,165 +348,42 @@ additional_inputs=[
|
|
458 |
|
459 |
]
|
460 |
|
461 |
-
examples
|
462 |
-
|
463 |
-
"
|
464 |
-
"
|
465 |
-
"
|
466 |
-
"
|
467 |
-
"
|
468 |
-
"
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
"
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
"
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
action_name, action_input = line.strip("action: ").split("=")
|
501 |
-
action_input = action_input.strip()
|
502 |
-
return action_name, action_input
|
503 |
-
|
504 |
-
def get_file_tree(path):
|
505 |
-
"""
|
506 |
-
Recursively explores a directory and returns a nested dictionary representing its file tree.
|
507 |
-
"""
|
508 |
-
tree = {}
|
509 |
-
for item in os.listdir(path):
|
510 |
-
item_path = os.path.join(path, item)
|
511 |
-
if os.path.isdir(item_path):
|
512 |
-
tree[item] = get_file_tree(item_path)
|
513 |
-
else:
|
514 |
-
tree[item] = None
|
515 |
-
return tree
|
516 |
-
|
517 |
-
def display_file_tree(tree, indent=0):
|
518 |
-
"""
|
519 |
-
Prints a formatted representation of the file tree.
|
520 |
-
"""
|
521 |
-
for name, subtree in tree.items():
|
522 |
-
print(f"{' ' * indent}{name}")
|
523 |
-
if subtree is not None:
|
524 |
-
display_file_tree(subtree, indent + 1)
|
525 |
-
|
526 |
-
def project_explorer(path):
|
527 |
-
"""
|
528 |
-
Displays the file tree of a given path in a Streamlit app.
|
529 |
-
"""
|
530 |
-
tree = get_file_tree(path)
|
531 |
-
tree_str = json.dumps(tree, indent=4) # Convert the tree to a string for display
|
532 |
-
return tree_str
|
533 |
-
|
534 |
-
def chat_app_logic(message, history, purpose, agent_name, sys_prompt, temperature, max_new_tokens, top_p, repetition_penalty, model):
|
535 |
-
# Your existing code here
|
536 |
-
|
537 |
-
try:
|
538 |
-
# Pass 'message' as 'prompt'
|
539 |
-
response = ''.join(generate(
|
540 |
-
model=model,
|
541 |
-
prompt=message, # Use 'prompt' here
|
542 |
-
history=history,
|
543 |
-
agent_name=agent_name,
|
544 |
-
sys_prompt=sys_prompt,
|
545 |
-
temperature=temperature,
|
546 |
-
max_new_tokens=max_new_tokens,
|
547 |
-
top_p=top_p,
|
548 |
-
repetition_penalty=repetition_penalty,
|
549 |
-
))
|
550 |
-
except TypeError:
|
551 |
-
# ... (rest of the exception handling)
|
552 |
-
|
553 |
-
response_parts = []
|
554 |
-
for part in generate(
|
555 |
-
model=model,
|
556 |
-
prompt=message, # Use 'prompt' here
|
557 |
-
history=history,
|
558 |
-
agent_name=agent_name,
|
559 |
-
sys_prompt=sys_prompt,
|
560 |
-
temperature=temperature,
|
561 |
-
max_new_tokens=max_new_tokens,
|
562 |
-
top_p=top_p,
|
563 |
-
repetition_penalty=repetition_penalty,
|
564 |
-
):
|
565 |
-
if isinstance(part, str):
|
566 |
-
response_parts.append(part)
|
567 |
-
elif isinstance(part, dict) and 'content' in part:
|
568 |
-
response_parts.append(part['content'])
|
569 |
-
|
570 |
-
response = ''.join(response_parts)
|
571 |
-
history.append((message, response))
|
572 |
-
return history
|
573 |
-
|
574 |
-
history.append((message, response))
|
575 |
-
return history
|
576 |
-
|
577 |
-
def main():
|
578 |
-
with gr.Blocks() as demo:
|
579 |
-
gr.Markdown("## FragMixt")
|
580 |
-
gr.Markdown("### Agents w/ Agents")
|
581 |
-
|
582 |
-
# Chat Interface
|
583 |
-
chatbot = gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel")
|
584 |
-
#chatbot.load(examples)
|
585 |
-
|
586 |
-
# Input Components
|
587 |
-
message = gr.Textbox(label="Enter your message", placeholder="Ask me anything!")
|
588 |
-
purpose = gr.Textbox(label="Purpose", placeholder="What is the purpose of this interaction?")
|
589 |
-
agent_name = gr.Dropdown(label="Agents", choices=[s for s in agents], value=agents[0], interactive=True)
|
590 |
-
sys_prompt = gr.Textbox(label="System Prompt", max_lines=1, interactive=True)
|
591 |
-
temperature = gr.Slider(label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs")
|
592 |
-
max_new_tokens = gr.Slider(label="Max new tokens", value=1048*10, minimum=0, maximum=1048*10, step=64, interactive=True, info="The maximum numbers of new tokens")
|
593 |
-
top_p = gr.Slider(label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens")
|
594 |
-
repetition_penalty = gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens")
|
595 |
-
model_input = gr.Textbox(label="Model", value="mistralai/Mixtral-8x7B-Instruct-v0.1", visible=False)
|
596 |
-
|
597 |
-
# Button to submit the message
|
598 |
-
submit_button = gr.Button(value="Send")
|
599 |
-
|
600 |
-
# Project Explorer Tab
|
601 |
-
with gr.Tab("Project Explorer"):
|
602 |
-
project_path = gr.Textbox(label="Project Path", placeholder="/home/user/app/current_project")
|
603 |
-
explore_button = gr.Button(value="Explore")
|
604 |
-
project_output = gr.Textbox(label="File Tree", lines=20)
|
605 |
-
|
606 |
-
# Chat App Logic Tab
|
607 |
-
with gr.Tab("Chat App"):
|
608 |
-
history = gr.State([])
|
609 |
-
for example in examples:
|
610 |
-
gr.Button(value=example[0]).click(lambda: chat_app_logic(example[0], history, purpose, agent_name, sys_prompt, temperature, max_new_tokens, top_p, repetition_penalty, model=model_input), outputs=chatbot)
|
611 |
-
|
612 |
-
# Connect components to the chat app logic
|
613 |
-
submit_button.click(chat_app_logic, inputs=[message, history, purpose, agent_name, sys_prompt, temperature, max_new_tokens, top_p, repetition_penalty, model_input], outputs=chatbot)
|
614 |
-
message.submit(chat_app_logic, inputs=[message, history, purpose, agent_name, sys_prompt, temperature, max_new_tokens, top_p, repetition_penalty, model_input], outputs=chatbot)
|
615 |
-
|
616 |
-
# Connect components to the project explorer
|
617 |
-
explore_button.click(project_explorer, inputs=project_path, outputs=project_output)
|
618 |
-
if __name__ == "__main__":
|
619 |
-
main() # Call main to initialize the Gradio interface
|
620 |
-
|
621 |
-
with gr.Blocks() as demo:
|
622 |
-
demo.launch(show_api=True)
|
|
|
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 (
|
10 |
+
ACTION_PROMPT,
|
11 |
+
ADD_PROMPT,
|
12 |
+
COMPRESS_HISTORY_PROMPT,
|
13 |
+
LOG_PROMPT,
|
14 |
+
LOG_RESPONSE,
|
15 |
+
MODIFY_PROMPT,
|
16 |
+
PREFIX,
|
17 |
+
SEARCH_QUERY,
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
now = datetime.now()
|
25 |
date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
|
26 |
|
|
|
28 |
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
29 |
)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
############################################
|
32 |
|
33 |
+
|
34 |
VERBOSE = True
|
35 |
+
MAX_HISTORY = 100
|
36 |
+
#MODEL = "gpt-3.5-turbo" # "gpt-4"
|
37 |
+
|
38 |
+
|
39 |
+
def format_prompt(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
prompt = "<s>"
|
41 |
+
for user_prompt, bot_response in history:
|
|
|
42 |
prompt += f"[INST] {user_prompt} [/INST]"
|
43 |
prompt += f" {bot_response}</s> "
|
44 |
prompt += f"[INST] {message} [/INST]"
|
45 |
return prompt
|
46 |
|
47 |
+
|
48 |
+
|
49 |
def run_gpt(
|
50 |
prompt_template,
|
51 |
stop_tokens,
|
|
|
54 |
**prompt_kwargs,
|
55 |
):
|
56 |
seed = random.randint(1,1111111111111111)
|
57 |
+
print (seed)
|
58 |
+
generate_kwargs = dict(
|
59 |
+
temperature=1.0,
|
60 |
+
max_new_tokens=2096,
|
61 |
+
top_p=0.99,
|
62 |
+
repetition_penalty=1.0,
|
63 |
+
do_sample=True,
|
64 |
+
seed=seed,
|
65 |
+
)
|
66 |
+
|
67 |
|
68 |
content = PREFIX.format(
|
69 |
date_time_str=date_time_str,
|
|
|
71 |
safe_search=safe_search,
|
72 |
) + prompt_template.format(**prompt_kwargs)
|
73 |
if VERBOSE:
|
74 |
+
print(LOG_PROMPT.format(content))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
|
77 |
+
#formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
78 |
+
#formatted_prompt = format_prompt(f'{content}', history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
resp = ""
|
82 |
for response in stream:
|
83 |
resp += response.token.text
|
|
|
84 |
|
85 |
if VERBOSE:
|
86 |
+
print(LOG_RESPONSE.format(resp))
|
87 |
+
return resp
|
88 |
+
|
89 |
|
90 |
def compress_history(purpose, task, history, directory):
|
91 |
resp = run_gpt(
|
|
|
100 |
return history
|
101 |
|
102 |
def call_search(purpose, task, history, directory, action_input):
|
103 |
+
print("CALLING SEARCH")
|
104 |
try:
|
105 |
|
106 |
if "http" in action_input:
|
|
|
111 |
|
112 |
response = i_s(action_input)
|
113 |
#response = google(search_return)
|
114 |
+
print(response)
|
115 |
history += "observation: search result is: {}\n".format(response)
|
116 |
else:
|
117 |
history += "observation: I need to provide a valid URL to 'action: SEARCH action_input=https://URL'\n"
|
|
|
120 |
return "MAIN", None, history, task
|
121 |
|
122 |
def call_main(purpose, task, history, directory, action_input):
|
|
|
123 |
resp = run_gpt(
|
124 |
ACTION_PROMPT,
|
125 |
stop_tokens=["observation:", "task:", "action:","thought:"],
|
126 |
+
max_tokens=2096,
|
127 |
purpose=purpose,
|
128 |
task=task,
|
129 |
history=history,
|
|
|
134 |
continue
|
135 |
if line.startswith("thought: "):
|
136 |
history += "{}\n".format(line)
|
|
|
137 |
elif line.startswith("action: "):
|
138 |
|
139 |
action_name, action_input = parse_action(line)
|
140 |
+
print (f'ACTION_NAME :: {action_name}')
|
141 |
+
print (f'ACTION_INPUT :: {action_input}')
|
142 |
+
|
143 |
history += "{}\n".format(line)
|
144 |
if "COMPLETE" in action_name or "COMPLETE" in action_input:
|
145 |
task = "END"
|
|
|
148 |
return action_name, action_input, history, task
|
149 |
else:
|
150 |
history += "{}\n".format(line)
|
|
|
151 |
#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)
|
152 |
|
153 |
#return action_name, action_input, history, task
|
|
|
156 |
|
157 |
|
158 |
def call_set_task(purpose, task, history, directory, action_input):
|
|
|
159 |
task = run_gpt(
|
160 |
TASK_PROMPT,
|
161 |
stop_tokens=[],
|
|
|
168 |
return "MAIN", None, history, task
|
169 |
|
170 |
def end_fn(purpose, task, history, directory, action_input):
|
|
|
171 |
task = "END"
|
172 |
return "COMPLETE", "COMPLETE", history, task
|
173 |
|
|
|
180 |
}
|
181 |
|
182 |
def run_action(purpose, task, history, directory, action_name, action_input):
|
183 |
+
print(f'action_name::{action_name}')
|
184 |
try:
|
185 |
if "RESPONSE" in action_name or "COMPLETE" in action_name:
|
186 |
action_name="COMPLETE"
|
|
|
189 |
|
190 |
# compress the history when it is long
|
191 |
if len(history.split("\n")) > MAX_HISTORY:
|
192 |
+
if VERBOSE:
|
193 |
+
print("COMPRESSING HISTORY")
|
194 |
history = compress_history(purpose, task, history, directory)
|
195 |
if not action_name in NAME_TO_FUNC:
|
196 |
action_name="MAIN"
|
|
|
198 |
action_name="MAIN"
|
199 |
assert action_name in NAME_TO_FUNC
|
200 |
|
201 |
+
print("RUN: ", action_name, action_input)
|
202 |
return NAME_TO_FUNC[action_name](purpose, task, history, directory, action_input)
|
203 |
except Exception as e:
|
204 |
history += "observation: the previous command did not produce any useful output, I need to check the commands syntax, or use a different command\n"
|
205 |
+
|
206 |
return "MAIN", None, history, task
|
207 |
|
208 |
+
def run(purpose,history):
|
209 |
+
|
210 |
#print(purpose)
|
211 |
#print(hist)
|
212 |
task=None
|
|
|
219 |
action_name = "UPDATE-TASK" if task is None else "MAIN"
|
220 |
action_input = None
|
221 |
while True:
|
222 |
+
print("")
|
223 |
+
print("")
|
224 |
+
print("---")
|
225 |
+
print("purpose:", purpose)
|
226 |
+
print("task:", task)
|
227 |
+
print("---")
|
228 |
+
print(history)
|
229 |
+
print("---")
|
230 |
|
231 |
action_name, action_input, history, task = run_action(
|
232 |
purpose,
|
|
|
246 |
|
247 |
################################################
|
248 |
|
249 |
+
def format_prompt(message, history):
|
250 |
prompt = "<s>"
|
251 |
+
for user_prompt, bot_response in history:
|
|
|
252 |
prompt += f"[INST] {user_prompt} [/INST]"
|
253 |
prompt += f" {bot_response}</s> "
|
254 |
prompt += f"[INST] {message} [/INST]"
|
255 |
return prompt
|
|
|
256 |
agents =[
|
257 |
"WEB_DEV",
|
258 |
"AI_SYSTEM_PROMPT",
|
259 |
"PYTHON_CODE_DEV"
|
260 |
]
|
|
|
261 |
def generate(
|
262 |
+
prompt, history, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
|
263 |
):
|
264 |
seed = random.randint(1,1111111111111111)
|
|
|
265 |
|
266 |
+
agent=prompts.WEB_DEV
|
|
|
267 |
if agent_name == "WEB_DEV":
|
268 |
+
agent = prompts.WEB_DEV
|
269 |
+
if agent_name == "AI_SYSTEM_PROMPT":
|
270 |
+
agent = prompts.AI_SYSTEM_PROMPT
|
271 |
+
if agent_name == "PYTHON_CODE_DEV":
|
272 |
+
agent = prompts.PYTHON_CODE_DEV
|
273 |
+
system_prompt=agent
|
274 |
+
temperature = float(temperature)
|
275 |
+
if temperature < 1e-2:
|
276 |
+
temperature = 1e-2
|
277 |
top_p = float(top_p)
|
278 |
|
279 |
+
generate_kwargs = dict(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
temperature=temperature,
|
281 |
max_new_tokens=max_new_tokens,
|
282 |
top_p=top_p,
|
283 |
repetition_penalty=repetition_penalty,
|
284 |
+
do_sample=True,
|
285 |
+
seed=seed,
|
|
|
286 |
)
|
|
|
|
|
|
|
|
|
287 |
|
288 |
+
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
289 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
290 |
+
output = ""
|
291 |
+
|
292 |
+
for response in stream:
|
293 |
+
output += response.token.text
|
294 |
+
yield output
|
295 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
|
|
|
297 |
|
|
|
298 |
additional_inputs=[
|
299 |
gr.Dropdown(
|
300 |
label="Agents",
|
|
|
348 |
|
349 |
]
|
350 |
|
351 |
+
examples=[["What are the biggest news stories today?", None, None, None, None, None, ],
|
352 |
+
["When is the next full moon?", None, None, None, None, None, ],
|
353 |
+
["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, ],
|
354 |
+
["Can you write a short story about a time-traveling detective who solves historical mysteries?", None, None, None, None, None,],
|
355 |
+
["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,],
|
356 |
+
["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,],
|
357 |
+
["Can you explain how the QuickSort algorithm works and provide a Python implementation?", None, None, None, None, None,],
|
358 |
+
["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,],
|
359 |
+
]
|
360 |
+
|
361 |
+
'''
|
362 |
+
gr.ChatInterface(
|
363 |
+
fn=run,
|
364 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
365 |
+
title="Mixtral 46.7B\nMicro-Agent\nInternet Search <br> development test",
|
366 |
+
examples=examples,
|
367 |
+
concurrency_limit=20,
|
368 |
+
with gr.Blocks() as ifacea:
|
369 |
+
gr.HTML("""TEST""")
|
370 |
+
ifacea.launch()
|
371 |
+
).launch()
|
372 |
+
with gr.Blocks() as iface:
|
373 |
+
#chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
374 |
+
chatbot=gr.Chatbot()
|
375 |
+
msg = gr.Textbox()
|
376 |
+
with gr.Row():
|
377 |
+
submit_b = gr.Button()
|
378 |
+
clear = gr.ClearButton([msg, chatbot])
|
379 |
+
submit_b.click(run, [msg,chatbot],[msg,chatbot])
|
380 |
+
msg.submit(run, [msg, chatbot], [msg, chatbot])
|
381 |
+
iface.launch()
|
382 |
+
'''
|
383 |
+
gr.ChatInterface(
|
384 |
+
fn=run,
|
385 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, super-intelligence=True, layout="panel"),
|
386 |
+
title="Mixtral 46.7B\nMicro-Agent\nInternet Search <br> development test",
|
387 |
+
examples=examples,
|
388 |
+
concurrency_limit=50,
|
389 |
+
).launch(show_api=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|