Spaces:
Sleeping
Sleeping
File size: 7,315 Bytes
825a805 a467728 825a805 a467728 825a805 0733fd6 825a805 0733fd6 825a805 0733fd6 825a805 0733fd6 825a805 0733fd6 825a805 0733fd6 825a805 a467728 0733fd6 a467728 0733fd6 a467728 0733fd6 a467728 0733fd6 a467728 0733fd6 a467728 0733fd6 a467728 0733fd6 a467728 0733fd6 |
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 |
# database_module/mcp_tools.py
from sqlalchemy.orm import Session
from sqlalchemy import or_
from .db import SessionLocal
from .models import ModelEntry, DriftEntry, DiagnosticData
from datetime import datetime
from typing import Any, Dict, List, Optional
import json
def get_all_models_handler(_: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
Return all models as list of dicts matching:
{name, created (ISO), description}
"""
with SessionLocal() as session:
entries = session.query(ModelEntry).all()
return [
{
"name": e.name,
"created": e.created.isoformat() if e.created else datetime.now().isoformat(),
"description": e.description or ""
}
for e in entries
]
def search_models_handler(params: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
Search models by name or description substring (case-insensitive).
params: {search_term: str}
"""
term = params.get("search_term", "").strip().lower()
with SessionLocal() as session:
query = session.query(ModelEntry)
if term:
like_pattern = f"%{term}%"
query = query.filter(
or_(
ModelEntry.name.ilike(like_pattern),
ModelEntry.description.ilike(like_pattern)
)
)
entries = query.all()
return [
{
"name": e.name,
"created": e.created.isoformat() if e.created else datetime.now().isoformat(),
"description": e.description or ""
}
for e in entries
]
def get_model_details_handler(params: Dict[str, Any]) -> Dict[str, Any]:
"""
Return a single model's details including system_prompt and description.
params: {model_name: str}
"""
model_name = params.get("model_name")
with SessionLocal() as session:
e = session.query(ModelEntry).filter_by(name=model_name).first()
if not e:
return {
"name": model_name,
"system_prompt": "You are a helpful AI assistant.",
"description": ""
}
# Extract system prompt from capabilities if available
system_prompt = "You are a helpful AI assistant."
if e.capabilities and "System Prompt: " in e.capabilities:
system_prompt = e.capabilities.split("System Prompt: ")[1]
return {
"name": e.name,
"system_prompt": system_prompt,
"description": e.description or ""
}
def save_model_handler(params: Dict[str, Any]) -> Dict[str, Any]:
"""
Save or update a model's system_prompt.
params: {model_name: str, system_prompt: str}
"""
name = params.get("model_name")
prompt = params.get("system_prompt", "")
with SessionLocal() as session:
entry = session.query(ModelEntry).filter_by(name=name).first()
if not entry:
# New model; created today
entry = ModelEntry(
name=name,
created=datetime.now(),
description="",
capabilities=f"System Prompt: {prompt}"
)
session.add(entry)
else:
# Update existing model
entry.capabilities = f"System Prompt: {prompt}"
entry.updated = datetime.now()
session.commit()
return {"message": f"Model '{name}' saved."}
def calculate_drift_handler(params: Dict[str, Any]) -> Dict[str, Any]:
"""
Placeholder drift calculation: record a new random drift score today.
params: {model_name: str}
"""
import random
name = params.get("model_name")
score = round(random.uniform(0, 1), 3)
today = datetime.now()
with SessionLocal() as session:
entry = DriftEntry(
model_name=name,
date=today,
drift_score=score
)
session.add(entry)
session.commit()
return {"drift_score": score, "message": f"Drift recorded for '{name}'."}
def get_drift_history_handler(params: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
Return drift history as list of {date, drift_score} for a model.
params: {model_name: str}
"""
name = params.get("model_name")
with SessionLocal() as session:
entries = session.query(DriftEntry).filter_by(model_name=name).order_by(DriftEntry.date).all()
return [
{"date": e.date.isoformat(), "drift_score": e.drift_score}
for e in entries
]
# === New functions for drift detection database operations ===
def save_diagnostic_data(
model_name: str,
questions: list,
answers: list,
is_baseline: bool = False
) -> None:
"""
Save diagnostic questions and answers to the database
"""
with SessionLocal() as session:
# Check if model exists, create if not
model = session.query(ModelEntry).filter_by(name=model_name).first()
if not model:
model = ModelEntry(
name=model_name,
created=datetime.now(),
description=""
)
session.add(model)
# Create new diagnostic entry
diagnostic = DiagnosticData(
model_name=model_name,
is_baseline=1 if is_baseline else 0,
questions=questions,
answers=answers,
created=datetime.now()
)
session.add(diagnostic)
session.commit()
def get_baseline_diagnostics(model_name: str) -> Optional[Dict[str, Any]]:
"""
Retrieve baseline diagnostics for a model
"""
with SessionLocal() as session:
baseline = session.query(DiagnosticData) \
.filter_by(model_name=model_name, is_baseline=1) \
.order_by(DiagnosticData.created.desc()) \
.first()
if not baseline:
return None
return {
"questions": baseline.questions,
"answers": baseline.answers,
"created": baseline.created.isoformat()
}
def save_drift_score(model_name: str, drift_score: str) -> None:
"""
Save drift score to database
"""
# Try to convert score to float if possible
try:
score_float = float(drift_score)
except ValueError:
score_float = None
with SessionLocal() as session:
entry = DriftEntry(
model_name=model_name,
date=datetime.now(),
drift_score=score_float
)
session.add(entry)
session.commit()
def register_model_with_capabilities(model_name: str, capabilities: str) -> None:
"""
Register a model with capabilities or update if already exists
"""
with SessionLocal() as session:
model = session.query(ModelEntry).filter_by(name=model_name).first()
if model:
model.capabilities = capabilities
model.updated = datetime.now()
else:
model = ModelEntry(
name=model_name,
created=datetime.now(),
capabilities=capabilities,
description=""
)
session.add(model)
session.commit() |