1
- import autogpt .commands as cmd
2
1
import json
3
2
import traceback
4
3
from tkinter .ttk import Style
5
4
6
5
from colorama import Fore
7
6
8
7
import autogpt .chat
8
+ import autogpt .commands as cmd
9
+ import autogpt .speak
9
10
from autogpt .config import Config
10
11
from autogpt .logger import logger
11
- import autogpt .speak
12
12
from autogpt .spinner import Spinner
13
13
14
14
@@ -24,13 +24,16 @@ class Agent:
24
24
user_input: The user input.
25
25
26
26
"""
27
- def __init__ (self ,
28
- ai_name ,
29
- memory ,
30
- full_message_history ,
31
- next_action_count ,
32
- prompt ,
33
- user_input ):
27
+
28
+ def __init__ (
29
+ self ,
30
+ ai_name ,
31
+ memory ,
32
+ full_message_history ,
33
+ next_action_count ,
34
+ prompt ,
35
+ user_input ,
36
+ ):
34
37
self .ai_name = ai_name
35
38
self .memory = memory
36
39
self .full_message_history = full_message_history
@@ -43,10 +46,16 @@ def start_interaction_loop(self):
43
46
cfg = Config ()
44
47
loop_count = 0
45
48
while True :
46
- # Discontinue if continuous limit is reached
49
+ # Discontinue if continuous limit is reached
47
50
loop_count += 1
48
- if cfg .continuous_mode and cfg .continuous_limit > 0 and loop_count > cfg .continuous_limit :
49
- logger .typewriter_log ("Continuous Limit Reached: " , Fore .YELLOW , f"{ cfg .continuous_limit } " )
51
+ if (
52
+ cfg .continuous_mode
53
+ and cfg .continuous_limit > 0
54
+ and loop_count > cfg .continuous_limit
55
+ ):
56
+ logger .typewriter_log (
57
+ "Continuous Limit Reached: " , Fore .YELLOW , f"{ cfg .continuous_limit } "
58
+ )
50
59
break
51
60
52
61
# Send message to AI, get response
@@ -56,15 +65,17 @@ def start_interaction_loop(self):
56
65
self .user_input ,
57
66
self .full_message_history ,
58
67
self .memory ,
59
- cfg .fast_token_limit ) # TODO: This hardcodes the model to use GPT3.5. Make this an argument
68
+ cfg .fast_token_limit ,
69
+ ) # TODO: This hardcodes the model to use GPT3.5. Make this an argument
60
70
61
71
# Print Assistant thoughts
62
72
print_assistant_thoughts (assistant_reply )
63
73
64
74
# Get command name and arguments
65
75
try :
66
76
command_name , arguments = cmd .get_command (
67
- attempt_to_fix_json_by_finding_outermost_brackets (assistant_reply ))
77
+ attempt_to_fix_json_by_finding_outermost_brackets (assistant_reply )
78
+ )
68
79
if cfg .speak_mode :
69
80
speak .say_text (f"I want to execute { command_name } " )
70
81
except Exception as e :
@@ -78,21 +89,29 @@ def start_interaction_loop(self):
78
89
logger .typewriter_log (
79
90
"NEXT ACTION: " ,
80
91
Fore .CYAN ,
81
- f"COMMAND = { Fore .CYAN } { command_name } { Style .RESET_ALL } ARGUMENTS = { Fore .CYAN } { arguments } { Style .RESET_ALL } " )
92
+ f"COMMAND = { Fore .CYAN } { command_name } { Style .RESET_ALL } ARGUMENTS = { Fore .CYAN } { arguments } { Style .RESET_ALL } " ,
93
+ )
82
94
print (
83
95
f"Enter 'y' to authorise command, 'y -N' to run N continuous commands, 'n' to exit program, or enter feedback for { self .ai_name } ..." ,
84
- flush = True )
96
+ flush = True ,
97
+ )
85
98
while True :
86
- console_input = utils .clean_input (Fore .MAGENTA + "Input:" + Style .RESET_ALL )
99
+ console_input = utils .clean_input (
100
+ Fore .MAGENTA + "Input:" + Style .RESET_ALL
101
+ )
87
102
if console_input .lower ().rstrip () == "y" :
88
103
self .user_input = "GENERATE NEXT COMMAND JSON"
89
104
break
90
105
elif console_input .lower ().startswith ("y -" ):
91
106
try :
92
- self .next_action_count = abs (int (console_input .split (" " )[1 ]))
107
+ self .next_action_count = abs (
108
+ int (console_input .split (" " )[1 ])
109
+ )
93
110
self .user_input = "GENERATE NEXT COMMAND JSON"
94
111
except ValueError :
95
- print ("Invalid input format. Please enter 'y -n' where n is the number of continuous tasks." )
112
+ print (
113
+ "Invalid input format. Please enter 'y -n' where n is the number of continuous tasks."
114
+ )
96
115
continue
97
116
break
98
117
elif console_input .lower () == "n" :
@@ -107,7 +126,8 @@ def start_interaction_loop(self):
107
126
logger .typewriter_log (
108
127
"-=-=-=-=-=-=-= COMMAND AUTHORISED BY USER -=-=-=-=-=-=-=" ,
109
128
Fore .MAGENTA ,
110
- "" )
129
+ "" ,
130
+ )
111
131
elif self .user_input == "EXIT" :
112
132
print ("Exiting..." , flush = True )
113
133
break
@@ -116,54 +136,68 @@ def start_interaction_loop(self):
116
136
logger .typewriter_log (
117
137
"NEXT ACTION: " ,
118
138
Fore .CYAN ,
119
- f"COMMAND = { Fore .CYAN } { command_name } { Style .RESET_ALL } ARGUMENTS = { Fore .CYAN } { arguments } { Style .RESET_ALL } " )
139
+ f"COMMAND = { Fore .CYAN } { command_name } { Style .RESET_ALL } ARGUMENTS = { Fore .CYAN } { arguments } { Style .RESET_ALL } " ,
140
+ )
120
141
121
142
# Execute command
122
143
if command_name is not None and command_name .lower ().startswith ("error" ):
123
- result = f"Command { command_name } threw the following error: " + arguments
144
+ result = (
145
+ f"Command { command_name } threw the following error: " + arguments
146
+ )
124
147
elif command_name == "human_feedback" :
125
148
result = f"Human feedback: { self .user_input } "
126
149
else :
127
150
result = f"Command { command_name } returned: { cmd .execute_command (command_name , arguments )} "
128
151
if self .next_action_count > 0 :
129
152
self .next_action_count -= 1
130
153
131
- memory_to_add = f"Assistant Reply: { assistant_reply } " \
132
- f"\n Result: { result } " \
133
- f"\n Human Feedback: { self .user_input } "
154
+ memory_to_add = (
155
+ f"Assistant Reply: { assistant_reply } "
156
+ f"\n Result: { result } "
157
+ f"\n Human Feedback: { self .user_input } "
158
+ )
134
159
135
160
self .memory .add (memory_to_add )
136
161
137
162
# Check if there's a result from the command append it to the message
138
163
# history
139
164
if result is not None :
140
- self .full_message_history .append (chat .create_chat_message ("system" , result ))
165
+ self .full_message_history .append (
166
+ chat .create_chat_message ("system" , result )
167
+ )
141
168
logger .typewriter_log ("SYSTEM: " , Fore .YELLOW , result )
142
169
else :
143
170
self .full_message_history .append (
144
- chat .create_chat_message (
145
- "system" , "Unable to execute command" ))
146
- logger .typewriter_log ("SYSTEM: " , Fore .YELLOW , "Unable to execute command" )
171
+ chat .create_chat_message ("system" , "Unable to execute command" )
172
+ )
173
+ logger .typewriter_log (
174
+ "SYSTEM: " , Fore .YELLOW , "Unable to execute command"
175
+ )
147
176
148
177
149
178
def attempt_to_fix_json_by_finding_outermost_brackets (json_string ):
150
179
cfg = Config ()
151
180
if cfg .speak_mode and cfg .debug_mode :
152
- speak .say_text ("I have received an invalid JSON response from the OpenAI API. Trying to fix it now." )
181
+ speak .say_text (
182
+ "I have received an invalid JSON response from the OpenAI API. Trying to fix it now."
183
+ )
153
184
logger .typewriter_log ("Attempting to fix JSON by finding outermost brackets\n " )
154
185
155
186
try :
156
187
# Use regex to search for JSON objects
157
188
import regex
189
+
158
190
json_pattern = regex .compile (r"\{(?:[^{}]|(?R))*\}" )
159
191
json_match = json_pattern .search (json_string )
160
192
161
193
if json_match :
162
194
# Extract the valid JSON object from the string
163
195
json_string = json_match .group (0 )
164
- logger .typewriter_log (title = "Apparently json was fixed." , title_color = Fore .GREEN )
196
+ logger .typewriter_log (
197
+ title = "Apparently json was fixed." , title_color = Fore .GREEN
198
+ )
165
199
if cfg .speak_mode and cfg .debug_mode :
166
- speak .say_text ("Apparently json was fixed." )
200
+ speak .say_text ("Apparently json was fixed." )
167
201
else :
168
202
raise ValueError ("No valid JSON object found" )
169
203
@@ -187,7 +221,9 @@ def print_assistant_thoughts(assistant_reply):
187
221
assistant_reply_json = fix_and_parse_json (assistant_reply )
188
222
except json .JSONDecodeError as e :
189
223
logger .error ("Error: Invalid JSON in assistant thoughts\n " , assistant_reply )
190
- assistant_reply_json = attempt_to_fix_json_by_finding_outermost_brackets (assistant_reply )
224
+ assistant_reply_json = attempt_to_fix_json_by_finding_outermost_brackets (
225
+ assistant_reply
226
+ )
191
227
assistant_reply_json = fix_and_parse_json (assistant_reply_json )
192
228
193
229
# Check if assistant_reply_json is a string and attempt to parse it into a JSON object
@@ -196,7 +232,11 @@ def print_assistant_thoughts(assistant_reply):
196
232
assistant_reply_json = json .loads (assistant_reply_json )
197
233
except json .JSONDecodeError as e :
198
234
logger .error ("Error: Invalid JSON\n " , assistant_reply )
199
- assistant_reply_json = attempt_to_fix_json_by_finding_outermost_brackets (assistant_reply_json )
235
+ assistant_reply_json = (
236
+ attempt_to_fix_json_by_finding_outermost_brackets (
237
+ assistant_reply_json
238
+ )
239
+ )
200
240
201
241
assistant_thoughts_reasoning = None
202
242
assistant_thoughts_plan = None
@@ -211,7 +251,9 @@ def print_assistant_thoughts(assistant_reply):
211
251
assistant_thoughts_criticism = assistant_thoughts .get ("criticism" )
212
252
assistant_thoughts_speak = assistant_thoughts .get ("speak" )
213
253
214
- logger .typewriter_log (f"{ ai_name .upper ()} THOUGHTS:" , Fore .YELLOW , assistant_thoughts_text )
254
+ logger .typewriter_log (
255
+ f"{ ai_name .upper ()} THOUGHTS:" , Fore .YELLOW , assistant_thoughts_text
256
+ )
215
257
logger .typewriter_log ("REASONING:" , Fore .YELLOW , assistant_thoughts_reasoning )
216
258
217
259
if assistant_thoughts_plan :
@@ -223,7 +265,7 @@ def print_assistant_thoughts(assistant_reply):
223
265
assistant_thoughts_plan = str (assistant_thoughts_plan )
224
266
225
267
# Split the input_string using the newline character and dashes
226
- lines = assistant_thoughts_plan .split (' \n ' )
268
+ lines = assistant_thoughts_plan .split (" \n " )
227
269
for line in lines :
228
270
line = line .lstrip ("- " )
229
271
logger .typewriter_log ("- " , Fore .GREEN , line .strip ())
@@ -237,7 +279,9 @@ def print_assistant_thoughts(assistant_reply):
237
279
except json .decoder .JSONDecodeError as e :
238
280
logger .error ("Error: Invalid JSON\n " , assistant_reply )
239
281
if cfg .speak_mode :
240
- speak .say_text ("I have received an invalid JSON response from the OpenAI API. I cannot ignore this response." )
282
+ speak .say_text (
283
+ "I have received an invalid JSON response from the OpenAI API. I cannot ignore this response."
284
+ )
241
285
242
286
# All other errors, return "Error: + error message"
243
287
except Exception as e :
0 commit comments