Скелет проекта
This commit is contained in:
29
src/rag_agent/index/embeddings.py
Normal file
29
src/rag_agent/index/embeddings.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
from dataclasses import dataclass
|
||||
from typing import Iterable, Protocol
|
||||
|
||||
|
||||
class EmbeddingClient(Protocol):
|
||||
def embed_texts(self, texts: Iterable[str]) -> list[list[float]]:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@dataclass
|
||||
class StubEmbeddingClient:
|
||||
dim: int
|
||||
|
||||
def embed_texts(self, texts: Iterable[str]) -> list[list[float]]:
|
||||
vectors: list[list[float]] = []
|
||||
for text in texts:
|
||||
digest = hashlib.sha256(text.encode("utf-8")).digest()
|
||||
values = [b / 255.0 for b in digest]
|
||||
if len(values) < self.dim:
|
||||
values = (values * ((self.dim // len(values)) + 1))[: self.dim]
|
||||
vectors.append(values[: self.dim])
|
||||
return vectors
|
||||
|
||||
|
||||
def get_embedding_client(dim: int) -> EmbeddingClient:
|
||||
return StubEmbeddingClient(dim=dim)
|
||||
Reference in New Issue
Block a user