mokrane25 commited on
Commit
a79f03b
·
verified ·
1 Parent(s): 85cb211

Update components/code_generator_loop/mcp_brainstormer.py

Browse files
components/code_generator_loop/mcp_brainstormer.py CHANGED
@@ -78,28 +78,16 @@ Do not include any explanatory text, comments, or formatting outside valid JSON.
78
  task: str,
79
  tools_list: str,
80
  retries: int = 3,
81
- ) -> Union[List[Dict[str, Any]], Dict[str, str]]:
82
  # 1) Validate inputs
83
  if not task.strip():
84
- return {"error": "User task must not be empty."}
85
  raw = tools_list.strip()
86
  if not raw:
87
- return {"error": "Please provide a list of existing tools (or 'none')."}
88
-
89
- # 2) Normalize the tools_list so that each tool is on its own line
90
- # (Assumes comma‐ or newline‐separated)
91
- entries = re.split(r"[\n,]+", raw)
92
- normalized = "\n".join(f"- {e.strip()}" for e in entries if e.strip())
93
-
94
- # 3) Build the prompt
95
- prompt_text = self.mcp_tools_spec_prompt.format(
96
- task=task,
97
- tools_list=normalized,
98
- example_tool=self.example_tool
99
- )
100
 
101
- # 4) Call the LLM with retry logic
102
- last_error = None
103
  for _ in range(retries):
104
  try:
105
  llm = Anthropic(model=self.model_name, temperature=0.0, max_tokens=512, timeout=30)
@@ -111,16 +99,15 @@ Do not include any explanatory text, comments, or formatting outside valid JSON.
111
  try:
112
  specs = json.loads(response_text)
113
  except json.JSONDecodeError:
114
- # This is a fallback: find the first top‐level JSON array
115
  specs = self._extract_json_array(response_text)
116
 
117
  if isinstance(specs, list):
118
  return specs
119
 
120
  if last_error:
121
- return {"error": f"Failed to call LLM: {last_error}"}
122
  else:
123
- return {"error": "Could not extract a valid JSON array from the LLM output."}
124
 
125
  def create_interface(self):
126
  return gr.Interface(
 
78
  task: str,
79
  tools_list: str,
80
  retries: int = 3,
81
+ ) -> List[Dict[str, Any]]:
82
  # 1) Validate inputs
83
  if not task.strip():
84
+ return [{"error": "User task must not be empty."}]
85
  raw = tools_list.strip()
86
  if not raw:
87
+ return [{"error": "Please provide a list of existing tools (or 'none')."}]
88
+
89
+ # ...existing code...
 
 
 
 
 
 
 
 
 
 
90
 
 
 
91
  for _ in range(retries):
92
  try:
93
  llm = Anthropic(model=self.model_name, temperature=0.0, max_tokens=512, timeout=30)
 
99
  try:
100
  specs = json.loads(response_text)
101
  except json.JSONDecodeError:
 
102
  specs = self._extract_json_array(response_text)
103
 
104
  if isinstance(specs, list):
105
  return specs
106
 
107
  if last_error:
108
+ return [{"error": f"Failed to call LLM: {last_error}"}]
109
  else:
110
+ return [{"error": "Could not extract a valid JSON array from the LLM output."}]
111
 
112
  def create_interface(self):
113
  return gr.Interface(