-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathouroboros.py
1042 lines (888 loc) · 41.3 KB
/
ouroboros.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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Ouroboros.py - An AI-driven self-improving experimentation system
import io
import os
import re
import sys
import time
import shutil
import json
import logging
import traceback
import sqlite3
import requests
import configparser
from io import BytesIO
from datetime import datetime
from logging.handlers import RotatingFileHandler
import anthropic
import openai
import git
import docker
import schedule
from tqdm import tqdm
from bs4 import BeautifulSoup
from googleapiclient.discovery import build
def read_config_with_multiline(filename):
with open(filename, 'r') as file:
config_string = file.read()
# Find the PROMPT value and replace newlines with '\n'
prompt_match = re.search(r'PROMPT\s*=\s*"""(.*?)"""', config_string, re.DOTALL)
if prompt_match:
prompt_value = prompt_match.group(1)
escaped_prompt = prompt_value.replace('\n', '\\n')
config_string = config_string.replace(prompt_match.group(0), f'PROMPT = "{escaped_prompt}"')
config = configparser.ConfigParser()
config.read_string(config_string)
return config
# Use the function to read the config file
config = read_config_with_multiline('config.ini')
# Initialize logging
def setup_logging():
logger = logging.getLogger('ouroboros')
logger.setLevel(logging.INFO)
# File handler
file_handler = RotatingFileHandler('ouroboros.log', maxBytes=10000000, backupCount=5)
file_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
# Console handler
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)
return logger
logger = setup_logging()
# Model token limits
MODEL_MAX_INPUT_TOKENS = {
# Claude models
"claude-2.1": 100000,
"claude-3-haiku-20240307": 200000,
"claude-3-sonnet-20240229": 200000,
"claude-3-opus-20240229": 200000,
"claude-3-5-sonnet-20240620": 200000,
# OpenAI models
"gpt-3.5-turbo": 4096,
"gpt-3.5-turbo-16k": 16384,
"gpt-4": 8192,
"gpt-4-32k": 32768,
}
MODEL_MAX_OUTPUT_TOKENS = {
# Claude models
"claude-2.1": 100000,
"claude-3-haiku-20240307": 4096,
"claude-3-sonnet-20240229": 4096,
"claude-3-opus-20240229": 4096,
"claude-3-5-sonnet-20240620": 4096,
# OpenAI models
"gpt-3.5-turbo": 4096,
"gpt-3.5-turbo-16k": 4096,
"gpt-4": 8192,
"gpt-4-32k": 8192,
}
def check_ai_libraries():
ai_provider = config.get('AI', 'PROVIDER', fallback='claude').lower()
if ai_provider == 'claude':
try:
import anthropic
required_version = "0.31.2"
if anthropic.__version__ < required_version:
logger.error(f"Anthropic version {anthropic.__version__} is below the required version {required_version}. Please upgrade.")
print(f"Anthropic library version {anthropic.__version__} is outdated. Please upgrade to version {required_version} or later.")
return False
return True
except ImportError:
logger.error("Anthropic library not found. Please install the library.")
print("Anthropic library not found. Please install the library.")
return False
elif ai_provider == 'openai':
try:
import openai
required_version = "1.35.14"
if openai.__version__ < required_version:
logger.error(f"OpenAI version {openai.__version__} is below the required version {required_version}. Please upgrade.")
print(f"OpenAI library version {openai.__version__} is outdated. Please upgrade to version {required_version} or later.")
return False
return True
except ImportError:
logger.error("OpenAI library not found. Please install the library.")
print("OpenAI library not found. Please install the library.")
return False
else:
logger.error(f"Unsupported AI provider: {ai_provider}")
return False
# Database functions
def init_db():
conn = None
try:
conn = sqlite3.connect('ouroboros.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS experiments
(id INTEGER PRIMARY KEY, code TEXT, results TEXT, ai_notes TEXT, dockerfile TEXT, timestamp TEXT)''')
conn.commit()
logger.info("Database initialized successfully")
except sqlite3.Error as e:
logger.error(f"Database initialization error: {e}")
finally:
if conn:
conn.close()
def get_last_experiment_id():
conn = None
try:
conn = sqlite3.connect('ouroboros.db')
c = conn.cursor()
c.execute("SELECT MAX(id) FROM experiments")
last_id = c.fetchone()[0]
return last_id or 0
except sqlite3.Error as e:
logger.error(f"Error fetching last experiment ID: {e}")
return 0
finally:
if conn:
conn.close()
# File functions
def read_access():
try:
with open('access.txt', 'r') as f:
access_info = {line.split('=')[0].strip(): line.split('=')[1].strip() for line in f if '=' in line}
ai_provider = config.get('AI', 'PROVIDER', fallback='claude').lower()
if ai_provider == 'claude':
if 'ANTHROPIC_API_KEY' not in access_info:
logger.error("ANTHROPIC_API_KEY not found in access.txt")
elif ai_provider == 'openai':
if 'OPENAI_API_KEY' not in access_info:
logger.error("OPENAI_API_KEY not found in access.txt")
return access_info
except FileNotFoundError:
logger.error("access.txt file not found")
return {}
def check_disk_space(min_space_gb=100):
total, used, free = shutil.disk_usage("/")
free_gb = free // (2**30)
if free_gb < min_space_gb:
logger.warning(f"Low disk space: {free_gb}GB free. Minimum recommended: {min_space_gb}GB")
return free_gb
# Git functions
def init_git_repo():
try:
exp_dir = os.path.abspath('experiments')
if not os.path.exists(exp_dir):
os.makedirs(exp_dir)
repo_path = os.path.join(exp_dir, '.git')
if not os.path.exists(repo_path):
repo = git.Repo.init(exp_dir)
logger.info(f"Initialized new Git repository in {exp_dir}")
else:
repo = git.Repo(exp_dir)
logger.info(f"Using existing Git repository in {exp_dir}")
return repo, exp_dir
except git.exc.GitCommandError as e:
logger.error(f"Git repository initialization error: {e}")
return None, None
def commit_to_git(repo, message, files_to_add=None):
if repo is None:
logger.error("Cannot commit: Git repository not initialized")
return
try:
if files_to_add:
existing_files = [f for f in files_to_add if os.path.exists(f)]
if existing_files:
repo.index.add(existing_files)
else:
logger.warning(f"No files exist to add for commit: {message}")
return
else:
repo.git.add(A=True)
repo.index.commit(message)
logger.info(f"Committed changes to Git: {message}")
except git.exc.GitCommandError as e:
logger.error(f"Git commit error: {e}")
# Web functions
def google_search(query, num_results=5):
if not config.get('Google', 'API_KEY') or not config.get('Google', 'CSE_ID'):
logger.warning("Google search feature is not configured.")
return []
try:
service = build("customsearch", "v1", developerKey=config.get('Google', 'API_KEY'))
res = service.cse().list(q=query, cx=config.get('Google', 'CSE_ID'), num=num_results).execute()
results = []
for item in res.get('items', []):
results.append({
'title': item['title'],
'link': item['link'],
'snippet': item['snippet']
})
logger.info(f"Google search completed for query: {query}")
return results
except Exception as e:
logger.error(f"Error in Google search: {str(e)}")
return []
def load_webpage(url):
try:
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# Remove script and style elements
for script in soup(["script", "style"]):
script.decompose()
# Get text
text = soup.get_text()
# Break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
# Break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
# Drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)
logger.info(f"Webpage loaded and parsed: {url}")
return text[:5000] # Limit to first 5000 characters
except Exception as e:
logger.error(f"Error loading webpage: {str(e)}")
return f"Error loading webpage: {str(e)}"
# Docker functions
def create_docker_client():
try:
client = docker.from_env()
logger.info("Docker client created successfully")
return client
except docker.errors.DockerException as e:
logger.error(f"Docker client creation error: {e}")
return None
def run_in_docker(client, container, code, exp_dir):
try:
# Execute the code directly in the container
exit_code, output = container.exec_run(
cmd=["python", "-c", code],
demux=True
)
result = output[0].decode() if output[0] else ""
error_output = output[1].decode() if output[1] else ""
if error_output:
logger.warning(f"Error output from code execution: {error_output}")
result += f"\nError output:\n{error_output}"
logger.info("Docker code execution completed")
return result
except Exception as e:
logger.error(f"Unexpected error in Docker execution: {e}")
return f"Unexpected error: {e}"
def cleanup_docker_resources():
if not config.getboolean('Docker', 'EnableCleanup', fallback=False):
logger.info("Docker cleanup is disabled. Skipping.")
return
keep_last_n = config.getint('Docker', 'KeepLastNImages', fallback=5)
try:
client = docker.from_env()
# Remove old containers
containers = client.containers.list(all=True)
for container in containers:
if container.status == 'exited':
container.remove()
logger.info(f"Removed Docker container: {container.id}")
# Remove dangling images
images = client.images.list(filters={'dangling': True})
for image in images:
client.images.remove(image.id)
logger.info(f"Removed dangling Docker image: {image.id}")
# Remove old images, keeping the last n
images = client.images.list()
sorted_images = sorted(images, key=lambda x: x.attrs['Created'], reverse=True)
for image in sorted_images[keep_last_n:]:
try:
client.images.remove(image.id)
logger.info(f"Removed old Docker image: {image.id}")
except docker.errors.APIError as e:
logger.warning(f"Could not remove Docker image {image.id}: {e}")
except Exception as e:
logger.error(f"Error during Docker resource cleanup: {e}")
def log_docker_resource_usage():
try:
client = docker.from_env()
# Log container usage
containers = client.containers.list(all=True)
logger.info(f"Total containers: {len(containers)}")
logger.info(f"Running containers: {len([c for c in containers if c.status == 'running'])}")
# Log image usage
images = client.images.list()
logger.info(f"Total images: {len(images)}")
# Log disk usage
total, used, free = shutil.disk_usage("/")
logger.info(f"Disk usage: {used // (2**30)}GB used, {free // (2**30)}GB free")
except Exception as e:
logger.error(f"Error logging Docker resource usage: {e}")
# AI interaction functions
def extract_json(text):
"""Extract JSON object from text."""
try:
start = text.index('{')
end = text.rindex('}') + 1
json_str = text[start:end]
return json.loads(json_str)
except ValueError:
return None
def get_ai_response(prompt, api_key):
ai_provider = config.get('AI', 'PROVIDER', fallback='claude').lower()
if ai_provider == 'claude':
return get_claude_response(prompt, api_key)
elif ai_provider == 'openai':
return get_openai_response(prompt, api_key)
else:
error_message = f"Unsupported AI provider: {ai_provider}"
logger.error(error_message)
return f"Error: {error_message}"
def get_claude_response(prompt, api_key):
try:
client = anthropic.Anthropic(api_key=api_key)
model = config.get('Anthropic', 'MODEL', fallback='claude-2.1')
max_output_tokens = min(
int(config.get('Anthropic', 'MAX_OUTPUT_TOKENS', fallback='4096')),
MODEL_MAX_OUTPUT_TOKENS.get(model, 4096)
)
temperature = float(config.get('Anthropic', 'TEMPERATURE', fallback='0.7'))
logger.info(f"Sending request to Claude AI using model: {model} with max_output_tokens: {max_output_tokens}")
if model.startswith("claude-3"):
# Use Messages API for Claude 3.x models
message = client.messages.create(
model=model,
max_tokens=max_output_tokens,
temperature=temperature,
messages=[
{"role": "user", "content": prompt}
]
)
response_content = message.content[0].text
else:
# Use Completions API for Claude 2.x models
response = client.completions.create(
model=model,
max_tokens_to_sample=max_output_tokens,
temperature=temperature,
prompt=f"Human: {prompt}\n\nAssistant:",
stop_sequences=["Human:"]
)
response_content = response.completion
logger.info("Claude AI response received")
return response_content
except anthropic.APIError as e:
logger.error(f"Anthropic API error: {str(e)}")
return f"Error: Anthropic API error - {str(e)}"
except Exception as e:
logger.error(f"Error getting Claude AI response: {str(e)}")
return f"Error: {str(e)}"
def get_openai_response(prompt, api_key):
try:
client = openai.OpenAI(api_key=api_key)
model = config.get('OpenAI', 'MODEL', fallback='gpt-3.5-turbo')
max_tokens = int(config.get('OpenAI', 'MAX_TOKENS', fallback='4096'))
temperature = float(config.get('OpenAI', 'TEMPERATURE', fallback='0.7'))
logger.info(f"Sending request to OpenAI using model: {model} with max_tokens: {max_tokens}")
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
max_tokens=max_tokens,
temperature=temperature
)
response_content = response.choices[0].message.content
logger.info("OpenAI response received")
return response_content
except openai.OpenAIError as e:
logger.error(f"OpenAI API error: {str(e)}")
return f"Error: OpenAI API error - {str(e)}"
except Exception as e:
logger.error(f"Error getting OpenAI response: {str(e)}")
return f"Error: {str(e)}"
def get_ai_prompt(experiment_id, prev_data, action_history, current_dockerfile, current_action, max_actions, time_remaining, access_info):
network_access = config.getboolean('Docker', 'NetworkAccess', fallback=False)
gpu_access = config.getboolean('Docker', 'GPUAccess', fallback=False)
# Get the prompt from the config file
prompt_template = config.get('AIPrompt', 'PROMPT', fallback=None)
# If no prompt is found in the config, use a fallback prompt
if not prompt_template:
prompt_template = """
Experiment #{experiment_id}
You are Ouroboros, an AI system for self-improvement and AI research.
Status:
- Action: {current_action}/{max_actions}
- Time left: {time_remaining:.2f}s
- Network: {network_access}
- GPU: {gpu_access}
Previous data: {prev_data}
Action history: {action_history}
Dockerfile: {current_dockerfile}
Access Info: {access_info}
Instructions:
1. Run code to advance AI research.
2. Update Dockerfile only when necessary.
3. Use "finalize" to end the experiment on or before {max_actions} actions.
Respond ONLY with the following JSON structure:
{{
"action": "run/dockerfile/search/google/loadurl/finalize",
"data": "action data",
"notes": "explanation"
}}
Be creative and push AI boundaries!
"""
# Log detailed information about the context
logger.info(f"Preparing AI prompt for Experiment {experiment_id}")
logger.info(f"Current action: {current_action}/{max_actions}")
logger.info(f"Time remaining: {time_remaining:.2f} seconds")
logger.info(f"Network access: {'Enabled' if network_access else 'Disabled'}")
logger.info(f"GPU access: {'Enabled' if gpu_access else 'Disabled'}")
# Enhanced logging of action history
logger.info("Action history:")
for i, action in enumerate(action_history, 1):
logger.info(f" Action {i}:")
logger.info(f" Type: {action['action']}")
logger.info(f" Notes: {action['notes'][:50]}...") # First 50 characters of notes
logger.info(f" Data: {action['data'][:50]}...") # First 50 characters of data
logger.info(f" Results: {str(action.get('results', ''))[:50]}...") # First 50 characters of results
logger.info(f"Current Dockerfile:\n{current_dockerfile}")
#logger.info(f"Access Info: {json.dumps(access_info, indent=2)}")
# Format the prompt with the current experiment data
formatted_prompt = prompt_template.format(
experiment_id=experiment_id,
current_action=current_action,
max_actions=max_actions,
time_remaining=time_remaining,
network_access="Enabled" if network_access else "Disabled",
gpu_access="Enabled" if gpu_access else "Disabled",
prev_data=json.dumps(prev_data, indent=2) if prev_data else 'No previous experiment',
action_history=json.dumps(action_history, indent=2),
current_dockerfile=current_dockerfile,
access_info=json.dumps(access_info, indent=2)
)
return formatted_prompt
# Action functions
def search_previous_experiments(query):
conn = None
try:
conn = sqlite3.connect('ouroboros.db')
c = conn.cursor()
c.execute("""
SELECT id, substr(code, 1, 200) as code_preview, substr(results, 1, 200) as results_preview,
ai_notes, substr(dockerfile, 1, 200) as dockerfile_preview, timestamp
FROM experiments
WHERE code LIKE ? OR results LIKE ? OR ai_notes LIKE ? OR dockerfile LIKE ?
ORDER BY id DESC
LIMIT ?
""", ('%' + query + '%', '%' + query + '%', '%' + query + '%', '%' + query + '%',
int(config.get('Search', 'MaxResults', fallback='5'))))
results = c.fetchall()
logger.info(f"Search completed for query: {query}")
return [dict(zip(['id', 'code_preview', 'results_preview', 'ai_notes', 'dockerfile_preview', 'timestamp'], row)) for row in results]
except sqlite3.Error as e:
logger.error(f"Database search error: {e}")
return []
finally:
if conn:
conn.close()
# Status update function
def print_status_update(experiment_id, action_count, max_actions, time_remaining):
status_message = f"\rExperiment {experiment_id}: Action {action_count}/{max_actions} | Time remaining: {time_remaining:.2f}s"
sys.stdout.write(status_message)
sys.stdout.flush()
logger.info(status_message.strip())
# Experiment functions
def setup_experiment(experiment_id, repo_dir):
exp_dir = os.path.abspath(os.path.join(repo_dir, f'experiment_{experiment_id}'))
os.makedirs(exp_dir, exist_ok=True)
logger.info(f"Created experiment directory: {exp_dir}")
if not exp_dir.startswith(repo_dir):
raise ValueError(f"Experiment directory {exp_dir} is not within the Git repository {repo_dir}")
return exp_dir
def get_previous_experiment_data(experiment_id):
with sqlite3.connect('ouroboros.db') as conn:
c = conn.cursor()
c.execute("SELECT code, results, ai_notes, dockerfile FROM experiments WHERE id = ?", (experiment_id - 1,))
return c.fetchone()
def save_experiment_results(experiment_id, action_history, results, final_notes, dockerfile):
with sqlite3.connect('ouroboros.db') as conn:
c = conn.cursor()
c.execute("INSERT INTO experiments (id, code, results, ai_notes, dockerfile, timestamp) VALUES (?, ?, ?, ?, ?, ?)",
(experiment_id, json.dumps(action_history), results, final_notes, dockerfile, datetime.now().isoformat()))
conn.commit()
def save_experiment_log(exp_dir, experiment_id, action_history, results, final_notes, dockerfile):
log_path = os.path.join(exp_dir, 'experiment_log.txt')
os.makedirs(os.path.dirname(log_path), exist_ok=True)
with open(log_path, 'w') as f:
json.dump({
"experiment_id": experiment_id,
"action_history": action_history,
"results": results,
"final_notes": final_notes,
"dockerfile": dockerfile
}, f, indent=2)
logger.info(f"Experiment log saved to {log_path}")
return log_path
def extract_json(text):
"""Extract JSON object from text."""
try:
start = text.index('{')
end = text.rindex('}') + 1
json_str = text[start:end]
return json.loads(json_str)
except ValueError:
return None
# Dockerfile and run code actions
def dockerfile_action(data, exp_dir, repo, experiment_id, docker_client):
dockerfile_path = os.path.join(exp_dir, 'Dockerfile')
os.makedirs(os.path.dirname(dockerfile_path), exist_ok=True)
with open(dockerfile_path, 'w') as f:
f.write(data)
if os.path.exists(dockerfile_path):
commit_to_git(repo, f"Experiment {experiment_id}: Update Dockerfile", [dockerfile_path])
logger.info(f"Dockerfile updated and committed for experiment {experiment_id}")
# Build new image
logger.info("Building new Docker image...")
try:
image, _ = docker_client.images.build(
path=exp_dir,
dockerfile='Dockerfile',
rm=True
)
logger.info(f"New Docker image built successfully: {image.id}")
# Create new container
logger.info("Creating new Docker container...")
container_args = {
'image': image.id,
'command': "tail -f /dev/null", # Keep container running
'volumes': {os.path.abspath(exp_dir): {'bind': '/app', 'mode': 'rw'}},
'mem_limit': config.get('Docker', 'MemoryLimit', fallback='512m'),
'cpu_period': 100000,
'cpu_quota': int(config.get('Docker', 'CPUQuota', fallback='50000')),
'network_mode': 'none'
}
# Add GPU access if enabled
if config.getboolean('Docker', 'GPUAccess', fallback=False):
container_args['device_requests'] = [docker.types.DeviceRequest(count=-1, capabilities=[['gpu']])]
container = docker_client.containers.create(**container_args)
logger.info(f"New Docker container created: {container.id}")
# Start the container
container.start()
logger.info("New Docker container started successfully")
return "Dockerfile updated and new container is ready for code execution.", data, image, container
except docker.errors.BuildError as e:
logger.error(f"Docker build error: {str(e)}")
return f"Error building Docker image: {str(e)}", None, None, None
except docker.errors.APIError as e:
logger.error(f"Docker API error: {str(e)}")
return f"Error in Docker operation: {str(e)}", None, None, None
else:
logger.error(f"Failed to create Dockerfile at {dockerfile_path}")
return "Failed to create Dockerfile", None, None, None
def run_action(data, docker_client, current_container, exp_dir, repo, experiment_id):
if not current_container:
return "No Docker container available. Please update the Dockerfile first."
results = run_in_docker(docker_client, current_container, data, exp_dir)
results_path = os.path.join(exp_dir, 'results.txt')
os.makedirs(os.path.dirname(results_path), exist_ok=True)
with open(results_path, 'w') as f:
f.write(results)
commit_to_git(repo, f"Experiment {experiment_id}: Run code", [results_path])
logger.info(f"Code executed for experiment {experiment_id}")
logger.info(f"{'='*50}")
logger.info(f"Docker execution results for experiment {experiment_id}:")
logger.info(results)
logger.info(f"{'='*50}")
return results
# Main experiment cycle
def run_ai_interaction_loop(experiment_id, prev_data, exp_dir, repo, access, docker_client):
action_history = []
current_dockerfile = "FROM python:3.9-slim\nWORKDIR /app\n"
results = ""
final_notes = ""
current_image = None
current_container = None
max_actions = int(config.get('Experiment', 'MaxActions', fallback='10'))
time_limit = float(config.get('Experiment', 'TimeLimit', fallback='3600'))
start_time = time.time()
error_count = 0
max_errors = int(config.get('Experiment', 'MaxErrors', fallback='3'))
def handle_dockerfile_action(data):
nonlocal current_dockerfile, current_image, current_container
result, new_dockerfile, new_image, new_container = dockerfile_action(data, exp_dir, repo, experiment_id, docker_client)
if new_dockerfile:
current_dockerfile = new_dockerfile
if new_image:
if current_image:
docker_client.images.remove(current_image.id, force=True)
current_image = new_image
if new_container:
if current_container:
current_container.remove(force=True)
current_container = new_container
return result
def handle_run_action(data):
return run_action(data, docker_client, current_container, exp_dir, repo, experiment_id)
def handle_search_action(data):
search_results = search_previous_experiments(data)
results = json.dumps(search_results, indent=2)
logger.info(f"Search performed for query: {data}")
return results
def handle_google_action(data):
search_results = google_search(data)
results = json.dumps(search_results, indent=2)
logger.info(f"Google search performed for query: {data}")
return results
def handle_loadurl_action(data):
webpage_content = load_webpage(data)
logger.info(f"Webpage loaded: {data}")
return webpage_content
def handle_finalize_action(data):
logger.info(f"Experiment {experiment_id} finalized. Waiting for next cycle...")
return "FINALIZE"
action_handlers = {
'dockerfile': handle_dockerfile_action,
'run': handle_run_action,
'search': handle_search_action,
'google': handle_google_action,
'loadurl': handle_loadurl_action,
'finalize': handle_finalize_action
}
# Create a detailed log file for the experiment
experiment_log_path = os.path.join(exp_dir, f'experiment_{experiment_id}_detailed_log.txt')
with open(experiment_log_path, 'w') as log_file:
log_file.write(f"Detailed log for Experiment {experiment_id}\n")
log_file.write("=" * 50 + "\n\n")
ai_provider = config.get('AI', 'PROVIDER', fallback='claude').lower()
api_key = access['ANTHROPIC_API_KEY'] if ai_provider == 'claude' else access['OPENAI_API_KEY']
# Test initial Dockerfile
logger.info("Testing initial Dockerfile...")
initial_container = None
try:
# Create initial container
logger.info("Creating initial Docker container...")
container_args = {
'image': "python:3.9-slim",
'command': "tail -f /dev/null", # Keep container running
'volumes': {os.path.abspath(exp_dir): {'bind': '/app', 'mode': 'rw'}},
'mem_limit': config.get('Docker', 'MemoryLimit', fallback='512m'),
'cpu_period': 100000,
'cpu_quota': int(config.get('Docker', 'CPUQuota', fallback='50000')),
'network_mode': 'none'
}
# Add GPU access if enabled
if config.getboolean('Docker', 'GPUAccess', fallback=False):
container_args['device_requests'] = [docker.types.DeviceRequest(count=-1, capabilities=[['gpu']])]
initial_container = docker_client.containers.create(**container_args)
logger.info(f"Initial Docker container created: {initial_container.id}")
# Start the container
initial_container.start()
logger.info("Initial Docker container started successfully")
# Run the test
test_code = "print('Initial Dockerfile test successful')"
test_result = run_in_docker(docker_client, initial_container, test_code, exp_dir)
logger.info(f"Initial Dockerfile test result: {test_result}")
except docker.errors.APIError as e:
logger.error(f"Failed to create or start initial Docker container: {str(e)}")
test_result = f"Error: {str(e)}"
except Exception as e:
logger.error(f"Unexpected error during initial Docker container setup: {str(e)}")
test_result = f"Unexpected error: {str(e)}"
finally:
# Clean up initial container
if initial_container:
logger.info("Cleaning up initial Docker container...")
try:
initial_container.stop()
initial_container.remove()
logger.info("Initial Docker container removed")
except Exception as e:
logger.error(f"Error cleaning up initial Docker container: {str(e)}")
# Main AI interaction loop
for action_count in range(1, max_actions + 1):
time_elapsed = time.time() - start_time
time_remaining = max(0, time_limit - time_elapsed)
time.sleep(1) # Added delay between actions
print_status_update(experiment_id, action_count, max_actions, time_remaining)
if time_remaining <= 0:
logger.warning(f"Experiment {experiment_id} reached time limit.")
break
ai_prompt = get_ai_prompt(
experiment_id, prev_data, action_history, current_dockerfile,
action_count, max_actions, time_remaining, access
)
# Log the prompt sent to AI
log_file.write(f"Action {action_count}: Prompt sent to AI\n")
log_file.write("-" * 40 + "\n")
log_file.write(ai_prompt + "\n\n")
logger.info(f"Sending request to AI - Action {action_count}/{max_actions}")
ai_response = get_ai_response(ai_prompt, api_key)
# Log the AI's response
log_file.write(f"AI Response for Action {action_count}\n")
log_file.write("-" * 40 + "\n")
log_file.write(ai_response + "\n\n")
logger.info(f"Full AI response:\n{ai_response}") # Log the full response
try:
response_json = extract_json(ai_response)
if response_json is None:
raise ValueError("No valid JSON found in the response")
if not all(key in response_json for key in ['action', 'data', 'notes']):
raise ValueError("Invalid JSON structure: missing required fields")
action_type = response_json['action']
action_data = response_json['data']
action_notes = response_json['notes']
if action_type in action_handlers:
result = action_handlers[action_type](action_data)
# Log the action result
log_file.write(f"Result of Action {action_count} ({action_type})\n")
log_file.write("-" * 40 + "\n")
log_file.write(str(result) + "\n\n")
if result == "FINALIZE": # Finalize action
final_notes = action_notes + "\n" + action_data
action_history.append({
"action": action_type,
"data": action_data,
"notes": action_notes,
"results": "Experiment finalized"
})
break
elif result:
results = result
action_history.append({
"action": action_type,
"data": action_data,
"notes": action_notes,
"results": results
})
else:
results = f"Unknown action in AI response: {action_type}"
logger.warning(results)
action_history.append({
"action": "UNKNOWN",
"data": action_data,
"notes": action_notes,
"results": results
})
# Log unknown action
log_file.write(f"Unknown Action {action_count}\n")
log_file.write("-" * 40 + "\n")
log_file.write(results + "\n\n")
except json.JSONDecodeError as json_error:
error_msg = f"Error decoding JSON response: {str(json_error)}"
logger.error(error_msg)
log_file.write(f"Error in Action {action_count}\n")
log_file.write("-" * 40 + "\n")
log_file.write(error_msg + "\n\n")
error_count += 1
except ValueError as value_error:
error_msg = f"Error processing AI response: {str(value_error)}"
logger.error(error_msg)
log_file.write(f"Error in Action {action_count}\n")
log_file.write("-" * 40 + "\n")
log_file.write(error_msg + "\n\n")
error_count += 1
except Exception as action_error:
error_msg = f"Error processing action {action_count}: {str(action_error)}"
logger.error(error_msg)
logger.error(f"Traceback: {traceback.format_exc()}")
log_file.write(f"Error in Action {action_count}\n")
log_file.write("-" * 40 + "\n")
log_file.write(error_msg + "\n")
log_file.write(f"Traceback: {traceback.format_exc()}\n\n")
action_history.append({"action": "ERROR", "error": str(action_error)})
error_count += 1
if error_count >= max_errors:
logger.error(f"Stopping experiment after {max_errors} consecutive errors")
log_file.write(f"Experiment stopped after {max_errors} consecutive errors\n")
break
error_count = 0 # Reset error count after successful processing of all actions
# Clean up Docker resources at the end of the experiment
if current_container:
current_container.remove(force=True)
if current_image:
docker_client.images.remove(current_image.id, force=True)
# If we've reached this point without finalizing, add a final note
if not final_notes:
final_notes = f"Experiment ended after {len(action_history)} actions without explicit finalization."
# Log final notes
log_file.write("Final Notes\n")
log_file.write("-" * 40 + "\n")
log_file.write(final_notes + "\n")
logger.info(f"Detailed experiment log saved to {experiment_log_path}")
return action_history, results, final_notes, current_dockerfile
def run_experiment_cycle(docker_client):
experiment_id = get_last_experiment_id() + 1 # Initialize experiment_id at the start
try:
free_space = check_disk_space()
min_space_gb = config.getint('Docker', 'MinimumDiskSpaceGB', fallback=10)
if free_space < min_space_gb:
logger.error(f"Not enough disk space to run experiment. Available: {free_space}GB, Required: {min_space_gb}GB. Aborting.")
return # Early return if there's not enough disk space
log_docker_resource_usage()
cleanup_docker_resources()
log_docker_resource_usage()
logger.info("Starting new experiment cycle")
access = read_access()
logger.info(f"Experiment ID: {experiment_id}")
ai_provider = config.get('AI', 'PROVIDER', fallback='claude').lower()
if ai_provider == 'claude':
temperature = config.get('Anthropic', 'TEMPERATURE', fallback='0.7')
else:
temperature = config.get('OpenAI', 'TEMPERATURE', fallback='0.7')
logger.info(f"Current AI temperature setting: {temperature}")
repo, repo_dir = init_git_repo()
if repo is None or repo_dir is None:
logger.error("Failed to initialize Git repository. Exiting experiment cycle.")
return
exp_dir = setup_experiment(experiment_id, repo_dir)
if not os.path.exists(exp_dir):
logger.error(f"Experiment directory does not exist: {exp_dir}")
return
prev_data = get_previous_experiment_data(experiment_id)
if prev_data:
logger.info(f"Retrieved data from previous experiment (ID: {experiment_id - 1})")
else:
logger.info("No previous experiment data available")
action_history, results, final_notes, dockerfile = run_ai_interaction_loop(
experiment_id, prev_data, exp_dir, repo, access, docker_client)
save_experiment_results(experiment_id, action_history, results, final_notes, dockerfile)
log_path = save_experiment_log(exp_dir, experiment_id, action_history, results, final_notes, dockerfile)
commit_to_git(repo, f"Experiment {experiment_id}: Save experiment log", [log_path])
logger.info(f"Experiment {experiment_id} completed with {len(action_history)} actions")
logger.info(f"Final notes: {final_notes}")
except Exception as e:
logger.error(f"Error in experiment cycle: {str(e)}")
logger.error(f"Traceback: {traceback.format_exc()}")
finally:
logger.info(f"Experiment cycle {experiment_id} finished")
# Main function
def main():
if not check_ai_libraries():
return
init_db()
docker_client = create_docker_client()