langfun 0.1.2.dev202504150804__py3-none-any.whl → 0.1.2.dev202504160804__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.
- langfun/core/__init__.py +2 -0
- langfun/core/language_model.py +8 -0
- langfun/core/llms/anthropic.py +5 -0
- langfun/core/llms/anthropic_test.py +11 -0
- langfun/core/llms/gemini.py +6 -0
- langfun/core/llms/gemini_test.py +16 -0
- langfun/core/llms/openai_compatible.py +6 -0
- langfun/core/llms/openai_compatible_test.py +18 -0
- {langfun-0.1.2.dev202504150804.dist-info → langfun-0.1.2.dev202504160804.dist-info}/METADATA +1 -1
- {langfun-0.1.2.dev202504150804.dist-info → langfun-0.1.2.dev202504160804.dist-info}/RECORD +13 -13
- {langfun-0.1.2.dev202504150804.dist-info → langfun-0.1.2.dev202504160804.dist-info}/WHEEL +0 -0
- {langfun-0.1.2.dev202504150804.dist-info → langfun-0.1.2.dev202504160804.dist-info}/licenses/LICENSE +0 -0
- {langfun-0.1.2.dev202504150804.dist-info → langfun-0.1.2.dev202504160804.dist-info}/top_level.txt +0 -0
langfun/core/__init__.py
CHANGED
@@ -111,6 +111,8 @@ from langfun.core.language_model import LMCache
|
|
111
111
|
from langfun.core.language_model import LMDebugMode
|
112
112
|
|
113
113
|
from langfun.core.language_model import LMError
|
114
|
+
from langfun.core.language_model import LMInputError
|
115
|
+
from langfun.core.language_model import ContextLimitError
|
114
116
|
from langfun.core.language_model import RetryableLMError
|
115
117
|
from langfun.core.language_model import RateLimitError
|
116
118
|
from langfun.core.language_model import TemporaryLMError
|
langfun/core/language_model.py
CHANGED
@@ -40,6 +40,14 @@ class LMError(RuntimeError):
|
|
40
40
|
"""Base class for language model errors."""
|
41
41
|
|
42
42
|
|
43
|
+
class LMInputError(LMError):
|
44
|
+
"""Base class for errors with bad input."""
|
45
|
+
|
46
|
+
|
47
|
+
class ContextLimitError(LMInputError):
|
48
|
+
"""Error for context limit exceeded."""
|
49
|
+
|
50
|
+
|
43
51
|
class RetryableLMError(LMError):
|
44
52
|
"""Base class for LLM errors that can be solved by retrying."""
|
45
53
|
|
langfun/core/llms/anthropic.py
CHANGED
@@ -573,6 +573,11 @@ class Anthropic(rest.REST):
|
|
573
573
|
),
|
574
574
|
)
|
575
575
|
|
576
|
+
def _error(self, status_code: int, content: str) -> lf.LMError:
|
577
|
+
if status_code == 413 and b'Prompt is too long' in content:
|
578
|
+
return lf.ContextLimitError(f'{status_code}: {content}')
|
579
|
+
return super()._error(status_code, content)
|
580
|
+
|
576
581
|
|
577
582
|
class Claude37(Anthropic):
|
578
583
|
"""Base class for Claude 3.7 models."""
|
@@ -217,6 +217,17 @@ class AnthropicTest(unittest.TestCase):
|
|
217
217
|
):
|
218
218
|
lm('hello', max_attempts=1)
|
219
219
|
|
220
|
+
def test_call_with_context_limit_error(self):
|
221
|
+
with mock.patch('requests.Session.post') as mock_request:
|
222
|
+
mock_request.side_effect = mock_requests_post_error(
|
223
|
+
413, 'bad_request', 'Prompt is too long.'
|
224
|
+
)
|
225
|
+
lm = anthropic.Claude3Haiku(api_key='fake_key')
|
226
|
+
with self.assertRaisesRegex(
|
227
|
+
lf.ContextLimitError, 'Prompt is too long.'
|
228
|
+
):
|
229
|
+
lm('hello', max_attempts=1)
|
230
|
+
|
220
231
|
def test_lm_get(self):
|
221
232
|
self.assertIsInstance(
|
222
233
|
lf.LanguageModel.get('claude-3-5-sonnet-latest'),
|
langfun/core/llms/gemini.py
CHANGED
@@ -645,3 +645,9 @@ class Gemini(rest.REST):
|
|
645
645
|
total_tokens=input_tokens + output_tokens,
|
646
646
|
),
|
647
647
|
)
|
648
|
+
|
649
|
+
def _error(self, status_code: int, content: str) -> lf.LMError:
|
650
|
+
if (status_code == 400
|
651
|
+
and b'exceeds the maximum number of tokens' in content):
|
652
|
+
return lf.ContextLimitError(f'{status_code}: {content}')
|
653
|
+
return super()._error(status_code, content)
|
langfun/core/llms/gemini_test.py
CHANGED
@@ -178,6 +178,22 @@ class GeminiTest(unittest.TestCase):
|
|
178
178
|
self.assertEqual(r.metadata.usage.prompt_tokens, 3)
|
179
179
|
self.assertEqual(r.metadata.usage.completion_tokens, 4)
|
180
180
|
|
181
|
+
def test_call_model_with_context_limit_error(self):
|
182
|
+
def mock_requests_post_error(*args, **kwargs):
|
183
|
+
del args, kwargs
|
184
|
+
response = requests.Response()
|
185
|
+
response.status_code = 400
|
186
|
+
response._content = b'exceeds the maximum number of tokens.'
|
187
|
+
return response
|
188
|
+
|
189
|
+
with mock.patch('requests.Session.post') as mock_generate:
|
190
|
+
mock_generate.side_effect = mock_requests_post_error
|
191
|
+
lm = gemini.Gemini('gemini-1.5-pro', api_endpoint='')
|
192
|
+
with self.assertRaisesRegex(
|
193
|
+
lf.ContextLimitError, 'exceeds the maximum number of tokens.'
|
194
|
+
):
|
195
|
+
lm('hello')
|
196
|
+
|
181
197
|
def test_call_model_with_system_message(self):
|
182
198
|
with mock.patch('requests.Session.post') as mock_generate:
|
183
199
|
mock_generate.side_effect = mock_requests_post
|
@@ -159,3 +159,9 @@ class OpenAICompatible(rest.REST):
|
|
159
159
|
|
160
160
|
),
|
161
161
|
)
|
162
|
+
|
163
|
+
def _error(self, status_code: int, content: str) -> lf.LMError:
|
164
|
+
if (status_code == 413
|
165
|
+
or (status_code == 400 and b'string_above_max_length' in content)):
|
166
|
+
return lf.ContextLimitError(f'{status_code}: {content}')
|
167
|
+
return super()._error(status_code, content)
|
@@ -505,6 +505,24 @@ class OpenAIComptibleTest(unittest.TestCase):
|
|
505
505
|
):
|
506
506
|
lm(lf.UserMessage('hello', json_schema={}))
|
507
507
|
|
508
|
+
def test_call_with_context_limit_error(self):
|
509
|
+
def mock_context_limit_error(*args, **kwargs):
|
510
|
+
del args, kwargs
|
511
|
+
response = requests.Response()
|
512
|
+
response.status_code = 400
|
513
|
+
response._content = b'string_above_max_length'
|
514
|
+
return response
|
515
|
+
|
516
|
+
with mock.patch('requests.Session.post') as mock_request:
|
517
|
+
mock_request.side_effect = mock_context_limit_error
|
518
|
+
lm = openai_compatible.OpenAICompatible(
|
519
|
+
api_endpoint='https://test-server', model='test-model'
|
520
|
+
)
|
521
|
+
with self.assertRaisesRegex(
|
522
|
+
lf.ContextLimitError, 'string_above_max_length'
|
523
|
+
):
|
524
|
+
lm(lf.UserMessage('hello'))
|
525
|
+
|
508
526
|
|
509
527
|
if __name__ == '__main__':
|
510
528
|
unittest.main()
|
@@ -1,5 +1,5 @@
|
|
1
1
|
langfun/__init__.py,sha256=LuB1difPJsI8V9H4kmUJM2DAEmMEV_WtaBLGYp3qEFM,2527
|
2
|
-
langfun/core/__init__.py,sha256
|
2
|
+
langfun/core/__init__.py,sha256=pW4prpiyWNkRbtWBGYF1thn7_0F_TgDVfAIZPvGn6HA,4758
|
3
3
|
langfun/core/component.py,sha256=g1kQM0bryYYYWVDrSMnHfc74wIBbpfe5_B3s-UIP5GE,3028
|
4
4
|
langfun/core/component_test.py,sha256=0CxTgjAud3aj8wBauFhG2FHDqrxCTl4OI4gzQTad-40,9254
|
5
5
|
langfun/core/concurrent.py,sha256=zY-pXqlGqss_GI20tM1gXvyW8QepVPUuFNmutcIdhbI,32760
|
@@ -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=kK486D5R3ifI6jN8ycTfnFwwghHAoLCj-g5F67izMZM,8848
|
11
|
-
langfun/core/language_model.py,sha256=
|
11
|
+
langfun/core/language_model.py,sha256=BMJFbx8fGZi9JKvge7DPjwWQWn7POBov_XHr7BR_ks0,45945
|
12
12
|
langfun/core/language_model_test.py,sha256=iA5uo7rIj2jAtCYzMzhyNg1fWqE2Onn60bOO58q72C0,36454
|
13
13
|
langfun/core/logging.py,sha256=W3mLEMXdo210Q5OX3a1ZTc4nU-xMy73-IfNKnsA-RFo,8051
|
14
14
|
langfun/core/logging_test.py,sha256=N7-YvSXC8zvnr2SNwWHOykn1CFmqvIuTLDgn41Ku9JU,6642
|
@@ -82,8 +82,8 @@ langfun/core/eval/v2/reporting_test.py,sha256=hcPJJaMtPulqERvHYTpId83WXdqDKnnexm
|
|
82
82
|
langfun/core/eval/v2/runners.py,sha256=De4d5QQ-Tpw0nPDODQexDPy0ti-FEgzHBvfH78zqdtg,15945
|
83
83
|
langfun/core/eval/v2/runners_test.py,sha256=A37fKK2MvAVTiShsg_laluJzJ9AuAQn52k7HPbfD0Ks,11666
|
84
84
|
langfun/core/llms/__init__.py,sha256=bwVMDRc4ypUY8KntfAccjVbqFxUzSGmvB6kHTcXmkLk,8255
|
85
|
-
langfun/core/llms/anthropic.py,sha256=
|
86
|
-
langfun/core/llms/anthropic_test.py,sha256=
|
85
|
+
langfun/core/llms/anthropic.py,sha256=qaclpfX3qeHoZMDxU3Gn-638Vi4IyCbxdow3zgGUHK4,22195
|
86
|
+
langfun/core/llms/anthropic_test.py,sha256=dFnNvrgwCYUseDuiuWCBoQ5jloYX9RIlZQf7cCLPNU4,8282
|
87
87
|
langfun/core/llms/azure_openai.py,sha256=-KkSLaR54MlsIqz_XIwv0TnsBnvNTAxnjA2Q2O2u5KM,2733
|
88
88
|
langfun/core/llms/azure_openai_test.py,sha256=lkMZkQdJBV97fTM4C4z8qNfvr6spgiN5G4hvVUIVr0M,1735
|
89
89
|
langfun/core/llms/compositional.py,sha256=csW_FLlgL-tpeyCOTVvfUQkMa_zCN5Y2I-YbSNuK27U,2872
|
@@ -92,8 +92,8 @@ 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=
|
96
|
-
langfun/core/llms/gemini_test.py,sha256=
|
95
|
+
langfun/core/llms/gemini.py,sha256=VwkNFoMQ763TQM90lj2bBf9dszxoRmUjrgiqL3_ALYE,23223
|
96
|
+
langfun/core/llms/gemini_test.py,sha256=d9Pvf3xmHgofv8AKXmbnfndsScxmgR5q_ctSIvEXYrU,6808
|
97
97
|
langfun/core/llms/google_genai.py,sha256=IANJlhTEvBNAgmK8Rsc_6xguQmwgE76GayESqae4NMk,4906
|
98
98
|
langfun/core/llms/google_genai_test.py,sha256=NKNtpebArQ9ZR7Qsnhd2prFIpMjleojy6o6VMXkJ1zY,1502
|
99
99
|
langfun/core/llms/groq.py,sha256=S9V10kFo3cgX89qPgt_umq-SpRnxEDLTt_hJmpERfbo,12066
|
@@ -101,8 +101,8 @@ langfun/core/llms/groq_test.py,sha256=P4EgexCqsh4K2x11w0UL_vz-YYNaPdQU0WsDAdnTRQ
|
|
101
101
|
langfun/core/llms/llama_cpp.py,sha256=Z7P3gc4xeIjc2bX0Ey1y5EUYJVMnMa2Q67PZ9iye9sE,1409
|
102
102
|
langfun/core/llms/llama_cpp_test.py,sha256=wfTO7nmUwL65U2kK9P9fcMt92JjNDuVia4G1E7znf_4,1086
|
103
103
|
langfun/core/llms/openai.py,sha256=zDi-wkV-r3vUZYoTFvU1gaNNVQVQytVuZ4CvTGLsRL8,39576
|
104
|
-
langfun/core/llms/openai_compatible.py,sha256=
|
105
|
-
langfun/core/llms/openai_compatible_test.py,sha256=
|
104
|
+
langfun/core/llms/openai_compatible.py,sha256=9REGdRDJ0sSR-4IpnQ97Qzz5MQSF07QNvTy50qFr0Dg,5561
|
105
|
+
langfun/core/llms/openai_compatible_test.py,sha256=KwOMA7tsmOxFBjezltkBDSU77AvOQkI23dO2nHLAlB4,17689
|
106
106
|
langfun/core/llms/openai_test.py,sha256=gwuO6aoa296iM2welWV9ua4KF8gEVGsEPakgbtkWkFQ,2687
|
107
107
|
langfun/core/llms/rest.py,sha256=ucMKHXlmg6pYSIMhQSktLmTSGMSIiqO8fp1r_GiEhaU,4333
|
108
108
|
langfun/core/llms/rest_test.py,sha256=_zM7nV8DEVyoXNiQOnuwJ917mWjki0614H88rNmDboE,5020
|
@@ -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.
|
160
|
-
langfun-0.1.2.
|
161
|
-
langfun-0.1.2.
|
162
|
-
langfun-0.1.2.
|
163
|
-
langfun-0.1.2.
|
159
|
+
langfun-0.1.2.dev202504160804.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
|
160
|
+
langfun-0.1.2.dev202504160804.dist-info/METADATA,sha256=by1MYh0J7o1UD-4GH_kCqol5IfTCq31R4nQry5OsQ6g,7692
|
161
|
+
langfun-0.1.2.dev202504160804.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
162
|
+
langfun-0.1.2.dev202504160804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
|
163
|
+
langfun-0.1.2.dev202504160804.dist-info/RECORD,,
|
File without changes
|
{langfun-0.1.2.dev202504150804.dist-info → langfun-0.1.2.dev202504160804.dist-info}/licenses/LICENSE
RENAMED
File without changes
|
{langfun-0.1.2.dev202504150804.dist-info → langfun-0.1.2.dev202504160804.dist-info}/top_level.txt
RENAMED
File without changes
|