youssefleb commited on
Commit
1455c2a
·
verified ·
1 Parent(s): e364ce0

Update agent_logic.py

Browse files
Files changed (1) hide show
  1. agent_logic.py +4 -10
agent_logic.py CHANGED
@@ -1,4 +1,4 @@
1
- # agent_logic.py (FINAL, Robust Version)
2
  import asyncio
3
  from typing import AsyncGenerator, Dict, Optional
4
  import json
@@ -6,14 +6,13 @@ import os
6
  import google.generativeai as genai
7
  from anthropic import AsyncAnthropic
8
  from openai import AsyncOpenAI
9
- import re # <-- Added: For score parsing fix
10
  from personas import PERSONAS_DATA
11
  import config
12
  from utils import load_prompt
13
  from mcp_servers import AgentCalibrator, BusinessSolutionEvaluator, get_llm_response
14
  from self_correction import SelfCorrector
15
 
16
- # (Configuration and Manager Prompts are loaded here)
17
  CLASSIFIER_SYSTEM_PROMPT = load_prompt(config.PROMPT_FILES["classifier"])
18
  HOMOGENEOUS_MANAGER_PROMPT = load_prompt(config.PROMPT_FILES["manager_homogeneous"])
19
  HETEROGENEOUS_MANAGER_PROMPT = load_prompt(config.PROMPT_FILES["manager_heterogeneous"])
@@ -170,17 +169,15 @@ class StrategicSelectorAgent:
170
  normalized_fitness = {}
171
  if isinstance(v_fitness_json, dict):
172
  for k, v in v_fitness_json.items():
 
173
  if isinstance(v, dict):
174
- # Standard format: {"score": 4, "justification": "..."}
175
  score_value = v.get('score')
176
  justification_value = v.get('justification', str(v))
177
  elif isinstance(v, list) and len(v) > 0 and isinstance(v[0], dict):
178
- # Handles list structure: [{"score": 4, "justification": "..."}]
179
  score_value = v[0].get('score')
180
  justification_value = v[0].get('justification', str(v[0]))
181
  else:
182
- # Fallback for unexpected structure
183
- score_value = 0
184
  justification_value = str(v)
185
 
186
  # FIX: Extract the integer score from the string (e.g., "4/5" -> 4)
@@ -190,7 +187,6 @@ class StrategicSelectorAgent:
190
  except:
191
  score_value = 0
192
 
193
- # Ensure score is an integer
194
  try:
195
  score_value = int(score_value)
196
  except (ValueError, TypeError):
@@ -199,11 +195,9 @@ class StrategicSelectorAgent:
199
  normalized_fitness[k] = {'score': score_value, 'justification': justification_value}
200
 
201
  else:
202
- # Fallback if the whole thing isn't a dict
203
  normalized_fitness = {k: {'score': 0, 'justification': "Invalid JSON structure"} for k in ["Novelty", "Usefulness_Feasibility", "Flexibility", "Elaboration", "Cultural_Appropriateness"]}
204
 
205
  v_fitness_json = normalized_fitness
206
- # ----------------------------------------------------
207
 
208
  scores = {k: v.get('score', 0) for k, v in v_fitness_json.items()}
209
  yield f"Evaluation Score: {scores}"
 
1
+ # agent_logic.py (Milestone 5 - FINAL & ROBUST)
2
  import asyncio
3
  from typing import AsyncGenerator, Dict, Optional
4
  import json
 
6
  import google.generativeai as genai
7
  from anthropic import AsyncAnthropic
8
  from openai import AsyncOpenAI
9
+ import re
10
  from personas import PERSONAS_DATA
11
  import config
12
  from utils import load_prompt
13
  from mcp_servers import AgentCalibrator, BusinessSolutionEvaluator, get_llm_response
14
  from self_correction import SelfCorrector
15
 
 
16
  CLASSIFIER_SYSTEM_PROMPT = load_prompt(config.PROMPT_FILES["classifier"])
17
  HOMOGENEOUS_MANAGER_PROMPT = load_prompt(config.PROMPT_FILES["manager_homogeneous"])
18
  HETEROGENEOUS_MANAGER_PROMPT = load_prompt(config.PROMPT_FILES["manager_heterogeneous"])
 
169
  normalized_fitness = {}
170
  if isinstance(v_fitness_json, dict):
171
  for k, v in v_fitness_json.items():
172
+ # Determine score value (safe check for list wrapping, which causes the crash)
173
  if isinstance(v, dict):
 
174
  score_value = v.get('score')
175
  justification_value = v.get('justification', str(v))
176
  elif isinstance(v, list) and len(v) > 0 and isinstance(v[0], dict):
 
177
  score_value = v[0].get('score')
178
  justification_value = v[0].get('justification', str(v[0]))
179
  else:
180
+ score_value = v.get('score', 0) if isinstance(v, dict) else 0 # Fallback check
 
181
  justification_value = str(v)
182
 
183
  # FIX: Extract the integer score from the string (e.g., "4/5" -> 4)
 
187
  except:
188
  score_value = 0
189
 
 
190
  try:
191
  score_value = int(score_value)
192
  except (ValueError, TypeError):
 
195
  normalized_fitness[k] = {'score': score_value, 'justification': justification_value}
196
 
197
  else:
 
198
  normalized_fitness = {k: {'score': 0, 'justification': "Invalid JSON structure"} for k in ["Novelty", "Usefulness_Feasibility", "Flexibility", "Elaboration", "Cultural_Appropriateness"]}
199
 
200
  v_fitness_json = normalized_fitness
 
201
 
202
  scores = {k: v.get('score', 0) for k, v in v_fitness_json.items()}
203
  yield f"Evaluation Score: {scores}"