-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_cli.py
744 lines (663 loc) Β· 37.4 KB
/
main_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
import datetime
import json
import os
import re
import subprocess
import sys
import time
from typing import Any, Dict, Set, Generator
from urllib.parse import urlparse
import yt_dlp
import pkg_resources
import requests
import urllib3
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from openai import OpenAI
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium_stealth import stealth
from tqdm import tqdm
import colorama
colorama.init(autoreset=True)
urllib3.disable_warnings()
# Load .env file
load_dotenv()
# Get values from .env
base_model = os.getenv("BASE_MODEL")
base_api = os.getenv("BASE_API")
base_url = os.getenv("BASE_URL")
temperature = float(os.getenv("TEMPERATURE", 0.3))
top_p = float(os.getenv("TOP_P", 1))
frequency_penalty = float(os.getenv("FREQUENCY_PENALTY", 0))
presence_penalty = float(os.getenv("PRESENCE_PENALTY", 0))
max_tokens = int(os.getenv("MAX_TOKENS", 2048))
max_context = int(os.getenv("MAX_CONTEXT", 32000))
def get_installed_packages() -> Set[str]:
return {pkg.key for pkg in pkg_resources.working_set}
def install_missing_packages(required_packages: Set[str]) -> Dict[str, bool]:
installed_packages = get_installed_packages()
missing_packages = required_packages - installed_packages
results = {}
for package in missing_packages:
try:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package])
results[package] = True
except subprocess.CalledProcessError:
results[package] = False
return results
def extract_imports(code: str) -> Set[str]:
import_pattern = re.compile(r'^(?:from\s+(\S+?)(?:.\S+)?\s+import\s+.*$|import\s+(\S+))', re.MULTILINE)
matches = import_pattern.finditer(code)
packages = set()
for match in matches:
package = match.group(1) or match.group(2)
base_package = package.split('.')[0]
if base_package not in sys.stdlib_module_names:
packages.add(base_package)
return packages
# Global variables
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Cache-Control": "max-age=0",
}
RATE_LIMIT = 1
TIMEOUT = 10
MAX_RETRIES = 3
last_request_time = {}
def respect_rate_limit(url):
domain = urlparse(url).netloc
current_time = time.time()
if domain in last_request_time:
time_since_last_request = current_time - last_request_time[domain]
if time_since_last_request < RATE_LIMIT:
time.sleep(RATE_LIMIT - time_since_last_request)
last_request_time[domain] = current_time
def scrape_website(url: str) -> dict[str, dict[str, Any] | str] | str:
try:
# Respect rate limits
respect_rate_limit(url)
# Fetch and parse response
response = requests.get(url, verify=False, headers=HEADERS)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
# Remove unnecessary elements
for tag in soup(['nav', 'footer', 'aside', 'script', 'style']):
tag.decompose()
# Extract and clean text
text = soup.get_text()
text = re.sub(r'\s+', ' ', text) # Collapse multiple spaces/newlines into one
text = re.sub(r'[^\w\s.,?!]', '', text) # Remove special characters
text = " ".join(text.split()[:int(max_context / 8)]) # Truncate to fit context size
# Extract and clean links
links = {
re.sub(r'\s+', ' ', a.text.strip()): a['href']
for a in soup.find_all('a', href=True)[:int(max_context / 1000)]
if a.text.strip()
}
return {'text': text, 'links': links}
except Exception as e:
print(f"Scraping failed with error: {e}")
return f"Scraping failed with error: {e}"
class Agent:
def __init__(self, base_url=None, api_key=None):
# Initialize client parameters with defaults
client_params = {
'base_url': base_url,
'api_key': api_key or os.environ.get('OPENAI_API_KEY')
}
# Validate API key
if not client_params['api_key']:
raise ValueError(
"API key not provided. Either pass it as 'api_key' or set it in the 'OPENAI_API_KEY' environment variable."
)
# Initialize OpenAI client
try:
self.client = OpenAI(**client_params)
except Exception as e:
raise RuntimeError(f"Failed to initialize OpenAI client: {e}")
# Retry settings
self.max_retries = 3
self.retry_delay = 1
# Additional properties
self.current_request = None
self.tasks = {}
self.global_history = []
def stream_response(self, task: str, previous_actions: list) -> Generator[str, None, None]:
if previous_actions is None:
messages = [
{"role": "system", "content": """
You are an AI assistant designed to help with various tasks, You will be given a task to solve,
and you will need to follow the instructions provided to solve it, you never say anything other then
using the actions provided in the instructions, imagine you are a robot that can only perform the actions
Bad Example:
User: whats the weather tomorrow in new york?
AI: I will have to perform 10 searches, {CONCLUDE} all done {SCRAPE} http://nework.com
{SEARCH} weather in new york.
Good Example:
User: whats the weather tomorrow in new york?
AI: {SEARCH} weather in new york.
The following are the actions you can perform:
- Thoughts - Response with {THOUGHTS} - these are intermediate steps that you double check before
performing an action, use a tree model to decide which action is best and only on the next step perform
the action, do not perform it in the same reply!.
- Web search: Respond with {SEARCH} followed by your query, try to create a logical search query
that will yield the most effective results, don't use long queries, if you need to look for multiple
items, example, nvidia stock, intel stock, just do one search at a time for each,
Once you have the search results, you MUST select between 3-8 results to scrape.
, never do more than 8.
- File web search: Respond with {SEARCH} followed by the file type and a colon and the query
example of a websearch of files: filetype:pdf "jane eyre".
- Execute Python code: Respond with {EXECUTE_PYTHON} followed by the code, never use anything with APIs
that require signup.
- Execute Bash commands: Respond with {EXECUTE_BASH} followed by the commands.
- Scrape a website: Respond with {SCRAPE} followed by the URL.
- Download files: Respond with {DOWNLOAD} followed by the URL - works for webpages with videos as well.
only download files, never webpages.
- Scrape python files in a project: Respond with {SCRAPE_PYTHON} followed by the folder path.
example: {DOWNLOAD} https://example.com/file.txt
Ensure accuracy at each step before proceeding, use {THOUGHTS} and try to decide what to do next.
Always respond with a single, actionable step from the list I provided.
"""},
{"role": "user",
"content": f"Task: {task}\n\nPrevious actions taken: {json.dumps(previous_actions)}\n\nToday's date: "
f"{datetime.datetime.now()} Pleas input next action"}
]
else:
messages = [
{"role": "system", "content": """Solve the following task efficiently and clearly:
You are an AI assistant designed to help with various tasks, You will be given a task to solve,
and you will need to follow the instructions provided to solve it, you never say anything other then
using the actions provided in the instructions, imagine you are a robot that can only perform the actions
Bad Example:
User: whats the weather tomorrow in new york?
AI: I will have to perform 10 searches, {CONCLUDE} all done {SCRAPE} http://nework.com
{SEARCH} weather in new york.
Good Example:
User: whats the weather tomorrow in new york?
AI: {SEARCH} weather in new york.
The following are the actions you can perform:
- Thoughts - Response with {THOUGHTS} - these are intermediate steps that you double check before
performing an action, use a tree model to decide which action is best and only on the next step perform
the action, do not perform it in the same reply!.
- Web search: Respond with {SEARCH} followed by your query, try to create a logical search query
that will yield the most effective results, don't use long queries, if you need to look for multiple
items, example, nvidia stock, intel stock, just do one search at a time for each.
Once you have the search results, you MUST select between 3-8 results to scrape.
, never do more than 8.
- File web search: Respond with {SEARCH} followed by the file type and a colon and the query
example of a websearch of files: filetype:pdf "jane eyre".
- Execute Python code: Respond with {EXECUTE_PYTHON} followed by the code, never use anything with APIs
that require signup.
- Execute Bash commands: Respond with {EXECUTE_BASH} followed by the commands.
- Scrape a website: Respond with {SCRAPE} followed by the URL.
- Download files: Respond with {DOWNLOAD} followed by the URL - works for webpages with videos as well.
only download files, never webpages.
- Scrape python files in a project: Respond with {SCRAPE_PYTHON} followed by the folder path.
example: {DOWNLOAD} https://example.com/file.txt
- Provide conclusions: Respond with {CONCLUDE} followed by your summary, do this ONLY if ALL of your
tasks are done and you are ready to provide the summary and end the session, never do it in the same
step as another action, it should be its own action, never do it in the first step.
If the subject is scientific related then The conclusion should be in a format similar to this if its concluding research or information gathering:
Abstract β summary of the research objectives, methods, findings, and conclusions.
Introduction β Provide background, state the research problem, and outline objectives.
Literature Review β Summarize relevant studies and identify gaps.
Methodology β Describe the research design, sample size, and methods.
Results β Present findings (include tables/graphs if necessary).
Discussion β Interpret results, compare with existing studies, and discuss limitations.
Conclusion β Summarize findings and suggest future research.
References β List citations used.
Otherwise if its none scientific, such as a simple question on weather tomorrow, just do a detailed
summary.
Always respond with a single, Always respond with a single, actionable step from the list I
provided, don't add an explanation beyond the action unless its the conclusion.
"""},
{"role": "user",
"content": f"Task: {task}\n\nPrevious actions taken: {json.dumps(previous_actions)}\n\nFor context Today's date is "
f"{datetime.datetime.now()} What should be the next action?"}
]
try:
response = self.client.chat.completions.create(
model=base_model,
messages=messages,
max_tokens=max_tokens,
stream=True
)
full_response = ""
for chunk in response:
if chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
full_response += content
print(content, end='', flush=True) # Log streaming response
yield content
if task in self.tasks:
self.tasks[task]['streamed_response'] = full_response
except Exception as e:
print(" ")
def search_web(self, query):
results = []
options = Options()
options.add_argument("--headless")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)
stealth(driver,
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
driver.get('https://www.google.com')
search_box = driver.find_element(By.NAME, 'q')
search_box.send_keys(query)
search_box.send_keys(Keys.RETURN)
time.sleep(2)
for page in range(2):
for result in driver.find_elements(By.CSS_SELECTOR, 'div.g'):
try:
results.append({
'title': result.find_element(By.CSS_SELECTOR, "h3").text,
'link': result.find_element(By.CSS_SELECTOR, "a").get_attribute("href"),
'summary': result.find_element(By.CSS_SELECTOR, ".VwiC3b").text
})
except:
pass
if page == 0:
try:
driver.find_element(By.CSS_SELECTOR, f'[aria-label="Page {page + 2}"]').click()
except Exception:
continue
time.sleep(2)
return results[:int(max_context / 500)]
def _scrape_python_files(self, path):
"""
Collects the names and contents of all Python files in a folder and its subfolders,
or from a single Python file if a file path is provided.
Args:
path (str): The file or folder path to scan.
Returns:
list[dict]: A list of dictionaries, each containing 'filename' and 'content' keys.
"""
python_files_data = []
if os.path.isfile(path): # Check if the input is a file
if path.endswith('.py'): # Ensure it's a Python file
try:
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
python_files_data.append({'filename': path, 'content': content})
except Exception as e:
print(f"{colorama.Fore.RED}Error reading file {path}: {e}")
elif os.path.isdir(path): # Check if the input is a folder
for root, _, files in os.walk(path):
for file in files:
if file.endswith('.py'):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
python_files_data.append({'filename': file_path, 'content': content})
except Exception as e:
print(f"{colorama.Fore.RED}Error reading file {file_path}: {e}")
else:
print(f"{colorama.Fore.RED}Invalid path: {path} is neither a file nor a directory.")
return python_files_data
def _download_file(self, url: str, output_path=None) -> str:
def has_video_content(url):
try:
# Get the webpage content
response = requests.get(url, verify=False, headers=HEADERS)
soup = BeautifulSoup(response.text, 'html.parser')
# Check for common video elements
video_elements = (
soup.find_all('video') or
soup.find_all('iframe', src=lambda x: x and ('youtube.com' in x or 'vimeo.com' in x)) or
'youtube.com' in url or
'vimeo.com' in url or
any(vid_site in url for vid_site in [
'dailymotion.com', 'twitter.com', 'tiktok.com',
'facebook.com', 'instagram.com', 'reddit.com'
])
)
return bool(video_elements)
except:
return False
# If video content is detected, use yt-dlp
if has_video_content(url):
try:
output_path = output_path or os.getcwd()
if not os.path.isdir(output_path):
output_path = os.path.dirname(output_path)
ydl_opts = {
'format': 'bestvideo+bestaudio/best', # Download best quality
'outtmpl': os.path.join(output_path, '%(title)s.%(ext)s'),
'quiet': True,
'no_warnings': True,
'progress_hooks': [
lambda d: print(
f"\rDownloading... {(d.get('downloaded_bytes', 0) / d.get('total_bytes', 1) * 100):.1f}%"
if d['status'] == 'downloading' and d.get('total_bytes')
else f"\rDownloading... {d.get('downloaded_bytes', 0) / 1024 / 1024:.1f}MB downloaded"
if d['status'] == 'downloading'
else "\nDownload completed. Processing...", end='')
],
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
video_title = info['title']
video_path = os.path.join(output_path, f"{video_title}.{info['ext']}")
print(f"{colorama.Fore.GREEN}\nβ
Video downloaded successfully: {video_path}")
return f"β
Video downloaded successfully: {video_path}"
except Exception as e:
print(f"{colorama.Fore.RED}\nβ Failed to download video: {e}")
return f"β Failed to download video: {e}"
# If no video content or video download fails, do regular file download
try:
response = requests.get(url, stream=True, verify=False, headers=HEADERS)
response.raise_for_status()
total_size = int(response.headers.get('content-length', 0))
output_path = output_path or os.path.join(os.getcwd(), os.path.basename(url))
if os.path.isdir(output_path):
output_path = os.path.join(output_path, os.path.basename(url))
with open(output_path, 'wb') as file, tqdm(
desc=f"Downloading {os.path.basename(output_path)}",
total=total_size,
unit='iB',
unit_scale=True,
unit_divisor=1024,
) as progress_bar:
for data in response.iter_content(chunk_size=1024):
size = file.write(data)
progress_bar.update(size)
print(f"{colorama.Fore.GREEN}\nβ
File downloaded successfully: {output_path}")
return f"β
File downloaded successfully: {output_path}"
except requests.exceptions.RequestException as e:
print(f"{colorama.Fore.RED}\nβ Failed to download file: {e}")
return f"β Failed to download file: {e}"
except IOError as e:
print(f"{colorama.Fore.RED}\nβ Error saving file: {e}")
return f"β Error saving file: {e}"
def execute_code(self, code: str, language: str) -> Dict[str, Any]:
temp_file = os.path.join(os.getcwd(), f"temp_{int(time.time())}.{'py' if language == 'python' else 'sh'}")
try:
if language == 'python':
required_packages = extract_imports(code)
if required_packages:
installation_results = install_missing_packages(required_packages)
if not all(installation_results.values()):
failed_packages = [pkg for pkg, success in installation_results.items() if not success]
return {'success': False, 'output': None,
'error': f"Failed to install required packages: {', '.join(failed_packages)}",
'return_code': -1}
with open(temp_file, 'w') as f:
f.write(code)
result = subprocess.run([language, temp_file], capture_output=True, text=True, timeout=30)
success = result.returncode == 0
print(f"{'β
Code executed successfully' if success else 'β Code execution failed'}")
print(f"Result: {result.stdout if success else result.stderr}")
return {'success': success, 'output': result.stdout if success else result.stderr,
'error': result.stderr if not success else None, 'return_code': result.returncode}
except Exception as e:
print(f"π₯ Code execution error: {e}")
return {'success': False, 'output': None, 'error': str(e) + '\nMake sure you only send the command and the code, without anything else in your message', 'return_code': -1}
finally:
if os.path.exists(temp_file):
os.remove(temp_file)
def get_next_action(self, task, previous_actions):
messages = [
{"role": "system", "content": """You are a genius AI, you analyze and think as long as needed about each
answer, don't EVER explain the step, until you reach the conclusion phase, just run the action!
NEVER DO MULTIPLE DIFFERENT ACTIONS IN ONE STEP, EXAMPLE: {SCRAPE} https://exmaple.com {DOWNLOAD} https://cnn.com/file
, ALWAYS DO ONE ACTION AT A TIME.
You MUST follow these instructions meticulously, you lose 100 points for each time you don't follow:
1. Break down tasks into clear, logical steps.
2. Perform the following actions as needed:
- Web search: Respond with {SEARCH} followed by your query, try to create a logical search query
that will yield the most effective results, don't use long queries, if you need to look for multiple
items, example, nvidia stock, intel stock, just do one search at a time for each.
- File web search: Respond with {SEARCH} followed by the file type and a colon and the query
example: filetype:pdf "jane eyre".
- Execute Python code: Respond with {EXECUTE_PYTHON} followed by the code.
- Execute Bash commands: Respond with {EXECUTE_BASH} followed by the commands.
- Scrape a website: Respond with {SCRAPE} followed by the URL.
- Download files: Respond with {DOWNLOAD} followed by the URL - works for webpages with videos as well.
- Scrape python files in a project: Respond with {SCRAPE_PYTHON} followed by the folder path.
example: {DOWNLOAD} https://example.com/file.txt
- End the session: Respond with {END_SESSION} if the task is impossible or complete.
- Provide conclusions: Respond with {CONCLUDE} followed by your summary, do this ONLY if ALL of your
tasks are done and you are ready to provide the summary and end the session, never do it in the same
step as another action, it should be its own action.
The conclusion should be simple for simple actions, in the case of research actions it must be
in a format similar to this if its concluding research or information gathering:
Abstract β 200-300 words summarizing the research objectives, methods, findings, and conclusions.
Introduction β Provide background, state the research problem, and outline objectives.
Literature Review β Summarize relevant studies and identify gaps.
Methodology β Describe the research design, sample size, and methods.
Results β Present findings (include tables/graphs if necessary).
Discussion β Interpret results, compare with existing studies, and discuss limitations.
Conclusion β Summarize findings and suggest future research.
References β List citations used.
3. Ensure accuracy at each step before proceeding.
4. Always respond with a single, actionable step, don't add an explanation beyond the action."""},
{"role": "user",
"content": f"Todays date is: {datetime.datetime.now()} your Task: {task}\n\nPrevious actions taken: {json.dumps(previous_actions)}\n\nWhat should be the next action?"}
]
for attempt in range(self.max_retries):
try:
response = self.client.chat.completions.create(
model=base_model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
top_p=top_p,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
)
next_action = response.choices[0].message.content
print(f"{colorama.Fore.CYAN}π Next action: {next_action}") # Log next action with emoji
return next_action
except Exception as e:
if attempt == self.max_retries - 1:
raise e
time.sleep(self.retry_delay * (attempt + 1))
def evaluate_completion(self, task, actions):
messages = [
{"role": "system", "content": "You are an AI agent that evaluates task completion."},
{"role": "user",
"content": f"Task: {task}\n\nActions taken: {json.dumps(actions)}\n\nHas the task been completed? Respond with 'YES' if completed, 'NO' if not."}
]
for attempt in range(self.max_retries):
try:
response = self.client.chat.completions.create(
model=base_model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
top_p=top_p,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty
)
return "YES" in response.choices[0].message.content.upper()
except Exception as e:
if attempt == self.max_retries - 1:
raise e
time.sleep(self.retry_delay * (attempt + 1))
def extract_actions(self, response: str) -> list:
actions = []
action_pattern = re.compile(r'\{([A-Z_]+)\}(.+?)(?=\{[A-Z_]+\}|$)', re.DOTALL)
matches = action_pattern.findall(response)
for action_type, action_content in matches:
action = f"{{{action_type}}}{action_content.strip()}"
if action not in actions:
actions.append(action)
return actions
def execute_task(self, task):
if task not in self.tasks:
self.tasks[task] = {'previous_actions': [], 'conclusions': []}
task_context = self.tasks[task]
previous_actions = task_context['previous_actions'] + self.global_history
conclusions = task_context['conclusions']
max_steps = 50
step = 0
print(f"{colorama.Fore.CYAN}π Starting task: {task}\nπ§ π§ π§ Analyzing the task... please wait...")
while step < max_steps:
full_response = ""
for chunk in self.stream_response(task, previous_actions):
full_response += chunk
actions = self.extract_actions(full_response)
if not actions:
print(f"{colorama.Fore.YELLOW}π€·ββοΈ No action taken: {full_response}\n")
previous_actions.append(f"the reply: {full_response} is not an action, you MUST reply using one of the "
f"following actions: {['{SEARCH}', '{DOWNLOAD}', '{SCRAPE}', '{EXECUTE_PYTHON}', '{EXECUTE_BASH}', '{CONCLUDE}', '{END_SESSION}']}")
for action in actions:
if action.startswith("{END_SESSION}"):
print("\nπ Session ended by agent.")
step = max_steps
break
elif "{CONCLUDE}" in action or "summary" in action or "conclusion" in action:
conclusion = action[10:].strip()
print(f"\nπ Here's my conclusion:\n{conclusion}")
conclusions.append(conclusion)
step = max_steps
break
elif action.startswith("{SCRAPE_PYTHON}"):
python_project_files = action[15:].strip()
print(f"{colorama.Fore.CYAN}\nFinished scraping python files in {python_project_files}\n")
previous_actions.append(
f"The python files requested:\n{json.dumps(self._scrape_python_files(python_project_files))}")
elif action.startswith("{SEARCH}"):
search_query = action[8:].strip()
search_query = search_query.replace('"', '')
print(f"{colorama.Fore.CYAN}\nπ Searching web for: {search_query}")
search_result = self.search_web(search_query)
previous_actions.append(f"Searched: {search_query}")
previous_actions.append(f"Search results: {json.dumps(search_result)}")
previous_actions.append(
"\nYou must select at least between 2 and 8 results from the search results to scrape using the {SCRAPE} "
"action"
"select the ones that you think will yield the most useful information."
"If the information returned is not sufficient, try again and scrape one of the other results."
"If the information is sufficient, you can proceed to the next action.")
print(f"{colorama.Fore.CYAN}\nπ Search results found: {json.dumps(len(search_result))}")
break
elif action.startswith("{DOWNLOAD}"):
try:
match = re.search(r'{DOWNLOAD}\s*(https?://\S+)', action)
if match is None:
raise ValueError("No match found for the provided action.")
url = match.group(1)
print(f"Extracted URL: {url}")
except ValueError as ve:
print(f"Value error: {ve}")
except AttributeError as ae:
print(f"Attribute error: {ae}")
print(f"{colorama.Fore.CYAN}\nπ₯ Downloading file: {url}")
download_result = self._download_file(url)
previous_actions.append(f"Downloaded: {url} - {download_result}")
print(f"{colorama.Fore.CYAN}\nπ₯ Downloaded: {url} - {download_result}")
break
elif action.startswith("{SCRAPE}"):
match = re.search(r'{SCRAPE}\s*(https?://\S+)', action)
try:
url = match.group(1)
if url.endswith(".pdf") \
or url.endswith(".doc") \
or url.endswith(".docx") \
or url.endswith(".mp4"):
previous_actions.append("Please use {DOWNLOAD} action for downloading files.")
break
print(f"{colorama.Fore.CYAN}\nπ·οΈ Scraping website: {url}")
result = scrape_website(url)
previous_actions.append(f"Scraped {url}")
previous_actions.append(f"Scraping results: {json.dumps(result)} is this the information you "
f"were looking for?"
f"is it sufficient? if not, select an "
f"additional website to scrape.")
print(f"{colorama.Fore.CYAN}\nπ·οΈβ
Scrape successful!\nπ§ π§ π§ analyzing the content... please wait...")
except Exception as e:
previous_actions.append(f"Scraping error: {str(e)}")
print(f"{colorama.Fore.RED}π·οΈ Scraping error: {str(e)}")
elif action.startswith("{EXECUTE_PYTHON}"):
code = action[16:].strip().removeprefix("```python").removesuffix("```").strip()
print(f"{colorama.Fore.CYAN}π Executing Python code:\n{code}")
result = self.execute_code(code, 'python')
previous_actions.append(f"Executed Python: {code}")
previous_actions.append(f"Result: {result}")
print(f"{colorama.Fore.CYAN}π Result: {result}")
break
elif action.startswith("{EXECUTE_BASH}"):
code = action[14:].strip().removeprefix("```bash").removesuffix("```").strip()
print(f"{colorama.Fore.CYAN}π» Executing Bash code:\n{code}")
result = self.execute_code(code, 'bash')
previous_actions.append(f"Executed Bash: {code}")
previous_actions.append(f"Result: {result}")
print(f"{colorama.Fore.CYAN}π» Result: {result}")
break
elif action.startswith("{CONCLUDE}"):
conclusion = action[10:].strip()
print(f"{colorama.Fore.CYAN}\nπ Here's my conclusion:\n\n\n===============================\n"
f"{conclusion}===============================")
conclusions.append(conclusion)
break
else:
previous_actions.append(f"the reply: {action} is not an action, please reply using one of the "
f"following actions: {['{SEARCH}', '{DOWNLOAD}', '{SCRAPE}', '{EXECUTE_PYTHON}', '{EXECUTE_BASH}', '{CONCLUDE}', '{END_SESSION}']}")
print(f"{colorama.Fore.YELLOW}π€·ββοΈ No action taken: {action}")
break
self.tasks[task] = {'previous_actions': previous_actions, 'conclusions': conclusions}
self.global_history.extend(previous_actions)
return len(conclusions) > 0
def save_session(self):
if self.session_file:
with open(self.session_file, 'w', encoding='utf-8') as f:
json.dump(self.tasks, f, indent=4)
print(f"{colorama.Fore.GREEN}β
Session saved to {self.session_file}")
def load_session(self, session_file):
try:
with open(session_file, 'r', encoding='utf-8') as f:
self.tasks = json.load(f)
self.session_file = session_file
print(f"{colorama.Fore.GREEN}β
Session loaded from {session_file}")
except FileNotFoundError:
print(f"{colorama.Fore.RED}β Session file not found: {session_file}")
def main():
agent = Agent(base_url=base_url, api_key=base_api)
current_task = ""
while True:
if current_task:
print(f"\n{colorama.Fore.CYAN}π Current task: {current_task}\nEnter your task (or 'quit' to exit):")
else:
print("\nEnter your task (or 'quit' to exit):")
task_input = input("INPUT: ").strip()
if task_input.lower() in ['quit', 'exit', 'q']:
print("π Goodbye!")
break
if not task_input:
print("Please enter a valid task.")
continue
task = task_input
current_task = task_input
try:
print("\n" + "=" * 50)
agent.execute_task(task)
print("=" * 50)
except KeyboardInterrupt:
print("\nπ Task interrupted by user.")
continue
except Exception as e:
print(f"\n{colorama.Fore.RED}β Error executing task: {e}")
continue
if __name__ == "__main__":
main()