|
| 1 | +""" |
| 2 | +Agent processor for handling interactions with Microsoft Foundry agents. |
| 3 | +Includes MCP (Model Context Protocol) integration for tool calling. |
| 4 | +""" |
| 5 | +import os |
| 6 | +import json |
| 7 | +from typing import List, Dict, Any |
| 8 | +try: |
| 9 | + from azure.ai.projects import AIProjectClient # type: ignore |
| 10 | + from azure.identity import DefaultAzureCredential # type: ignore |
| 11 | + _REMOTE_AVAILABLE = True |
| 12 | +except Exception: |
| 13 | + _REMOTE_AVAILABLE = False |
| 14 | + |
| 15 | + |
| 16 | +def create_function_tool_for_agent(agent_name: str) -> List[Dict[str, Any]]: |
| 17 | + """ |
| 18 | + Create function tools for a specific agent using MCP. |
| 19 | + |
| 20 | + Args: |
| 21 | + agent_name: Name of the agent (e.g., 'interior_designer', 'inventory_agent') |
| 22 | + |
| 23 | + Returns: |
| 24 | + List of function tool definitions |
| 25 | + """ |
| 26 | + # Placeholder for MCP tool integration |
| 27 | + # In production, this would connect to MCP servers to get available tools |
| 28 | + tools = [] |
| 29 | + |
| 30 | + # Define tools based on agent type |
| 31 | + if agent_name == "interior_designer": |
| 32 | + tools.append({ |
| 33 | + "type": "function", |
| 34 | + "function": { |
| 35 | + "name": "create_image", |
| 36 | + "description": "Create or modify images based on user requirements", |
| 37 | + "parameters": { |
| 38 | + "type": "object", |
| 39 | + "properties": { |
| 40 | + "prompt": {"type": "string", "description": "Image generation prompt"}, |
| 41 | + "path": {"type": "string", "description": "Path to existing image (optional)"} |
| 42 | + }, |
| 43 | + "required": ["prompt"] |
| 44 | + } |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + elif agent_name == "inventory_agent": |
| 49 | + tools.append({ |
| 50 | + "type": "function", |
| 51 | + "function": { |
| 52 | + "name": "inventory_check", |
| 53 | + "description": "Check inventory levels for products", |
| 54 | + "parameters": { |
| 55 | + "type": "object", |
| 56 | + "properties": { |
| 57 | + "product_dict": { |
| 58 | + "type": "object", |
| 59 | + "description": "Dictionary mapping product names to product IDs" |
| 60 | + } |
| 61 | + }, |
| 62 | + "required": ["product_dict"] |
| 63 | + } |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + elif agent_name == "customer_loyalty": |
| 68 | + tools.append({ |
| 69 | + "type": "function", |
| 70 | + "function": { |
| 71 | + "name": "customer_loyalty_check", |
| 72 | + "description": "Check customer loyalty status and calculate discount", |
| 73 | + "parameters": { |
| 74 | + "type": "object", |
| 75 | + "properties": { |
| 76 | + "customer_id": {"type": "string", "description": "Customer ID"} |
| 77 | + }, |
| 78 | + "required": ["customer_id"] |
| 79 | + } |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + elif agent_name == "cora": |
| 84 | + # Cora (shopper agent) might have general query tools |
| 85 | + tools.append({ |
| 86 | + "type": "function", |
| 87 | + "function": { |
| 88 | + "name": "search_products", |
| 89 | + "description": "Search for products in catalog", |
| 90 | + "parameters": { |
| 91 | + "type": "object", |
| 92 | + "properties": { |
| 93 | + "query": {"type": "string", "description": "Search query"} |
| 94 | + }, |
| 95 | + "required": ["query"] |
| 96 | + } |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + return tools |
| 101 | + |
| 102 | + |
| 103 | +class AgentProcessor: |
| 104 | + """Handles communication with Microsoft Foundry agents""" |
| 105 | + |
| 106 | + def __init__(self, agent_id: str, project_endpoint: str = None): |
| 107 | + """ |
| 108 | + Initialize agent processor. |
| 109 | + |
| 110 | + Args: |
| 111 | + agent_id: The agent ID from Microsoft Foundry |
| 112 | + project_endpoint: Optional project endpoint (reads from env if not provided) |
| 113 | + """ |
| 114 | + self.agent_id = agent_id |
| 115 | + self.project_endpoint = project_endpoint or os.environ.get("AZURE_AI_AGENT_ENDPOINT") |
| 116 | + |
| 117 | + if not self.project_endpoint or not _REMOTE_AVAILABLE: |
| 118 | + raise ValueError("Remote agent support unavailable (endpoint or SDK missing)") |
| 119 | + self.client = AIProjectClient(endpoint=self.project_endpoint, credential=DefaultAzureCredential()) |
| 120 | + |
| 121 | + def run_conversation_with_text_stream( |
| 122 | + self, |
| 123 | + user_message: str, |
| 124 | + conversation_history: List[Dict[str, str]] = None, |
| 125 | + additional_context: Dict[str, Any] = None |
| 126 | + ): |
| 127 | + """ |
| 128 | + Run a conversation with the agent and stream the response. |
| 129 | + |
| 130 | + Args: |
| 131 | + user_message: The user's message |
| 132 | + conversation_history: Optional conversation history |
| 133 | + additional_context: Additional context to provide to the agent |
| 134 | + |
| 135 | + Yields: |
| 136 | + Chunks of the agent's response |
| 137 | + """ |
| 138 | + try: |
| 139 | + # Create a thread for this conversation |
| 140 | + thread = self.client.agents.create_thread() |
| 141 | + |
| 142 | + # Build the message content |
| 143 | + message_content = user_message |
| 144 | + if additional_context: |
| 145 | + message_content = f"Context: {json.dumps(additional_context)}\n\nUser: {user_message}" |
| 146 | + |
| 147 | + # Add message to thread |
| 148 | + self.client.agents.create_message( |
| 149 | + thread_id=thread.id, |
| 150 | + role="user", |
| 151 | + content=message_content |
| 152 | + ) |
| 153 | + |
| 154 | + # Run the agent |
| 155 | + run = self.client.agents.create_and_process_run( |
| 156 | + thread_id=thread.id, |
| 157 | + assistant_id=self.agent_id |
| 158 | + ) |
| 159 | + |
| 160 | + # Get messages |
| 161 | + messages = self.client.agents.list_messages(thread_id=thread.id) |
| 162 | + |
| 163 | + # Find the assistant's response |
| 164 | + for message in messages: |
| 165 | + if message.role == "assistant": |
| 166 | + for content in message.content: |
| 167 | + if hasattr(content, 'text'): |
| 168 | + yield content.text.value |
| 169 | + |
| 170 | + # Clean up |
| 171 | + self.client.agents.delete_thread(thread.id) |
| 172 | + |
| 173 | + except Exception as e: |
| 174 | + yield f"Error communicating with agent: {str(e)}" |
0 commit comments