langfun 0.1.2.dev202505110803__py3-none-any.whl → 0.1.2.dev202505130804__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of langfun might be problematic. Click here for more details.

@@ -1019,11 +1019,21 @@ class LanguageModel(component.Component):
1019
1019
  ] = RetryableLMError,
1020
1020
  ) -> list[Any]:
1021
1021
  """Helper method for subclasses for implementing _sample."""
1022
+ if self.max_concurrency is None:
1023
+ execute = action
1024
+ executor = None
1025
+ max_workers = len(inputs)
1026
+ else:
1027
+ execute = lambda x: _ConcurrencyControl.get(
1028
+ self.resource_id, self.max_concurrency)(action, x)
1029
+ executor = self.resource_id if len(inputs) > 1 else None
1030
+ max_workers = self.max_concurrency
1031
+
1022
1032
  executed_jobs = concurrent.concurrent_execute(
1023
- action,
1033
+ execute,
1024
1034
  inputs,
1025
- executor=self.resource_id if self.max_concurrency else None,
1026
- max_workers=self.max_concurrency or len(inputs),
1035
+ executor=executor,
1036
+ max_workers=max_workers,
1027
1037
  retry_on_errors=retry_on_errors,
1028
1038
  max_attempts=self.max_attempts,
1029
1039
  retry_interval=self.retry_interval,
@@ -1323,6 +1333,46 @@ class LanguageModel(component.Component):
1323
1333
  return None
1324
1334
 
1325
1335
 
1336
+ class _ConcurrencyControl:
1337
+ """Controls the max concurrent LLM calls for a given model."""
1338
+
1339
+ _MODEL_CONCURRENCY: ClassVar[dict[str, '_ConcurrencyControl']] = {}
1340
+
1341
+ def __init__(self, max_concurrency: int):
1342
+ self.max_concurrency = max_concurrency
1343
+ self._concurrency = 0
1344
+
1345
+ @property
1346
+ def concurrency(self) -> int:
1347
+ """Returns the current concurrency."""
1348
+ return self._concurrency
1349
+
1350
+ def __call__(self, fn: Callable[..., Any], *args, **kwargs):
1351
+ """Calls the function with concurrency control."""
1352
+ while self._concurrency >= self.max_concurrency:
1353
+ time.sleep(0.01)
1354
+
1355
+ try:
1356
+ # Increment/decrement is atomic in Python, so we don't need to protect it
1357
+ # with a lock.
1358
+ self._concurrency += 1
1359
+ return fn(*args, **kwargs)
1360
+ finally:
1361
+ self._concurrency -= 1
1362
+
1363
+ @classmethod
1364
+ def get(
1365
+ cls, model_id: str, max_concurrency: int | None = None
1366
+ ) -> '_ConcurrencyControl':
1367
+ """Returns the concurrency control for the given model ID."""
1368
+ control = cls._MODEL_CONCURRENCY.get(model_id, None)
1369
+ if control is None:
1370
+ assert max_concurrency is not None
1371
+ control = cls(max_concurrency)
1372
+ cls._MODEL_CONCURRENCY[model_id] = control
1373
+ return control
1374
+
1375
+
1326
1376
  class UsageSummary(pg.Object, pg.views.HtmlTreeView.Extension):
1327
1377
  """Usage sumary."""
1328
1378
 
@@ -678,9 +678,15 @@ class Gemini(rest.REST):
678
678
  return config
679
679
 
680
680
  def result(self, json: dict[str, Any]) -> lf.LMSamplingResult:
681
+ candidates = json.get('candidates')
682
+ if candidates is None:
683
+ raise lf.TemporaryLMError(
684
+ 'No candidates found in response. This is a Gemini API issue that '
685
+ 'happens occasionally, and retrying should fix it. '
686
+ )
681
687
  messages = [
682
688
  lf.Message.from_value(candidate['content'], format='gemini')
683
- for candidate in json.get('candidates', [])
689
+ for candidate in candidates
684
690
  ]
685
691
  usage = json['usageMetadata']
686
692
  input_tokens = usage['promptTokenCount']
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langfun
3
- Version: 0.1.2.dev202505110803
3
+ Version: 0.1.2.dev202505130804
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -8,7 +8,7 @@ langfun/core/console.py,sha256=V_mOiFi9oGh8gLsUeR56pdFDkuvYOpvQt7DY1KUTWTA,2535
8
8
  langfun/core/console_test.py,sha256=pBOcuNMJdVELywvroptfcRtJMsegMm3wSlHAL2TdxVk,1679
9
9
  langfun/core/langfunc.py,sha256=G50YgoVZ0y1GFw2ev41MlOqr6qa8YakbvNC0h_E0PiA,11140
10
10
  langfun/core/langfunc_test.py,sha256=CDn-gJCa5EnjN7cotAVCfSCbuzddq2o-HzEt7kV8HbY,8882
11
- langfun/core/language_model.py,sha256=c6Vds9qXuC0BiHeZstxCTTsoU8ZL9K4w4co_hXA3-RI,46336
11
+ langfun/core/language_model.py,sha256=dw81qgdl-zn-qOIbRMSurtAmmpiwr2sSYlSlW19nUw0,47829
12
12
  langfun/core/language_model_test.py,sha256=iA5uo7rIj2jAtCYzMzhyNg1fWqE2Onn60bOO58q72C0,36454
13
13
  langfun/core/logging.py,sha256=ypjSwGzQpIhadewEKkIH5VWS55rfU4e6mlni41PMHlw,9116
14
14
  langfun/core/logging_test.py,sha256=vbVGOQxwMmVSiFfbt2897gUt-8nqDpV64jCAeUG_q5U,6924
@@ -92,7 +92,7 @@ langfun/core/llms/deepseek.py,sha256=jvTxdXPr-vH6HNakn_Ootx1heDg8Fen2FUkUW36bpCs
92
92
  langfun/core/llms/deepseek_test.py,sha256=DvROWPlDuow5E1lfoSkhyGt_ELA19JoQoDsTnRgDtTg,1847
93
93
  langfun/core/llms/fake.py,sha256=xmgCkk9y0I4x0IT32SZ9_OT27aLadXH8PRiYNo5VTd4,3265
94
94
  langfun/core/llms/fake_test.py,sha256=2h13qkwEz_JR0mtUDPxdAhQo7MueXaFSwsD2DIRDW9g,7653
95
- langfun/core/llms/gemini.py,sha256=UGI89WjBTZHBAmWbGh4FUCryw6OW3yuiN7htY-Xv_zM,25358
95
+ langfun/core/llms/gemini.py,sha256=ij3NJHhAcs8-H4H2P4ny78q5L9iAmjQC4cu8nUyoPDA,25591
96
96
  langfun/core/llms/gemini_test.py,sha256=Ve9X2Wvwu9wVFHpKZDP-qoM1_hzB4kgt6_HR9wxtNkg,7592
97
97
  langfun/core/llms/google_genai.py,sha256=YjvyasibmXYPFr_sxdrIdHDPHfaNzMCiJjWJs0iQyms,5188
98
98
  langfun/core/llms/google_genai_test.py,sha256=NKNtpebArQ9ZR7Qsnhd2prFIpMjleojy6o6VMXkJ1zY,1502
@@ -156,8 +156,8 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
156
156
  langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
157
157
  langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
158
158
  langfun/core/templates/selfplay_test.py,sha256=Ot__1P1M8oJfoTp-M9-PQ6HUXqZKyMwvZ5f7yQ3yfyM,2326
159
- langfun-0.1.2.dev202505110803.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
160
- langfun-0.1.2.dev202505110803.dist-info/METADATA,sha256=j97AkwZGIIKZJQPvRAgH1zf2fbdLkWQ6DwN-ZFXUtbI,8178
161
- langfun-0.1.2.dev202505110803.dist-info/WHEEL,sha256=DnLRTWE75wApRYVsjgc6wsVswC54sMSJhAEd4xhDpBk,91
162
- langfun-0.1.2.dev202505110803.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
163
- langfun-0.1.2.dev202505110803.dist-info/RECORD,,
159
+ langfun-0.1.2.dev202505130804.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
160
+ langfun-0.1.2.dev202505130804.dist-info/METADATA,sha256=i-rMyyw0gOG8YgoQmvMEXZUGF9OPziH59QA8SJpLS4g,8178
161
+ langfun-0.1.2.dev202505130804.dist-info/WHEEL,sha256=DnLRTWE75wApRYVsjgc6wsVswC54sMSJhAEd4xhDpBk,91
162
+ langfun-0.1.2.dev202505130804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
163
+ langfun-0.1.2.dev202505130804.dist-info/RECORD,,