File size: 5,608 Bytes
ca033f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ml_service/main.py

import logging
import time
import os
import torch
from typing import List
from contextlib import asynccontextmanager

from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel, Field, constr
from sentence_transformers import SentenceTransformer
from fastembed import SparseTextEmbedding

# -----------------------------
# Configuration
# -----------------------------

MAX_TEXT_LENGTH = 5000
MAX_BATCH_SIZE = 32
DENSE_MODEL_NAME = "nomic-ai/nomic-embed-text-v1.5"
SPARSE_MODEL_NAME = "prithivida/Splade_PP_en_v1"

# -----------------------------
# Structured Logging Setup
# -----------------------------

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)

logger = logging.getLogger("athena.vector_engine")

# -----------------------------
# Lifespan Management
# -----------------------------

@asynccontextmanager
async def lifespan(app: FastAPI):
    logger.info("🧠 Booting Vector Engine...")

    start_time = time.time()

    try:
        device = "cuda" if torch.cuda.is_available() else "cpu"
        logger.info(f"Using device: {device}")

        # Load dense model
        dense_model = SentenceTransformer(
            DENSE_MODEL_NAME,
            trust_remote_code=True,
            device=device,
        )

        # Load sparse model
        sparse_model = SparseTextEmbedding(
            model_name=SPARSE_MODEL_NAME
        )

        # Warmup (prevents cold-start latency spike)
        logger.info("πŸ”₯ Warming up models...")
        dense_model.encode("warmup", normalize_embeddings=True)
        list(sparse_model.embed(["warmup"]))

        # Attach to app state
        app.state.dense_model = dense_model
        app.state.sparse_model = sparse_model
        app.state.device = device
        app.state.start_time = time.time()

        duration = time.time() - start_time
        logger.info(f"βœ… Models loaded successfully in {duration:.2f}s")

        yield

    except Exception as e:
        logger.exception("❌ Failed during startup")
        raise e

    finally:
        logger.info("πŸ›‘ Shutting down Vector Engine...")
        app.state.__dict__.clear()

# -----------------------------
# FastAPI App
# -----------------------------

app = FastAPI(
    title="Athena Vector Engine",
    description="Production-grade ML microservice for dense + sparse embeddings",
    version="2.0.0",
    lifespan=lifespan,
)

# -----------------------------
# Schemas
# -----------------------------

class VectorRequest(BaseModel):
    texts: List[constr(min_length=1, max_length=MAX_TEXT_LENGTH)] = Field(
        ..., description="List of input texts to embed"
    )

class SparseData(BaseModel):
    indices: List[int]
    values: List[float]

class VectorResponse(BaseModel):
    dense_vectors: List[List[float]]
    sparse_vectors: List[SparseData]

# -----------------------------
# Embedding Endpoint
# -----------------------------

@app.post("/vectorize", response_model=VectorResponse)
def generate_vectors(req: VectorRequest, request: Request):

    if len(req.texts) > MAX_BATCH_SIZE:
        raise HTTPException(
            status_code=400,
            detail=f"Batch size exceeds maximum limit of {MAX_BATCH_SIZE}",
        )

    dense_model = request.app.state.dense_model
    sparse_model = request.app.state.sparse_model

    try:
        start_time = time.perf_counter()

        # Prefix required for Nomic retrieval queries
        prefixed_texts = [f"search_query: {text}" for text in req.texts]

        # Dense embeddings (batched)
        dense_results = dense_model.encode(
            prefixed_texts,
            normalize_embeddings=True,
            batch_size=len(prefixed_texts),
        ).tolist()

        # Sparse embeddings (batched)
        sparse_raw = list(sparse_model.embed(req.texts))

        sparse_results = [
            {
                "indices": vec.indices.tolist(),
                "values": vec.values.tolist(),
            }
            for vec in sparse_raw
        ]

        duration = time.perf_counter() - start_time

        logger.info(
            f"Vectorized batch_size={len(req.texts)} "
            f"latency={duration:.4f}s"
        )

        return {
            "dense_vectors": dense_results,
            "sparse_vectors": sparse_results,
        }

    except Exception as e:
        logger.exception("πŸ”₯ Vectorization failed")
        raise HTTPException(
            status_code=500,
            detail="Failed to generate embeddings",
        )

# -----------------------------
# Health Endpoints
# -----------------------------

@app.api_route("/health/live", methods=["GET", "HEAD"])
async def liveness():
    return {"status": "alive"}

@app.api_route("/health/ready", methods=["GET", "HEAD"])
async def readiness(request: Request):
    ready = (
        hasattr(request.app.state, "dense_model")
        and hasattr(request.app.state, "sparse_model")
    )
    return {"ready": ready}

# -----------------------------
# Metadata Endpoint
# -----------------------------

@app.get("/info")
async def model_info(request: Request):
    dense_model = request.app.state.dense_model
    device = request.app.state.device

    return {
        "dense_model": DENSE_MODEL_NAME,
        "sparse_model": SPARSE_MODEL_NAME,
        "embedding_dimension": dense_model.get_sentence_embedding_dimension(),
        "device": device,
        "uptime_seconds": int(time.time() - request.app.state.start_time),
        "max_batch_size": MAX_BATCH_SIZE,
        "max_text_length": MAX_TEXT_LENGTH,
    }