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

Method process_command

mlesolver.py:329–405  ·  view source on GitHub ↗

Take command from language model and execute if valid @param model_resp: (str) language model output @return: (tuple) tuple containing the following items - cmd_str: (str) code execution return and success flag - code_lines: (list) list of code lines

(self, model_resp)

Source from the content-addressed store, hash-verified

327 return query_model(prompt="Please reflect on ideas for how to improve your current code. Examine the provided code and think very specifically (with precise ideas) on how to improve performance, which methods to use, how to improve generalization on the test set with line-by-line examples below:\n", system_prompt=syst, model_str=f"{self.llm_str}", openai_api_key=self.openai_api_key)
328
329 def process_command(self, model_resp):
330 """
331 Take command from language model and execute if valid
332 @param model_resp: (str) language model output
333 @return: (tuple) tuple containing the following items
334 - cmd_str: (str) code execution return and success flag
335 - code_lines: (list) list of code lines as strings
336 - prev_code_ret: (str) output from running code
337 - should_execute_code: (bool) did the code change, if so we need to re-execute it
338 - score: (float) score of model
339 """
340 prev_code_ret = self.prev_code_ret
341 should_execute_code = self.should_execute_code
342 code_lines = copy(self.code_lines)
343 remove_figures()
344 for cmd in self.commands:
345 if cmd.matches_command(model_resp):
346 # attempt to execute the code edit command
347 if cmd.cmd_type == "CODE-edit":
348 score = None
349 failed = True
350 code_err = str()
351 for _tries in range(GLOBAL_REPAIR_ATTEMPTS):
352 success, args = cmd.parse_command(model_resp, copy(self.code_lines), self.dataset_code)
353 if success:
354 cmd_return = cmd.execute_command(args)
355 code_err = f"Return from executing code: {cmd_return[2]}"
356 if cmd_return[0]: # if success
357 code_lines = copy(cmd_return[1])
358 score, cmd_str, is_valid = get_score(self.plan, "\n".join(code_lines), cmd_return[2], openai_api_key=self.openai_api_key, REWARD_MODEL_LLM=self.llm_str)
359 if is_valid:
360 failed = False
361 break
362 code_err += f"\nReturn from executing code on real test set {cmd_str}"
363 repaired_code = code_repair(model_resp, code_err, REPAIR_LLM=self.llm_str, ctype="edit", openai_api_key=self.openai_api_key)
364 model_resp = repaired_code
365 if not self.supress_print: print(f" * Attempting repair // try {_tries}*")
366 if failed:
367 cmd_str = f"Code editing FAILED due to the following error: {code_err}. Code was reverted back to original state before edits."
368 if not self.supress_print: print("$$$$ CODE EDIT (failed)")
369 else:
370 cmd_str = "Code was successfully edited."
371 prev_code_ret = copy(cmd_return[2])
372 if not self.supress_print: print("$$$$ CODE EDIT (success)")
373 should_execute_code = True
374 return cmd_str, code_lines, prev_code_ret, should_execute_code, score
375 # attempt to execute the code replace command
376 elif cmd.cmd_type == "CODE-replace": # DONE
377 score = None
378 failed = True
379 code_err = str()
380 for _tries in range(GLOBAL_REPAIR_ATTEMPTS):
381 success, args = cmd.parse_command(model_resp, self.dataset_code)
382 code_err = f"Return from executing code: {args[1]}"
383 if success:
384 code_lines = copy(args[0])
385 score, cmd_str, is_valid = get_score(self.plan, "\n".join(code_lines), args[1], openai_api_key=self.openai_api_key, REWARD_MODEL_LLM=self.llm_str)
386 if is_valid:

Callers 2

gen_initial_codeMethod · 0.95
solveMethod · 0.95

Calls 7

remove_figuresFunction · 0.85
code_repairFunction · 0.85
extract_promptFunction · 0.85
get_scoreFunction · 0.70
matches_commandMethod · 0.45
parse_commandMethod · 0.45
execute_commandMethod · 0.45

Tested by

no test coverage detected