feat(ml): fuse two embedding models and protect the ML service with a token
This commit is contained in:
1 parent
bc00d6901f
commit
b24e5ff906
63 files changed
+2287
-276
No files matched your search
@@ -17,7 +17,12 @@ from lincodex_ml.api.models import (
|
||||
)
|
||||
from lincodex_ml.api.problems import ServiceUnavailableError
|
||||
from lincodex_ml.inference.embedder import InputKind
|
||||
from lincodex_ml.inference.runtime import EmbeddingRuntime, NotReadyError
|
||||
from lincodex_ml.inference.runtime import (
|
||||
CapacityError,
|
||||
EmbeddingRuntime,
|
||||
NotReadyError,
|
||||
UnknownModelError,
|
||||
)
|
||||
|
||||
router = APIRouter(tags=["inference"])
|
||||
|
||||
@@ -32,7 +37,7 @@ def get_runtime(request: Request) -> EmbeddingRuntime:
|
||||
operation_id="CreateMLEmbeddings",
|
||||
response_model=EmbeddingResponse,
|
||||
status_code=status.HTTP_200_OK,
|
||||
summary="Encode texts into normalized vectors with the pinned embedding model.",
|
||||
summary="Encode texts into normalized vectors with a pinned embedding model.",
|
||||
)
|
||||
async def create_embeddings(
|
||||
payload: EmbeddingRequest,
|
||||
@@ -41,14 +46,12 @@ async def create_embeddings(
|
||||
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:
|
||||
info, vectors = await run_in_threadpool(runtime.encode, kind, texts, payload.model)
|
||||
except UnknownModelError as error:
|
||||
raise RequestValidationError(
|
||||
[{"loc": ("body", "model"), "type": "value_error", "msg": "model not served"}]
|
||||
) from error
|
||||
except (NotReadyError, CapacityError) as error:
|
||||
raise ServiceUnavailableError from error
|
||||
return EmbeddingResponse(
|
||||
model=EmbeddingModel(
|
||||
|
||||
Reference in new issue
Block a user