langfun 0.0.2.dev20240330__py3-none-any.whl → 0.0.2.dev20240511__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/__init__.py +7 -0
- langfun/core/__init__.py +1 -0
- langfun/core/coding/python/correction.py +0 -7
- langfun/core/component.py +6 -0
- langfun/core/component_test.py +1 -0
- langfun/core/eval/__init__.py +15 -0
- langfun/core/eval/base.py +665 -95
- langfun/core/eval/base_test.py +224 -53
- langfun/core/eval/matching.py +48 -30
- langfun/core/eval/matching_test.py +25 -3
- langfun/core/eval/patching.py +130 -0
- langfun/core/eval/patching_test.py +170 -0
- langfun/core/eval/scoring.py +19 -10
- langfun/core/eval/scoring_test.py +21 -3
- langfun/core/langfunc.py +1 -22
- langfun/core/langfunc_test.py +10 -4
- langfun/core/language_model.py +130 -24
- langfun/core/language_model_test.py +249 -26
- langfun/core/llms/__init__.py +27 -2
- langfun/core/llms/anthropic.py +263 -0
- langfun/core/llms/anthropic_test.py +167 -0
- langfun/core/llms/cache/in_memory_test.py +37 -28
- langfun/core/llms/fake.py +34 -25
- langfun/core/llms/fake_test.py +122 -11
- langfun/core/llms/google_genai.py +8 -0
- langfun/core/llms/google_genai_test.py +8 -3
- langfun/core/llms/groq.py +260 -0
- langfun/core/llms/groq_test.py +170 -0
- langfun/core/llms/llama_cpp.py +3 -1
- langfun/core/llms/openai.py +100 -81
- langfun/core/llms/openai_test.py +287 -60
- langfun/core/llms/vertexai.py +291 -0
- langfun/core/llms/vertexai_test.py +233 -0
- langfun/core/modalities/image.py +1 -3
- langfun/core/modalities/mime.py +6 -0
- langfun/core/modalities/video.py +6 -5
- langfun/core/structured/__init__.py +5 -0
- langfun/core/structured/completion_test.py +2 -2
- langfun/core/structured/function_generation.py +245 -0
- langfun/core/structured/function_generation_test.py +329 -0
- langfun/core/structured/mapping.py +61 -3
- langfun/core/structured/mapping_test.py +17 -0
- langfun/core/structured/parsing_test.py +18 -13
- langfun/core/structured/prompting.py +61 -12
- langfun/core/structured/prompting_test.py +122 -12
- langfun/core/structured/schema.py +38 -6
- langfun/core/structured/schema_generation_test.py +2 -2
- langfun/core/structured/schema_test.py +36 -7
- langfun/core/structured/scoring.py +4 -1
- langfun/core/structured/scoring_test.py +6 -0
- langfun/core/template.py +147 -11
- langfun/core/template_test.py +75 -0
- langfun/core/templates/selfplay_test.py +6 -2
- {langfun-0.0.2.dev20240330.dist-info → langfun-0.0.2.dev20240511.dist-info}/METADATA +3 -2
- langfun-0.0.2.dev20240511.dist-info/RECORD +112 -0
- langfun-0.0.2.dev20240330.dist-info/RECORD +0 -102
- {langfun-0.0.2.dev20240330.dist-info → langfun-0.0.2.dev20240511.dist-info}/LICENSE +0 -0
- {langfun-0.0.2.dev20240330.dist-info → langfun-0.0.2.dev20240511.dist-info}/WHEEL +0 -0
- {langfun-0.0.2.dev20240330.dist-info → langfun-0.0.2.dev20240511.dist-info}/top_level.txt +0 -0
langfun/core/llms/openai.py
CHANGED
@@ -26,65 +26,55 @@ from openai import openai_object
|
|
26
26
|
import pyglove as pg
|
27
27
|
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
#
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
('babbage', 4),
|
79
|
-
('ada', 4),
|
80
|
-
# GPT-3 base models without instruction tuning.
|
81
|
-
('babbage-002', 4),
|
82
|
-
('davinci-002', 4),
|
83
|
-
]
|
84
|
-
|
85
|
-
|
86
|
-
# Model concurreny setting.
|
87
|
-
_MODEL_CONCURRENCY = {m[0]: m[1] for m in SUPPORTED_MODELS_AND_SETTINGS}
|
29
|
+
# From https://platform.openai.com/settings/organization/limits
|
30
|
+
_DEFAULT_TPM = 250000
|
31
|
+
_DEFAULT_RPM = 3000
|
32
|
+
|
33
|
+
SUPPORTED_MODELS_AND_SETTINGS = {
|
34
|
+
# Models from https://platform.openai.com/docs/models
|
35
|
+
# RPM is from https://platform.openai.com/docs/guides/rate-limits
|
36
|
+
# GPT-4-Turbo models
|
37
|
+
'gpt-4-turbo': pg.Dict(rpm=10000, tpm=1500000),
|
38
|
+
'gpt-4-turbo-2024-04-09': pg.Dict(rpm=10000, tpm=1500000),
|
39
|
+
'gpt-4-turbo-preview': pg.Dict(rpm=10000, tpm=1500000),
|
40
|
+
'gpt-4-0125-preview': pg.Dict(rpm=10000, tpm=1500000),
|
41
|
+
'gpt-4-1106-preview': pg.Dict(rpm=10000, tpm=1500000),
|
42
|
+
'gpt-4-vision-preview': pg.Dict(rpm=10000, tpm=1500000),
|
43
|
+
'gpt-4-1106-vision-preview': pg.Dict(
|
44
|
+
rpm=10000, tpm=1500000
|
45
|
+
),
|
46
|
+
# GPT-4 models
|
47
|
+
'gpt-4': pg.Dict(rpm=10000, tpm=300000),
|
48
|
+
'gpt-4-0613': pg.Dict(rpm=10000, tpm=300000),
|
49
|
+
'gpt-4-0314': pg.Dict(rpm=10000, tpm=300000),
|
50
|
+
'gpt-4-32k': pg.Dict(rpm=10000, tpm=300000),
|
51
|
+
'gpt-4-32k-0613': pg.Dict(rpm=10000, tpm=300000),
|
52
|
+
'gpt-4-32k-0314': pg.Dict(rpm=10000, tpm=300000),
|
53
|
+
# GPT-3.5-Turbo models
|
54
|
+
'gpt-3.5-turbo': pg.Dict(rpm=10000, tpm=2000000),
|
55
|
+
'gpt-3.5-turbo-0125': pg.Dict(rpm=10000, tpm=2000000),
|
56
|
+
'gpt-3.5-turbo-1106': pg.Dict(rpm=10000, tpm=2000000),
|
57
|
+
'gpt-3.5-turbo-0613': pg.Dict(rpm=10000, tpm=2000000),
|
58
|
+
'gpt-3.5-turbo-0301': pg.Dict(rpm=10000, tpm=2000000),
|
59
|
+
'gpt-3.5-turbo-16k': pg.Dict(rpm=10000, tpm=2000000),
|
60
|
+
'gpt-3.5-turbo-16k-0613': pg.Dict(rpm=10000, tpm=2000000),
|
61
|
+
'gpt-3.5-turbo-16k-0301': pg.Dict(rpm=10000, tpm=2000000),
|
62
|
+
# GPT-3.5 models
|
63
|
+
'text-davinci-003': pg.Dict(rpm=_DEFAULT_RPM, tpm=_DEFAULT_TPM),
|
64
|
+
'text-davinci-002': pg.Dict(rpm=_DEFAULT_RPM, tpm=_DEFAULT_TPM),
|
65
|
+
'code-davinci-002': pg.Dict(rpm=_DEFAULT_RPM, tpm=_DEFAULT_TPM),
|
66
|
+
# GPT-3 instruction-tuned models
|
67
|
+
'text-curie-001': pg.Dict(rpm=_DEFAULT_RPM, tpm=_DEFAULT_TPM),
|
68
|
+
'text-babbage-001': pg.Dict(rpm=_DEFAULT_RPM, tpm=_DEFAULT_TPM),
|
69
|
+
'text-ada-001': pg.Dict(rpm=_DEFAULT_RPM, tpm=_DEFAULT_TPM),
|
70
|
+
'davinci': pg.Dict(rpm=_DEFAULT_RPM, tpm=_DEFAULT_TPM),
|
71
|
+
'curie': pg.Dict(rpm=_DEFAULT_RPM, tpm=_DEFAULT_TPM),
|
72
|
+
'babbage': pg.Dict(rpm=_DEFAULT_RPM, tpm=_DEFAULT_TPM),
|
73
|
+
'ada': pg.Dict(rpm=_DEFAULT_RPM, tpm=_DEFAULT_TPM),
|
74
|
+
# GPT-3 base models
|
75
|
+
'babbage-002': pg.Dict(rpm=_DEFAULT_RPM, tpm=_DEFAULT_TPM),
|
76
|
+
'davinci-002': pg.Dict(rpm=_DEFAULT_RPM, tpm=_DEFAULT_TPM),
|
77
|
+
}
|
88
78
|
|
89
79
|
|
90
80
|
@lf.use_init_args(['model'])
|
@@ -93,7 +83,7 @@ class OpenAI(lf.LanguageModel):
|
|
93
83
|
|
94
84
|
model: pg.typing.Annotated[
|
95
85
|
pg.typing.Enum(
|
96
|
-
pg.MISSING_VALUE,
|
86
|
+
pg.MISSING_VALUE, list(SUPPORTED_MODELS_AND_SETTINGS.keys())
|
97
87
|
),
|
98
88
|
'The name of the model to use.',
|
99
89
|
] = 'gpt-3.5-turbo'
|
@@ -145,7 +135,11 @@ class OpenAI(lf.LanguageModel):
|
|
145
135
|
|
146
136
|
@property
|
147
137
|
def max_concurrency(self) -> int:
|
148
|
-
|
138
|
+
rpm = SUPPORTED_MODELS_AND_SETTINGS[self.model].get('rpm', 0)
|
139
|
+
tpm = SUPPORTED_MODELS_AND_SETTINGS[self.model].get('tpm', 0)
|
140
|
+
return self.rate_to_max_concurrency(
|
141
|
+
requests_per_min=rpm, tokens_per_min=tpm
|
142
|
+
)
|
149
143
|
|
150
144
|
@classmethod
|
151
145
|
def dir(cls):
|
@@ -163,8 +157,6 @@ class OpenAI(lf.LanguageModel):
|
|
163
157
|
# NOTE(daiyip): options.top_k is not applicable.
|
164
158
|
args = dict(
|
165
159
|
n=options.n,
|
166
|
-
temperature=options.temperature,
|
167
|
-
max_tokens=options.max_tokens,
|
168
160
|
stream=False,
|
169
161
|
timeout=self.timeout,
|
170
162
|
logprobs=options.logprobs,
|
@@ -173,13 +165,17 @@ class OpenAI(lf.LanguageModel):
|
|
173
165
|
# Completion and ChatCompletion uses different parameter name for model.
|
174
166
|
args['model' if self.is_chat_model else 'engine'] = self.model
|
175
167
|
|
168
|
+
if options.temperature is not None:
|
169
|
+
args['temperature'] = options.temperature
|
170
|
+
if options.max_tokens is not None:
|
171
|
+
args['max_tokens'] = options.max_tokens
|
176
172
|
if options.top_p is not None:
|
177
173
|
args['top_p'] = options.top_p
|
178
174
|
if options.stop:
|
179
175
|
args['stop'] = options.stop
|
180
176
|
return args
|
181
177
|
|
182
|
-
def _sample(self, prompts: list[lf.Message]) -> list[LMSamplingResult]:
|
178
|
+
def _sample(self, prompts: list[lf.Message]) -> list[lf.LMSamplingResult]:
|
183
179
|
assert self._api_initialized
|
184
180
|
if self.is_chat_model:
|
185
181
|
return self._chat_complete_batch(prompts)
|
@@ -187,7 +183,8 @@ class OpenAI(lf.LanguageModel):
|
|
187
183
|
return self._complete_batch(prompts)
|
188
184
|
|
189
185
|
def _complete_batch(
|
190
|
-
self, prompts: list[lf.Message]
|
186
|
+
self, prompts: list[lf.Message]
|
187
|
+
) -> list[lf.LMSamplingResult]:
|
191
188
|
|
192
189
|
def _open_ai_completion(prompts):
|
193
190
|
response = openai.Completion.create(
|
@@ -202,13 +199,13 @@ class OpenAI(lf.LanguageModel):
|
|
202
199
|
lf.LMSample(choice.text.strip(), score=choice.logprobs or 0.0)
|
203
200
|
)
|
204
201
|
|
205
|
-
usage =
|
202
|
+
usage = lf.LMSamplingUsage(
|
206
203
|
prompt_tokens=response.usage.prompt_tokens,
|
207
204
|
completion_tokens=response.usage.completion_tokens,
|
208
205
|
total_tokens=response.usage.total_tokens,
|
209
206
|
)
|
210
207
|
return [
|
211
|
-
LMSamplingResult(
|
208
|
+
lf.LMSamplingResult(
|
212
209
|
samples_by_index[index], usage=usage if index == 0 else None
|
213
210
|
)
|
214
211
|
for index in sorted(samples_by_index.keys())
|
@@ -220,20 +217,25 @@ class OpenAI(lf.LanguageModel):
|
|
220
217
|
retry_on_errors=(
|
221
218
|
openai_error.ServiceUnavailableError,
|
222
219
|
openai_error.RateLimitError,
|
220
|
+
# Handling transient OpenAI server error (code 500). Check out
|
221
|
+
# https://platform.openai.com/docs/guides/error-codes/error-codes
|
222
|
+
(openai_error.APIError,
|
223
|
+
'.*The server had an error processing your request'),
|
223
224
|
),
|
224
225
|
)[0]
|
225
226
|
|
226
227
|
def _chat_complete_batch(
|
227
228
|
self, prompts: list[lf.Message]
|
228
|
-
) -> list[LMSamplingResult]:
|
229
|
+
) -> list[lf.LMSamplingResult]:
|
229
230
|
def _open_ai_chat_completion(prompt: lf.Message):
|
230
231
|
if self.multimodal:
|
231
232
|
content = []
|
232
233
|
for chunk in prompt.chunk():
|
233
234
|
if isinstance(chunk, str):
|
234
235
|
item = dict(type='text', text=chunk)
|
235
|
-
elif isinstance(chunk, lf_modalities.Image)
|
236
|
-
|
236
|
+
elif isinstance(chunk, lf_modalities.Image):
|
237
|
+
uri = chunk.uri or chunk.content_uri
|
238
|
+
item = dict(type='image_url', image_url=dict(url=uri))
|
237
239
|
else:
|
238
240
|
raise ValueError(f'Unsupported modality object: {chunk!r}.')
|
239
241
|
content.append(item)
|
@@ -266,9 +268,9 @@ class OpenAI(lf.LanguageModel):
|
|
266
268
|
)
|
267
269
|
)
|
268
270
|
|
269
|
-
return LMSamplingResult(
|
271
|
+
return lf.LMSamplingResult(
|
270
272
|
samples=samples,
|
271
|
-
usage=
|
273
|
+
usage=lf.LMSamplingUsage(
|
272
274
|
prompt_tokens=response.usage.prompt_tokens,
|
273
275
|
completion_tokens=response.usage.completion_tokens,
|
274
276
|
total_tokens=response.usage.total_tokens,
|
@@ -291,26 +293,43 @@ class Gpt4(OpenAI):
|
|
291
293
|
|
292
294
|
|
293
295
|
class Gpt4Turbo(Gpt4):
|
294
|
-
"""GPT-4 Turbo with 128K context window
|
295
|
-
model = 'gpt-4-turbo
|
296
|
+
"""GPT-4 Turbo with 128K context window. Knowledge up to Dec. 2023."""
|
297
|
+
model = 'gpt-4-turbo'
|
298
|
+
multimodal = True
|
296
299
|
|
297
300
|
|
298
|
-
class
|
299
|
-
"""GPT-4 Turbo with
|
300
|
-
model = 'gpt-4-
|
301
|
+
class Gpt4Turbo_20240409(Gpt4Turbo): # pylint:disable=invalid-name
|
302
|
+
"""GPT-4 Turbo with 128K context window. Knowledge up to Dec. 2023."""
|
303
|
+
model = 'gpt-4-turbo-2024-04-09'
|
301
304
|
multimodal = True
|
302
305
|
|
303
306
|
|
304
|
-
class
|
305
|
-
"""GPT-4 Turbo with
|
307
|
+
class Gpt4TurboPreview(Gpt4):
|
308
|
+
"""GPT-4 Turbo Preview with 128k context window. Knowledge up to Dec. 2023."""
|
309
|
+
model = 'gpt-4-turbo-preview'
|
310
|
+
|
311
|
+
|
312
|
+
class Gpt4TurboPreview_0125(Gpt4TurboPreview): # pylint: disable=invalid-name
|
313
|
+
"""GPT-4 Turbo Preview with 128k context window. Knowledge up to Dec. 2023."""
|
306
314
|
model = 'gpt-4-0125-preview'
|
307
315
|
|
308
316
|
|
309
|
-
class
|
310
|
-
"""GPT-4 Turbo
|
317
|
+
class Gpt4TurboPreview_1106(Gpt4TurboPreview): # pylint: disable=invalid-name
|
318
|
+
"""GPT-4 Turbo Preview with 128k context window. Knowledge up to Apr. 2023."""
|
311
319
|
model = 'gpt-4-1106-preview'
|
312
320
|
|
313
321
|
|
322
|
+
class Gpt4VisionPreview(Gpt4):
|
323
|
+
"""GPT-4 Turbo vision preview. 128k context window. Knowledge to Apr. 2023."""
|
324
|
+
model = 'gpt-4-vision-preview'
|
325
|
+
multimodal = True
|
326
|
+
|
327
|
+
|
328
|
+
class Gpt4VisionPreview_1106(Gpt4): # pylint: disable=invalid-name
|
329
|
+
"""GPT-4 Turbo vision preview. 128k context window. Knowledge to Apr. 2023."""
|
330
|
+
model = 'gpt-4-1106-vision-preview'
|
331
|
+
|
332
|
+
|
314
333
|
class Gpt4_0613(Gpt4): # pylint:disable=invalid-name
|
315
334
|
"""GPT-4 @20230613. 8K context window. Knowledge up to 9-2021."""
|
316
335
|
model = 'gpt-4-0613'
|