Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,8 @@ 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 |
now = datetime.now()
|
11 |
date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
|
12 |
|
@@ -14,8 +16,14 @@ client = InferenceClient(
|
|
14 |
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
15 |
)
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
18 |
|
|
|
19 |
|
20 |
VERBOSE = True
|
21 |
MAX_HISTORY = 100
|
@@ -72,16 +80,15 @@ What do you want to know about the test results?
|
|
72 |
thought:
|
73 |
"""
|
74 |
|
75 |
-
def format_prompt(message, history):
|
76 |
prompt = "<s>"
|
77 |
-
|
|
|
78 |
prompt += f"[INST] {user_prompt} [/INST]"
|
79 |
prompt += f" {bot_response}</s> "
|
80 |
prompt += f"[INST] {message} [/INST]"
|
81 |
return prompt
|
82 |
|
83 |
-
|
84 |
-
|
85 |
def run_gpt(
|
86 |
prompt_template,
|
87 |
stop_tokens,
|
@@ -90,7 +97,7 @@ def run_gpt(
|
|
90 |
**prompt_kwargs,
|
91 |
):
|
92 |
seed = random.randint(1,1111111111111111)
|
93 |
-
|
94 |
generate_kwargs = dict(
|
95 |
temperature=1.0,
|
96 |
max_new_tokens=2096,
|
@@ -100,26 +107,21 @@ def run_gpt(
|
|
100 |
seed=seed,
|
101 |
)
|
102 |
|
103 |
-
|
104 |
content = PREFIX.format(
|
105 |
date_time_str=date_time_str,
|
106 |
purpose=purpose,
|
107 |
safe_search=safe_search,
|
108 |
) + prompt_template.format(**prompt_kwargs)
|
109 |
if VERBOSE:
|
110 |
-
|
111 |
-
|
112 |
|
113 |
-
#formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
114 |
-
#formatted_prompt = format_prompt(f'{content}', history)
|
115 |
-
|
116 |
stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
117 |
resp = ""
|
118 |
for response in stream:
|
119 |
resp += response.token.text
|
120 |
|
121 |
if VERBOSE:
|
122 |
-
|
123 |
return resp
|
124 |
|
125 |
|
@@ -136,7 +138,7 @@ def compress_history(purpose, task, history, directory):
|
|
136 |
return history
|
137 |
|
138 |
def call_search(purpose, task, history, directory, action_input):
|
139 |
-
|
140 |
try:
|
141 |
|
142 |
if "http" in action_input:
|
@@ -147,7 +149,7 @@ def call_search(purpose, task, history, directory, action_input):
|
|
147 |
|
148 |
response = i_s(action_input)
|
149 |
#response = google(search_return)
|
150 |
-
|
151 |
history += "observation: search result is: {}\n".format(response)
|
152 |
else:
|
153 |
history += "observation: I need to provide a valid URL to 'action: SEARCH action_input=https://URL'\n"
|
@@ -156,6 +158,7 @@ def call_search(purpose, task, history, directory, action_input):
|
|
156 |
return "MAIN", None, history, task
|
157 |
|
158 |
def call_main(purpose, task, history, directory, action_input):
|
|
|
159 |
resp = run_gpt(
|
160 |
ACTION_PROMPT,
|
161 |
stop_tokens=["observation:", "task:", "action:","thought:"],
|
@@ -170,12 +173,11 @@ def call_main(purpose, task, history, directory, action_input):
|
|
170 |
continue
|
171 |
if line.startswith("thought: "):
|
172 |
history += "{}\n".format(line)
|
|
|
173 |
elif line.startswith("action: "):
|
174 |
|
175 |
action_name, action_input = parse_action(line)
|
176 |
-
|
177 |
-
print (f'ACTION_INPUT :: {action_input}')
|
178 |
-
|
179 |
history += "{}\n".format(line)
|
180 |
if "COMPLETE" in action_name or "COMPLETE" in action_input:
|
181 |
task = "END"
|
@@ -184,6 +186,7 @@ def call_main(purpose, task, history, directory, action_input):
|
|
184 |
return action_name, action_input, history, task
|
185 |
else:
|
186 |
history += "{}\n".format(line)
|
|
|
187 |
#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)
|
188 |
|
189 |
#return action_name, action_input, history, task
|
@@ -192,6 +195,7 @@ def call_main(purpose, task, history, directory, action_input):
|
|
192 |
|
193 |
|
194 |
def call_set_task(purpose, task, history, directory, action_input):
|
|
|
195 |
task = run_gpt(
|
196 |
TASK_PROMPT,
|
197 |
stop_tokens=[],
|
@@ -204,6 +208,7 @@ def call_set_task(purpose, task, history, directory, action_input):
|
|
204 |
return "MAIN", None, history, task
|
205 |
|
206 |
def end_fn(purpose, task, history, directory, action_input):
|
|
|
207 |
task = "END"
|
208 |
return "COMPLETE", "COMPLETE", history, task
|
209 |
|
@@ -216,7 +221,7 @@ NAME_TO_FUNC = {
|
|
216 |
}
|
217 |
|
218 |
def run_action(purpose, task, history, directory, action_name, action_input):
|
219 |
-
|
220 |
try:
|
221 |
if "RESPONSE" in action_name or "COMPLETE" in action_name:
|
222 |
action_name="COMPLETE"
|
@@ -225,8 +230,7 @@ def run_action(purpose, task, history, directory, action_name, action_input):
|
|
225 |
|
226 |
# compress the history when it is long
|
227 |
if len(history.split("\n")) > MAX_HISTORY:
|
228 |
-
|
229 |
-
print("COMPRESSING HISTORY")
|
230 |
history = compress_history(purpose, task, history, directory)
|
231 |
if not action_name in NAME_TO_FUNC:
|
232 |
action_name="MAIN"
|
@@ -234,11 +238,11 @@ def run_action(purpose, task, history, directory, action_name, action_input):
|
|
234 |
action_name="MAIN"
|
235 |
assert action_name in NAME_TO_FUNC
|
236 |
|
237 |
-
|
238 |
return NAME_TO_FUNC[action_name](purpose, task, history, directory, action_input)
|
239 |
except Exception as e:
|
240 |
history += "observation: the previous command did not produce any useful output, I need to check the commands syntax, or use a different command\n"
|
241 |
-
|
242 |
return "MAIN", None, history, task
|
243 |
|
244 |
def run(purpose,history):
|
@@ -255,14 +259,12 @@ def run(purpose,history):
|
|
255 |
action_name = "UPDATE-TASK" if task is None else "MAIN"
|
256 |
action_input = None
|
257 |
while True:
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
print(history)
|
265 |
-
print("---")
|
266 |
|
267 |
action_name, action_input, history, task = run_action(
|
268 |
purpose,
|
@@ -282,9 +284,10 @@ def run(purpose,history):
|
|
282 |
|
283 |
################################################
|
284 |
|
285 |
-
def format_prompt(message, history):
|
286 |
prompt = "<s>"
|
287 |
-
|
|
|
288 |
prompt += f"[INST] {user_prompt} [/INST]"
|
289 |
prompt += f" {bot_response}</s> "
|
290 |
prompt += f"[INST] {message} [/INST]"
|
@@ -322,6 +325,7 @@ def generate(
|
|
322 |
)
|
323 |
|
324 |
formatted_prompt = format_prompt(prompt, history, max_history_turns=5) # Truncated history
|
|
|
325 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
326 |
output = ""
|
327 |
|
@@ -331,7 +335,6 @@ def generate(
|
|
331 |
return output
|
332 |
|
333 |
|
334 |
-
|
335 |
additional_inputs=[
|
336 |
gr.Dropdown(
|
337 |
label="Agents",
|
|
|
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 |
+
|
12 |
now = datetime.now()
|
13 |
date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
|
14 |
|
|
|
16 |
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
17 |
)
|
18 |
|
19 |
+
# --- Set up logging ---
|
20 |
+
logging.basicConfig(
|
21 |
+
filename="app.log", # Name of the log file
|
22 |
+
level=logging.INFO, # Set the logging level (INFO, DEBUG, etc.)
|
23 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
24 |
+
)
|
25 |
|
26 |
+
############################################
|
27 |
|
28 |
VERBOSE = True
|
29 |
MAX_HISTORY = 100
|
|
|
80 |
thought:
|
81 |
"""
|
82 |
|
83 |
+
def format_prompt(message, history, max_history_turns=5):
|
84 |
prompt = "<s>"
|
85 |
+
# Keep only the last 'max_history_turns' turns
|
86 |
+
for user_prompt, bot_response in history[-max_history_turns:]:
|
87 |
prompt += f"[INST] {user_prompt} [/INST]"
|
88 |
prompt += f" {bot_response}</s> "
|
89 |
prompt += f"[INST] {message} [/INST]"
|
90 |
return prompt
|
91 |
|
|
|
|
|
92 |
def run_gpt(
|
93 |
prompt_template,
|
94 |
stop_tokens,
|
|
|
97 |
**prompt_kwargs,
|
98 |
):
|
99 |
seed = random.randint(1,1111111111111111)
|
100 |
+
logging.info(f"Seed: {seed}") # Log the seed
|
101 |
generate_kwargs = dict(
|
102 |
temperature=1.0,
|
103 |
max_new_tokens=2096,
|
|
|
107 |
seed=seed,
|
108 |
)
|
109 |
|
|
|
110 |
content = PREFIX.format(
|
111 |
date_time_str=date_time_str,
|
112 |
purpose=purpose,
|
113 |
safe_search=safe_search,
|
114 |
) + prompt_template.format(**prompt_kwargs)
|
115 |
if VERBOSE:
|
116 |
+
logging.info(LOG_PROMPT.format(content)) # Log the prompt
|
|
|
117 |
|
|
|
|
|
|
|
118 |
stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
119 |
resp = ""
|
120 |
for response in stream:
|
121 |
resp += response.token.text
|
122 |
|
123 |
if VERBOSE:
|
124 |
+
logging.info(LOG_RESPONSE.format(resp)) # Log the response
|
125 |
return resp
|
126 |
|
127 |
|
|
|
138 |
return history
|
139 |
|
140 |
def call_search(purpose, task, history, directory, action_input):
|
141 |
+
logging.info(f"CALLING SEARCH: {action_input}")
|
142 |
try:
|
143 |
|
144 |
if "http" in action_input:
|
|
|
149 |
|
150 |
response = i_s(action_input)
|
151 |
#response = google(search_return)
|
152 |
+
logging.info(f"Search Result: {response}")
|
153 |
history += "observation: search result is: {}\n".format(response)
|
154 |
else:
|
155 |
history += "observation: I need to provide a valid URL to 'action: SEARCH action_input=https://URL'\n"
|
|
|
158 |
return "MAIN", None, history, task
|
159 |
|
160 |
def call_main(purpose, task, history, directory, action_input):
|
161 |
+
logging.info(f"CALLING MAIN: {action_input}")
|
162 |
resp = run_gpt(
|
163 |
ACTION_PROMPT,
|
164 |
stop_tokens=["observation:", "task:", "action:","thought:"],
|
|
|
173 |
continue
|
174 |
if line.startswith("thought: "):
|
175 |
history += "{}\n".format(line)
|
176 |
+
logging.info(f"Thought: {line}")
|
177 |
elif line.startswith("action: "):
|
178 |
|
179 |
action_name, action_input = parse_action(line)
|
180 |
+
logging.info(f"Action: {action_name} - {action_input}")
|
|
|
|
|
181 |
history += "{}\n".format(line)
|
182 |
if "COMPLETE" in action_name or "COMPLETE" in action_input:
|
183 |
task = "END"
|
|
|
186 |
return action_name, action_input, history, task
|
187 |
else:
|
188 |
history += "{}\n".format(line)
|
189 |
+
logging.info(f"Other Output: {line}")
|
190 |
#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)
|
191 |
|
192 |
#return action_name, action_input, history, task
|
|
|
195 |
|
196 |
|
197 |
def call_set_task(purpose, task, history, directory, action_input):
|
198 |
+
logging.info(f"CALLING SET_TASK: {action_input}")
|
199 |
task = run_gpt(
|
200 |
TASK_PROMPT,
|
201 |
stop_tokens=[],
|
|
|
208 |
return "MAIN", None, history, task
|
209 |
|
210 |
def end_fn(purpose, task, history, directory, action_input):
|
211 |
+
logging.info(f"CALLING END_FN: {action_input}")
|
212 |
task = "END"
|
213 |
return "COMPLETE", "COMPLETE", history, task
|
214 |
|
|
|
221 |
}
|
222 |
|
223 |
def run_action(purpose, task, history, directory, action_name, action_input):
|
224 |
+
logging.info(f"RUNNING ACTION: {action_name} - {action_input}")
|
225 |
try:
|
226 |
if "RESPONSE" in action_name or "COMPLETE" in action_name:
|
227 |
action_name="COMPLETE"
|
|
|
230 |
|
231 |
# compress the history when it is long
|
232 |
if len(history.split("\n")) > MAX_HISTORY:
|
233 |
+
logging.info("COMPRESSING HISTORY")
|
|
|
234 |
history = compress_history(purpose, task, history, directory)
|
235 |
if not action_name in NAME_TO_FUNC:
|
236 |
action_name="MAIN"
|
|
|
238 |
action_name="MAIN"
|
239 |
assert action_name in NAME_TO_FUNC
|
240 |
|
241 |
+
logging.info(f"RUN: {action_name} - {action_input}")
|
242 |
return NAME_TO_FUNC[action_name](purpose, task, history, directory, action_input)
|
243 |
except Exception as e:
|
244 |
history += "observation: the previous command did not produce any useful output, I need to check the commands syntax, or use a different command\n"
|
245 |
+
logging.error(f"Error in run_action: {e}")
|
246 |
return "MAIN", None, history, task
|
247 |
|
248 |
def run(purpose,history):
|
|
|
259 |
action_name = "UPDATE-TASK" if task is None else "MAIN"
|
260 |
action_input = None
|
261 |
while True:
|
262 |
+
logging.info(f"---")
|
263 |
+
logging.info(f"Purpose: {purpose}")
|
264 |
+
logging.info(f"Task: {task}")
|
265 |
+
logging.info(f"---")
|
266 |
+
logging.info(f"History: {history}")
|
267 |
+
logging.info(f"---")
|
|
|
|
|
268 |
|
269 |
action_name, action_input, history, task = run_action(
|
270 |
purpose,
|
|
|
284 |
|
285 |
################################################
|
286 |
|
287 |
+
def format_prompt(message, history, max_history_turns=5):
|
288 |
prompt = "<s>"
|
289 |
+
# Keep only the last 'max_history_turns' turns
|
290 |
+
for user_prompt, bot_response in history[-max_history_turns:]:
|
291 |
prompt += f"[INST] {user_prompt} [/INST]"
|
292 |
prompt += f" {bot_response}</s> "
|
293 |
prompt += f"[INST] {message} [/INST]"
|
|
|
325 |
)
|
326 |
|
327 |
formatted_prompt = format_prompt(prompt, history, max_history_turns=5) # Truncated history
|
328 |
+
logging.info(f"Formatted Prompt: {formatted_prompt}")
|
329 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
330 |
output = ""
|
331 |
|
|
|
335 |
return output
|
336 |
|
337 |
|
|
|
338 |
additional_inputs=[
|
339 |
gr.Dropdown(
|
340 |
label="Agents",
|