langfun 0.1.2.dev202502110804__py3-none-any.whl → 0.1.2.dev202502120804__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.
- langfun/core/__init__.py +6 -2
- langfun/core/language_model.py +365 -22
- langfun/core/language_model_test.py +123 -35
- langfun/core/llms/__init__.py +50 -57
- langfun/core/llms/anthropic.py +434 -163
- langfun/core/llms/anthropic_test.py +20 -1
- langfun/core/llms/deepseek.py +90 -51
- langfun/core/llms/deepseek_test.py +15 -16
- langfun/core/llms/fake.py +6 -0
- langfun/core/llms/gemini.py +480 -390
- langfun/core/llms/gemini_test.py +27 -7
- langfun/core/llms/google_genai.py +80 -50
- langfun/core/llms/google_genai_test.py +11 -4
- langfun/core/llms/groq.py +268 -167
- langfun/core/llms/groq_test.py +9 -3
- langfun/core/llms/openai.py +839 -328
- langfun/core/llms/openai_compatible.py +3 -18
- langfun/core/llms/openai_compatible_test.py +20 -5
- langfun/core/llms/openai_test.py +14 -4
- langfun/core/llms/rest.py +11 -6
- langfun/core/llms/vertexai.py +238 -240
- langfun/core/llms/vertexai_test.py +35 -8
- {langfun-0.1.2.dev202502110804.dist-info → langfun-0.1.2.dev202502120804.dist-info}/METADATA +1 -1
- {langfun-0.1.2.dev202502110804.dist-info → langfun-0.1.2.dev202502120804.dist-info}/RECORD +27 -27
- {langfun-0.1.2.dev202502110804.dist-info → langfun-0.1.2.dev202502120804.dist-info}/LICENSE +0 -0
- {langfun-0.1.2.dev202502110804.dist-info → langfun-0.1.2.dev202502120804.dist-info}/WHEEL +0 -0
- {langfun-0.1.2.dev202502110804.dist-info → langfun-0.1.2.dev202502120804.dist-info}/top_level.txt +0 -0
langfun/core/llms/anthropic.py
CHANGED
@@ -14,6 +14,8 @@
|
|
14
14
|
"""Language models from Anthropic."""
|
15
15
|
|
16
16
|
import base64
|
17
|
+
import datetime
|
18
|
+
import functools
|
17
19
|
import os
|
18
20
|
from typing import Annotated, Any
|
19
21
|
|
@@ -23,107 +25,388 @@ from langfun.core.llms import rest
|
|
23
25
|
import pyglove as pg
|
24
26
|
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
28
|
+
class AnthropicModelInfo(lf.ModelInfo):
|
29
|
+
"""Anthropic model info."""
|
30
|
+
|
31
|
+
# Constants for supported MIME types.
|
32
|
+
INPUT_IMAGE_TYPES = [
|
33
|
+
'image/png',
|
34
|
+
'image/jpeg',
|
35
|
+
'image/gif',
|
36
|
+
'image/webp',
|
37
|
+
]
|
38
|
+
INPUT_DOC_TYPES = [
|
39
|
+
'application/pdf',
|
40
|
+
]
|
41
|
+
|
42
|
+
LINKS = dict(
|
43
|
+
models='https://docs.anthropic.com/claude/docs/models-overview',
|
44
|
+
pricing='https://www.anthropic.com/pricing#anthropic-api',
|
45
|
+
rate_limits='https://docs.anthropic.com/en/api/rate-limits',
|
46
|
+
error_codes='https://docs.anthropic.com/en/api/errors',
|
47
|
+
)
|
48
|
+
|
49
|
+
class RateLimits(lf.ModelInfo.RateLimits):
|
50
|
+
"""Rate limits for Anthropic models."""
|
51
|
+
|
52
|
+
max_input_tokens_per_minute: int
|
53
|
+
max_output_tokens_per_minute: int
|
54
|
+
|
55
|
+
@property
|
56
|
+
def max_tokens_per_minute(self) -> int:
|
57
|
+
return (self.max_input_tokens_per_minute
|
58
|
+
+ self.max_output_tokens_per_minute)
|
59
|
+
|
60
|
+
|
61
|
+
SUPPORTED_MODELS = [
|
62
|
+
# 3.5 Sonnet models.
|
63
|
+
AnthropicModelInfo(
|
64
|
+
model_id='claude-3-5-sonnet-latest',
|
65
|
+
alias_for='claude-3-5-sonnet-20241022',
|
66
|
+
provider='Anthropic',
|
67
|
+
in_service=True,
|
68
|
+
description=(
|
69
|
+
'Claude 3.5 Sonnet model (latest).'
|
70
|
+
),
|
71
|
+
input_modalities=(
|
72
|
+
AnthropicModelInfo.INPUT_IMAGE_TYPES
|
73
|
+
+ AnthropicModelInfo.INPUT_DOC_TYPES
|
74
|
+
),
|
75
|
+
context_length=lf.ModelInfo.ContextLength(
|
76
|
+
max_input_tokens=200_000,
|
77
|
+
max_output_tokens=8_192,
|
78
|
+
),
|
79
|
+
pricing=lf.ModelInfo.Pricing(
|
80
|
+
cost_per_1m_cached_input_tokens=0.3,
|
81
|
+
cost_per_1m_input_tokens=3,
|
82
|
+
cost_per_1m_output_tokens=15,
|
83
|
+
),
|
84
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
85
|
+
# Tier 4 rate limits
|
86
|
+
max_requests_per_minute=4000,
|
87
|
+
max_input_tokens_per_minute=400_000,
|
88
|
+
max_output_tokens_per_minute=80_000,
|
89
|
+
),
|
40
90
|
),
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
91
|
+
AnthropicModelInfo(
|
92
|
+
model_id='claude-3-5-sonnet-20241022',
|
93
|
+
provider='Anthropic',
|
94
|
+
in_service=True,
|
95
|
+
description=(
|
96
|
+
'Claude 3.5 Sonnet model (10/22/2024).'
|
97
|
+
),
|
98
|
+
release_date=datetime.datetime(2024, 10, 22),
|
99
|
+
input_modalities=(
|
100
|
+
AnthropicModelInfo.INPUT_IMAGE_TYPES
|
101
|
+
+ AnthropicModelInfo.INPUT_DOC_TYPES
|
102
|
+
),
|
103
|
+
context_length=lf.ModelInfo.ContextLength(
|
104
|
+
max_input_tokens=200_000,
|
105
|
+
max_output_tokens=8_192,
|
106
|
+
),
|
107
|
+
pricing=lf.ModelInfo.Pricing(
|
108
|
+
cost_per_1m_cached_input_tokens=0.3,
|
109
|
+
cost_per_1m_input_tokens=3,
|
110
|
+
cost_per_1m_output_tokens=15,
|
111
|
+
),
|
112
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
113
|
+
# Tier 4 rate limits
|
114
|
+
max_requests_per_minute=4000,
|
115
|
+
max_input_tokens_per_minute=400_000,
|
116
|
+
max_output_tokens_per_minute=80_000,
|
117
|
+
),
|
47
118
|
),
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
119
|
+
AnthropicModelInfo(
|
120
|
+
model_id='claude-3-5-sonnet-v2@20241022',
|
121
|
+
alias_for='claude-3-5-sonnet-20241022',
|
122
|
+
provider='VertexAI',
|
123
|
+
in_service=True,
|
124
|
+
description=(
|
125
|
+
'Claude 3.5 Sonnet model served on VertexAI (10/22/2024).'
|
126
|
+
),
|
127
|
+
release_date=datetime.datetime(2024, 10, 22),
|
128
|
+
input_modalities=(
|
129
|
+
AnthropicModelInfo.INPUT_IMAGE_TYPES
|
130
|
+
+ AnthropicModelInfo.INPUT_DOC_TYPES
|
131
|
+
),
|
132
|
+
context_length=lf.ModelInfo.ContextLength(
|
133
|
+
max_input_tokens=200_000,
|
134
|
+
max_output_tokens=8_192,
|
135
|
+
),
|
136
|
+
pricing=lf.ModelInfo.Pricing(
|
137
|
+
cost_per_1m_cached_input_tokens=0.3,
|
138
|
+
cost_per_1m_input_tokens=3,
|
139
|
+
cost_per_1m_output_tokens=15,
|
140
|
+
),
|
141
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
142
|
+
# Tier 4 rate limits
|
143
|
+
max_requests_per_minute=4000,
|
144
|
+
max_input_tokens_per_minute=400_000,
|
145
|
+
max_output_tokens_per_minute=80_000,
|
146
|
+
),
|
54
147
|
),
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
148
|
+
# 3.5 Haiku models.
|
149
|
+
AnthropicModelInfo(
|
150
|
+
model_id='claude-3-5-haiku-latest',
|
151
|
+
alias_for='claude-3-5-haiku-20241022',
|
152
|
+
provider='Anthropic',
|
153
|
+
in_service=True,
|
154
|
+
description=(
|
155
|
+
'Claude 3.5 Haiku v2 model (10/22/2024).'
|
156
|
+
),
|
157
|
+
input_modalities=lf.ModelInfo.TEXT_INPUT_ONLY,
|
158
|
+
context_length=lf.ModelInfo.ContextLength(
|
159
|
+
max_input_tokens=200_000,
|
160
|
+
max_output_tokens=8_192,
|
161
|
+
),
|
162
|
+
pricing=lf.ModelInfo.Pricing(
|
163
|
+
cost_per_1m_cached_input_tokens=0.08,
|
164
|
+
cost_per_1m_input_tokens=0.8,
|
165
|
+
cost_per_1m_output_tokens=4,
|
166
|
+
),
|
167
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
168
|
+
# Tier 4 rate limits
|
169
|
+
max_requests_per_minute=4000,
|
170
|
+
max_input_tokens_per_minute=400_000,
|
171
|
+
max_output_tokens_per_minute=80_000,
|
172
|
+
),
|
61
173
|
),
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
174
|
+
AnthropicModelInfo(
|
175
|
+
model_id='claude-3-5-haiku-20241022',
|
176
|
+
provider='Anthropic',
|
177
|
+
in_service=True,
|
178
|
+
description=(
|
179
|
+
'Claude 3.5 Haiku v2 model (10/22/2024).'
|
180
|
+
),
|
181
|
+
release_date=datetime.datetime(2024, 10, 22),
|
182
|
+
input_modalities=lf.ModelInfo.TEXT_INPUT_ONLY,
|
183
|
+
context_length=lf.ModelInfo.ContextLength(
|
184
|
+
max_input_tokens=200_000,
|
185
|
+
max_output_tokens=8_192,
|
186
|
+
),
|
187
|
+
pricing=lf.ModelInfo.Pricing(
|
188
|
+
cost_per_1m_cached_input_tokens=0.08,
|
189
|
+
cost_per_1m_input_tokens=0.8,
|
190
|
+
cost_per_1m_output_tokens=4,
|
191
|
+
),
|
192
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
193
|
+
# Tier 4 rate limits
|
194
|
+
max_requests_per_minute=4000,
|
195
|
+
max_input_tokens_per_minute=400_000,
|
196
|
+
max_output_tokens_per_minute=80_000,
|
197
|
+
),
|
69
198
|
),
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
199
|
+
AnthropicModelInfo(
|
200
|
+
model_id='claude-3-5-haiku@20241022',
|
201
|
+
alias_for='claude-3-5-haiku-20241022',
|
202
|
+
provider='VertexAI',
|
203
|
+
in_service=True,
|
204
|
+
description=(
|
205
|
+
'Claude 3.5 Haiku model served on VertexAI (10/22/2024).'
|
206
|
+
),
|
207
|
+
release_date=datetime.datetime(2024, 10, 22),
|
208
|
+
input_modalities=lf.ModelInfo.TEXT_INPUT_ONLY,
|
209
|
+
context_length=lf.ModelInfo.ContextLength(
|
210
|
+
max_input_tokens=200_000,
|
211
|
+
max_output_tokens=8_192,
|
212
|
+
),
|
213
|
+
pricing=lf.ModelInfo.Pricing(
|
214
|
+
cost_per_1m_cached_input_tokens=0.08,
|
215
|
+
cost_per_1m_input_tokens=0.8,
|
216
|
+
cost_per_1m_output_tokens=4,
|
217
|
+
),
|
218
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
219
|
+
# Tier 4 rate limits
|
220
|
+
max_requests_per_minute=4000,
|
221
|
+
max_input_tokens_per_minute=400_000,
|
222
|
+
max_output_tokens_per_minute=80_000,
|
223
|
+
),
|
76
224
|
),
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
225
|
+
# 3.0 Opus models.
|
226
|
+
AnthropicModelInfo(
|
227
|
+
model_id='claude-3-opus-latest',
|
228
|
+
alias_for='claude-3-opus-20240229',
|
229
|
+
provider='Anthropic',
|
230
|
+
in_service=True,
|
231
|
+
description=(
|
232
|
+
'Claude 3 Opus model (latest).'
|
233
|
+
),
|
234
|
+
input_modalities=AnthropicModelInfo.INPUT_IMAGE_TYPES,
|
235
|
+
context_length=lf.ModelInfo.ContextLength(
|
236
|
+
max_input_tokens=200_000,
|
237
|
+
max_output_tokens=4_096,
|
238
|
+
),
|
239
|
+
pricing=lf.ModelInfo.Pricing(
|
240
|
+
cost_per_1m_cached_input_tokens=1.5,
|
241
|
+
cost_per_1m_input_tokens=15,
|
242
|
+
cost_per_1m_output_tokens=75,
|
243
|
+
),
|
244
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
245
|
+
# Tier 4 rate limits
|
246
|
+
max_requests_per_minute=4000,
|
247
|
+
max_input_tokens_per_minute=400_000,
|
248
|
+
max_output_tokens_per_minute=80_000,
|
249
|
+
),
|
83
250
|
),
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
251
|
+
AnthropicModelInfo(
|
252
|
+
model_id='claude-3-opus-20240229',
|
253
|
+
provider='Anthropic',
|
254
|
+
in_service=True,
|
255
|
+
description=(
|
256
|
+
'Claude 3 Opus model (02/29/2024).'
|
257
|
+
),
|
258
|
+
release_date=datetime.datetime(2024, 2, 29),
|
259
|
+
input_modalities=AnthropicModelInfo.INPUT_IMAGE_TYPES,
|
260
|
+
context_length=lf.ModelInfo.ContextLength(
|
261
|
+
max_input_tokens=200_000,
|
262
|
+
max_output_tokens=4_096,
|
263
|
+
),
|
264
|
+
pricing=lf.ModelInfo.Pricing(
|
265
|
+
cost_per_1m_cached_input_tokens=1.5,
|
266
|
+
cost_per_1m_input_tokens=15,
|
267
|
+
cost_per_1m_output_tokens=75,
|
268
|
+
),
|
269
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
270
|
+
# Tier 4 rate limits
|
271
|
+
max_requests_per_minute=4000,
|
272
|
+
max_input_tokens_per_minute=400_000,
|
273
|
+
max_output_tokens_per_minute=80_000,
|
274
|
+
),
|
90
275
|
),
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
276
|
+
AnthropicModelInfo(
|
277
|
+
model_id='claude-3-opus@20240229',
|
278
|
+
alias_for='claude-3-opus-20240229',
|
279
|
+
provider='VertexAI',
|
280
|
+
in_service=True,
|
281
|
+
description=(
|
282
|
+
'Claude 3 Opus model served on VertexAI (02/29/2024).'
|
283
|
+
),
|
284
|
+
release_date=datetime.datetime(2024, 2, 29),
|
285
|
+
input_modalities=AnthropicModelInfo.INPUT_IMAGE_TYPES,
|
286
|
+
context_length=lf.ModelInfo.ContextLength(
|
287
|
+
max_input_tokens=200_000,
|
288
|
+
max_output_tokens=4_096,
|
289
|
+
),
|
290
|
+
pricing=lf.ModelInfo.Pricing(
|
291
|
+
cost_per_1m_cached_input_tokens=1.5,
|
292
|
+
cost_per_1m_input_tokens=15,
|
293
|
+
cost_per_1m_output_tokens=75,
|
294
|
+
),
|
295
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
296
|
+
# Tier 4 rate limits
|
297
|
+
max_requests_per_minute=4000,
|
298
|
+
max_input_tokens_per_minute=400_000,
|
299
|
+
max_output_tokens_per_minute=80_000,
|
300
|
+
),
|
97
301
|
),
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
302
|
+
# 3.0 Sonnet models.
|
303
|
+
AnthropicModelInfo(
|
304
|
+
model_id='claude-3-sonnet-20240229',
|
305
|
+
provider='Anthropic',
|
306
|
+
in_service=True,
|
307
|
+
description=(
|
308
|
+
'Claude 3 Sonnet model (02/29/2024).'
|
309
|
+
),
|
310
|
+
release_date=datetime.datetime(2024, 2, 29),
|
311
|
+
input_modalities=AnthropicModelInfo.INPUT_IMAGE_TYPES,
|
312
|
+
context_length=lf.ModelInfo.ContextLength(
|
313
|
+
max_input_tokens=200_000,
|
314
|
+
max_output_tokens=4_096,
|
315
|
+
),
|
316
|
+
pricing=lf.ModelInfo.Pricing(
|
317
|
+
cost_per_1m_cached_input_tokens=None,
|
318
|
+
cost_per_1m_input_tokens=3,
|
319
|
+
cost_per_1m_output_tokens=15,
|
320
|
+
),
|
321
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
322
|
+
# Tier 4 rate limits
|
323
|
+
max_requests_per_minute=4000,
|
324
|
+
max_input_tokens_per_minute=400_000,
|
325
|
+
max_output_tokens_per_minute=80_000,
|
326
|
+
),
|
104
327
|
),
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
328
|
+
AnthropicModelInfo(
|
329
|
+
model_id='claude-3-sonnet@20240229',
|
330
|
+
alias_for='claude-3-sonnet-20240229',
|
331
|
+
provider='VertexAI',
|
332
|
+
in_service=True,
|
333
|
+
description=(
|
334
|
+
'Claude 3 Sonnet model served on VertexAI (02/29/2024).'
|
335
|
+
),
|
336
|
+
release_date=datetime.datetime(2024, 2, 29),
|
337
|
+
input_modalities=AnthropicModelInfo.INPUT_IMAGE_TYPES,
|
338
|
+
context_length=lf.ModelInfo.ContextLength(
|
339
|
+
max_input_tokens=200_000,
|
340
|
+
max_output_tokens=4_096,
|
341
|
+
),
|
342
|
+
pricing=lf.ModelInfo.Pricing(
|
343
|
+
cost_per_1m_cached_input_tokens=None,
|
344
|
+
cost_per_1m_input_tokens=3,
|
345
|
+
cost_per_1m_output_tokens=15,
|
346
|
+
),
|
347
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
348
|
+
# Tier 4 rate limits
|
349
|
+
max_requests_per_minute=4000,
|
350
|
+
max_input_tokens_per_minute=400_000,
|
351
|
+
max_output_tokens_per_minute=80_000,
|
352
|
+
),
|
111
353
|
),
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
354
|
+
# 3.0 Haiku models.
|
355
|
+
AnthropicModelInfo(
|
356
|
+
model_id='claude-3-haiku-20240307',
|
357
|
+
provider='Anthropic',
|
358
|
+
in_service=True,
|
359
|
+
description=(
|
360
|
+
'Claude 3 Haiku model (03/07/2024).'
|
361
|
+
),
|
362
|
+
release_date=datetime.datetime(2024, 3, 7),
|
363
|
+
input_modalities=AnthropicModelInfo.INPUT_IMAGE_TYPES,
|
364
|
+
context_length=lf.ModelInfo.ContextLength(
|
365
|
+
max_input_tokens=200_000,
|
366
|
+
max_output_tokens=4_096,
|
367
|
+
),
|
368
|
+
pricing=lf.ModelInfo.Pricing(
|
369
|
+
cost_per_1m_cached_input_tokens=None,
|
370
|
+
cost_per_1m_input_tokens=0.25,
|
371
|
+
cost_per_1m_output_tokens=1.25,
|
372
|
+
),
|
373
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
374
|
+
# Tier 4 rate limits
|
375
|
+
max_requests_per_minute=4000,
|
376
|
+
max_input_tokens_per_minute=400_000,
|
377
|
+
max_output_tokens_per_minute=80_000,
|
378
|
+
),
|
118
379
|
),
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
380
|
+
AnthropicModelInfo(
|
381
|
+
model_id='claude-3-haiku@20240307',
|
382
|
+
alias_for='claude-3-haiku-20240307',
|
383
|
+
provider='Anthropic',
|
384
|
+
in_service=True,
|
385
|
+
description=(
|
386
|
+
'Claude 3 Haiku model served on VertexAI (03/07/2024).'
|
387
|
+
),
|
388
|
+
release_date=datetime.datetime(2024, 3, 7),
|
389
|
+
input_modalities=AnthropicModelInfo.INPUT_IMAGE_TYPES,
|
390
|
+
context_length=lf.ModelInfo.ContextLength(
|
391
|
+
max_input_tokens=200_000,
|
392
|
+
max_output_tokens=4_096,
|
393
|
+
),
|
394
|
+
pricing=lf.ModelInfo.Pricing(
|
395
|
+
cost_per_1m_cached_input_tokens=None,
|
396
|
+
cost_per_1m_input_tokens=0.25,
|
397
|
+
cost_per_1m_output_tokens=1.25,
|
398
|
+
),
|
399
|
+
rate_limits=AnthropicModelInfo.RateLimits(
|
400
|
+
# Tier 4 rate limits
|
401
|
+
max_requests_per_minute=4000,
|
402
|
+
max_input_tokens_per_minute=400_000,
|
403
|
+
max_output_tokens_per_minute=80_000,
|
404
|
+
),
|
125
405
|
),
|
126
|
-
|
406
|
+
]
|
407
|
+
|
408
|
+
|
409
|
+
_SUPPORTED_MODELS_BY_MODEL_ID = {m.model_id: m for m in SUPPORTED_MODELS}
|
127
410
|
|
128
411
|
|
129
412
|
@lf.use_init_args(['model'])
|
@@ -135,15 +418,11 @@ class Anthropic(rest.REST):
|
|
135
418
|
|
136
419
|
model: pg.typing.Annotated[
|
137
420
|
pg.typing.Enum(
|
138
|
-
pg.MISSING_VALUE,
|
421
|
+
pg.MISSING_VALUE, [m.model_id for m in SUPPORTED_MODELS]
|
139
422
|
),
|
140
423
|
'The name of the model to use.',
|
141
424
|
]
|
142
425
|
|
143
|
-
multimodal: Annotated[bool, 'Whether this model has multimodal support.'] = (
|
144
|
-
True
|
145
|
-
)
|
146
|
-
|
147
426
|
api_key: Annotated[
|
148
427
|
str | None,
|
149
428
|
(
|
@@ -182,37 +461,14 @@ class Anthropic(rest.REST):
|
|
182
461
|
'anthropic-beta': 'pdfs-2024-09-25',
|
183
462
|
}
|
184
463
|
|
185
|
-
@
|
186
|
-
def
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
tpm = SUPPORTED_MODELS_AND_SETTINGS[self.model].get('tpm', 0)
|
194
|
-
return self.rate_to_max_concurrency(
|
195
|
-
requests_per_min=rpm, tokens_per_min=tpm
|
196
|
-
)
|
197
|
-
|
198
|
-
def estimate_cost(
|
199
|
-
self,
|
200
|
-
num_input_tokens: int,
|
201
|
-
num_output_tokens: int
|
202
|
-
) -> float | None:
|
203
|
-
"""Estimate the cost based on usage."""
|
204
|
-
cost_per_1k_input_tokens = SUPPORTED_MODELS_AND_SETTINGS[self.model].get(
|
205
|
-
'cost_per_1k_input_tokens', None
|
206
|
-
)
|
207
|
-
cost_per_1k_output_tokens = SUPPORTED_MODELS_AND_SETTINGS[self.model].get(
|
208
|
-
'cost_per_1k_output_tokens', None
|
209
|
-
)
|
210
|
-
if cost_per_1k_output_tokens is None or cost_per_1k_input_tokens is None:
|
211
|
-
return None
|
212
|
-
return (
|
213
|
-
cost_per_1k_input_tokens * num_input_tokens
|
214
|
-
+ cost_per_1k_output_tokens * num_output_tokens
|
215
|
-
) / 1000
|
464
|
+
@functools.cached_property
|
465
|
+
def model_info(self) -> lf.ModelInfo:
|
466
|
+
mi = _SUPPORTED_MODELS_BY_MODEL_ID[self.model]
|
467
|
+
if mi.provider != 'Anthropic':
|
468
|
+
assert mi.alias_for is not None
|
469
|
+
mi = _SUPPORTED_MODELS_BY_MODEL_ID[mi.alias_for]
|
470
|
+
assert mi.provider == 'Anthropic', mi
|
471
|
+
return mi
|
216
472
|
|
217
473
|
def request(
|
218
474
|
self,
|
@@ -235,8 +491,7 @@ class Anthropic(rest.REST):
|
|
235
491
|
"""Returns a dict as request arguments."""
|
236
492
|
# Authropic requires `max_tokens` to be specified.
|
237
493
|
max_tokens = (
|
238
|
-
options.max_tokens
|
239
|
-
or SUPPORTED_MODELS_AND_SETTINGS[self.model].max_tokens
|
494
|
+
options.max_tokens or self.model_info.context_length.max_output_tokens
|
240
495
|
)
|
241
496
|
args = dict(
|
242
497
|
model=self.model,
|
@@ -256,12 +511,14 @@ class Anthropic(rest.REST):
|
|
256
511
|
def _content_from_message(self, prompt: lf.Message) -> list[dict[str, Any]]:
|
257
512
|
"""Converts an message to Anthropic's content protocol (list of dicts)."""
|
258
513
|
# Refer: https://docs.anthropic.com/claude/reference/messages-examples
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
514
|
+
content = []
|
515
|
+
for chunk in prompt.chunk():
|
516
|
+
if isinstance(chunk, str):
|
517
|
+
content.append(dict(type='text', text=chunk))
|
518
|
+
elif isinstance(chunk, lf_modalities.Mime):
|
519
|
+
if not self.supports_input(chunk.mime_type):
|
520
|
+
raise ValueError(f'Unsupported modality: {chunk!r}.')
|
521
|
+
if isinstance(chunk, lf_modalities.Image):
|
265
522
|
item = dict(
|
266
523
|
type='image',
|
267
524
|
source=dict(
|
@@ -280,11 +537,11 @@ class Anthropic(rest.REST):
|
|
280
537
|
),
|
281
538
|
)
|
282
539
|
else:
|
283
|
-
raise
|
540
|
+
raise NotImplementedError(
|
541
|
+
f'Modality conversion not implemented: {chunk!r}'
|
542
|
+
)
|
284
543
|
content.append(item)
|
285
|
-
|
286
|
-
else:
|
287
|
-
return [dict(type='text', text=prompt.text)]
|
544
|
+
return content
|
288
545
|
|
289
546
|
def result(self, json: dict[str, Any]) -> lf.LMSamplingResult:
|
290
547
|
message = self._message_from_content(json['content'])
|
@@ -296,10 +553,6 @@ class Anthropic(rest.REST):
|
|
296
553
|
prompt_tokens=input_tokens,
|
297
554
|
completion_tokens=output_tokens,
|
298
555
|
total_tokens=input_tokens + output_tokens,
|
299
|
-
estimated_cost=self.estimate_cost(
|
300
|
-
num_input_tokens=input_tokens,
|
301
|
-
num_output_tokens=output_tokens,
|
302
|
-
),
|
303
556
|
),
|
304
557
|
)
|
305
558
|
|
@@ -311,56 +564,74 @@ class Anthropic(rest.REST):
|
|
311
564
|
)
|
312
565
|
|
313
566
|
|
314
|
-
class
|
315
|
-
"""Base class for Claude 3 models.
|
316
|
-
|
567
|
+
class Claude35(Anthropic):
|
568
|
+
"""Base class for Claude 3.5 models."""
|
569
|
+
|
317
570
|
|
571
|
+
class Claude35Sonnet(Claude35):
|
572
|
+
"""Claude 3.5 Sonnet model (latest)."""
|
573
|
+
model = 'claude-3-5-sonnet-latest'
|
318
574
|
|
319
|
-
|
320
|
-
|
575
|
+
|
576
|
+
class Claude35Sonnet_20241022(Claude35): # pylint: disable=invalid-name
|
577
|
+
"""Claude 3.5 Sonnet model (10/22/2024)."""
|
321
578
|
model = 'claude-3-5-sonnet-20241022'
|
322
579
|
|
323
580
|
|
324
|
-
class
|
325
|
-
"""
|
581
|
+
class Claude35Haiku(Claude35):
|
582
|
+
"""Claude 3.5 Haiku model (latest)."""
|
583
|
+
model = 'claude-3-5-haiku-latest'
|
326
584
|
|
327
|
-
model = 'claude-3-5-sonnet-20241022'
|
328
585
|
|
586
|
+
class Claude35Haiku_20241022(Claude35): # pylint: disable=invalid-name
|
587
|
+
"""Claude 3.5 Haiku model (10/22/2024)."""
|
588
|
+
model = 'claude-3-5-haiku-20241022'
|
329
589
|
|
330
|
-
class Claude35Sonnet20240620(Claude3):
|
331
|
-
"""A balance between between Opus and Haiku."""
|
332
590
|
|
333
|
-
|
591
|
+
class Claude3(Anthropic):
|
592
|
+
"""Base class for Claude 3 models."""
|
334
593
|
|
335
594
|
|
336
595
|
class Claude3Opus(Claude3):
|
337
|
-
"""
|
596
|
+
"""Claude 3 Opus model (latest)."""
|
597
|
+
|
598
|
+
model = 'claude-3-opus-latest'
|
599
|
+
|
600
|
+
|
601
|
+
class Claude3Opus_20240229(Claude3): # pylint: disable=invalid-name
|
602
|
+
"""Claude 3 Opus model (02/29/2024)."""
|
338
603
|
|
339
604
|
model = 'claude-3-opus-20240229'
|
340
605
|
|
341
606
|
|
342
607
|
class Claude3Sonnet(Claude3):
|
343
|
-
"""
|
608
|
+
"""Claude 3 Sonnet model."""
|
609
|
+
|
610
|
+
model = 'claude-3-sonnet-20240229'
|
611
|
+
|
612
|
+
|
613
|
+
class Claude3Sonnet_20240229(Claude3): # pylint: disable=invalid-name
|
614
|
+
"""Claude 3 Sonnet model (02/29/2024)."""
|
344
615
|
|
345
616
|
model = 'claude-3-sonnet-20240229'
|
346
617
|
|
347
618
|
|
348
619
|
class Claude3Haiku(Claude3):
|
349
|
-
"""
|
620
|
+
"""Claude 3 Haiku model."""
|
350
621
|
|
351
622
|
model = 'claude-3-haiku-20240307'
|
352
623
|
|
353
624
|
|
354
|
-
class
|
355
|
-
"""
|
356
|
-
model = 'claude-2.0'
|
625
|
+
class Claude3Haiku_20240307(Claude3): # pylint: disable=invalid-name
|
626
|
+
"""Claude 3 Haiku model (03/07/2024)."""
|
357
627
|
|
628
|
+
model = 'claude-3-haiku-20240307'
|
358
629
|
|
359
|
-
class Claude21(Anthropic):
|
360
|
-
"""Updated Claude 2 model with improved accuracy and 200K context window."""
|
361
|
-
model = 'claude-2.1'
|
362
630
|
|
631
|
+
def _register_anthropic_models():
|
632
|
+
"""Registers Anthropic models."""
|
633
|
+
for m in SUPPORTED_MODELS:
|
634
|
+
if m.provider == 'Anthropic':
|
635
|
+
lf.LanguageModel.register(m.model_id, Anthropic)
|
363
636
|
|
364
|
-
|
365
|
-
"""Cheapest small and fast model, 100K context window."""
|
366
|
-
model = 'claude-instant-1.2'
|
637
|
+
_register_anthropic_models()
|