Narrow types using @overload in embedders

This commit is contained in:
Yiorgis Gozadinos 2025-10-15 10:39:00 +03:00
parent 7a119dd5aa
commit eca68cd30c
No known key found for this signature in database
5 changed files with 40 additions and 0 deletions

View file

@ -1,3 +1,5 @@
from typing import overload
from haiku.rag.config import Config from haiku.rag.config import Config
@ -9,6 +11,12 @@ class EmbedderBase:
self._model = model self._model = model
self._vector_dim = vector_dim self._vector_dim = vector_dim
@overload
async def embed(self, text: str) -> list[float]: ...
@overload
async def embed(self, text: list[str]) -> list[list[float]]: ...
async def embed(self, text: str | list[str]) -> list[float] | list[list[float]]: async def embed(self, text: str | list[str]) -> list[float] | list[list[float]]:
raise NotImplementedError( raise NotImplementedError(
"Embedder is an abstract class. Please implement the embed method in a subclass." "Embedder is an abstract class. Please implement the embed method in a subclass."

View file

@ -1,3 +1,5 @@
from typing import overload
from openai import AsyncOpenAI from openai import AsyncOpenAI
from haiku.rag.config import Config from haiku.rag.config import Config
@ -5,6 +7,12 @@ from haiku.rag.embeddings.base import EmbedderBase
class Embedder(EmbedderBase): class Embedder(EmbedderBase):
@overload
async def embed(self, text: str) -> list[float]: ...
@overload
async def embed(self, text: list[str]) -> list[list[float]]: ...
async def embed(self, text: str | list[str]) -> list[float] | list[list[float]]: async def embed(self, text: str | list[str]) -> list[float] | list[list[float]]:
client = AsyncOpenAI(base_url=f"{Config.OLLAMA_BASE_URL}/v1", api_key="dummy") client = AsyncOpenAI(base_url=f"{Config.OLLAMA_BASE_URL}/v1", api_key="dummy")
if not text: if not text:

View file

@ -1,9 +1,17 @@
from typing import overload
from openai import AsyncOpenAI from openai import AsyncOpenAI
from haiku.rag.embeddings.base import EmbedderBase from haiku.rag.embeddings.base import EmbedderBase
class Embedder(EmbedderBase): class Embedder(EmbedderBase):
@overload
async def embed(self, text: str) -> list[float]: ...
@overload
async def embed(self, text: list[str]) -> list[list[float]]: ...
async def embed(self, text: str | list[str]) -> list[float] | list[list[float]]: async def embed(self, text: str | list[str]) -> list[float] | list[list[float]]:
client = AsyncOpenAI() client = AsyncOpenAI()
if not text: if not text:

View file

@ -1,3 +1,5 @@
from typing import overload
from openai import AsyncOpenAI from openai import AsyncOpenAI
from haiku.rag.config import Config from haiku.rag.config import Config
@ -5,6 +7,12 @@ from haiku.rag.embeddings.base import EmbedderBase
class Embedder(EmbedderBase): class Embedder(EmbedderBase):
@overload
async def embed(self, text: str) -> list[float]: ...
@overload
async def embed(self, text: list[str]) -> list[list[float]]: ...
async def embed(self, text: str | list[str]) -> list[float] | list[list[float]]: async def embed(self, text: str | list[str]) -> list[float] | list[list[float]]:
client = AsyncOpenAI( client = AsyncOpenAI(
base_url=f"{Config.VLLM_EMBEDDINGS_BASE_URL}/v1", api_key="dummy" base_url=f"{Config.VLLM_EMBEDDINGS_BASE_URL}/v1", api_key="dummy"

View file

@ -1,9 +1,17 @@
try: try:
from typing import overload
from voyageai.client import Client # type: ignore from voyageai.client import Client # type: ignore
from haiku.rag.embeddings.base import EmbedderBase from haiku.rag.embeddings.base import EmbedderBase
class Embedder(EmbedderBase): class Embedder(EmbedderBase):
@overload
async def embed(self, text: str) -> list[float]: ...
@overload
async def embed(self, text: list[str]) -> list[list[float]]: ...
async def embed(self, text: str | list[str]) -> list[float] | list[list[float]]: async def embed(self, text: str | list[str]) -> list[float] | list[list[float]]:
client = Client() client = Client()
if not text: if not text: