-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathagent.py
More file actions
293 lines (255 loc) · 13.6 KB
/
agent.py
File metadata and controls
293 lines (255 loc) · 13.6 KB
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
import chromadb
import time
import threading
import re
from llm import *
from utils import *
import config
import logging
chroma_client = chromadb.Client()
class Memory:
def __init__(self, agent_name, initial_message):
self.history = []
self.history_pool = chroma_client.create_collection(name=agent_name)
self.initial_message = initial_message
self.name = agent_name
self.subordinates = {}
self.initialize_logger(agent_name)
def add_memory(self, memory):
if (memory['role']!='function' and memory['content'] != None):
self.history_pool.add(documents=[memory['content']], ids=[str(time.time())])
self.logger.info(str(memory))
self.history.append(memory)
def add_subordinate(self, name, description, initial_prompt):
self.subordinates[name] = description
agent_dict[name] = Agent(name, initial_prompt+config.additional_prompt+"\nYour supervisor is: "+self.name)
def get_subordinates(self):
ret = ""
for i in self.subordinates:
ret+=f"{i}: {self.subordinates[i]}\n"
return ret
def add_dialogue(self, speaker, message):
content = f"{speaker}: {message}"
self.history.append({"role": "user", "content": content})
self.logger.info(content)
self.history_pool.add(documents=[content], ids=[str(time.time())])
def get(self):
init = self.initial_message
try:
with git_lock:
f = open(f"files/todo_{self.name}.txt", 'r')
todo_list = f.read()
f.close()
except FileNotFoundError:
todo_list = ''
if todo_list and todo_list != '':
init+=f"\n\nHere is your current todo list: {todo_list}"
try:
with git_lock:
f = open(f"files/status_{self.name}.txt", 'r')
task_status = f.read()
f.close()
except FileNotFoundError:
task_status = ''
if task_status and task_status != '':
init+=f"\n\nHere is your current task status(what you have done): {task_status}\nYou can use 'change_task_status' function to update it."
else:
init+=f"\n\nYou do not have a task status yet. You can use 'change_task_status' function to update it."
if self.subordinates:
init+="\n\nYou have added these subordinates. Do not add them again:\n"
for i in self.subordinates:
init+=f"{i}"
if self.subordinates[i]!="":
init+=f": {self.subordinates[i]}"
init+=",\n"
init+="\nYou can talk to them by using the function called talk."
if self.history and self.history[-1]['content']:
relevant_history = self.history_pool.query(query_texts=self.history[-1]['content'], n_results=1)
if relevant_history:
init+=f"\n\nHere is a relevant memory: \n{relevant_history['documents'][0][0]}\nBelow is the recent dialogue."
memory = [{"role": "system", "content": init}]
for i in self.history[-config.MAX_MEMORY:]:
memory.append(i)
return memory
def initialize_logger(self, name):
self.logger = logging.getLogger(name)
# Create a file handler for logging
file_handler = logging.FileHandler(f"logs/{name}.log", encoding='utf-8')
file_handler.setLevel(logging.INFO)
# Create a console handler for logging
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
# Create a formatter that includes the time
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# Add the handlers to the logger
self.logger.addHandler(file_handler)
# self.logger.addHandler(console_handler)
# Set the logger level
self.logger.setLevel(logging.INFO)
class Agent(Memory):
def __init__(self, name, initial_message):
super().__init__(name, initial_message)
self.message_queue = []
self.state = "idle"
self.lock = threading.Lock()
self.package = None
# self.logger.info(f"Agent {name} initialized with initial message: {initial_message}")
def enqueue(self, speaker, message):
with self.lock:
self.message_queue.append({"role": speaker, "content": message})
if self.state == "idle":
threading.Thread(target=self.run, args=()).start()
def execute(self, tool_name, tool_info, arguments):
tool_name = tool_name.lower()
tool_info['name'] = tool_name
try:
if tool_name == 'exec_python_file':
filename = arguments['filename']
try:
result, self.package = start_interactive_subprocess(filename)
except Exception as e:
result = f"Error: {e}"
tool_info['content'] = str(result)
# self.logger.info(f"{filename}\n---Result---\n{result}")
elif tool_name == 'input':
content = arguments['content']
if self.package:
try:
result, self.package = send_input(content,self.package)
except Exception as e:
result = f"Error: {e}"
else:
result = "Error: No process to input."
tool_info['content'] = str(result)
# self.logger.info(f"Input:\n{content}\n---Result---\n{result}")
elif tool_name == 'read_file':
tool_info['name'] = 'read_file'
filename = arguments['filename']
content, hashvalue = read_file(filename)
result = f"{filename}\n---Content---\n{content}\n---base_commit_hash---\n{hashvalue}"
tool_info['content'] = result
# self.logger.info(result)
elif tool_name == 'change_task_status':
old_content, hashvalue = read_file(f"todo_{self.name}.txt")
write_file(f"todo_{self.name}.txt", arguments['todo'], True, hashvalue, agent_name=self.name)
old_content, hashvalue = read_file(f"status_{self.name}.txt")
write_file(f"status_{self.name}.txt", arguments['done'], True, hashvalue, agent_name=self.name)
tool_info['content'] = f"Success."
# self.logger.info(f"Change todo list to: {arguments['todo']}\nChange task status to: {arguments['done']}")
elif tool_name == 'write_file':
tool_info['name'] = 'write_file'
filename = arguments['filename']
content = arguments['content']
if 'overwrite' in arguments:
overwrite = arguments['overwrite']
base_commit_hash = arguments['base_commit_hash'] if 'base_commit_hash' in arguments else None
result = write_file(filename, content, overwrite, base_commit_hash, agent_name=self.name)
else:
result = write_file(filename, content, agent_name=self.name)
tool_info['content'] = result
self.logger.info(f"{filename}\n---Content---\n{content}\n---Result---\n{result}")
elif tool_name == 'add_agent':
if len(self.subordinates) > config.MAX_SUBORDINATES:
tool_info['name'] = 'add_agent'
result = tool_info['content'] = f'Error: You have already recruited {config.MAX_SUBORDINATES} agents. No more agents can be recruited.\nRecruited agents: {self.get_subordinates()}'
else:
tool_info['name'] = 'add_agent'
tool_info['content'] = 'Success.'
agent_name = arguments['name']
if agent_name in used_names:
# tool_info['content'] = f"Error: {agent_name} already exists. Please use another name."
# logging.error(f"Error: {agent_name} already exists. Please use another name.")
agent_name = agent_name + '_' + str(time.time())[-5:]
tool_info['content'] = f"Warning: {arguments['name']} already exists. Automatically change to {agent_name}.\nSuccess."
self.add_subordinate(agent_name,arguments['description'], arguments['initial_prompt'])
self.logger.info(f"Add agent: {agent_name} success, with description: {arguments['description']}, and initial_prompt: {arguments['initial_prompt']}")
elif tool_name == 'talk':
pattern = re.compile(r'<talk goal=(?:"([^"]+)"|\'([^\']+)\')>(.*?)</talk>', re.IGNORECASE | re.DOTALL)
matches = re.findall(pattern, arguments['messages'])
tool_info['name'] = 'talk'
result = arguments['messages']
if matches:
tool_info['content'] = 'Successfully sent. You may terminate now to wait for the response, or complete the rest of your TODO list first.'
else:
tool_info['content'] = 'Error: No matched <talk goal="Name"></talk> found in your response. You must talk in this specific XML format.'
for match in matches:
name = match[0] if match[0] else match[1]
content = match[2].strip()
if name in agent_dict:
agent_dict[name].enqueue("user", f"{self.name} : {content}")
else:
raise ValueError(f"Error: {name} is not a valid agent name")
elif tool_name == 'terminate':
try:
with git_lock:
f = open(f"files/todo_{self.name}.txt", 'r')
content = f.read()
f.close()
except FileNotFoundError:
content = ''
# if content != None and content != '':
# tool_info['content'] = f"Error: You have not finished your TODO list. That is:\n\n{content}\n\n Please finish it and clear it by calling 'change_task_status' function."
# else:
return {}
else:
raise ValueError(f"Error: {tool_name} is not a valid function name")
return tool_info
except Exception as e:
self.logger.error(e)
return {"role": "user", "content": "Error:"+str(e)}
def run(self):
if(self.state!="idle"):
return
self.state = "running"
while self.message_queue:
previous_memory = self.get()
with self.lock:
new_message = self.message_queue
self.message_queue = []
for i in new_message:
self.add_memory(i)
req = previous_memory + new_message
round = 0
self.package = None
assistant_output = None
while round < config.MAX_ROUNDS:
response = get_llm_response(req, agent_name=self.name)
assistant_output = response['choices'][0]['message']
llm_output = assistant_output['content']
self.add_memory(assistant_output)
req = self.get()
if llm_output != None:
self.logger.info(f"Assistant: {llm_output}")
if 'function_call' not in assistant_output:
self.add_dialogue("user", "Error: No function call found in the response. You must use function calls to work and communicate with other agents. If you have nothing to do now, please call 'terminate' function.")
req = self.get()
round += 1
else:
break
round = 0
while round < config.MAX_ROUNDS:
tool_call = assistant_output['function_call']
tool_name = tool_call['name']
arguments = json.loads(tool_call['arguments'])
tool_info = self.execute(tool_name, {"role": "function"}, arguments)
if tool_info == {}:
break
self.add_memory(tool_info)
req += [tool_info]
round += 1
response = get_llm_response(req, agent_name=self.name)
assistant_output = response['choices'][0]['message']
llm_output = assistant_output['content']
self.add_memory(assistant_output)
req += [assistant_output]
while 'function_call' not in assistant_output:
req += [{"role":"user", "content": "Error: No function call found in the response. You must use function calls to work and communicate with other agents. If you have nothing to do now, please call 'terminate' function."}]
response = get_llm_response(req, agent_name=self.name)
assistant_output = response['choices'][0]['message']
llm_output = assistant_output['content']
self.add_memory(assistant_output)
round += 1
self.state = "idle"
agent_dict = {}