|
| 1 | +import logging |
| 2 | +import os |
| 3 | + |
| 4 | +from azure.identity import DefaultAzureCredential as SyncDefaultAzureCredential |
| 5 | +from semantic_kernel import Kernel |
| 6 | +from semantic_kernel.agents import ChatCompletionAgent # pylint: disable=E0611 |
| 7 | +from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion |
| 8 | +from semantic_kernel.connectors.azure_ai_search import AzureAISearchCollection |
| 9 | +from v3.magentic_agents.common.lifecycle import MCPEnabledBase |
| 10 | +from v3.magentic_agents.models.agent_models import MCPConfig, SearchConfig |
| 11 | +from v3.magentic_agents.reasoning_search import ReasoningSearch |
| 12 | + |
| 13 | + |
| 14 | +class ReasoningAgentTemplate(MCPEnabledBase): |
| 15 | + """ |
| 16 | + SK ChatCompletionAgent with optional MCP plugin injected as a Kernel plugin. |
| 17 | + No Azure AI Agents client is needed here. We only need a token provider for SK. |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self, agent_name: str, |
| 21 | + agent_description: str, |
| 22 | + agent_instructions: str, |
| 23 | + model_deployment_name: str, |
| 24 | + azure_openai_endpoint: str, |
| 25 | + search_config: SearchConfig | None = None, |
| 26 | + mcp_config: MCPConfig | None = None) -> None: |
| 27 | + super().__init__(mcp=mcp_config) |
| 28 | + self.agent_name = agent_name |
| 29 | + self.agent_description = agent_description |
| 30 | + self.agent_instructions = agent_instructions |
| 31 | + self._model_deployment_name = model_deployment_name |
| 32 | + self._openai_endpoint = azure_openai_endpoint |
| 33 | + self.search_config = search_config |
| 34 | + self.reasoning_search: ReasoningSearch | None = None |
| 35 | + self.logger = logging.getLogger(__name__) |
| 36 | + |
| 37 | + async def _after_open(self) -> None: |
| 38 | + self.kernel = Kernel() |
| 39 | + |
| 40 | + # Token provider for SK chat completion |
| 41 | + sync_cred = SyncDefaultAzureCredential() |
| 42 | + |
| 43 | + def ad_token_provider() -> str: |
| 44 | + token = sync_cred.get_token("https://cognitiveservices.azure.com/.default") |
| 45 | + return token.token |
| 46 | + |
| 47 | + chat = AzureChatCompletion( |
| 48 | + deployment_name=self._model_deployment_name, |
| 49 | + endpoint=self._openai_endpoint, |
| 50 | + ad_token_provider=ad_token_provider |
| 51 | + ) |
| 52 | + self.kernel.add_service(chat) |
| 53 | + |
| 54 | + # Initialize search capabilities |
| 55 | + if self.search_config: |
| 56 | + self.reasoning_search = ReasoningSearch(self.search_config) |
| 57 | + await self.reasoning_search.initialize(self.kernel) |
| 58 | + |
| 59 | + # Inject MCP plugin into the SK kernel if available |
| 60 | + if self.mcp_plugin: |
| 61 | + try: |
| 62 | + self.kernel.add_plugin(self.mcp_plugin, plugin_name="mcp_tools") |
| 63 | + self.logger.info("Added MCP plugin") |
| 64 | + except Exception as ex: |
| 65 | + self.logger.exception(f"Could not add MCP plugin to kernel: {ex}") |
| 66 | + |
| 67 | + self._agent = ChatCompletionAgent( |
| 68 | + kernel=self.kernel, |
| 69 | + name=self.agent_name, |
| 70 | + description=self.agent_description, |
| 71 | + instructions=self.agent_instructions |
| 72 | + ) |
| 73 | + |
| 74 | + async def invoke(self, message: str): |
| 75 | + """Invoke the agent with a message.""" |
| 76 | + if not self._agent: |
| 77 | + raise RuntimeError("Agent not initialized. Call open() first.") |
| 78 | + |
| 79 | + async for response in self._agent.invoke(message): |
| 80 | + yield response |
| 81 | + |
| 82 | +# Backward‑compatible factory |
| 83 | +async def create_reasoning_agent( |
| 84 | + agent_name: str, |
| 85 | + agent_description: str, |
| 86 | + agent_instructions: str, |
| 87 | + model_deployment_name: str, |
| 88 | + azure_openai_endpoint: str, |
| 89 | + search_config: SearchConfig | None = None, |
| 90 | + mcp_config: MCPConfig | None = None) -> ReasoningAgentTemplate: |
| 91 | + agent = ReasoningAgentTemplate( |
| 92 | + agent_name=agent_name, |
| 93 | + agent_description=agent_description, |
| 94 | + agent_instructions=agent_instructions, |
| 95 | + model_deployment_name=model_deployment_name, |
| 96 | + azure_openai_endpoint=azure_openai_endpoint, |
| 97 | + search_config= search_config, |
| 98 | + mcp_config=mcp_config |
| 99 | + ) |
| 100 | + await agent.open() |
| 101 | + return agent |
0 commit comments