|
| 1 | +"""Manages all agent communication and selection strategies for the SQL agents.""" |
| 2 | + |
| 3 | +from semantic_kernel.agents import AgentGroupChat # pylint: disable=E0611 |
| 4 | +from semantic_kernel.agents.azure_ai.azure_ai_agent import AzureAIAgent |
| 5 | +from semantic_kernel.agents.strategies import ( |
| 6 | + SequentialSelectionStrategy, |
| 7 | + TerminationStrategy, |
| 8 | +) |
| 9 | + |
| 10 | +from sql_agents.fixer.response import FixerResponse |
| 11 | +from sql_agents.helpers.models import AgentType |
| 12 | +from sql_agents.migrator.response import MigratorResponse |
| 13 | +from sql_agents.semantic_verifier.response import SemanticVerifierResponse |
| 14 | +from sql_agents.syntax_checker.response import SyntaxCheckerResponse |
| 15 | + |
| 16 | + |
| 17 | +class CommsManager: |
| 18 | + """Manages all agent communication and selection strategies for the SQL agents.""" |
| 19 | + |
| 20 | + group_chat: AgentGroupChat = None |
| 21 | + |
| 22 | + class SelectionStrategy(SequentialSelectionStrategy): |
| 23 | + """A strategy for determining which agent should take the next turn in the chat.""" |
| 24 | + |
| 25 | + # Select the next agent that should take the next turn in the chat |
| 26 | + async def select_agent(self, agents, history): |
| 27 | + """ "Check which agent should take the next turn in the chat.""" |
| 28 | + |
| 29 | + match history[-1].name: |
| 30 | + case AgentType.MIGRATOR.value: |
| 31 | + # The Migrator should go first |
| 32 | + agent_name = AgentType.PICKER.value |
| 33 | + return next( |
| 34 | + (agent for agent in agents if agent.name == agent_name), None |
| 35 | + ) |
| 36 | + # The Incident Manager should go after the User or the Devops Assistant |
| 37 | + case AgentType.PICKER.value: |
| 38 | + agent_name = AgentType.SYNTAX_CHECKER.value |
| 39 | + return next( |
| 40 | + (agent for agent in agents if agent.name == agent_name), None |
| 41 | + ) |
| 42 | + case AgentType.SYNTAX_CHECKER.value: |
| 43 | + agent_name = AgentType.FIXER.value |
| 44 | + return next( |
| 45 | + (agent for agent in agents if agent.name == agent_name), |
| 46 | + None, |
| 47 | + ) |
| 48 | + case AgentType.FIXER.value: |
| 49 | + # The Fixer should always go after the Syntax Checker |
| 50 | + agent_name = AgentType.SYNTAX_CHECKER.value |
| 51 | + return next( |
| 52 | + (agent for agent in agents if agent.name == agent_name), None |
| 53 | + ) |
| 54 | + case "candidate": |
| 55 | + # The candidate message is created in the orchestration loop to pass the |
| 56 | + # candidate and source sql queries to the Semantic Verifier |
| 57 | + # It is created when the Syntax Checker returns an empty list of errors |
| 58 | + agent_name = AgentType.SEMANTIC_VERIFIER.value |
| 59 | + return next( |
| 60 | + (agent for agent in agents if agent.name == agent_name), |
| 61 | + None, |
| 62 | + ) |
| 63 | + case _: |
| 64 | + # Start run with this one - no history |
| 65 | + return next( |
| 66 | + ( |
| 67 | + agent |
| 68 | + for agent in agents |
| 69 | + if agent.name == AgentType.MIGRATOR.value |
| 70 | + ), |
| 71 | + None, |
| 72 | + ) |
| 73 | + |
| 74 | + # class for termination strategy |
| 75 | + class ApprovalTerminationStrategy(TerminationStrategy): |
| 76 | + """ |
| 77 | + A strategy for determining when an agent should terminate. |
| 78 | + This, combined with the maximum_iterations setting on the group chat, determines |
| 79 | + when the agents are finished processing a file when there are no errors. |
| 80 | + """ |
| 81 | + |
| 82 | + async def should_agent_terminate(self, agent, history): |
| 83 | + """Check if the agent should terminate.""" |
| 84 | + # May need to convert to models to get usable content using history[-1].name |
| 85 | + terminate: bool = False |
| 86 | + lower_case_hist: str = history[-1].content.lower() |
| 87 | + match history[-1].name: |
| 88 | + case AgentType.MIGRATOR.value: |
| 89 | + response = MigratorResponse.model_validate_json( |
| 90 | + lower_case_hist or "" |
| 91 | + ) |
| 92 | + if ( |
| 93 | + response.input_error is not None |
| 94 | + or response.rai_error is not None |
| 95 | + ): |
| 96 | + terminate = True |
| 97 | + case AgentType.SEMANTIC_VERIFIER.value: |
| 98 | + # Always terminate after the Semantic Verifier runs |
| 99 | + terminate = True |
| 100 | + case _: |
| 101 | + # If the agent is not the Migrator or Semantic Verifier, don't terminate |
| 102 | + # Note that the Syntax Checker and Fixer loop are only terminated by correct SQL |
| 103 | + # or by iterations exceeding the max_iterations setting |
| 104 | + pass |
| 105 | + |
| 106 | + return terminate |
| 107 | + |
| 108 | + def __init__(self, agent_dict): |
| 109 | + """Initialize the CommsManager and agent_chat with the given agents.""" |
| 110 | + self.group_chat = AgentGroupChat( |
| 111 | + agents=agent_dict.values(), |
| 112 | + termination_strategy=self.ApprovalTerminationStrategy( |
| 113 | + agents=[ |
| 114 | + agent_dict[AgentType.MIGRATOR], |
| 115 | + agent_dict[AgentType.SEMANTIC_VERIFIER], |
| 116 | + ], |
| 117 | + maximum_iterations=10, |
| 118 | + automatic_reset=True, |
| 119 | + ), |
| 120 | + selection_strategy=self.SelectionStrategy(agents=agent_dict.values()), |
| 121 | + ) |
0 commit comments