Spaces:
Sleeping
Sleeping
Update core/evolution_engine.py
Browse files- 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 |
-
#
|
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
|
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
|
24 |
-
) -> str:
|
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")
|
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
|
38 |
-
|
39 |
-
#
|
40 |
-
|
41 |
-
|
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
|
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"
|
51 |
f"Then, address other critique points like efficiency, clarity, or completeness. "
|
52 |
-
f"Output the *complete evolved
|
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
|
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 |
-
|
71 |
-
print(f"ERROR: evolution_engine.py - {error_msg}")
|
72 |
-
return error_msg
|
73 |
|
74 |
if llm_response_obj.success:
|
75 |
-
|
|
|
|
|
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.")
|