feat(matching): rank with semantic embeddings and explain matches by evidence

This commit is contained in:
shira committed 2026-10-11 10:39:46 +03:00
1 parent facc1789b1
commit 24b64d698a
215 files changed
+16951 -327

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
"""POST /v1/embeddings: encode texts with the loaded encoder."""
from __future__ import annotations
from typing import Annotated
from fastapi import APIRouter, Depends, Request, status
from fastapi.concurrency import run_in_threadpool
from fastapi.exceptions import RequestValidationError
from lincodex_ml.api.models import (
EmbeddingInputKind,
EmbeddingModel,
EmbeddingRequest,
EmbeddingResponse,
EmbeddingVector,
)
from lincodex_ml.api.problems import ServiceUnavailableError
from lincodex_ml.inference.embedder import InputKind
from lincodex_ml.inference.runtime import EmbeddingRuntime, NotReadyError
router = APIRouter(tags=["inference"])
def get_runtime(request: Request) -> EmbeddingRuntime:
runtime: EmbeddingRuntime = request.app.state.embedding_runtime
return runtime
@router.post(
"/v1/embeddings",
operation_id="CreateMLEmbeddings",
response_model=EmbeddingResponse,
status_code=status.HTTP_200_OK,
summary="Encode texts into normalized vectors with the pinned embedding model.",
)
async def create_embeddings(
payload: EmbeddingRequest,
runtime: Annotated[EmbeddingRuntime, Depends(get_runtime)],
) -> EmbeddingResponse:
kind = InputKind(payload.kind.root)
texts = [text.root for text in payload.texts]
try:
# The service loads one model today; the field keeps the contract ready
# for several (an ensemble), and a model it does not serve is refused.
if payload.model is not None and payload.model != runtime.info().model_id:
raise RequestValidationError(
[{"loc": ("body", "model"), "type": "value_error", "msg": "model not served"}]
)
info, vectors = await run_in_threadpool(runtime.encode, kind, texts)
except NotReadyError as error:
raise ServiceUnavailableError from error
return EmbeddingResponse(
model=EmbeddingModel(
id=info.model_id,
revision=info.revision,
dimension=info.dimension,
max_tokens=info.max_tokens,
prompt_version=info.prompt_version,
),
kind=EmbeddingInputKind(payload.kind.root),
vectors=[EmbeddingVector(vector) for vector in vectors],
)