Add engine abstraction

This commit is contained in:
2025-09-26 14:38:44 +00:00
parent de9826a4b6
commit 0656ed93f1
6 changed files with 168 additions and 42 deletions

View File

@@ -0,0 +1,43 @@
from abc import ABC, abstractmethod
from typing import Generic, TypeVar
from ..models import Condition, SearchRow
ResponseType = TypeVar("ResponseType")
ConditionType = TypeVar("ConditionType")
__all__ = ["BaseEngine"]
class BaseEngine(ABC, Generic[ResponseType, ConditionType]):
@abstractmethod
def transform_conditions(
self, conditions: list[Condition] | None
) -> ConditionType | None: ...
@abstractmethod
def transform_response(self, response: ResponseType) -> list[SearchRow]: ...
@abstractmethod
async def run_similarity_query(
self,
embedding: list[float],
collection: str,
limit: int = 10,
conditions: ConditionType | None = None,
threshold: float | None = None,
) -> ResponseType: ...
async def semantic_search(
self,
vector: list[float],
collection: str,
limit: int = 10,
conditions: list[Condition] | None = None,
threshold: float | None = None,
) -> list[SearchRow]:
transformed_conditions = self.transform_conditions(conditions)
response = await self.run_similarity_query(
vector, collection, limit, transformed_conditions, threshold
)
return self.transform_response(response)