|
18 | 18 | # Framework-specific imports with availability checks |
19 | 19 | CREWAI_AVAILABLE = False |
20 | 20 | AUTOGEN_AVAILABLE = False |
| 21 | +AUTOGEN_V4_AVAILABLE = False |
21 | 22 | PRAISONAI_TOOLS_AVAILABLE = False |
22 | 23 | AGENTOPS_AVAILABLE = False |
23 | 24 | PRAISONAI_AVAILABLE = False |
|
41 | 42 | except ImportError: |
42 | 43 | pass |
43 | 44 |
|
| 45 | +try: |
| 46 | + from autogen_agentchat.agents import AssistantAgent as AutoGenV4AssistantAgent |
| 47 | + from autogen_ext.models.openai import OpenAIChatCompletionClient |
| 48 | + from autogen_agentchat.teams import RoundRobinGroupChat |
| 49 | + from autogen_agentchat.conditions import TextMentionTermination, MaxMessageTermination |
| 50 | + from autogen_agentchat.messages import TextMessage |
| 51 | + from autogen_core import CancellationToken |
| 52 | + AUTOGEN_V4_AVAILABLE = True |
| 53 | +except ImportError: |
| 54 | + pass |
| 55 | + |
44 | 56 | try: |
45 | 57 | import agentops |
46 | 58 | AGENTOPS_AVAILABLE = True |
@@ -123,8 +135,8 @@ def __init__(self, agent_file, framework, config_list, log_level=None, agent_cal |
123 | 135 | # Validate framework availability |
124 | 136 | if framework == "crewai" and not CREWAI_AVAILABLE: |
125 | 137 | raise ImportError("CrewAI is not installed. Please install it with 'pip install praisonai[crewai]'") |
126 | | - elif framework == "autogen" and not AUTOGEN_AVAILABLE: |
127 | | - raise ImportError("AutoGen is not installed. Please install it with 'pip install praisonai[autogen]'") |
| 138 | + elif framework == "autogen" and not (AUTOGEN_AVAILABLE or AUTOGEN_V4_AVAILABLE): |
| 139 | + raise ImportError("AutoGen is not installed. Please install it with 'pip install praisonai[autogen]' for v0.2 or 'pip install praisonai[autogen-v4]' for v0.4") |
128 | 140 | elif framework == "praisonai" and not PRAISONAI_AVAILABLE: |
129 | 141 | raise ImportError("PraisonAI is not installed. Please install it with 'pip install praisonaiagents'") |
130 | 142 |
|
@@ -316,11 +328,35 @@ def generate_crew_and_kickoff(self): |
316 | 328 | framework = self.framework or config.get('framework') |
317 | 329 |
|
318 | 330 | if framework == "autogen": |
319 | | - if not AUTOGEN_AVAILABLE: |
320 | | - raise ImportError("AutoGen is not installed. Please install it with 'pip install praisonai[autogen]'") |
| 331 | + if not (AUTOGEN_AVAILABLE or AUTOGEN_V4_AVAILABLE): |
| 332 | + raise ImportError("AutoGen is not installed. Please install it with 'pip install praisonai[autogen]' for v0.2 or 'pip install praisonai[autogen-v4]' for v0.4") |
| 333 | + |
| 334 | + # Choose autogen version based on availability and environment preference |
| 335 | + # AUTOGEN_VERSION can be set to "v0.2" or "v0.4" to force a specific version |
| 336 | + autogen_version = os.environ.get("AUTOGEN_VERSION", "auto").lower() |
| 337 | + |
| 338 | + use_v4 = False |
| 339 | + if autogen_version == "v0.4" and AUTOGEN_V4_AVAILABLE: |
| 340 | + use_v4 = True |
| 341 | + elif autogen_version == "v0.2" and AUTOGEN_AVAILABLE: |
| 342 | + use_v4 = False |
| 343 | + elif autogen_version == "auto": |
| 344 | + # Default preference: use v0.4 if available, fallback to v0.2 |
| 345 | + use_v4 = AUTOGEN_V4_AVAILABLE |
| 346 | + else: |
| 347 | + # Fallback to whatever is available |
| 348 | + use_v4 = AUTOGEN_V4_AVAILABLE and not AUTOGEN_AVAILABLE |
| 349 | + |
321 | 350 | if AGENTOPS_AVAILABLE: |
322 | | - agentops.init(os.environ.get("AGENTOPS_API_KEY"), default_tags=["autogen"]) |
323 | | - return self._run_autogen(config, topic, tools_dict) |
| 351 | + version_tag = "autogen-v4" if use_v4 else "autogen-v2" |
| 352 | + agentops.init(os.environ.get("AGENTOPS_API_KEY"), default_tags=[version_tag]) |
| 353 | + |
| 354 | + if use_v4: |
| 355 | + self.logger.info("Using AutoGen v0.4") |
| 356 | + return self._run_autogen_v4(config, topic, tools_dict) |
| 357 | + else: |
| 358 | + self.logger.info("Using AutoGen v0.2") |
| 359 | + return self._run_autogen(config, topic, tools_dict) |
324 | 360 | elif framework == "praisonai": |
325 | 361 | if not PRAISONAI_AVAILABLE: |
326 | 362 | raise ImportError("PraisonAI is not installed. Please install it with 'pip install praisonaiagents'") |
@@ -407,6 +443,111 @@ def _run_autogen(self, config, topic, tools_dict): |
407 | 443 |
|
408 | 444 | return result |
409 | 445 |
|
| 446 | + def _run_autogen_v4(self, config, topic, tools_dict): |
| 447 | + """ |
| 448 | + Run agents using the AutoGen v0.4 framework with async, event-driven architecture. |
| 449 | + |
| 450 | + Args: |
| 451 | + config (dict): Configuration dictionary |
| 452 | + topic (str): The topic to process |
| 453 | + tools_dict (dict): Dictionary of available tools |
| 454 | + |
| 455 | + Returns: |
| 456 | + str: Result of the agent interactions |
| 457 | + """ |
| 458 | + import asyncio |
| 459 | + |
| 460 | + async def run_autogen_v4_async(): |
| 461 | + # Create model client for v0.4 |
| 462 | + model_config = self.config_list[0] if self.config_list else {} |
| 463 | + model_client = OpenAIChatCompletionClient( |
| 464 | + model=model_config.get('model', 'gpt-4o'), |
| 465 | + api_key=model_config.get('api_key', os.environ.get("OPENAI_API_KEY")), |
| 466 | + base_url=model_config.get('base_url', "https://api.openai.com/v1") |
| 467 | + ) |
| 468 | + |
| 469 | + agents = [] |
| 470 | + combined_tasks = [] |
| 471 | + |
| 472 | + # Create agents from config |
| 473 | + for role, details in config['roles'].items(): |
| 474 | + agent_name = details['role'].format(topic=topic).replace("{topic}", topic) |
| 475 | + backstory = details['backstory'].format(topic=topic) |
| 476 | + |
| 477 | + # Convert tools for v0.4 - simplified tool passing |
| 478 | + agent_tools = [] |
| 479 | + for tool_name in details.get('tools', []): |
| 480 | + if tool_name in tools_dict: |
| 481 | + tool_instance = tools_dict[tool_name] |
| 482 | + # For v0.4, we can pass the tool's run method directly if it's callable |
| 483 | + if hasattr(tool_instance, 'run') and callable(tool_instance.run): |
| 484 | + agent_tools.append(tool_instance.run) |
| 485 | + |
| 486 | + # Create v0.4 AssistantAgent |
| 487 | + assistant = AutoGenV4AssistantAgent( |
| 488 | + name=agent_name, |
| 489 | + system_message=backstory + ". Must reply with 'TERMINATE' when the task is complete.", |
| 490 | + model_client=model_client, |
| 491 | + tools=agent_tools, |
| 492 | + reflect_on_tool_use=True |
| 493 | + ) |
| 494 | + |
| 495 | + agents.append(assistant) |
| 496 | + |
| 497 | + # Collect all task descriptions for sequential execution |
| 498 | + for task_name, task_details in details.get('tasks', {}).items(): |
| 499 | + description_filled = task_details['description'].format(topic=topic) |
| 500 | + combined_tasks.append(description_filled) |
| 501 | + |
| 502 | + if not agents: |
| 503 | + return "No agents created from configuration" |
| 504 | + |
| 505 | + # Create termination conditions |
| 506 | + text_termination = TextMentionTermination("TERMINATE") |
| 507 | + max_messages_termination = MaxMessageTermination(max_messages=20) |
| 508 | + termination_condition = text_termination | max_messages_termination |
| 509 | + |
| 510 | + # Create RoundRobinGroupChat for parallel/sequential execution |
| 511 | + group_chat = RoundRobinGroupChat( |
| 512 | + agents, |
| 513 | + termination_condition=termination_condition, |
| 514 | + max_turns=len(agents) * 3 # Allow multiple rounds |
| 515 | + ) |
| 516 | + |
| 517 | + # Combine all tasks into a single task description |
| 518 | + task_description = f"Topic: {topic}\n\nTasks to complete:\n" + "\n".join( |
| 519 | + f"{i+1}. {task}" for i, task in enumerate(combined_tasks) |
| 520 | + ) |
| 521 | + |
| 522 | + # Run the group chat |
| 523 | + try: |
| 524 | + result = await group_chat.run(task=task_description) |
| 525 | + |
| 526 | + # Extract the final message content |
| 527 | + if result.messages: |
| 528 | + final_message = result.messages[-1] |
| 529 | + if hasattr(final_message, 'content'): |
| 530 | + return f"### AutoGen v0.4 Output ###\n{final_message.content}" |
| 531 | + else: |
| 532 | + return f"### AutoGen v0.4 Output ###\n{str(final_message)}" |
| 533 | + else: |
| 534 | + return "### AutoGen v0.4 Output ###\nNo messages generated" |
| 535 | + |
| 536 | + except Exception as e: |
| 537 | + self.logger.error(f"Error in AutoGen v0.4 execution: {str(e)}") |
| 538 | + return f"### AutoGen v0.4 Error ###\n{str(e)}" |
| 539 | + |
| 540 | + finally: |
| 541 | + # Close the model client |
| 542 | + await model_client.close() |
| 543 | + |
| 544 | + # Run the async function |
| 545 | + try: |
| 546 | + return asyncio.run(run_autogen_v4_async()) |
| 547 | + except Exception as e: |
| 548 | + self.logger.error(f"Error running AutoGen v0.4: {str(e)}") |
| 549 | + return f"### AutoGen v0.4 Error ###\n{str(e)}" |
| 550 | + |
410 | 551 | def _run_crewai(self, config, topic, tools_dict): |
411 | 552 | """ |
412 | 553 | Run agents using the CrewAI framework. |
|
0 commit comments