Skip to content

Commit

Permalink
refactor: pass callable instead of object
Browse files Browse the repository at this point in the history
  • Loading branch information
winstxnhdw committed Sep 24, 2024
1 parent 14a66a5 commit bd4a96e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 26 deletions.
2 changes: 1 addition & 1 deletion server/api/v1/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def upload_files(
chunk_generator = store_chunks(
redis,
chat_id,
embedder,
embedder.encode_normalise,
extract_documents_from_pdfs([{'data': BytesIO(await file.read()), 'name': file.filename} for file in data]),
chunk_document,
text_splitter,
Expand Down
29 changes: 4 additions & 25 deletions server/databases/redis/features/store_chunks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
from asyncio import gather
from typing import (
AsyncGenerator,
Callable,
Iterable,
Protocol,
)
from typing import AsyncIterator, Callable, Iterable

from numpy import float32
from numpy.typing import NDArray
Expand All @@ -15,30 +10,14 @@
from server.features.extraction.models import Document


class Embedder(Protocol):
"""
Summary
-------
a generic protocol for embedding text
"""

def encode_normalise(self, sentences: str | list[str]) -> NDArray[float32]:
"""
Summary
-------
encode a sentence or list of sentences into a normalised embedding
"""
...


async def store_chunks(
redis: RedisAsync,
chat_id: str,
embedder: Embedder,
embedder: Callable[[str], NDArray[float32]],
documents: Iterable[Document | None],
chunker: Callable[[Document, TextSplitter], Iterable[Chunk]],
text_splitter: TextSplitter,
) -> AsyncGenerator[tuple[str, str] | tuple[None, None], None]:
) -> AsyncIterator[tuple[str, str] | tuple[None, None]]:
"""
Summary
-------
Expand Down Expand Up @@ -70,7 +49,7 @@ async def store_chunks(
chunk.source_id,
chunk.id,
mapping={
'vector': embedder.encode_normalise(chunk.content).tobytes(),
'vector': embedder(chunk.content).tobytes(),
'content': chunk.content,
'chat_id': chat_id,
},
Expand Down

0 comments on commit bd4a96e

Please sign in to comment.