File size: 12,185 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# Databricks Agent Bricks Implementation

This directory contains the Databricks Agent Bricks (Mosaic AI Agent Framework) implementation for CommunityOne - a generic civic engagement and community data platform.

## Schema Files

- **`communityone_schema.sql`** - Current comprehensive schema for all community data (jurisdictions, nonprofits, grants, meetings, observations)
- **`oral_health_schema.sql`** - DEPRECATED - Legacy oral health-specific schema (use communityone_schema.sql instead)

## Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Databricks Workspace                      β”‚
β”‚                                                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚  β”‚ Unity Catalog  │◄──────  MLflow Tracking β”‚              β”‚
β”‚  β”‚  - Models      β”‚      β”‚  - Experiments   β”‚              β”‚
β”‚  β”‚  - Governance  β”‚      β”‚  - Runs          β”‚              β”‚
β”‚  β”‚  - Lineage     β”‚      β”‚  - Artifacts     β”‚              β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”‚           β”‚                                                  β”‚
β”‚           β–Ό                                                  β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”               β”‚
β”‚  β”‚         Model Serving Endpoints          β”‚               β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚               β”‚
β”‚  β”‚  β”‚ Classifier   β”‚  β”‚ Sentiment       β”‚ β”‚               β”‚
β”‚  β”‚  β”‚ Agent        β”‚  β”‚ Analyzer        β”‚ β”‚               β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚               β”‚
β”‚  β”‚  Auto-scaling β€’ Observability β€’ A/B    β”‚               β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚
β”‚               β”‚                                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚
                β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚  REST API      β”‚
        β”‚  Clients       β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## Database Schema

### CommunityOne Schema (`communityone_schema.sql`)

Comprehensive data warehouse schema supporting:

**Dimension Tables:**
- `dim_jurisdiction` - Cities, counties, states, school districts
- `dim_organization` - Nonprofits, foundations, churches (IRS EO-BMF)
- `dim_geography` - Geographic hierarchies and FIPS codes
- `dim_date` - Time dimension for temporal analysis
- `dim_measure` - Community outcome indicators (health, education, economic, social)

**Fact Tables:**
- `fact_communityone_observation` - Community outcome measurements (replaces fact_oral_health_observation)
- `fact_grant` - **NEW** Grant transactions (990 Schedule I, 990-PF, USASpending.gov)
- `fact_nonprofit_finance` - **NEW** Annual 990 filings with revenue breakdowns
- `fact_jurisdiction_budget` - **NEW** Government budgets and spending
- `fact_meeting` - **NEW** Government meetings and public hearings

**Bridge Tables:**
- `bridge_grant_program_area` - **NEW** Multi-purpose grant program areas

### Key Changes from oral_health_schema.sql

βœ… **Generic community platform** (not oral health-specific)  
βœ… **Grant tracking system** aligned with ERD documentation  
βœ… **Nonprofit-government relationships** via fact_grant  
βœ… **Foundation giving patterns** (990-PF Schedule I data)  
βœ… **Complete financial transparency** for grants and budgets  

## Components

### 1. MLflow Agent Base (`agents/mlflow_base.py`)
- `MLflowAgentBase`: Base class for all agents with MLflow Pyfunc interface
- `MLflowChainAgent`: Base for LangChain-powered agents
- Automatic tracing and observability
- Model Serving compatibility

### 2. Classifier Agent (`agents/mlflow_classifier.py`)
- Policy topic classification
- Hybrid keyword + LLM approach
- Unity Catalog registered
- Deployable to Model Serving

### 3. Deployment (`databricks/deployment.py`)
- `AgentDeploymentManager`: Handles registration and deployment
- Unity Catalog integration
- Endpoint management
- A/B testing support

### 4. Evaluation (`databricks/evaluation.py`)
- `AgentEvaluator`: Quality metrics tracking
- Automated evaluation pipelines
- Regression detection
- Version comparison

### 5. Notebooks (`databricks/notebooks/`)
- Interactive development environment
- Step-by-step deployment guide
- Evaluation examples
- Delta Lake integration

## Getting Started

### Option 1: Databricks Notebook (Recommended)

1. Import notebook to your workspace:
   ```
   databricks workspace import \
     databricks/notebooks/01_agent_bricks_quickstart.py \
     /Users/your-email@company.com/oral-health-agents
   ```

2. Attach to a cluster with:
   - DBR 14.3 LTS ML or higher
   - Unity Catalog enabled

3. Run all cells to:
   - Register agents
   - Deploy to Model Serving
   - Evaluate performance

### Option 2: Python Script

1. Set environment variables:
   ```bash
   export DATABRICKS_HOST="https://your-workspace.cloud.databricks.com"
   export DATABRICKS_TOKEN="your-token"
   export OPENAI_API_KEY="your-openai-key"
   ```

2. Register agents:
   ```bash
   source venv/bin/activate
   python -m databricks.deployment
   ```

3. Run evaluation:
   ```bash
   python -m databricks.evaluation
   ```

## Unity Catalog Structure

```
main/                          # Catalog
β”œβ”€β”€ agents/                    # Schema for agent models
β”‚   β”œβ”€β”€ policy_classifier      # Classifier agent
β”‚   β”œβ”€β”€ sentiment_analyzer     # Sentiment agent
β”‚   └── advocacy_writer        # Advocacy agent
└── policy_data/               # Schema for data
    β”œβ”€β”€ raw_documents          # Scraped documents
    β”œβ”€β”€ classified_documents   # Classified results
    └── advocacy_opportunities # Identified opportunities
```

## Model Serving Endpoints

### Development Endpoints
- `policy-classifier-dev`: Classifier for testing
- `sentiment-analyzer-dev`: Sentiment analysis
- `advocacy-writer-dev`: Content generation

### Production Endpoints
- `policy-classifier-prod`: Production classifier
- `multi-agent-pipeline`: Full pipeline with traffic splitting

## Deployment Workflow

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Development  │────►│   Staging    │────►│  Production  β”‚
β”‚              β”‚     β”‚              β”‚     β”‚              β”‚
β”‚ β€’ Local test β”‚     β”‚ β€’ A/B test   β”‚     β”‚ β€’ Monitor    β”‚
β”‚ β€’ Register   β”‚     β”‚ β€’ Evaluate   β”‚     β”‚ β€’ Scale      β”‚
β”‚ β€’ Deploy dev β”‚     β”‚ β€’ Approve    β”‚     β”‚ β€’ Feedback   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## API Usage

### Invoke via REST

```bash
curl -X POST https://your-workspace.cloud.databricks.com/serving-endpoints/policy-classifier-prod/invocations \
  -H "Authorization: Bearer $DATABRICKS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "dataframe_records": [{
      "document_id": "doc_001",
      "title": "City Council Meeting",
      "content": "Discussion on water fluoridation..."
    }]
  }'
```

### Invoke via Python SDK

```python
from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

response = w.serving_endpoints.query(
    name="policy-classifier-prod",
    dataframe_records=[{
        "document_id": "doc_001",
        "title": "City Council Meeting",
        "content": "Discussion on water fluoridation..."
    }]
)

print(response.predictions[0])
```

## Monitoring & Observability

### MLflow Tracing
- Automatic trace capture for all agent calls
- LLM request/response logging
- Latency tracking
- Cost estimation

### View Traces
```python
import mlflow

# Get traces for a run
traces = mlflow.get_traces(
    experiment_id="your-experiment-id",
    filter_string="attributes.agent_role = 'classifier'"
)

for trace in traces:
    print(f"Trace ID: {trace.request_id}")
    print(f"Latency: {trace.execution_time_ms}ms")
    print(f"Status: {trace.status}")
```

### Endpoint Metrics
- Request rate
- Latency (P50, P95, P99)
- Error rate
- Token usage
- Cost per request

## Evaluation

### Automated Evaluation
```python
from databricks.evaluation import AgentEvaluator

evaluator = AgentEvaluator("policy_classifier")

metrics = evaluator.evaluate_classifier(
    model_uri="models:/main.agents.policy_classifier/1",
    test_documents=test_docs,
    ground_truth=labels
)

print(f"Accuracy: {metrics.accuracy:.2%}")
print(f"F1 Score: {metrics.f1_score:.2%}")
```

### A/B Testing
```python
comparison = evaluator.compare_versions(
    version_a="1",
    version_b="2",
    eval_data=eval_df
)

# Automatically promote if v2 is better
if comparison["improvements"]["accuracy"]["improvement_pct"] > 5:
    # Promote to production
    manager.deploy_agent(
        agent_name="policy_classifier",
        endpoint_name="policy-classifier-prod",
        version="2"
    )
```

## Best Practices

1. **Version Control**: Always register new versions to Unity Catalog
2. **Evaluate First**: Run evaluation before deploying to production
3. **Monitor Continuously**: Set up alerts for drift and errors
4. **Use Feedback**: Collect corrections and retrain regularly
5. **Scale Gradually**: Start with small workloads, scale up
6. **Cost Optimization**: Use scale-to-zero for dev/staging endpoints

## Cost Considerations

| Component | Estimated Cost |
|-----------|---------------|
| Model Serving (Small, scale-to-zero) | ~$0.10-0.50/hour active |
| MLflow Tracking | Included |
| Unity Catalog | Included |
| LLM API calls | $0.002-0.03 per request |

**Cost Optimization Tips:**
- Use keyword classification before LLM
- Enable scale-to-zero for dev endpoints
- Batch requests when possible
- Cache frequent queries
- Monitor token usage

## Troubleshooting

### Issue: Agent fails to load
```python
# Check model status
from mlflow.tracking import MlflowClient

client = MlflowClient()
versions = client.search_model_versions(
    filter_string="name='main.agents.policy_classifier'"
)
print(versions[0].status)
```

### Issue: Endpoint is slow
- Check workload size (upgrade from Small to Medium)
- Enable auto-scaling
- Review LLM prompt length
- Add caching layer

### Issue: High error rate
- Check MLflow traces for specific errors
- Verify input schema matches signature
- Review LLM API rate limits
- Check Unity Catalog permissions

## Next Steps

1. **Deploy More Agents**: Add sentiment analyzer and advocacy writer
2. **Create Workflows**: Use Databricks Workflows for scheduled processing
3. **Add Feedback Loop**: Store corrections in Delta Lake
4. **Set Up Alerts**: Monitor for drift and errors
5. **Scale Production**: Process thousands of documents

## Resources

- [Databricks Agent Framework Docs](https://docs.databricks.com/en/generative-ai/agent-framework/index.html)
- [MLflow Agent Deployment](https://mlflow.org/docs/latest/llms/deployments/index.html)
- [Unity Catalog AI](https://docs.databricks.com/en/generative-ai/unity-catalog-ai.html)
- [Model Serving Guide](https://docs.databricks.com/en/machine-learning/model-serving/index.html)