MCPcopy Create free account
hub / github.com/SamuelSchmidgall/AgentLaboratory / solve

Method solve

mlesolver.py:276–317  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

274 return code_lines, prev_code_ret, score
275
276 def solve(self):
277 num_attempts = 0
278 best_pkg = None
279 top_score = None
280 self.prev_code_ret = None
281 self.should_execute_code = False
282 while True:
283 if len(self.commands) == 2: cmd_app_str = "You must output either the ```EDIT or ```REPLACE command immediately. "
284 else: cmd_app_str = ""
285 model_resp = query_model(
286 openai_api_key=self.openai_api_key,
287 model_str=self.model,
288 system_prompt=self.system_prompt(),
289 prompt=f"The following is your history:{self.history_str()}\n\n{cmd_app_str}Now please enter a command: ", temp=1.0)
290 model_resp = self.clean_text(model_resp)
291 self.code_lines = copy(random.choice(self.best_codes)[0])
292 cmd_str, code_lines, prev_code_ret, should_execute_code, score = self.process_command(model_resp)
293 self.st_history.append([model_resp, prev_code_ret, code_lines, cmd_str])
294 if len(self.st_history) > self.st_hist_len: self.st_history.pop(0)
295 if score is not None:
296 if top_score is None:
297 best_pkg = copy(code_lines), copy(prev_code_ret), copy(should_execute_code), copy(model_resp), copy(cmd_str)
298 top_score = score
299 elif score > top_score:
300 best_pkg = copy(code_lines), copy(prev_code_ret), copy(should_execute_code), copy(model_resp), copy(cmd_str)
301 top_score = score
302 if not self.supress_print: print(f"@@@ Command Exec // Attempt {num_attempts}: ", str(cmd_str).replace("\n", " | "))
303 if not self.supress_print: print(f"$$$ Score: {score}")
304 if num_attempts >= self.min_gen_trials and top_score is not None: break
305 num_attempts += 1
306 self.code_lines, self.prev_code_ret, self.should_execute_code, model_resp, cmd_str = best_pkg
307 if not self.supress_print: print(prev_code_ret)
308 # add top scoring code that was successful to the best codes
309 if top_score > self.best_codes[-1][1]:
310 # replace the lowest scoring one
311 if len(self.best_codes) >= self.max_codes:
312 self.best_codes.pop(-1)
313 self.code_reflect = self.reflect_code()
314 self.best_codes.append((copy(self.code_lines), copy(top_score), self.prev_code_ret))
315 # sort by score, to make sure lowest are removed in future
316 self.best_codes.sort(key=lambda x: x[1], reverse=True)
317 return model_resp, cmd_str
318
319 def reflect_code(self):
320 """

Callers 1

running_experimentsMethod · 0.95

Calls 6

system_promptMethod · 0.95
history_strMethod · 0.95
clean_textMethod · 0.95
process_commandMethod · 0.95
reflect_codeMethod · 0.95
query_modelFunction · 0.85

Tested by

no test coverage detected