Skip to content

Commit

Permalink
add concurrent embedding limit
Browse files Browse the repository at this point in the history
  • Loading branch information
davidleon committed Dec 10, 2024
1 parent d0a4ef2 commit f6eeedb
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lightrag/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@

from lightrag.prompt import PROMPTS


class UnlimitedSemaphore:
"""A context manager that allows unlimited access."""

async def __aenter__(self):
pass

async def __aexit__(self, exc_type, exc, tb):
pass


ENCODER = None

logger = logging.getLogger("lightrag")
Expand All @@ -42,9 +53,17 @@ class EmbeddingFunc:
embedding_dim: int
max_token_size: int
func: callable
concurrent_limit: int = 16

def __post_init__(self):
if self.concurrent_limit != 0:
self._semaphore = asyncio.Semaphore(self.concurrent_limit)
else:
self._semaphore = UnlimitedSemaphore()

async def __call__(self, *args, **kwargs) -> np.ndarray:
return await self.func(*args, **kwargs)
async with self._semaphore:
return await self.func(*args, **kwargs)


def locate_json_string_body_from_string(content: str) -> Union[str, None]:
Expand Down

0 comments on commit f6eeedb

Please sign in to comment.