-
Notifications
You must be signed in to change notification settings - Fork 188
Add AG2 (formerly AutoGen) as a third AI engine (AI_ENGINE=ag2) #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VasiliyRad
wants to merge
6
commits into
EvolutionAPI:main
Choose a base branch
from
VasiliyRad:vasiliyr/03112026
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0f2c823
feat(ag2): add AG2 multi-agent service, builder, runner, and unit tests
VasiliyRad 8720544
feat(ag2): wire AG2 engine into chat_routes and service_providers
VasiliyRad b55724f
docs: add AG2 engine to root README supported frameworks list
VasiliyRad 89ba46e
fix(ag2): address sourcery-ai review comments on PR #46
VasiliyRad 927672c
test(ag2): add handoff, error, and exception propagation coverage
VasiliyRad b3a97d6
Updated .env.example for clarity
VasiliyRad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| from .adk.agent_runner import run_agent | ||
| # google-adk is an optional dependency — guard so unit tests run without the full stack | ||
| try: | ||
| from .adk.agent_runner import run_agent | ||
| except ImportError: | ||
| pass |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| import uuid | ||
| from typing import Tuple, Optional | ||
| from autogen import ConversableAgent, LLMConfig | ||
| from autogen.agentchat import initiate_group_chat | ||
| from autogen.agentchat.group.patterns import DefaultPattern, AutoPattern | ||
| from autogen.agentchat.group import ( | ||
| ContextVariables, | ||
| RevertToUserTarget, | ||
| TerminateTarget, | ||
| AgentTarget, | ||
| OnCondition, | ||
| StringLLMCondition, | ||
| OnContextCondition, | ||
| ExpressionContextCondition, | ||
| ContextExpression, | ||
| ) | ||
| from sqlalchemy.orm import Session | ||
| from src.services.agent_service import get_agent | ||
| from src.services.apikey_service import get_decrypted_api_key | ||
| from src.utils.logger import setup_logger | ||
|
|
||
| logger = setup_logger(__name__) | ||
|
|
||
|
|
||
| class AG2AgentBuilder: | ||
| def __init__(self, db: Session): | ||
| self.db = db | ||
|
|
||
| async def _get_api_key(self, agent) -> str: | ||
| """Reuse the same key resolution logic as ADK and CrewAI builders.""" | ||
| if hasattr(agent, "api_key_id") and agent.api_key_id: | ||
| key = get_decrypted_api_key(self.db, agent.api_key_id) | ||
| if key: | ||
| return key | ||
| raise ValueError(f"API key {agent.api_key_id} not found or inactive") | ||
| config_key = agent.config.get("api_key") if agent.config else None | ||
| if config_key: | ||
| try: | ||
| key = get_decrypted_api_key(self.db, uuid.UUID(config_key)) | ||
| return key or config_key | ||
| except (ValueError, TypeError): | ||
| return config_key | ||
| raise ValueError(f"No API key configured for agent {agent.name}") | ||
|
|
||
| def _build_llm_config(self, agent, api_key: str) -> LLMConfig: | ||
| return LLMConfig({"model": agent.model, "api_key": api_key}) | ||
|
|
||
| def _build_system_message(self, agent) -> str: | ||
| parts = [] | ||
| if agent.role: | ||
| parts.append(f"Role: {agent.role}") | ||
| if agent.goal: | ||
| parts.append(f"Goal: {agent.goal}") | ||
| if agent.instruction: | ||
| parts.append(agent.instruction) | ||
| return "\n\n".join(parts) | ||
|
|
||
| async def build_conversable_agent(self, agent) -> ConversableAgent: | ||
| api_key = await self._get_api_key(agent) | ||
| # AG2 0.11+ rejects names containing whitespace for OpenAI models | ||
| safe_name = agent.name.replace(" ", "_") | ||
| return ConversableAgent( | ||
| name=safe_name, | ||
| system_message=self._build_system_message(agent), | ||
| description=agent.description or "", | ||
| llm_config=self._build_llm_config(agent, api_key), | ||
| ) | ||
|
|
||
| def _apply_handoffs(self, ca: ConversableAgent, config: dict, all_agents: dict): | ||
| """ | ||
| Apply AG2 handoff conditions from the agent config's optional 'handoffs' field. | ||
|
|
||
| Config format: | ||
| { | ||
| "handoffs": [ | ||
| { | ||
| "type": "llm", | ||
| "target_agent_id": "<uuid>", | ||
| "condition": "Route when the user asks about billing" | ||
| }, | ||
| { | ||
| "type": "context", | ||
| "target_agent_id": "<uuid>", | ||
| "expression": "${is_vip} == True" | ||
| } | ||
| ], | ||
| "after_work": "revert_to_user" // or "terminate" | ||
| } | ||
| """ | ||
| handoffs_config = config.get("handoffs", []) | ||
| llm_conditions = [] | ||
| context_conditions = [] | ||
|
|
||
| for h in handoffs_config: | ||
| target_id = h.get("target_agent_id") | ||
| target_agent = all_agents.get(str(target_id)) | ||
| if not target_agent: | ||
| logger.warning(f"Handoff target {target_id} not found, skipping") | ||
| continue | ||
|
|
||
| h_type = h.get("type") | ||
| if h_type not in ("llm", "context"): | ||
| logger.warning(f"Unknown or missing handoff type {h_type!r} for target {target_id}, skipping") | ||
| continue | ||
|
|
||
| if h_type == "llm": | ||
| llm_conditions.append( | ||
| OnCondition( | ||
| target=AgentTarget(target_agent), | ||
| condition=StringLLMCondition(prompt=h.get("condition", "")), | ||
| ) | ||
| ) | ||
| elif h_type == "context": | ||
| context_conditions.append( | ||
| OnContextCondition( | ||
| target=AgentTarget(target_agent), | ||
| condition=ExpressionContextCondition( | ||
| expression=ContextExpression(h.get("expression", "")) | ||
| ), | ||
| ) | ||
| ) | ||
|
|
||
| if llm_conditions: | ||
| ca.handoffs.add_llm_conditions(llm_conditions) | ||
| if context_conditions: | ||
| ca.handoffs.add_context_conditions(context_conditions) | ||
|
|
||
| after_work = config.get("after_work", "revert_to_user") | ||
| if after_work == "terminate": | ||
| ca.handoffs.set_after_work(TerminateTarget()) | ||
| else: | ||
| ca.handoffs.set_after_work(RevertToUserTarget()) | ||
|
|
||
| async def build_group_chat_setup(self, root_agent) -> dict: | ||
| """ | ||
| Build a GroupChat pattern from an agent record with sub_agents. | ||
| Returns a dict consumed by the runner's initiate_group_chat call. | ||
| """ | ||
| config = root_agent.config or {} | ||
| sub_agent_ids = config.get("sub_agents", []) | ||
| if not sub_agent_ids: | ||
| raise ValueError("group_chat agent requires at least one sub_agent") | ||
|
|
||
| # Build all sub-agents first so handoff resolution can reference them. | ||
| # Cache db_agent records to avoid re-fetching them in the handoff pass. | ||
| all_agents: dict = {} | ||
| agents = [] | ||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| db_sub_agents: dict = {} | ||
| for aid in sub_agent_ids: | ||
| db_agent = get_agent(self.db, str(aid)) | ||
| if db_agent is None: | ||
| raise ValueError(f"Sub-agent {aid} not found") | ||
| db_sub_agents[str(aid)] = db_agent | ||
| ca = await self.build_conversable_agent(db_agent) | ||
| all_agents[str(aid)] = ca | ||
| agents.append(ca) | ||
|
|
||
| root_ca = await self.build_conversable_agent(root_agent) | ||
| all_agents[str(root_agent.id)] = root_ca | ||
|
|
||
| # Apply handoffs using the already-fetched db_agent records | ||
| for aid in sub_agent_ids: | ||
| db_agent = db_sub_agents.get(str(aid)) | ||
| if db_agent and db_agent.config: | ||
| self._apply_handoffs(all_agents[str(aid)], db_agent.config, all_agents) | ||
|
|
||
| api_key = await self._get_api_key(root_agent) | ||
| manager_llm = self._build_llm_config(root_agent, api_key) | ||
|
|
||
| pattern_type = config.get("pattern", "auto") | ||
| if pattern_type == "auto": | ||
| pattern = AutoPattern( | ||
| initial_agent=root_ca, | ||
| agents=[root_ca] + agents, | ||
| group_manager_args={"llm_config": manager_llm}, | ||
| ) | ||
| else: | ||
| pattern = DefaultPattern( | ||
| initial_agent=root_ca, | ||
| agents=[root_ca] + agents, | ||
| group_after_work=RevertToUserTarget(), | ||
| ) | ||
|
|
||
| return { | ||
| "pattern": pattern, | ||
| "agents": [root_ca] + agents, | ||
| "max_rounds": config.get("max_rounds", 10), | ||
| "context_variables": ContextVariables( | ||
| data=config.get("context_variables", {}) | ||
| ), | ||
| } | ||
|
|
||
| async def build_agent(self, root_agent) -> Tuple[object, None]: | ||
| """ | ||
| Entry point matching the ADK/CrewAI AgentBuilder interface. | ||
| Returns (agent_or_setup_dict, exit_stack). | ||
|
|
||
| Orchestration mode is read from config["ag2_mode"]: | ||
| "group_chat" → GroupChat with sub-agents from config["sub_agents"] | ||
| "single" / absent → single ConversableAgent (default) | ||
| No new agent type is required in the DB; all AG2 agents use type="llm". | ||
| """ | ||
| ag2_mode = (root_agent.config or {}).get("ag2_mode", "single") | ||
| if ag2_mode == "group_chat": | ||
| return await self.build_group_chat_setup(root_agent), None | ||
| else: | ||
| return await self.build_conversable_agent(root_agent), None | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.