3
3
4
4
import google .generativeai as genai
5
5
6
- from fractale .agent .decorators import callback , save_logs
7
6
import fractale .agent .defaults as defaults
8
7
import fractale .agent .logger as logger
9
8
import fractale .utils as utils
10
9
from fractale .agent .context import get_context
10
+ from fractale .agent .decorators import callback , save_logs
11
11
12
12
13
13
class Agent :
@@ -23,18 +23,17 @@ class Agent:
23
23
24
24
# name and description should be on the class
25
25
26
- def __init__ (self , use_cache = False , results_dir = None , incremental = False ):
27
-
28
- # Max attempts defaults to unlimited
29
- # We start counting at 1 for the user to see.
30
- # Eat your heart out, Matlab.
31
- self .attempts = 1
32
- self .max_attempts = None
26
+ def __init__ (
27
+ self , use_cache = False , results_dir = None , save_incremental = False , max_attempts = None
28
+ ):
29
+ self .attempts = 0
30
+ self .max_attempts = max_attempts
33
31
34
- # For now, assume this is for the manager.
32
+ # For now, assume these are for the manager.
33
+ # They get added to other agents via the step creation
35
34
# We can optionally save incremental result objects
36
35
self .results_dir = results_dir or os .getcwd ()
37
- self .save_incremental = incremental
36
+ self .save_incremental = save_incremental
38
37
39
38
# The user can save if desired - caching the context to skip steps that already run.
40
39
self .setup_cache (use_cache )
@@ -50,9 +49,6 @@ def run(self, context):
50
49
"""
51
50
Run the agent - a wrapper around internal function _run that prepares it.
52
51
"""
53
- # Init attempts. Each agent has an internal counter for total attempts
54
- self .attempts = self .attempts or 1
55
-
56
52
# Load cached context. This is assumed to override user provided args
57
53
# If we have a saved context, we assume we want to use it, return early
58
54
cached_context = self .load_cache ()
@@ -66,6 +62,7 @@ def run(self, context):
66
62
context = get_context (context )
67
63
68
64
# Run, wrapping with a load and save of cache
65
+ # This will return here when the internal loop is done
69
66
context = self ._run (context )
70
67
self .save_cache (context )
71
68
return context
@@ -79,6 +76,17 @@ def print_result(self, result):
79
76
"""
80
77
pass
81
78
79
+ def reset_context (self , context ):
80
+ """
81
+ Remove output and any stateful variables. This is assuming we
82
+ are starting again.
83
+ """
84
+ for key in ["result" , "error_message" ]:
85
+ if key in context .data :
86
+ del context .data [key ]
87
+ # We don't need a return here, but let's be explicit
88
+ return context
89
+
82
90
def setup_cache (self , use_cache = False ):
83
91
"""
84
92
Setup (or load) a cache.
@@ -132,10 +140,7 @@ def reached_max_attempts(self):
132
140
# Unset (None) or 1.
133
141
if not self .max_attempts :
134
142
return False
135
- return self .attempts >= self .max_attempts
136
-
137
- def set_max_attempts (self , max_attempts ):
138
- self .max_attempts = max_attempts
143
+ return self .attempts > self .max_attempts
139
144
140
145
def add_shared_arguments (self , agent ):
141
146
"""
@@ -214,14 +219,6 @@ def _run(self, context):
214
219
assert context
215
220
raise NotImplementedError (f"The { self .name } agent is missing internal 'run' function" )
216
221
217
- def get_initial_prompt (self , context ):
218
- """
219
- Get the initial prompt (with details) to provide context to the manager.
220
-
221
- If we don't do this, the manager can provide a bad instruction for how to fix the error.
222
- """
223
- return self .get_prompt (context )
224
-
225
222
def get_prompt (self , context ):
226
223
"""
227
224
This function should take the same context as run and return the parsed prompt that
0 commit comments