youssefleb commited on
Commit
b72b21b
·
verified ·
1 Parent(s): 8a20ef7

Update mcp_servers.py

Browse files
Files changed (1) hide show
  1. mcp_servers.py +49 -19
mcp_servers.py CHANGED
@@ -1,6 +1,7 @@
1
- # mcp_servers.py (Corrected for TypeError)
2
  import asyncio
3
  import json
 
4
  from typing import Dict, Optional, Tuple, List, Any
5
  from personas import PERSONAS_DATA
6
  import google.generativeai as genai
@@ -11,9 +12,36 @@ from utils import load_prompt
11
 
12
  EVALUATION_PROMPT_TEMPLATE = load_prompt(config.PROMPT_FILES["evaluator"])
13
 
14
- class BusinessSolutionEvaluator:
15
- """Implements the "LLM-as-a-Judge"."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
 
 
 
 
 
 
17
  def __init__(self, gemini_client: Optional[genai.GenerativeModel]):
18
  if not gemini_client:
19
  raise ValueError("BusinessSolutionEvaluator requires a Google/Gemini client.")
@@ -24,33 +52,36 @@ class BusinessSolutionEvaluator:
24
  async def evaluate(self, problem: str, solution_text: str) -> dict:
25
  print(f"Evaluating solution (live): {solution_text[:50]}...")
26
  prompt = EVALUATION_PROMPT_TEMPLATE.format(problem=problem, solution_text=solution_text)
 
27
  try:
28
  response = await self.gemini_model.generate_content_async(
29
  prompt,
30
  generation_config=genai.types.GenerationConfig(
31
- response_mime_type="application/json"
32
- # --- FIX: REMOVED a line here ---
33
- # model=config.MODELS["Gemini"]["judge"] <-- This was the bug
34
  )
35
  )
36
- json_text = response.text.strip().replace("```json", "").replace("```", "")
37
- if not json_text.startswith("{"):
38
- raise Exception(f"API returned non-JSON text: {json_text}")
39
- v_fitness = json.loads(json_text)
40
  print(f"Evaluation complete (live): {v_fitness}")
41
  return v_fitness
 
42
  except Exception as e:
43
- print(f"ERROR: BusinessSolutionEvaluator failed to parse JSON or call API: {e}")
 
 
44
  return {
45
- "Novelty": {"score": 1, "justification": f"Error during evaluation: {e}"},
46
- "Usefulness_Feasibility": {"score": 1, "justification": f"Error during evaluation: {e}"},
47
- "Flexibility": {"score": 1, "justification": f"Error during evaluation: {e}"},
48
- "Elaboration": {"score": 1, "justification": f"Error during evaluation: {e}"},
49
- "Cultural_Appropriateness": {"score": 1, "justification": f"Error during evaluation: {e}"}
50
  }
51
 
 
52
  class AgentCalibrator:
53
- # (This class is unchanged)
54
  def __init__(self, api_clients: dict, evaluator: BusinessSolutionEvaluator):
55
  self.evaluator = evaluator
56
  self.api_clients = {name: client for name, client in api_clients.items() if client}
@@ -136,8 +167,7 @@ async def get_llm_response(client_name: str, client, system_prompt: str, user_pr
136
  ]
137
  response = await model.generate_content_async(full_prompt,
138
  generation_config=genai.types.GenerationConfig(
139
- # --- FIX: REMOVED a line here ---
140
- # model=config.MODELS["Gemini"]["default"] <-- This was the bug
141
  ))
142
  return response.text
143
 
 
1
+ # mcp_servers.py (Robust JSON Parsing Update)
2
  import asyncio
3
  import json
4
+ import re # <-- 1. Import regex module
5
  from typing import Dict, Optional, Tuple, List, Any
6
  from personas import PERSONAS_DATA
7
  import google.generativeai as genai
 
12
 
13
  EVALUATION_PROMPT_TEMPLATE = load_prompt(config.PROMPT_FILES["evaluator"])
14
 
15
+ # --- 2. New Helper Function ---
16
+ def extract_json(text: str) -> dict:
17
+ """
18
+ Robustly extracts a JSON object from a string, ignoring markdown or text headers.
19
+ """
20
+ try:
21
+ # First, try standard cleaning (removing markdown code blocks)
22
+ clean_text = text.strip()
23
+ if "```json" in clean_text:
24
+ clean_text = clean_text.split("```json")[1].split("```")[0].strip()
25
+ elif "```" in clean_text:
26
+ clean_text = clean_text.split("```")[1].split("```")[0].strip()
27
+
28
+ return json.loads(clean_text)
29
+ except json.JSONDecodeError:
30
+ # If that fails, use Regex to find the first '{' and last '}'
31
+ # This handles cases like "Here is the JSON: { ... }"
32
+ try:
33
+ match = re.search(r'(\{.*\})', text, re.DOTALL)
34
+ if match:
35
+ return json.loads(match.group(1))
36
+ except:
37
+ pass
38
 
39
+ # If we still can't find JSON, raise the original text for debugging
40
+ raise ValueError(f"Could not extract JSON from response: {text[:100]}...")
41
+
42
+
43
+ class BusinessSolutionEvaluator:
44
+ # (Init is unchanged)
45
  def __init__(self, gemini_client: Optional[genai.GenerativeModel]):
46
  if not gemini_client:
47
  raise ValueError("BusinessSolutionEvaluator requires a Google/Gemini client.")
 
52
  async def evaluate(self, problem: str, solution_text: str) -> dict:
53
  print(f"Evaluating solution (live): {solution_text[:50]}...")
54
  prompt = EVALUATION_PROMPT_TEMPLATE.format(problem=problem, solution_text=solution_text)
55
+
56
  try:
57
  response = await self.gemini_model.generate_content_async(
58
  prompt,
59
  generation_config=genai.types.GenerationConfig(
60
+ response_mime_type="application/json",
61
+ model=config.MODELS["Gemini"]["judge"]
 
62
  )
63
  )
64
+
65
+ # --- 3. Use the new robust extractor ---
66
+ v_fitness = extract_json(response.text)
67
+
68
  print(f"Evaluation complete (live): {v_fitness}")
69
  return v_fitness
70
+
71
  except Exception as e:
72
+ print(f"ERROR: BusinessSolutionEvaluator failed: {e}")
73
+ # We update the fallback justification so you can see the error in the UI logs
74
+ error_msg = str(e).replace("\n", " ")[:100]
75
  return {
76
+ "Novelty": {"score": 1, "justification": f"System Error: {error_msg}"},
77
+ "Usefulness_Feasibility": {"score": 1, "justification": "System Error"},
78
+ "Flexibility": {"score": 1, "justification": "System Error"},
79
+ "Elaboration": {"score": 1, "justification": "System Error"},
80
+ "Cultural_Appropriateness": {"score": 1, "justification": "System Error"}
81
  }
82
 
83
+ # (Rest of the file: AgentCalibrator, get_llm_response remains UNCHANGED)
84
  class AgentCalibrator:
 
85
  def __init__(self, api_clients: dict, evaluator: BusinessSolutionEvaluator):
86
  self.evaluator = evaluator
87
  self.api_clients = {name: client for name, client in api_clients.items() if client}
 
167
  ]
168
  response = await model.generate_content_async(full_prompt,
169
  generation_config=genai.types.GenerationConfig(
170
+ # model parameter removed here as it's set on client init
 
171
  ))
172
  return response.text
173