File size: 10,803 Bytes
61d29fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""
MLflow-based agent foundation for Databricks Agent Bricks.

Provides:
- MLflow Pyfunc model wrappers for agents
- Unity Catalog integration
- Automatic tracing and observability
- Model serving compatibility
"""
from typing import Any, Dict, List, Optional, Union
from abc import ABC, abstractmethod
import mlflow
from mlflow.pyfunc import PythonModel
from mlflow.models import infer_signature
from mlflow.tracking import MlflowClient
import pandas as pd
from datetime import datetime
from loguru import logger

from agents.base import AgentRole, AgentMessage, AgentStatus
from config import settings


class MLflowAgentBase(PythonModel, ABC):
    """
    Base class for agents that can be deployed via MLflow Model Serving.
    
    Integrates with:
    - Unity Catalog for governance
    - MLflow Tracking for experimentation
    - Databricks Model Serving for deployment
    - Mosaic AI Agent Framework for evaluation
    """
    
    def __init__(self, agent_id: str, role: AgentRole):
        """
        Initialize MLflow agent.
        
        Args:
            agent_id: Unique identifier for this agent
            role: Agent role in the pipeline
        """
        super().__init__()
        self.agent_id = agent_id
        self.role = role
        self.status = AgentStatus.IDLE
        self.client = MlflowClient()
        
    def load_context(self, context):
        """
        Load agent context from MLflow (called during model loading).
        
        Args:
            context: MLflow context with model artifacts
        """
        logger.info(f"Loading {self.role.value} agent from MLflow context")
        # Load any model artifacts, configs, etc.
        pass
    
    @abstractmethod
    def _process_request(self, request: Dict[str, Any]) -> Dict[str, Any]:
        """
        Process a single agent request.
        
        Args:
            request: Input request dictionary
            
        Returns:
            Response dictionary
        """
        pass
    
    def predict(
        self, 
        context, 
        model_input: Union[pd.DataFrame, Dict[str, Any], List[Dict[str, Any]]]
    ) -> Union[pd.DataFrame, List[Dict[str, Any]]]:
        """
        MLflow Pyfunc predict interface.
        
        This is the main entry point when the agent is deployed as a Model Serving endpoint.
        
        Args:
            context: MLflow context
            model_input: Input data (DataFrame, dict, or list of dicts)
            
        Returns:
            Predictions in same format as input
        """
        # Enable MLflow tracing for observability
        with mlflow.start_span(name=f"{self.role.value}_agent") as span:
            span.set_attribute("agent_id", self.agent_id)
            span.set_attribute("agent_role", self.role.value)
            
            try:
                # Convert input to standard format
                if isinstance(model_input, pd.DataFrame):
                    requests = model_input.to_dict('records')
                    return_df = True
                elif isinstance(model_input, dict):
                    requests = [model_input]
                    return_df = False
                else:
                    requests = model_input
                    return_df = False
                
                # Process each request with tracing
                results = []
                for idx, request in enumerate(requests):
                    with mlflow.start_span(name=f"process_request_{idx}") as req_span:
                        req_span.set_attribute("request_id", request.get("request_id", f"req_{idx}"))
                        
                        try:
                            result = self._process_request(request)
                            result["status"] = "success"
                            result["agent_id"] = self.agent_id
                            result["timestamp"] = datetime.utcnow().isoformat()
                            results.append(result)
                            
                            req_span.set_attribute("status", "success")
                            
                        except Exception as e:
                            error_result = {
                                "status": "error",
                                "error": str(e),
                                "agent_id": self.agent_id,
                                "timestamp": datetime.utcnow().isoformat()
                            }
                            results.append(error_result)
                            
                            req_span.set_attribute("status", "error")
                            req_span.set_attribute("error", str(e))
                            logger.error(f"Error processing request {idx}: {e}")
                
                # Return in requested format
                if return_df:
                    return pd.DataFrame(results)
                elif len(results) == 1 and not isinstance(model_input, list):
                    return results[0]
                else:
                    return results
                    
            except Exception as e:
                span.set_attribute("status", "error")
                span.set_attribute("error", str(e))
                logger.error(f"Error in {self.role.value} agent: {e}")
                raise
    
    def log_to_mlflow(
        self, 
        model_name: str,
        artifact_path: str = "agent",
        registered_model_name: Optional[str] = None,
        **kwargs
    ):
        """
        Log this agent to MLflow.
        
        Args:
            model_name: Name for the MLflow run
            artifact_path: Path within the run to store the model
            registered_model_name: Unity Catalog model name (e.g., "main.agents.scraper")
            **kwargs: Additional MLflow logging parameters
        """
        with mlflow.start_run(run_name=model_name) as run:
            # Log agent metadata
            mlflow.log_param("agent_id", self.agent_id)
            mlflow.log_param("agent_role", self.role.value)
            mlflow.log_param("framework", "databricks-agent-bricks")
            
            # Create example input/output for signature
            example_input = self._get_example_input()
            example_output = self.predict(None, example_input)
            signature = infer_signature(example_input, example_output)
            
            # Log the model
            mlflow.pyfunc.log_model(
                artifact_path=artifact_path,
                python_model=self,
                signature=signature,
                registered_model_name=registered_model_name,
                **kwargs
            )
            
            logger.info(f"Logged {self.role.value} agent to MLflow run {run.info.run_id}")
            
            if registered_model_name:
                logger.info(f"Registered model as {registered_model_name}")
                
            return run.info.run_id
    
    @abstractmethod
    def _get_example_input(self) -> Union[pd.DataFrame, Dict[str, Any]]:
        """
        Get example input for MLflow signature inference.
        
        Returns:
            Example input data
        """
        pass
    
    def deploy_to_model_serving(
        self,
        model_name: str,
        endpoint_name: str,
        workload_size: str = "Small",
        scale_to_zero: bool = True
    ) -> str:
        """
        Deploy this agent to Databricks Model Serving.
        
        Args:
            model_name: Registered model name in Unity Catalog
            endpoint_name: Name for the serving endpoint
            workload_size: Endpoint size (Small, Medium, Large)
            scale_to_zero: Whether to scale to zero when idle
            
        Returns:
            Endpoint URL
        """
        from databricks.sdk import WorkspaceClient
        from databricks.sdk.service.serving import ServedEntityInput, EndpointCoreConfigInput
        
        w = WorkspaceClient(
            host=settings.databricks_host,
            token=settings.databricks_token
        )
        
        # Get latest model version
        latest_version = self.client.get_latest_versions(model_name, stages=["None"])[0].version
        
        # Create or update endpoint
        endpoint_config = EndpointCoreConfigInput(
            name=endpoint_name,
            served_entities=[
                ServedEntityInput(
                    entity_name=model_name,
                    entity_version=latest_version,
                    workload_size=workload_size,
                    scale_to_zero_enabled=scale_to_zero
                )
            ]
        )
        
        try:
            endpoint = w.serving_endpoints.create_and_wait(
                name=endpoint_name,
                config=endpoint_config
            )
            logger.info(f"Created endpoint: {endpoint_name}")
        except Exception as e:
            if "already exists" in str(e):
                endpoint = w.serving_endpoints.update_config_and_wait(
                    name=endpoint_name,
                    served_entities=endpoint_config.served_entities
                )
                logger.info(f"Updated endpoint: {endpoint_name}")
            else:
                raise
        
        endpoint_url = f"{settings.databricks_host}/serving-endpoints/{endpoint_name}/invocations"
        return endpoint_url


class MLflowChainAgent(MLflowAgentBase):
    """
    Agent that uses LangChain with MLflow tracing.
    
    Provides integration with:
    - LangChain agents and chains
    - Automatic prompt logging
    - LLM call tracing
    - Tool usage tracking
    """
    
    def __init__(self, agent_id: str, role: AgentRole):
        """Initialize LangChain-based agent."""
        super().__init__(agent_id, role)
        self.chain = None
        
    def _setup_langchain_tracing(self):
        """Enable MLflow tracing for LangChain."""
        mlflow.langchain.autolog()
        
    @abstractmethod
    def _build_chain(self):
        """
        Build the LangChain chain for this agent.
        
        Returns:
            LangChain chain or agent
        """
        pass
    
    def _process_request(self, request: Dict[str, Any]) -> Dict[str, Any]:
        """Process request through LangChain."""
        if self.chain is None:
            self.chain = self._build_chain()
            
        with mlflow.start_span(name="langchain_invoke") as span:
            result = self.chain.invoke(request)
            
            # Log relevant metrics
            if hasattr(result, "llm_output"):
                span.set_attribute("tokens_used", result.llm_output.get("token_usage", {}).get("total_tokens", 0))
            
            return result