|
| 1 | +""" |
| 2 | +Provider model catalog management (ADR-800). |
| 3 | +
|
| 4 | +Handles fetching, upserting, and querying the provider_model_catalog table. |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | +import logging |
| 9 | +from datetime import datetime, timezone |
| 10 | +from typing import Any, Dict, List, Optional |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +def upsert_catalog_entries(conn, entries: List[Dict[str, Any]]) -> int: |
| 16 | + """ |
| 17 | + Upsert model catalog entries into provider_model_catalog. |
| 18 | +
|
| 19 | + Args: |
| 20 | + conn: psycopg2 connection |
| 21 | + entries: List of dicts with catalog column values from fetch_model_catalog() |
| 22 | +
|
| 23 | + Returns: |
| 24 | + Number of rows upserted |
| 25 | + """ |
| 26 | + if not entries: |
| 27 | + return 0 |
| 28 | + |
| 29 | + now = datetime.now(timezone.utc) |
| 30 | + count = 0 |
| 31 | + |
| 32 | + with conn.cursor() as cur: |
| 33 | + for entry in entries: |
| 34 | + raw = entry.get("raw_metadata") |
| 35 | + raw_json = json.dumps(raw) if raw is not None else None |
| 36 | + |
| 37 | + cur.execute( |
| 38 | + """INSERT INTO kg_api.provider_model_catalog |
| 39 | + (provider, model_id, display_name, category, context_length, |
| 40 | + max_completion_tokens, supports_vision, supports_json_mode, |
| 41 | + supports_tool_use, supports_streaming, |
| 42 | + price_prompt_per_m, price_completion_per_m, price_cache_read_per_m, |
| 43 | + upstream_provider, raw_metadata, fetched_at, updated_at) |
| 44 | + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) |
| 45 | + ON CONFLICT (provider, model_id, category) DO UPDATE SET |
| 46 | + display_name = EXCLUDED.display_name, |
| 47 | + context_length = COALESCE(EXCLUDED.context_length, kg_api.provider_model_catalog.context_length), |
| 48 | + max_completion_tokens = COALESCE(EXCLUDED.max_completion_tokens, kg_api.provider_model_catalog.max_completion_tokens), |
| 49 | + supports_vision = EXCLUDED.supports_vision, |
| 50 | + supports_json_mode = EXCLUDED.supports_json_mode, |
| 51 | + supports_tool_use = EXCLUDED.supports_tool_use, |
| 52 | + supports_streaming = EXCLUDED.supports_streaming, |
| 53 | + price_prompt_per_m = COALESCE(EXCLUDED.price_prompt_per_m, kg_api.provider_model_catalog.price_prompt_per_m), |
| 54 | + price_completion_per_m = COALESCE(EXCLUDED.price_completion_per_m, kg_api.provider_model_catalog.price_completion_per_m), |
| 55 | + price_cache_read_per_m = COALESCE(EXCLUDED.price_cache_read_per_m, kg_api.provider_model_catalog.price_cache_read_per_m), |
| 56 | + upstream_provider = EXCLUDED.upstream_provider, |
| 57 | + raw_metadata = EXCLUDED.raw_metadata, |
| 58 | + fetched_at = EXCLUDED.fetched_at, |
| 59 | + updated_at = EXCLUDED.updated_at |
| 60 | + """, |
| 61 | + ( |
| 62 | + entry["provider"], |
| 63 | + entry["model_id"], |
| 64 | + entry.get("display_name"), |
| 65 | + entry["category"], |
| 66 | + entry.get("context_length"), |
| 67 | + entry.get("max_completion_tokens"), |
| 68 | + entry.get("supports_vision", False), |
| 69 | + entry.get("supports_json_mode", False), |
| 70 | + entry.get("supports_tool_use", False), |
| 71 | + entry.get("supports_streaming", True), |
| 72 | + entry.get("price_prompt_per_m"), |
| 73 | + entry.get("price_completion_per_m"), |
| 74 | + entry.get("price_cache_read_per_m"), |
| 75 | + entry.get("upstream_provider"), |
| 76 | + raw_json, |
| 77 | + now, |
| 78 | + now, |
| 79 | + ), |
| 80 | + ) |
| 81 | + count += 1 |
| 82 | + |
| 83 | + conn.commit() |
| 84 | + logger.info(f"Upserted {count} catalog entries") |
| 85 | + return count |
| 86 | + |
| 87 | + |
| 88 | +def list_catalog( |
| 89 | + conn, |
| 90 | + provider: Optional[str] = None, |
| 91 | + category: Optional[str] = None, |
| 92 | + enabled_only: bool = False, |
| 93 | +) -> List[Dict[str, Any]]: |
| 94 | + """ |
| 95 | + Query provider_model_catalog with optional filters. |
| 96 | +
|
| 97 | + Returns list of dicts with all catalog columns. |
| 98 | + """ |
| 99 | + conditions = [] |
| 100 | + params = [] |
| 101 | + |
| 102 | + if provider: |
| 103 | + conditions.append("provider = %s") |
| 104 | + params.append(provider) |
| 105 | + if category: |
| 106 | + conditions.append("category = %s") |
| 107 | + params.append(category) |
| 108 | + if enabled_only: |
| 109 | + conditions.append("enabled = TRUE") |
| 110 | + |
| 111 | + where = f"WHERE {' AND '.join(conditions)}" if conditions else "" |
| 112 | + |
| 113 | + with conn.cursor() as cur: |
| 114 | + cur.execute( |
| 115 | + f"""SELECT id, provider, model_id, display_name, category, |
| 116 | + context_length, max_completion_tokens, |
| 117 | + supports_vision, supports_json_mode, supports_tool_use, |
| 118 | + supports_streaming, |
| 119 | + price_prompt_per_m, price_completion_per_m, price_cache_read_per_m, |
| 120 | + enabled, is_default, sort_order, |
| 121 | + upstream_provider, fetched_at, created_at, updated_at |
| 122 | + FROM kg_api.provider_model_catalog |
| 123 | + {where} |
| 124 | + ORDER BY provider, sort_order, model_id""", |
| 125 | + params, |
| 126 | + ) |
| 127 | + columns = [desc[0] for desc in cur.description] |
| 128 | + return [dict(zip(columns, row)) for row in cur.fetchall()] |
| 129 | + |
| 130 | + |
| 131 | +def set_model_enabled(conn, catalog_id: int, enabled: bool) -> bool: |
| 132 | + """Enable or disable a model in the catalog.""" |
| 133 | + with conn.cursor() as cur: |
| 134 | + cur.execute( |
| 135 | + """UPDATE kg_api.provider_model_catalog |
| 136 | + SET enabled = %s, updated_at = NOW() |
| 137 | + WHERE id = %s""", |
| 138 | + (enabled, catalog_id), |
| 139 | + ) |
| 140 | + conn.commit() |
| 141 | + return cur.rowcount > 0 |
| 142 | + |
| 143 | + |
| 144 | +def set_model_default(conn, catalog_id: int) -> bool: |
| 145 | + """ |
| 146 | + Set a model as the default for its provider+category. |
| 147 | +
|
| 148 | + Clears existing default for that provider+category first. |
| 149 | + """ |
| 150 | + with conn.cursor() as cur: |
| 151 | + # Get the provider and category for this model |
| 152 | + cur.execute( |
| 153 | + "SELECT provider, category FROM kg_api.provider_model_catalog WHERE id = %s", |
| 154 | + (catalog_id,), |
| 155 | + ) |
| 156 | + row = cur.fetchone() |
| 157 | + if not row: |
| 158 | + return False |
| 159 | + |
| 160 | + provider, category = row |
| 161 | + |
| 162 | + # Clear existing default |
| 163 | + cur.execute( |
| 164 | + """UPDATE kg_api.provider_model_catalog |
| 165 | + SET is_default = FALSE, updated_at = NOW() |
| 166 | + WHERE provider = %s AND category = %s AND is_default = TRUE""", |
| 167 | + (provider, category), |
| 168 | + ) |
| 169 | + |
| 170 | + # Set new default (also ensures enabled) |
| 171 | + cur.execute( |
| 172 | + """UPDATE kg_api.provider_model_catalog |
| 173 | + SET is_default = TRUE, enabled = TRUE, updated_at = NOW() |
| 174 | + WHERE id = %s""", |
| 175 | + (catalog_id,), |
| 176 | + ) |
| 177 | + conn.commit() |
| 178 | + return True |
| 179 | + |
| 180 | + |
| 181 | +def update_model_pricing( |
| 182 | + conn, |
| 183 | + catalog_id: int, |
| 184 | + price_prompt_per_m: Optional[float] = None, |
| 185 | + price_completion_per_m: Optional[float] = None, |
| 186 | +) -> bool: |
| 187 | + """Manually override pricing for a catalog entry.""" |
| 188 | + updates = [] |
| 189 | + params = [] |
| 190 | + |
| 191 | + if price_prompt_per_m is not None: |
| 192 | + updates.append("price_prompt_per_m = %s") |
| 193 | + params.append(price_prompt_per_m) |
| 194 | + if price_completion_per_m is not None: |
| 195 | + updates.append("price_completion_per_m = %s") |
| 196 | + params.append(price_completion_per_m) |
| 197 | + |
| 198 | + if not updates: |
| 199 | + return False |
| 200 | + |
| 201 | + updates.append("updated_at = NOW()") |
| 202 | + params.append(catalog_id) |
| 203 | + |
| 204 | + with conn.cursor() as cur: |
| 205 | + cur.execute( |
| 206 | + f"""UPDATE kg_api.provider_model_catalog |
| 207 | + SET {', '.join(updates)} |
| 208 | + WHERE id = %s""", |
| 209 | + params, |
| 210 | + ) |
| 211 | + conn.commit() |
| 212 | + return cur.rowcount > 0 |
| 213 | + |
| 214 | + |
| 215 | +def get_model_pricing(conn, provider: str, model_id: str) -> Optional[Dict[str, Any]]: |
| 216 | + """ |
| 217 | + Look up pricing for a specific model from the catalog. |
| 218 | +
|
| 219 | + Returns dict with price_prompt_per_m and price_completion_per_m, or None. |
| 220 | + """ |
| 221 | + with conn.cursor() as cur: |
| 222 | + cur.execute( |
| 223 | + """SELECT price_prompt_per_m, price_completion_per_m, price_cache_read_per_m |
| 224 | + FROM kg_api.provider_model_catalog |
| 225 | + WHERE provider = %s AND model_id = %s AND enabled = TRUE |
| 226 | + LIMIT 1""", |
| 227 | + (provider, model_id), |
| 228 | + ) |
| 229 | + row = cur.fetchone() |
| 230 | + if row: |
| 231 | + return { |
| 232 | + "price_prompt_per_m": float(row[0]) if row[0] is not None else None, |
| 233 | + "price_completion_per_m": float(row[1]) if row[1] is not None else None, |
| 234 | + "price_cache_read_per_m": float(row[2]) if row[2] is not None else None, |
| 235 | + } |
| 236 | + return None |
0 commit comments