Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 8,628 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 | # Databricks notebook source
# MAGIC %md
# MAGIC # Oral Health Policy Finder - Agent Bricks Quickstart
# MAGIC
# MAGIC This notebook demonstrates the Databricks Agent Bricks implementation of the Oral Health Policy Finder system.
# MAGIC
# MAGIC **Features:**
# MAGIC - MLflow-based agents with automatic tracing
# MAGIC - Unity Catalog governance
# MAGIC - Model Serving deployment
# MAGIC - Agent evaluation framework
# MAGIC - Delta Lake integration
# MAGIC
# MAGIC **Prerequisites:**
# MAGIC - Databricks Runtime 14.3 LTS ML or higher
# MAGIC - Unity Catalog enabled
# MAGIC - Model Serving permissions
# COMMAND ----------
# MAGIC %md
# MAGIC ## 1. Setup and Configuration
# COMMAND ----------
# Install dependencies
%pip install -q mlflow>=2.10.0 databricks-agents>=0.1.0 langchain>=0.1.0 openai>=1.6.0
# COMMAND ----------
# Configure MLflow
import mlflow
mlflow.set_registry_uri("databricks-uc")
# Set Unity Catalog
CATALOG = "main"
SCHEMA = "agents"
# Ensure catalog and schema exist
spark.sql(f"CREATE CATALOG IF NOT EXISTS {CATALOG}")
spark.sql(f"CREATE SCHEMA IF NOT EXISTS {CATALOG}.{SCHEMA}")
print(f"✅ Using Unity Catalog: {CATALOG}.{SCHEMA}")
# COMMAND ----------
# MAGIC %md
# MAGIC ## 2. Test Policy Classifier Agent Locally
# COMMAND ----------
# Import agent
import sys
sys.path.append("/Workspace/Repos/your-repo/open-navigator")
from agents.mlflow_classifier import PolicyClassifierAgent
# Initialize agent
agent = PolicyClassifierAgent()
# Test with sample document
test_input = {
"document_id": "test_001",
"title": "City Council Meeting - Water Infrastructure",
"content": """
The city council voted 5-2 to approve fluoridation of the municipal water supply.
The program will begin next quarter with monitoring by the health department.
Expected to benefit approximately 50,000 residents.
"""
}
# Get prediction
result = agent.predict(None, test_input)
print("Classification Result:")
print(f" Topic: {result['primary_topic']}")
print(f" Confidence: {result['confidence']:.2%}")
print(f" Method: {result['method']}")
print(f" Reasoning: {result['reasoning']}")
# COMMAND ----------
# MAGIC %md
# MAGIC ## 3. Register Agent to Unity Catalog
# COMMAND ----------
from databricks.deployment import AgentDeploymentManager
# Initialize deployment manager
manager = AgentDeploymentManager()
# Register agent
version = manager.register_agent(
agent_class=PolicyClassifierAgent,
agent_name="policy_classifier",
description="Classifies government meeting documents for oral health policy topics",
tags={
"team": "advocacy",
"domain": "oral_health",
"framework": "databricks-agent-bricks"
}
)
print(f"✅ Registered policy_classifier version {version}")
print(f" Model: {CATALOG}.{SCHEMA}.policy_classifier")
# COMMAND ----------
# MAGIC %md
# MAGIC ## 4. Deploy to Model Serving
# COMMAND ----------
# Deploy agent to serving endpoint
endpoint_url = manager.deploy_agent(
agent_name="policy_classifier",
endpoint_name="policy-classifier-dev",
version=version,
workload_size="Small",
scale_to_zero=True
)
print(f"✅ Deployed to Model Serving")
print(f" Endpoint: policy-classifier-dev")
print(f" URL: {endpoint_url}")
# COMMAND ----------
# MAGIC %md
# MAGIC ## 5. Test Deployed Endpoint
# COMMAND ----------
import requests
import os
# Test endpoint
endpoint_name = "policy-classifier-dev"
databricks_host = dbutils.notebook.entry_point.getDbutils().notebook().getContext().apiUrl().get()
databricks_token = dbutils.notebook.entry_point.getDbutils().notebook().getContext().apiToken().get()
url = f"{databricks_host}/serving-endpoints/{endpoint_name}/invocations"
headers = {
"Authorization": f"Bearer {databricks_token}",
"Content-Type": "application/json"
}
test_payload = {
"dataframe_records": [
{
"document_id": "endpoint_test_001",
"title": "School Board Meeting",
"content": "Discussion of new dental screening program for elementary students"
}
]
}
response = requests.post(url, headers=headers, json=test_payload)
result = response.json()
print("Endpoint Response:")
print(result)
# COMMAND ----------
# MAGIC %md
# MAGIC ## 6. Evaluate Agent Performance
# COMMAND ----------
from databricks.evaluation import AgentEvaluator
import pandas as pd
# Create evaluation dataset
eval_data = pd.DataFrame([
{
"document_id": "eval_001",
"title": "Water Board Meeting",
"content": "Approved fluoride addition to water supply",
"ground_truth": "water_fluoridation"
},
{
"document_id": "eval_002",
"title": "School Board Session",
"content": "New dental screening program for students",
"ground_truth": "school_dental_screening"
},
{
"document_id": "eval_003",
"title": "Budget Review",
"content": "General fund allocation discussion",
"ground_truth": "not_oral_health_related"
}
])
# Evaluate
evaluator = AgentEvaluator("policy_classifier")
metrics = evaluator.evaluate_classifier(
model_uri=f"models:/{CATALOG}.{SCHEMA}.policy_classifier/{version}",
test_documents=eval_data[["document_id", "title", "content"]].to_dict('records'),
ground_truth=eval_data["ground_truth"].tolist()
)
print(f"\n📊 Evaluation Metrics:")
print(f" Accuracy: {metrics.accuracy:.2%}")
print(f" Precision: {metrics.precision:.2%}")
print(f" Recall: {metrics.recall:.2%}")
print(f" F1 Score: {metrics.f1_score:.2%}")
print(f" Avg Latency: {metrics.avg_latency_ms:.0f}ms")
# COMMAND ----------
# MAGIC %md
# MAGIC ## 7. Query Results from Delta Lake
# COMMAND ----------
# Create sample data in Delta Lake
spark.sql(f"""
CREATE TABLE IF NOT EXISTS {CATALOG}.{SCHEMA}.classified_documents (
document_id STRING,
municipality STRING,
state STRING,
meeting_date TIMESTAMP,
primary_topic STRING,
confidence DOUBLE,
relevant_excerpts ARRAY<STRING>,
classification_timestamp TIMESTAMP
)
USING DELTA
PARTITIONED BY (state)
""")
# Query documents by topic
df = spark.sql(f"""
SELECT
state,
primary_topic,
COUNT(*) as document_count,
AVG(confidence) as avg_confidence
FROM {CATALOG}.{SCHEMA}.classified_documents
WHERE primary_topic != 'not_oral_health_related'
GROUP BY state, primary_topic
ORDER BY document_count DESC
""")
display(df)
# COMMAND ----------
# MAGIC %md
# MAGIC ## 8. Create Advocacy Heatmap
# COMMAND ----------
# Query advocacy opportunities
opportunities = spark.sql(f"""
SELECT
state,
municipality,
primary_topic,
confidence,
relevant_excerpts
FROM {CATALOG}.{SCHEMA}.classified_documents
WHERE
primary_topic IN ('water_fluoridation', 'school_dental_screening', 'low_income_dental_funding')
AND confidence > 0.7
ORDER BY confidence DESC
LIMIT 100
""")
# Convert to pandas for visualization
pdf = opportunities.toPandas()
print(f"Found {len(pdf)} advocacy opportunities")
display(pdf.head(10))
# COMMAND ----------
# MAGIC %md
# MAGIC ## 9. Monitor Agent Performance
# COMMAND ----------
# Get endpoint metrics
status = manager.get_endpoint_status("policy-classifier-dev")
print(f"Endpoint Status:")
print(f" Name: {status['name']}")
print(f" State: {status['state']}")
print(f"\nServed Entities:")
for entity in status['served_entities']:
print(f" - {entity['name']} v{entity['version']}: {entity['state']}")
# COMMAND ----------
# MAGIC %md
# MAGIC ## 10. A/B Test Model Versions
# COMMAND ----------
# Compare two versions (if you have multiple)
# comparison = evaluator.compare_versions(
# version_a="1",
# version_b="2",
# eval_data=eval_data
# )
#
# print("Version Comparison:")
# for metric, data in comparison["improvements"].items():
# print(f" {metric}: {data['improvement_pct']:.1f}% improvement")
# COMMAND ----------
# MAGIC %md
# MAGIC ## Next Steps
# MAGIC
# MAGIC 1. **Scale Up**: Process thousands of documents using Spark
# MAGIC 2. **Add Monitoring**: Set up alerts for model drift
# MAGIC 3. **Feedback Loop**: Collect user corrections in Delta Lake
# MAGIC 4. **Multi-Agent**: Deploy sentiment and advocacy writer agents
# MAGIC 5. **Production**: Promote to production endpoint with traffic splitting
# MAGIC
# MAGIC **Resources:**
# MAGIC - [Databricks Agent Framework Docs](https://docs.databricks.com/en/generative-ai/agent-framework/index.html)
# MAGIC - [MLflow Guide](https://mlflow.org/docs/latest/index.html)
# MAGIC - [Unity Catalog](https://docs.databricks.com/en/data-governance/unity-catalog/index.html)
|