davidlzs commited on
Commit
20403dd
·
1 Parent(s): a0d277b

add concurrent embedding limit

Browse files
Files changed (1) hide show
  1. lightrag/utils.py +20 -1
lightrag/utils.py CHANGED
@@ -17,6 +17,17 @@ import tiktoken
17
 
18
  from lightrag.prompt import PROMPTS
19
 
 
 
 
 
 
 
 
 
 
 
 
20
  ENCODER = None
21
 
22
  logger = logging.getLogger("lightrag")
@@ -42,9 +53,17 @@ class EmbeddingFunc:
42
  embedding_dim: int
43
  max_token_size: int
44
  func: callable
 
 
 
 
 
 
 
45
 
46
  async def __call__(self, *args, **kwargs) -> np.ndarray:
47
- return await self.func(*args, **kwargs)
 
48
 
49
 
50
  def locate_json_string_body_from_string(content: str) -> Union[str, None]:
 
17
 
18
  from lightrag.prompt import PROMPTS
19
 
20
+
21
+ class UnlimitedSemaphore:
22
+ """A context manager that allows unlimited access."""
23
+
24
+ async def __aenter__(self):
25
+ pass
26
+
27
+ async def __aexit__(self, exc_type, exc, tb):
28
+ pass
29
+
30
+
31
  ENCODER = None
32
 
33
  logger = logging.getLogger("lightrag")
 
53
  embedding_dim: int
54
  max_token_size: int
55
  func: callable
56
+ concurrent_limit: int = 16
57
+
58
+ def __post_init__(self):
59
+ if self.concurrent_limit != 0:
60
+ self._semaphore = asyncio.Semaphore(self.concurrent_limit)
61
+ else:
62
+ self._semaphore = UnlimitedSemaphore()
63
 
64
  async def __call__(self, *args, **kwargs) -> np.ndarray:
65
+ async with self._semaphore:
66
+ return await self.func(*args, **kwargs)
67
 
68
 
69
  def locate_json_string_body_from_string(content: str) -> Union[str, None]: