Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import random
|
| 4 |
-
import
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
import uuid # Import the uuid library
|
| 7 |
-
|
| 8 |
-
from huggingface_hub import InferenceClient, cached_download, hf_hub_url
|
| 9 |
import gradio as gr
|
| 10 |
-
|
| 11 |
from safe_search import safe_search
|
| 12 |
-
from i_search import google
|
| 13 |
-
|
| 14 |
from agent import (
|
| 15 |
ACTION_PROMPT,
|
| 16 |
ADD_PROMPT,
|
|
@@ -24,274 +19,372 @@ from agent import (
|
|
| 24 |
TASK_PROMPT,
|
| 25 |
UNDERSTAND_TEST_RESULTS_PROMPT,
|
| 26 |
)
|
| 27 |
-
|
| 28 |
from utils import parse_action, parse_file_content, read_python_module_structure
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
else:
|
| 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 |
-
query = command.split("safesearch ")[1]
|
| 121 |
-
return safe_search(query)
|
| 122 |
-
elif command.startswith("read "):
|
| 123 |
-
file_path = command.split("read ")[1]
|
| 124 |
-
return parse_file_content(file_path)
|
| 125 |
-
elif command == "task":
|
| 126 |
-
return TASK_PROMPT
|
| 127 |
-
elif command == "modify":
|
| 128 |
-
return MODIFY_PROMPT
|
| 129 |
-
elif command == "log":
|
| 130 |
-
return LOG_PROMPT
|
| 131 |
-
elif command.startswith("understand test results "):
|
| 132 |
-
test_results = command.split("understand test results ")[1]
|
| 133 |
-
return self.understand_test_results(test_results)
|
| 134 |
-
elif command.startswith("compress history"):
|
| 135 |
-
return self.compress_history(self.terminal_history)
|
| 136 |
-
elif command == "help":
|
| 137 |
-
return self.get_help_message()
|
| 138 |
-
elif command == "exit":
|
| 139 |
-
exit()
|
| 140 |
else:
|
| 141 |
-
|
| 142 |
-
except Exception as e:
|
| 143 |
-
output = str(e)
|
| 144 |
-
return output or "No output\n"
|
| 145 |
-
|
| 146 |
-
def compress_history(self, history):
|
| 147 |
-
compressed_history = ""
|
| 148 |
-
lines = history.strip().split("\n")
|
| 149 |
-
for line in lines:
|
| 150 |
-
if not line.strip().startswith("#"):
|
| 151 |
-
compressed_history += line + "\n"
|
| 152 |
-
return compressed_history
|
| 153 |
-
|
| 154 |
-
def understand_test_results(self, test_results):
|
| 155 |
-
return UNDERSTAND_TEST_RESULTS_PROMPT
|
| 156 |
-
|
| 157 |
-
def get_help_message(self):
|
| 158 |
-
return """
|
| 159 |
-
Available commands:
|
| 160 |
-
- add [component_type]: Add a component to the app canvas
|
| 161 |
-
- search [query]: Perform a Google search
|
| 162 |
-
- i search [query]: Perform an intelligent search
|
| 163 |
-
- safe search [query]: Perform a safe search
|
| 164 |
-
- read [file_path]: Read and parse the content of a Python module
|
| 165 |
-
- task: Prompt for a task to perform
|
| 166 |
-
- modify: Prompt to modify a component property
|
| 167 |
-
- log: Prompt to log a response
|
| 168 |
-
- understand test results [test_results]: Understand test results
|
| 169 |
-
- compress history: Compress the terminal history by removing comments
|
| 170 |
-
- help: Show this help message
|
| 171 |
-
- exit: Exit the program
|
| 172 |
-
"""
|
| 173 |
-
|
| 174 |
-
def process_input(self, input_text):
|
| 175 |
-
if input_text.strip().startswith("/"):
|
| 176 |
-
command = input_text.strip().lstrip("/")
|
| 177 |
-
output = self.run_terminal_command(command)
|
| 178 |
-
self.terminal_history += f"{input_text}\n{output}\n"
|
| 179 |
-
return output, ""
|
| 180 |
else:
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
component_id, action, property_name, property_value = parse_action(response)
|
| 184 |
-
if component_id:
|
| 185 |
-
component = next((comp for comp in self.app_state["components"] if comp["id"] == component_id), None)
|
| 186 |
-
if component:
|
| 187 |
-
if action == "update":
|
| 188 |
-
component["properties"][property_name] = property_value
|
| 189 |
-
return self.update_app_canvas(), f"System: Updated property '{property_name}' of component with ID {component_id}\n"
|
| 190 |
-
elif action == "remove":
|
| 191 |
-
self.app_state["components"].remove(component)
|
| 192 |
-
return self.update_app_canvas(), f"System: Removed component with ID {component_id}\n"
|
| 193 |
-
else:
|
| 194 |
-
return "", f"Error: Invalid action: {action}\n"
|
| 195 |
-
else:
|
| 196 |
-
return "", f"Error: Component with ID {component_id} not found\n"
|
| 197 |
-
else:
|
| 198 |
-
return "", f"Error: Failed to parse action from NLP response\n"
|
| 199 |
-
|
| 200 |
-
def build_app(self):
|
| 201 |
-
with gr.Blocks() as demo:
|
| 202 |
-
with gr.Tab("App Canvas"):
|
| 203 |
-
app_canvas = gr.HTML(self.update_app_canvas())
|
| 204 |
-
with gr.Tab("Terminal"):
|
| 205 |
-
gr.Markdown("## Terminal")
|
| 206 |
-
terminal_input = gr.Textbox(label="Input")
|
| 207 |
-
terminal_output = gr.Textbox(label="Output")
|
| 208 |
-
run_terminal_command_button = gr.Button("Run Command")
|
| 209 |
-
run_terminal_command_button.click(
|
| 210 |
-
self.run_terminal_command,
|
| 211 |
-
inputs=[terminal_input],
|
| 212 |
-
outputs=[terminal_output]
|
| 213 |
-
)
|
| 214 |
-
with gr.Tab("NLP Models"):
|
| 215 |
-
gr.Markdown("## Available NLP Models")
|
| 216 |
-
model_index = gr.Slider(label="Model Index", minimum=0, maximum=len(self.nlp_model_names)-1, step=1)
|
| 217 |
-
input_text = gr.Textbox(label="Input Text")
|
| 218 |
-
get_nlp_response_button = gr.Button("Get NLP Response")
|
| 219 |
-
get_nlp_response_button.click(
|
| 220 |
-
self.get_nlp_response,
|
| 221 |
-
inputs=[input_text, model_index],
|
| 222 |
-
outputs="text"
|
| 223 |
-
)
|
| 224 |
-
# Add components to the app canvas dynamically
|
| 225 |
-
for component in self.app_state["components"]:
|
| 226 |
-
component_type = component["type"]
|
| 227 |
-
properties = component["properties"]
|
| 228 |
-
|
| 229 |
-
if component_type == "Button":
|
| 230 |
-
gr.Button(value=properties["label"], variant="primary")
|
| 231 |
-
elif component_type == "Text Input":
|
| 232 |
-
gr.Textbox(label=properties["placeholder"])
|
| 233 |
-
elif component_type == "Image":
|
| 234 |
-
gr.Image(label=properties["alt"])
|
| 235 |
-
elif component_type == "Dropdown":
|
| 236 |
-
gr.Dropdown(choices=properties["choices"], label="Dropdown")
|
| 237 |
-
|
| 238 |
-
return demo
|
| 239 |
-
|
| 240 |
-
def run(self):
|
| 241 |
-
self._print_welcome_message()
|
| 242 |
-
|
| 243 |
-
while True:
|
| 244 |
-
try:
|
| 245 |
-
input_text = self._get_user_input()
|
| 246 |
-
if input_text.lower() == 'exit':
|
| 247 |
-
break
|
| 248 |
-
elif input_text.lower() == 'launch':
|
| 249 |
-
self.launch_app()
|
| 250 |
-
continue
|
| 251 |
-
|
| 252 |
-
output, system_message = self.process_input(input_text)
|
| 253 |
-
self._display_output(output, system_message)
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
except Exception as e:
|
| 287 |
-
print(f"Error launching app: {str(e)}")
|
| 288 |
-
|
| 289 |
-
def main():
|
| 290 |
try:
|
| 291 |
-
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
except Exception as e:
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
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 (
|
| 10 |
ACTION_PROMPT,
|
| 11 |
ADD_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 |
+
|
| 27 |
+
client = InferenceClient(
|
| 28 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1", cache_dir=None
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
############################################
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
VERBOSE = True
|
| 36 |
+
MAX_HISTORY = 100
|
| 37 |
+
#MODEL = "gpt-3.5-turbo" # "gpt-4"
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def format_prompt(message, history):
|
| 41 |
+
prompt = "<s>"
|
| 42 |
+
for user_prompt, bot_response in history:
|
| 43 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
| 44 |
+
prompt += f" {bot_response}</s> "
|
| 45 |
+
prompt += f"[INST] {message} [/INST]"
|
| 46 |
+
return prompt
|
| 47 |
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def run_gpt(
|
| 51 |
+
prompt_template,
|
| 52 |
+
stop_tokens,
|
| 53 |
+
max_tokens,
|
| 54 |
+
purpose,
|
| 55 |
+
**prompt_kwargs,
|
| 56 |
+
):
|
| 57 |
+
seed = random.randint(1,1111111111111111)
|
| 58 |
+
print (seed)
|
| 59 |
+
generate_kwargs = dict(
|
| 60 |
+
temperature=1.0,
|
| 61 |
+
max_new_tokens=2096,
|
| 62 |
+
top_p=0.99,
|
| 63 |
+
repetition_penalty=1.0,
|
| 64 |
+
do_sample=True,
|
| 65 |
+
seed=seed,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
content = PREFIX.format(
|
| 70 |
+
date_time_str=date_time_str,
|
| 71 |
+
purpose=purpose,
|
| 72 |
+
safe_search=safe_search,
|
| 73 |
+
) + prompt_template.format(**prompt_kwargs)
|
| 74 |
+
if VERBOSE:
|
| 75 |
+
print(LOG_PROMPT.format(content))
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
#formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
| 79 |
+
#formatted_prompt = format_prompt(f'{content}', history)
|
| 80 |
+
|
| 81 |
+
stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 82 |
+
resp = ""
|
| 83 |
+
for response in stream:
|
| 84 |
+
resp += response.token.text
|
| 85 |
+
|
| 86 |
+
if VERBOSE:
|
| 87 |
+
print(LOG_RESPONSE.format(resp))
|
| 88 |
+
return resp
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def compress_history(purpose, task, history, directory):
|
| 92 |
+
resp = run_gpt(
|
| 93 |
+
COMPRESS_HISTORY_PROMPT,
|
| 94 |
+
stop_tokens=["observation:", "task:", "action:", "thought:"],
|
| 95 |
+
max_tokens=512,
|
| 96 |
+
purpose=purpose,
|
| 97 |
+
task=task,
|
| 98 |
+
history=history,
|
| 99 |
+
)
|
| 100 |
+
history = "observation: {}\n".format(resp)
|
| 101 |
+
return history
|
| 102 |
+
|
| 103 |
+
def call_search(purpose, task, history, directory, action_input):
|
| 104 |
+
print("CALLING SEARCH")
|
| 105 |
+
try:
|
| 106 |
+
|
| 107 |
+
if "http" in action_input:
|
| 108 |
+
if "<" in action_input:
|
| 109 |
+
action_input = action_input.strip("<")
|
| 110 |
+
if ">" in action_input:
|
| 111 |
+
action_input = action_input.strip(">")
|
| 112 |
+
|
| 113 |
+
response = i_s(action_input)
|
| 114 |
+
#response = google(search_return)
|
| 115 |
+
print(response)
|
| 116 |
+
history += "observation: search result is: {}\n".format(response)
|
| 117 |
else:
|
| 118 |
+
history += "observation: I need to provide a valid URL to 'action: SEARCH action_input=https://URL'\n"
|
| 119 |
+
except Exception as e:
|
| 120 |
+
history += "observation: {}'\n".format(e)
|
| 121 |
+
return "MAIN", None, history, task
|
| 122 |
+
|
| 123 |
+
def call_main(purpose, task, history, directory, action_input):
|
| 124 |
+
resp = run_gpt(
|
| 125 |
+
ACTION_PROMPT,
|
| 126 |
+
stop_tokens=["observation:", "task:", "action:","thought:"],
|
| 127 |
+
max_tokens=2096,
|
| 128 |
+
purpose=purpose,
|
| 129 |
+
task=task,
|
| 130 |
+
history=history,
|
| 131 |
+
)
|
| 132 |
+
lines = resp.strip().strip("\n").split("\n")
|
| 133 |
+
for line in lines:
|
| 134 |
+
if line == "":
|
| 135 |
+
continue
|
| 136 |
+
if line.startswith("thought: "):
|
| 137 |
+
history += "{}\n".format(line)
|
| 138 |
+
elif line.startswith("action: "):
|
| 139 |
+
|
| 140 |
+
action_name, action_input = parse_action(line)
|
| 141 |
+
print (f'ACTION_NAME :: {action_name}')
|
| 142 |
+
print (f'ACTION_INPUT :: {action_input}')
|
| 143 |
+
|
| 144 |
+
history += "{}\n".format(line)
|
| 145 |
+
if "COMPLETE" in action_name or "COMPLETE" in action_input:
|
| 146 |
+
task = "END"
|
| 147 |
+
return action_name, action_input, history, task
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
else:
|
| 149 |
+
return action_name, action_input, history, task
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
else:
|
| 151 |
+
history += "{}\n".format(line)
|
| 152 |
+
#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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
#return action_name, action_input, history, task
|
| 155 |
+
#assert False, "unknown action: {}".format(line)
|
| 156 |
+
return "MAIN", None, history, task
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def call_set_task(purpose, task, history, directory, action_input):
|
| 160 |
+
task = run_gpt(
|
| 161 |
+
TASK_PROMPT,
|
| 162 |
+
stop_tokens=[],
|
| 163 |
+
max_tokens=64,
|
| 164 |
+
purpose=purpose,
|
| 165 |
+
task=task,
|
| 166 |
+
history=history,
|
| 167 |
+
).strip("\n")
|
| 168 |
+
history += "observation: task has been updated to: {}\n".format(task)
|
| 169 |
+
return "MAIN", None, history, task
|
| 170 |
+
|
| 171 |
+
def end_fn(purpose, task, history, directory, action_input):
|
| 172 |
+
task = "END"
|
| 173 |
+
return "COMPLETE", "COMPLETE", history, task
|
| 174 |
+
|
| 175 |
+
NAME_TO_FUNC = {
|
| 176 |
+
"MAIN": call_main,
|
| 177 |
+
"UPDATE-TASK": call_set_task,
|
| 178 |
+
"SEARCH": call_search,
|
| 179 |
+
"COMPLETE": end_fn,
|
| 180 |
+
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
def run_action(purpose, task, history, directory, action_name, action_input):
|
| 184 |
+
print(f'action_name::{action_name}')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
try:
|
| 186 |
+
if "RESPONSE" in action_name or "COMPLETE" in action_name:
|
| 187 |
+
action_name="COMPLETE"
|
| 188 |
+
task="END"
|
| 189 |
+
return action_name, "COMPLETE", history, task
|
| 190 |
+
|
| 191 |
+
# compress the history when it is long
|
| 192 |
+
if len(history.split("\n")) > MAX_HISTORY:
|
| 193 |
+
if VERBOSE:
|
| 194 |
+
print("COMPRESSING HISTORY")
|
| 195 |
+
history = compress_history(purpose, task, history, directory)
|
| 196 |
+
if not action_name in NAME_TO_FUNC:
|
| 197 |
+
action_name="MAIN"
|
| 198 |
+
if action_name == "" or action_name == None:
|
| 199 |
+
action_name="MAIN"
|
| 200 |
+
assert action_name in NAME_TO_FUNC
|
| 201 |
+
|
| 202 |
+
print("RUN: ", action_name, action_input)
|
| 203 |
+
return NAME_TO_FUNC[action_name](purpose, task, history, directory, action_input)
|
| 204 |
except Exception as e:
|
| 205 |
+
history += "observation: the previous command did not produce any useful output, I need to check the commands syntax, or use a different command\n"
|
| 206 |
+
|
| 207 |
+
return "MAIN", None, history, task
|
| 208 |
+
|
| 209 |
+
def run(purpose,history):
|
| 210 |
+
|
| 211 |
+
#print(purpose)
|
| 212 |
+
#print(hist)
|
| 213 |
+
task=None
|
| 214 |
+
directory="./"
|
| 215 |
+
if history:
|
| 216 |
+
history=str(history).strip("[]")
|
| 217 |
+
if not history:
|
| 218 |
+
history = ""
|
| 219 |
+
|
| 220 |
+
action_name = "UPDATE-TASK" if task is None else "MAIN"
|
| 221 |
+
action_input = None
|
| 222 |
+
while True:
|
| 223 |
+
print("")
|
| 224 |
+
print("")
|
| 225 |
+
print("---")
|
| 226 |
+
print("purpose:", purpose)
|
| 227 |
+
print("task:", task)
|
| 228 |
+
print("---")
|
| 229 |
+
print(history)
|
| 230 |
+
print("---")
|
| 231 |
+
|
| 232 |
+
action_name, action_input, history, task = run_action(
|
| 233 |
+
purpose,
|
| 234 |
+
task,
|
| 235 |
+
history,
|
| 236 |
+
directory,
|
| 237 |
+
action_name,
|
| 238 |
+
action_input,
|
| 239 |
+
)
|
| 240 |
+
yield (history)
|
| 241 |
+
#yield ("",[(purpose,history)])
|
| 242 |
+
if task == "END":
|
| 243 |
+
return (history)
|
| 244 |
+
#return ("", [(purpose,history)])
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
################################################
|
| 249 |
+
|
| 250 |
+
def format_prompt(message, history):
|
| 251 |
+
prompt = "<s>"
|
| 252 |
+
for user_prompt, bot_response in history:
|
| 253 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
| 254 |
+
prompt += f" {bot_response}</s> "
|
| 255 |
+
prompt += f"[INST] {message} [/INST]"
|
| 256 |
+
return prompt
|
| 257 |
+
agents =[
|
| 258 |
+
"WEB_DEV",
|
| 259 |
+
"AI_SYSTEM_PROMPT",
|
| 260 |
+
"PYTHON_CODE_DEV"
|
| 261 |
+
]
|
| 262 |
+
def generate(
|
| 263 |
+
prompt, history, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
|
| 264 |
+
):
|
| 265 |
+
seed = random.randint(1,1111111111111111)
|
| 266 |
+
|
| 267 |
+
agent=prompts.WEB_DEV
|
| 268 |
+
if agent_name == "WEB_DEV":
|
| 269 |
+
agent = prompts.WEB_DEV
|
| 270 |
+
if agent_name == "AI_SYSTEM_PROMPT":
|
| 271 |
+
agent = prompts.AI_SYSTEM_PROMPT
|
| 272 |
+
if agent_name == "PYTHON_CODE_DEV":
|
| 273 |
+
agent = prompts.PYTHON_CODE_DEV
|
| 274 |
+
system_prompt=agent
|
| 275 |
+
temperature = float(temperature)
|
| 276 |
+
if temperature < 1e-2:
|
| 277 |
+
temperature = 1e-2
|
| 278 |
+
top_p = float(top_p)
|
| 279 |
+
|
| 280 |
+
generate_kwargs = dict(
|
| 281 |
+
temperature=temperature,
|
| 282 |
+
max_new_tokens=max_new_tokens,
|
| 283 |
+
top_p=top_p,
|
| 284 |
+
repetition_penalty=repetition_penalty,
|
| 285 |
+
do_sample=True,
|
| 286 |
+
seed=seed,
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
| 290 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 291 |
+
output = ""
|
| 292 |
+
|
| 293 |
+
for response in stream:
|
| 294 |
+
output += response.token.text
|
| 295 |
+
yield output
|
| 296 |
+
return output
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
additional_inputs=[
|
| 300 |
+
gr.Dropdown(
|
| 301 |
+
label="Agents",
|
| 302 |
+
choices=[s for s in agents],
|
| 303 |
+
value=agents[0],
|
| 304 |
+
interactive=True,
|
| 305 |
+
),
|
| 306 |
+
gr.Textbox(
|
| 307 |
+
label="System Prompt",
|
| 308 |
+
max_lines=1,
|
| 309 |
+
interactive=True,
|
| 310 |
+
),
|
| 311 |
+
gr.Slider(
|
| 312 |
+
label="Temperature",
|
| 313 |
+
value=0.9,
|
| 314 |
+
minimum=0.0,
|
| 315 |
+
maximum=1.0,
|
| 316 |
+
step=0.05,
|
| 317 |
+
interactive=True,
|
| 318 |
+
info="Higher values produce more diverse outputs",
|
| 319 |
+
),
|
| 320 |
+
|
| 321 |
+
gr.Slider(
|
| 322 |
+
label="Max new tokens",
|
| 323 |
+
value=1048*10,
|
| 324 |
+
minimum=0,
|
| 325 |
+
maximum=1048*10,
|
| 326 |
+
step=64,
|
| 327 |
+
interactive=True,
|
| 328 |
+
info="The maximum numbers of new tokens",
|
| 329 |
+
),
|
| 330 |
+
gr.Slider(
|
| 331 |
+
label="Top-p (nucleus sampling)",
|
| 332 |
+
value=0.90,
|
| 333 |
+
minimum=0.0,
|
| 334 |
+
maximum=1,
|
| 335 |
+
step=0.05,
|
| 336 |
+
interactive=True,
|
| 337 |
+
info="Higher values sample more low-probability tokens",
|
| 338 |
+
),
|
| 339 |
+
gr.Slider(
|
| 340 |
+
label="Repetition penalty",
|
| 341 |
+
value=1.2,
|
| 342 |
+
minimum=1.0,
|
| 343 |
+
maximum=2.0,
|
| 344 |
+
step=0.05,
|
| 345 |
+
interactive=True,
|
| 346 |
+
info="Penalize repeated tokens",
|
| 347 |
+
),
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
]
|
| 351 |
+
|
| 352 |
+
examples=[["What are the biggest news stories today?", None, None, None, None, None, ],
|
| 353 |
+
["When is the next full moon?", None, None, None, None, None, ],
|
| 354 |
+
["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, ],
|
| 355 |
+
["Can you write a short story about a time-traveling detective who solves historical mysteries?", None, None, None, None, None,],
|
| 356 |
+
["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,],
|
| 357 |
+
["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,],
|
| 358 |
+
["Can you explain how the QuickSort algorithm works and provide a Python implementation?", None, None, None, None, None,],
|
| 359 |
+
["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,],
|
| 360 |
+
]
|
| 361 |
|
| 362 |
+
'''
|
| 363 |
+
gr.ChatInterface(
|
| 364 |
+
fn=run,
|
| 365 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
| 366 |
+
title="Mixtral 46.7B\nMicro-Agent\nInternet Search <br> development test",
|
| 367 |
+
examples=examples,
|
| 368 |
+
concurrency_limit=20,
|
| 369 |
+
with gr.Blocks() as ifacea:
|
| 370 |
+
gr.HTML("""TEST""")
|
| 371 |
+
ifacea.launch()
|
| 372 |
+
).launch()
|
| 373 |
+
with gr.Blocks() as iface:
|
| 374 |
+
#chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
| 375 |
+
chatbot=gr.Chatbot()
|
| 376 |
+
msg = gr.Textbox()
|
| 377 |
+
with gr.Row():
|
| 378 |
+
submit_b = gr.Button()
|
| 379 |
+
clear = gr.ClearButton([msg, chatbot])
|
| 380 |
+
submit_b.click(run, [msg,chatbot],[msg,chatbot])
|
| 381 |
+
msg.submit(run, [msg, chatbot], [msg, chatbot])
|
| 382 |
+
iface.launch()
|
| 383 |
+
'''
|
| 384 |
+
gr.ChatInterface(
|
| 385 |
+
fn=run,
|
| 386 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, super-intelligence=True, layout="panel"),
|
| 387 |
+
title="Mixtral 46.7B\nMicro-Agent\nInternet Search <br> development test",
|
| 388 |
+
examples=examples,
|
| 389 |
+
concurrency_limit=50,
|
| 390 |
+
).launch(show_api=True)
|