praisonaiagents 0.0.121__py3-none-any.whl → 0.0.122__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1274,27 +1274,40 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
1274
1274
  messages.append({"role": "user", "content": reflection_prompt})
1275
1275
 
1276
1276
  try:
1277
- # Check if OpenAI client is available
1278
- if self._openai_client is None:
1279
- # For custom LLMs, self-reflection with structured output is not supported
1280
- if self.verbose:
1281
- display_self_reflection(f"Agent {self.name}: Self-reflection with structured output is not supported for custom LLM providers. Skipping reflection.", console=self.console)
1282
- # Return the original response without reflection
1283
- self.chat_history.append({"role": "user", "content": prompt})
1284
- self.chat_history.append({"role": "assistant", "content": response_text})
1285
- # Only display interaction if not using custom LLM (to avoid double output) and verbose is True
1286
- if self.verbose and not self._using_custom_llm:
1287
- display_interaction(prompt, response_text, markdown=self.markdown, generation_time=time.time() - start_time, console=self.console)
1288
- return response_text
1289
-
1290
- reflection_response = self._openai_client.sync_client.beta.chat.completions.parse(
1291
- model=self.reflect_llm if self.reflect_llm else self.llm,
1292
- messages=messages,
1293
- temperature=temperature,
1294
- response_format=ReflectionOutput
1295
- )
1277
+ # Check if we're using a custom LLM (like Gemini)
1278
+ if self._using_custom_llm or self._openai_client is None:
1279
+ # For custom LLMs, we need to handle reflection differently
1280
+ # Use non-streaming to get complete JSON response
1281
+ reflection_response = self._chat_completion(messages, temperature=temperature, tools=None, stream=False, reasoning_steps=False)
1282
+
1283
+ if not reflection_response or not reflection_response.choices:
1284
+ raise Exception("No response from reflection request")
1285
+
1286
+ reflection_text = reflection_response.choices[0].message.content.strip()
1287
+
1288
+ # Clean the JSON output
1289
+ cleaned_json = self.clean_json_output(reflection_text)
1290
+
1291
+ # Parse the JSON manually
1292
+ reflection_data = json.loads(cleaned_json)
1293
+
1294
+ # Create a reflection output object manually
1295
+ class CustomReflectionOutput:
1296
+ def __init__(self, data):
1297
+ self.reflection = data.get('reflection', '')
1298
+ self.satisfactory = data.get('satisfactory', 'no').lower()
1299
+
1300
+ reflection_output = CustomReflectionOutput(reflection_data)
1301
+ else:
1302
+ # Use OpenAI's structured output for OpenAI models
1303
+ reflection_response = self._openai_client.sync_client.beta.chat.completions.parse(
1304
+ model=self.reflect_llm if self.reflect_llm else self.llm,
1305
+ messages=messages,
1306
+ temperature=temperature,
1307
+ response_format=ReflectionOutput
1308
+ )
1296
1309
 
1297
- reflection_output = reflection_response.choices[0].message.parsed
1310
+ reflection_output = reflection_response.choices[0].message.parsed
1298
1311
 
1299
1312
  if self.verbose:
1300
1313
  display_self_reflection(f"Agent {self.name} self reflection (using {self.reflect_llm if self.reflect_llm else self.llm}): reflection='{reflection_output.reflection}' satisfactory='{reflection_output.satisfactory}'", console=self.console)
@@ -1337,7 +1350,9 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
1337
1350
 
1338
1351
  logging.debug(f"{self.name} reflection count {reflection_count + 1}, continuing reflection process")
1339
1352
  messages.append({"role": "user", "content": "Now regenerate your response using the reflection you made"})
1340
- response = self._chat_completion(messages, temperature=temperature, tools=None, stream=self.stream)
1353
+ # For custom LLMs during reflection, always use non-streaming to ensure complete responses
1354
+ use_stream = self.stream if not self._using_custom_llm else False
1355
+ response = self._chat_completion(messages, temperature=temperature, tools=None, stream=use_stream)
1341
1356
  response_text = response.choices[0].message.content.strip()
1342
1357
  reflection_count += 1
1343
1358
  continue # Continue the loop for more reflections
@@ -214,9 +214,37 @@ Tools: {', '.join(agent_tools)}"""
214
214
 
215
215
  return assigned_tools
216
216
 
217
+ def _validate_config(self, config: AutoAgentsConfig) -> tuple[bool, str]:
218
+ """
219
+ Validate that the configuration has proper TaskConfig objects.
220
+
221
+ Returns:
222
+ Tuple of (is_valid, error_message)
223
+ """
224
+ for agent_idx, agent in enumerate(config.agents):
225
+ if not hasattr(agent, 'tasks') or not agent.tasks:
226
+ return False, f"Agent '{agent.name}' has no tasks defined"
227
+
228
+ for task_idx, task in enumerate(agent.tasks):
229
+ # Check if task is a proper TaskConfig instance
230
+ if not isinstance(task, TaskConfig):
231
+ return False, f"Task at index {task_idx} for agent '{agent.name}' is not a proper TaskConfig object"
232
+
233
+ # Check required fields
234
+ if not task.name:
235
+ return False, f"Task at index {task_idx} for agent '{agent.name}' has no name"
236
+ if not task.description:
237
+ return False, f"Task at index {task_idx} for agent '{agent.name}' has no description"
238
+ if not task.expected_output:
239
+ return False, f"Task at index {task_idx} for agent '{agent.name}' has no expected_output"
240
+ if task.tools is None:
241
+ return False, f"Task at index {task_idx} for agent '{agent.name}' has no tools field"
242
+
243
+ return True, ""
244
+
217
245
  def _generate_config(self) -> AutoAgentsConfig:
218
- """Generate the configuration for agents and tasks"""
219
- prompt = f"""
246
+ """Generate the configuration for agents and tasks with retry logic"""
247
+ base_prompt = f"""
220
248
  Generate a configuration for AI agents to accomplish this task: "{self.instructions}"
221
249
 
222
250
  The configuration should include:
@@ -237,84 +265,164 @@ Requirements:
237
265
  4. The process type should match the task requirements
238
266
  5. Generate maximum {self.max_agents} agents to handle this task efficiently
239
267
 
240
- Return the configuration in a structured JSON format matching the AutoAgentsConfig schema.
268
+ Return the configuration in a structured JSON format matching this exact schema:
269
+ {{
270
+ "main_instruction": "Overall goal description",
271
+ "process_type": "sequential|workflow|hierarchical",
272
+ "agents": [
273
+ {{
274
+ "name": "Agent Name",
275
+ "role": "Agent Role",
276
+ "goal": "Agent Goal",
277
+ "backstory": "Agent Backstory",
278
+ "tools": ["tool1", "tool2"],
279
+ "tasks": [
280
+ {{
281
+ "name": "Task Name",
282
+ "description": "Detailed task description",
283
+ "expected_output": "What the task should produce",
284
+ "tools": ["tool1", "tool2"]
285
+ }}
286
+ ]
287
+ }}
288
+ ]
289
+ }}
290
+
291
+ IMPORTANT: Each task MUST be an object with name, description, expected_output, and tools fields, NOT a simple string.
241
292
  """
242
293
 
243
- try:
244
- # Try to use OpenAI's structured output if available
245
- use_openai_structured = False
246
- client = None
294
+ max_retries = 3
295
+ last_response = None
296
+ last_error = None
297
+
298
+ for attempt in range(max_retries):
299
+ # Prepare prompt for this attempt
300
+ if attempt > 0 and last_response and last_error:
301
+ # On retry, include the previous response and error
302
+ prompt = f"""{base_prompt}
303
+
304
+ PREVIOUS ATTEMPT FAILED!
305
+ Your previous response was:
306
+ ```json
307
+ {last_response}
308
+ ```
309
+
310
+ Error: {last_error}
311
+
312
+ REMEMBER: Tasks MUST be objects with the following structure:
313
+ {{
314
+ "name": "Task Name",
315
+ "description": "Task Description",
316
+ "expected_output": "Expected Output",
317
+ "tools": ["tool1", "tool2"]
318
+ }}
319
+
320
+ DO NOT use strings for tasks. Each task MUST be a complete object with all four fields."""
321
+ else:
322
+ prompt = base_prompt
247
323
 
248
324
  try:
249
- # Check if we have OpenAI API and the model supports structured output
250
- if self.llm and (self.llm.startswith('gpt-') or self.llm.startswith('o1-') or self.llm.startswith('o3-')):
251
- # Create a new client instance if custom parameters are provided
252
- if self.api_key or self.base_url:
253
- client = OpenAIClient(api_key=self.api_key, base_url=self.base_url)
254
- else:
255
- client = get_openai_client()
256
- use_openai_structured = True
257
- except:
258
- # If OpenAI client is not available, we'll use the LLM class
259
- pass
260
-
261
- if use_openai_structured and client:
262
- # Use OpenAI's structured output for OpenAI models (backward compatibility)
263
- config = client.parse_structured_output(
264
- messages=[
265
- {"role": "system", "content": "You are a helpful assistant designed to generate AI agent configurations."},
266
- {"role": "user", "content": prompt}
267
- ],
268
- response_format=AutoAgentsConfig,
269
- model=self.llm
270
- )
271
- else:
272
- # Use LLM class for all other providers (Gemini, Anthropic, etc.)
273
- llm_instance = LLM(
274
- model=self.llm,
275
- base_url=self.base_url,
276
- api_key=self.api_key
277
- )
325
+ # Try to use OpenAI's structured output if available
326
+ use_openai_structured = False
327
+ client = None
278
328
 
279
- response_text = llm_instance.response(
280
- prompt=prompt,
281
- system_prompt="You are a helpful assistant designed to generate AI agent configurations.",
282
- output_pydantic=AutoAgentsConfig,
283
- temperature=0.7,
284
- stream=False,
285
- verbose=False
286
- )
287
-
288
- # Parse the JSON response
289
329
  try:
290
- # First try to parse as is
291
- config_dict = json.loads(response_text)
292
- config = AutoAgentsConfig(**config_dict)
293
- except json.JSONDecodeError:
294
- # If that fails, try to extract JSON from the response
295
- # Handle cases where the model might wrap JSON in markdown blocks
296
- cleaned_response = response_text.strip()
297
- if cleaned_response.startswith("```json"):
298
- cleaned_response = cleaned_response[7:]
299
- if cleaned_response.startswith("```"):
300
- cleaned_response = cleaned_response[3:]
301
- if cleaned_response.endswith("```"):
302
- cleaned_response = cleaned_response[:-3]
303
- cleaned_response = cleaned_response.strip()
330
+ # Check if we have OpenAI API and the model supports structured output
331
+ if self.llm and (self.llm.startswith('gpt-') or self.llm.startswith('o1-') or self.llm.startswith('o3-')):
332
+ # Create a new client instance if custom parameters are provided
333
+ if self.api_key or self.base_url:
334
+ client = OpenAIClient(api_key=self.api_key, base_url=self.base_url)
335
+ else:
336
+ client = get_openai_client()
337
+ use_openai_structured = True
338
+ except:
339
+ # If OpenAI client is not available, we'll use the LLM class
340
+ pass
341
+
342
+ if use_openai_structured and client:
343
+ # Use OpenAI's structured output for OpenAI models (backward compatibility)
344
+ config = client.parse_structured_output(
345
+ messages=[
346
+ {"role": "system", "content": "You are a helpful assistant designed to generate AI agent configurations."},
347
+ {"role": "user", "content": prompt}
348
+ ],
349
+ response_format=AutoAgentsConfig,
350
+ model=self.llm
351
+ )
352
+ # Store the response for potential retry
353
+ last_response = json.dumps(config.model_dump(), indent=2)
354
+ else:
355
+ # Use LLM class for all other providers (Gemini, Anthropic, etc.)
356
+ llm_instance = LLM(
357
+ model=self.llm,
358
+ base_url=self.base_url,
359
+ api_key=self.api_key
360
+ )
304
361
 
305
- config_dict = json.loads(cleaned_response)
306
- config = AutoAgentsConfig(**config_dict)
307
-
308
- # Ensure we have exactly max_agents number of agents
309
- if len(config.agents) > self.max_agents:
310
- config.agents = config.agents[:self.max_agents]
311
- elif len(config.agents) < self.max_agents:
312
- logging.warning(f"Generated {len(config.agents)} agents, expected {self.max_agents}")
313
-
314
- return config
315
- except Exception as e:
316
- logging.error(f"Error generating configuration: {e}")
317
- raise
362
+ response_text = llm_instance.response(
363
+ prompt=prompt,
364
+ system_prompt="You are a helpful assistant designed to generate AI agent configurations.",
365
+ output_pydantic=AutoAgentsConfig,
366
+ temperature=0.7,
367
+ stream=False,
368
+ verbose=False
369
+ )
370
+
371
+ # Store the raw response for potential retry
372
+ last_response = response_text
373
+
374
+ # Parse the JSON response
375
+ try:
376
+ # First try to parse as is
377
+ config_dict = json.loads(response_text)
378
+ config = AutoAgentsConfig(**config_dict)
379
+ except json.JSONDecodeError:
380
+ # If that fails, try to extract JSON from the response
381
+ # Handle cases where the model might wrap JSON in markdown blocks
382
+ cleaned_response = response_text.strip()
383
+ if cleaned_response.startswith("```json"):
384
+ cleaned_response = cleaned_response[7:]
385
+ if cleaned_response.startswith("```"):
386
+ cleaned_response = cleaned_response[3:]
387
+ if cleaned_response.endswith("```"):
388
+ cleaned_response = cleaned_response[:-3]
389
+ cleaned_response = cleaned_response.strip()
390
+
391
+ config_dict = json.loads(cleaned_response)
392
+ config = AutoAgentsConfig(**config_dict)
393
+
394
+ # Validate the configuration
395
+ is_valid, error_msg = self._validate_config(config)
396
+ if not is_valid:
397
+ last_error = error_msg
398
+ if attempt < max_retries - 1:
399
+ logging.warning(f"Configuration validation failed (attempt {attempt + 1}/{max_retries}): {error_msg}")
400
+ continue
401
+ else:
402
+ raise ValueError(f"Configuration validation failed after {max_retries} attempts: {error_msg}")
403
+
404
+ # Ensure we have exactly max_agents number of agents
405
+ if len(config.agents) > self.max_agents:
406
+ config.agents = config.agents[:self.max_agents]
407
+ elif len(config.agents) < self.max_agents:
408
+ logging.warning(f"Generated {len(config.agents)} agents, expected {self.max_agents}")
409
+
410
+ return config
411
+
412
+ except ValueError as e:
413
+ # Re-raise validation errors
414
+ raise
415
+ except Exception as e:
416
+ last_error = str(e)
417
+ if attempt < max_retries - 1:
418
+ logging.warning(f"Error generating configuration (attempt {attempt + 1}/{max_retries}): {e}")
419
+ continue
420
+ else:
421
+ logging.error(f"Error generating configuration after {max_retries} attempts: {e}")
422
+ raise
423
+
424
+ # This should never be reached due to the raise statements above
425
+ raise RuntimeError(f"Failed to generate valid configuration after {max_retries} attempts")
318
426
 
319
427
  def _create_agents_and_tasks(self, config: AutoAgentsConfig) -> Tuple[List[Agent], List[Task]]:
320
428
  """Create agents and tasks from configuration"""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: praisonaiagents
3
- Version: 0.0.121
3
+ Version: 0.0.122
4
4
  Summary: Praison AI agents for completing complex tasks with Self Reflection Agents
5
5
  Author: Mervin Praison
6
6
  Requires-Python: >=3.10
@@ -3,12 +3,12 @@ praisonaiagents/approval.py,sha256=UJ4OhfihpFGR5CAaMphqpSvqdZCHi5w2MGw1MByZ1FQ,9
3
3
  praisonaiagents/main.py,sha256=bamnEu5PaekloGi52VqAFclm-HzjEVeKtWF0Zpdmfzs,15479
4
4
  praisonaiagents/session.py,sha256=d-CZPYikOHb0q-H9f_IWKJsypnQfz1YKeLLkyxs6oDo,15532
5
5
  praisonaiagents/agent/__init__.py,sha256=IhIDtAkfJ99cxbttwou52coih_AejS2-jpazsX6LbDY,350
6
- praisonaiagents/agent/agent.py,sha256=_ROVyOTPBMB5Porv4YvZ4-kKWr4-tGMbSN7V8uDWZgk,109619
6
+ praisonaiagents/agent/agent.py,sha256=BZx0iCP4hHfKFlfGlkZtanBJDDBDZe54dhf4Oskhmhw,110427
7
7
  praisonaiagents/agent/handoff.py,sha256=Saq0chqfvC6Zf5UbXvmctybbehqnotrXn72JsS-76Q0,13099
8
8
  praisonaiagents/agent/image_agent.py,sha256=-5MXG594HVwSpFMcidt16YBp7udtik-Cp7eXlzLE1fY,8696
9
9
  praisonaiagents/agents/__init__.py,sha256=_1d6Pqyk9EoBSo7E68sKyd1jDRlN1vxvVIRpoMc0Jcw,168
10
10
  praisonaiagents/agents/agents.py,sha256=WnptTEMSDMAM30Ka6rOAu6rBD-ZLev3qphb1a3BbP1g,63301
11
- praisonaiagents/agents/autoagents.py,sha256=gLzNsYkvefY667p3xbbvgEBLu4VzEZeyh3a_3yxt1e8,16478
11
+ praisonaiagents/agents/autoagents.py,sha256=1stF8z94eyVg6hyfrLgdArlarftz_OFvEDtRMsGZFvg,21094
12
12
  praisonaiagents/guardrails/__init__.py,sha256=HA8zhp-KRHTxo0194MUwXOUJjPyjOu7E3d7xUIKYVVY,310
13
13
  praisonaiagents/guardrails/guardrail_result.py,sha256=2K1WIYRyT_s1H6vBGa-7HEHzXCFIyZXZVY4f0hnQyWc,1352
14
14
  praisonaiagents/guardrails/llm_guardrail.py,sha256=MTTqmYDdZX-18QN9T17T5P_6H2qnV8GVgymJufW1WuM,3277
@@ -53,7 +53,7 @@ praisonaiagents/tools/xml_tools.py,sha256=iYTMBEk5l3L3ryQ1fkUnNVYK-Nnua2Kx2S0dxN
53
53
  praisonaiagents/tools/yaml_tools.py,sha256=uogAZrhXV9O7xvspAtcTfpKSQYL2nlOTvCQXN94-G9A,14215
54
54
  praisonaiagents/tools/yfinance_tools.py,sha256=s2PBj_1v7oQnOobo2fDbQBACEHl61ftG4beG6Z979ZE,8529
55
55
  praisonaiagents/tools/train/data/generatecot.py,sha256=H6bNh-E2hqL5MW6kX3hqZ05g9ETKN2-kudSjiuU_SD8,19403
56
- praisonaiagents-0.0.121.dist-info/METADATA,sha256=okAbJt5iVUK3GgBI66uk_0sKZSSs2orSgKepbSpQb-8,1669
57
- praisonaiagents-0.0.121.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
58
- praisonaiagents-0.0.121.dist-info/top_level.txt,sha256=_HsRddrJ23iDx5TTqVUVvXG2HeHBL5voshncAMDGjtA,16
59
- praisonaiagents-0.0.121.dist-info/RECORD,,
56
+ praisonaiagents-0.0.122.dist-info/METADATA,sha256=pU6W0akH1O1raC15FOsFQW3GXuflwpznV2ij10vYUP4,1669
57
+ praisonaiagents-0.0.122.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
58
+ praisonaiagents-0.0.122.dist-info/top_level.txt,sha256=_HsRddrJ23iDx5TTqVUVvXG2HeHBL5voshncAMDGjtA,16
59
+ praisonaiagents-0.0.122.dist-info/RECORD,,