File size: 1,742 Bytes
ec855e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""

AsyncEmbeddingsService:

- Wraps the synchronous EmbeddingsService to provide async interface

- Uses asyncio.to_thread to offload blocking model operations to a thread pool

- Manages concurrency with asyncio.Lock

"""

import asyncio
from typing import List, Optional
from .embeddings_service import EmbeddingsService

class AsyncEmbeddingsService:
    def __init__(self, model_name: Optional[str] = None):
        # We compose the synchronous service
        self._sync_service = EmbeddingsService(model_name)
        self._lock = asyncio.Lock()

    @property
    def is_ready(self) -> bool:
        return self._sync_service.is_ready

    @property
    def device(self):
        return self._sync_service.device

    async def load_model(self) -> None:
        """Async wrapper for loading the model."""
        if self.is_ready:
            return
            
        async with self._lock:
            if self.is_ready:
                return
            # Run the heavy lifting in a thread
            await asyncio.to_thread(self._sync_service.load_model)

    async def generate_single_embedding(self, text: str) -> List[float]:
        """

        Generate a single embedding asynchronously.

        """
        # Ensure model is loaded first (non-blocking check mostly)
        await self.load_model()
        return await asyncio.to_thread(self._sync_service.generate_single_embedding, text)

    async def generate_batch_embeddings(self, texts: List[str]) -> List[List[float]]:
        """

        Efficient batch processing asynchronously.

        """
        await self.load_model()
        return await asyncio.to_thread(self._sync_service.generate_batch_embeddings, texts)