mgbam commited on
Commit
2eda30d
·
verified ·
1 Parent(s): ebbb706

Update core/evolution_engine.py

Browse files
Files changed (1) hide show
  1. core/evolution_engine.py +22 -45
core/evolution_engine.py CHANGED
@@ -1,80 +1,57 @@
1
  # algoforge_prime/core/evolution_engine.py
2
  print("DEBUG: Importing core.evolution_engine")
3
 
4
- # --- Corrected Imports ---
5
- # Absolute imports for modules outside the 'core' package
6
- from prompts.system_prompts import get_system_prompt
7
-
8
- # Absolute imports for other modules within the 'core' package (or relative for siblings)
9
  from core.llm_clients import call_huggingface_api, call_gemini_api, LLMResponse
10
-
11
- # Relative import for a sibling module within the 'core' package
12
- # from .safe_executor import ExecutionResult # Not directly used in this module, but evaluation_output_obj might contain it
13
- # from .evaluation_engine import EvaluationResultOutput # For type hinting the parameter
14
 
15
  print("DEBUG: core.evolution_engine - Imports successful")
16
 
17
  def evolve_solution(
18
  original_solution_text: str,
19
- evaluation_output_obj, # This object comes from evaluation_engine and contains EvaluationResultOutput
20
- # It will have a .get_display_critique() method and .combined_score attribute
21
  problem_description: str,
22
  problem_type: str,
23
- llm_client_config: dict # {"type": ..., "model_id": ..., "temp": ..., "max_tokens": ...}
24
- ) -> str: # Returns evolved solution text or an error string
25
- """
26
- Attempts to evolve a solution based on its comprehensive evaluation details.
27
- """
28
  print(f"DEBUG: evolution_engine.py - Evolving solution. Problem type: {problem_type}")
29
- system_p_evolve = get_system_prompt("evolution_general") # problem_type can be used for specialization here too
30
 
31
- # Extract necessary info from the evaluation_output_obj
32
- # This assumes evaluation_output_obj is an instance of EvaluationResultOutput from evaluation_engine.py
33
- # or at least has these attributes/methods.
34
  try:
 
35
  critique_and_test_feedback = evaluation_output_obj.get_display_critique()
36
  original_score = evaluation_output_obj.combined_score
37
- except AttributeError as e:
38
- print(f"ERROR: evolution_engine.py - evaluation_output_obj is missing expected attributes/methods: {e}")
39
- # Fallback if the object structure is not as expected
40
- critique_and_test_feedback = "Critique data was not in the expected format."
41
- original_score = 0 # Assign a neutral score if real one can't be found
42
 
43
  user_p_evolve = (
44
  f"Original Problem Context: \"{problem_description}\"\n\n"
45
  f"The solution to be evolved achieved a combined score of {original_score}/10.\n"
46
  f"Here is the original solution text:\n```python\n{original_solution_text}\n```\n\n"
47
- f"Here is the comprehensive evaluation it received (including LLM critique and automated test feedback if run):\n'''\n{critique_and_test_feedback}\n'''\n\n"
48
  f"Your Task: Based on ALL the information above (solution, LLM critique, and crucially any test execution results/errors mentioned in the evaluation), "
49
  f"evolve the provided solution to make it demonstrably superior. "
50
- f"Prioritize fixing any reported execution errors or failed tests. "
51
  f"Then, address other critique points like efficiency, clarity, or completeness. "
52
- f"Output the *complete evolved solution*. "
53
- f"Follow this with a brief explanation of the key changes and improvements you implemented, especially how you addressed test failures or execution issues."
54
  )
55
 
56
- llm_response_obj = None # type: LLMResponse
57
  if llm_client_config["type"] == "hf":
58
- llm_response_obj = call_huggingface_api(
59
- user_p_evolve, llm_client_config["model_id"],
60
- temperature=llm_client_config["temp"], max_new_tokens=llm_client_config["max_tokens"],
61
- system_prompt_text=system_p_evolve
62
- )
63
  elif llm_client_config["type"] == "google_gemini":
64
- llm_response_obj = call_gemini_api(
65
- user_p_evolve, llm_client_config["model_id"],
66
- temperature=llm_client_config["temp"], max_new_tokens=llm_client_config["max_tokens"],
67
- system_prompt_text=system_p_evolve
68
- )
69
  else:
70
- error_msg = f"ERROR (Evolution): Unknown LLM client type '{llm_client_config['type']}'"
71
- print(f"ERROR: evolution_engine.py - {error_msg}")
72
- return error_msg
73
 
74
  if llm_response_obj.success:
75
- return llm_response_obj.text
 
 
76
  else:
77
- # Error is already logged by call_..._api functions if it's from there
78
  return f"ERROR (Evolution with {llm_response_obj.model_id_used}): {llm_response_obj.error}"
79
 
80
  print("DEBUG: core.evolution_engine - Module fully defined.")
 
1
  # algoforge_prime/core/evolution_engine.py
2
  print("DEBUG: Importing core.evolution_engine")
3
 
 
 
 
 
 
4
  from core.llm_clients import call_huggingface_api, call_gemini_api, LLMResponse
5
+ from prompts.system_prompts import get_system_prompt
6
+ # from core.evaluation_engine import EvaluationResultOutput # For type hinting if needed
 
 
7
 
8
  print("DEBUG: core.evolution_engine - Imports successful")
9
 
10
  def evolve_solution(
11
  original_solution_text: str,
12
+ evaluation_output_obj, # This is an EvaluationResultOutput object
 
13
  problem_description: str,
14
  problem_type: str,
15
+ llm_client_config: dict
16
+ ) -> str:
 
 
 
17
  print(f"DEBUG: evolution_engine.py - Evolving solution. Problem type: {problem_type}")
18
+ system_p_evolve = get_system_prompt("evolution_general")
19
 
 
 
 
20
  try:
21
+ # Use the method from EvaluationResultOutput to get formatted critique and test results
22
  critique_and_test_feedback = evaluation_output_obj.get_display_critique()
23
  original_score = evaluation_output_obj.combined_score
24
+ except AttributeError: # Fallback if evaluation_output_obj is not as expected
25
+ critique_and_test_feedback = "Detailed evaluation feedback was not available or malformed."
26
+ original_score = 0 # Or try to get it from evaluation_output_obj if it's just a simple dict
27
+ if hasattr(evaluation_output_obj, 'score'): original_score = evaluation_output_obj.score
28
+ elif isinstance(evaluation_output_obj, dict) and 'score' in evaluation_output_obj: original_score = evaluation_output_obj['score']
29
 
30
  user_p_evolve = (
31
  f"Original Problem Context: \"{problem_description}\"\n\n"
32
  f"The solution to be evolved achieved a combined score of {original_score}/10.\n"
33
  f"Here is the original solution text:\n```python\n{original_solution_text}\n```\n\n"
34
+ f"Here is the comprehensive evaluation it received (including LLM critique AND automated test feedback/errors if run):\n'''\n{critique_and_test_feedback}\n'''\n\n"
35
  f"Your Task: Based on ALL the information above (solution, LLM critique, and crucially any test execution results/errors mentioned in the evaluation), "
36
  f"evolve the provided solution to make it demonstrably superior. "
37
+ f"**Your HIGHEST PRIORITY is to fix any reported execution errors or failed tests.** "
38
  f"Then, address other critique points like efficiency, clarity, or completeness. "
39
+ f"Output ONLY the *complete, raw, evolved Python code block*. Do not include explanations outside the code block unless explicitly part of the solution's comments."
 
40
  )
41
 
42
+ llm_response_obj = None
43
  if llm_client_config["type"] == "hf":
44
+ llm_response_obj = call_huggingface_api(user_p_evolve, llm_client_config["model_id"], llm_client_config["temp"], llm_client_config["max_tokens"], system_p_evolve)
 
 
 
 
45
  elif llm_client_config["type"] == "google_gemini":
46
+ llm_response_obj = call_gemini_api(user_p_evolve, llm_client_config["model_id"], llm_client_config["temp"], llm_client_config["max_tokens"], system_p_evolve)
 
 
 
 
47
  else:
48
+ return f"ERROR (Evolution): Unknown LLM client type '{llm_client_config['type']}'"
 
 
49
 
50
  if llm_response_obj.success:
51
+ # Optional: basic cleanup of the LLM output if it tends to add markdown
52
+ from core.utils import basic_text_cleanup # Assuming you have this
53
+ return basic_text_cleanup(llm_response_obj.text)
54
  else:
 
55
  return f"ERROR (Evolution with {llm_response_obj.model_id_used}): {llm_response_obj.error}"
56
 
57
  print("DEBUG: core.evolution_engine - Module fully defined.")