-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathllm_claude_3.py
286 lines (250 loc) · 10.3 KB
/
llm_claude_3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from anthropic import Anthropic, AsyncAnthropic
import llm
from pydantic import Field, field_validator, model_validator
from typing import Optional, List
@llm.hookimpl
def register_models(register):
# https://docs.anthropic.com/claude/docs/models-overview
register(
ClaudeMessages("claude-3-opus-20240229"),
AsyncClaudeMessages("claude-3-opus-20240229"),
),
register(
ClaudeMessages("claude-3-opus-latest"),
AsyncClaudeMessages("claude-3-opus-latest"),
aliases=("claude-3-opus",),
)
register(
ClaudeMessages("claude-3-sonnet-20240229"),
AsyncClaudeMessages("claude-3-sonnet-20240229"),
aliases=("claude-3-sonnet",),
)
register(
ClaudeMessages("claude-3-haiku-20240307"),
AsyncClaudeMessages("claude-3-haiku-20240307"),
aliases=("claude-3-haiku",),
)
# 3.5 models
register(
ClaudeMessagesLong("claude-3-5-sonnet-20240620"),
AsyncClaudeMessagesLong("claude-3-5-sonnet-20240620"),
)
register(
ClaudeMessagesLong("claude-3-5-sonnet-20241022", supports_pdf=True),
AsyncClaudeMessagesLong("claude-3-5-sonnet-20241022", supports_pdf=True),
)
register(
ClaudeMessagesLong("claude-3-5-sonnet-latest", supports_pdf=True),
AsyncClaudeMessagesLong("claude-3-5-sonnet-latest", supports_pdf=True),
aliases=("claude-3.5-sonnet", "claude-3.5-sonnet-latest"),
)
register(
ClaudeMessagesLong("claude-3-5-haiku-latest", supports_images=False),
AsyncClaudeMessagesLong("claude-3-5-haiku-latest", supports_images=False),
aliases=("claude-3.5-haiku",),
)
class ClaudeOptions(llm.Options):
max_tokens: Optional[int] = Field(
description="The maximum number of tokens to generate before stopping",
default=4_096,
)
temperature: Optional[float] = Field(
description="Amount of randomness injected into the response. Defaults to 1.0. Ranges from 0.0 to 1.0. Use temperature closer to 0.0 for analytical / multiple choice, and closer to 1.0 for creative and generative tasks. Note that even with temperature of 0.0, the results will not be fully deterministic.",
default=1.0,
)
top_p: Optional[float] = Field(
description="Use nucleus sampling. In nucleus sampling, we compute the cumulative distribution over all the options for each subsequent token in decreasing probability order and cut it off once it reaches a particular probability specified by top_p. You should either alter temperature or top_p, but not both. Recommended for advanced use cases only. You usually only need to use temperature.",
default=None,
)
top_k: Optional[int] = Field(
description="Only sample from the top K options for each subsequent token. Used to remove 'long tail' low probability responses. Recommended for advanced use cases only. You usually only need to use temperature.",
default=None,
)
user_id: Optional[str] = Field(
description="An external identifier for the user who is associated with the request",
default=None,
)
@field_validator("max_tokens")
@classmethod
def validate_max_tokens(cls, max_tokens):
real_max = cls.model_fields["max_tokens"].default
if not (0 < max_tokens <= real_max):
raise ValueError("max_tokens must be in range 1-{}".format(real_max))
return max_tokens
@field_validator("temperature")
@classmethod
def validate_temperature(cls, temperature):
if not (0.0 <= temperature <= 1.0):
raise ValueError("temperature must be in range 0.0-1.0")
return temperature
@field_validator("top_p")
@classmethod
def validate_top_p(cls, top_p):
if top_p is not None and not (0.0 <= top_p <= 1.0):
raise ValueError("top_p must be in range 0.0-1.0")
return top_p
@field_validator("top_k")
@classmethod
def validate_top_k(cls, top_k):
if top_k is not None and top_k <= 0:
raise ValueError("top_k must be a positive integer")
return top_k
@model_validator(mode="after")
def validate_temperature_top_p(self):
if self.temperature != 1.0 and self.top_p is not None:
raise ValueError("Only one of temperature and top_p can be set")
return self
long_field = Field(
description="The maximum number of tokens to generate before stopping",
default=4_096 * 2,
)
class _Shared:
needs_key = "claude"
key_env_var = "ANTHROPIC_API_KEY"
can_stream = True
class Options(ClaudeOptions): ...
def __init__(
self,
model_id,
claude_model_id=None,
extra_headers=None,
supports_images=True,
supports_pdf=False,
):
self.model_id = model_id
self.claude_model_id = claude_model_id or model_id
self.extra_headers = extra_headers or {}
if supports_pdf:
self.extra_headers["anthropic-beta"] = "pdfs-2024-09-25"
self.attachment_types = set()
if supports_images:
self.attachment_types.update(
{
"image/png",
"image/jpeg",
"image/webp",
"image/gif",
}
)
if supports_pdf:
self.attachment_types.add("application/pdf")
def build_messages(self, prompt, conversation) -> List[dict]:
messages = []
if conversation:
for response in conversation.responses:
if response.attachments:
content = [
{
"type": (
"document"
if attachment.resolve_type() == "application/pdf"
else "image"
),
"source": {
"data": attachment.base64_content(),
"media_type": attachment.resolve_type(),
"type": "base64",
},
}
for attachment in response.attachments
]
content.append({"type": "text", "text": response.prompt.prompt})
else:
content = response.prompt.prompt
messages.extend(
[
{
"role": "user",
"content": content,
},
{"role": "assistant", "content": response.text()},
]
)
if prompt.attachments:
content = [
{
"type": (
"document"
if attachment.resolve_type() == "application/pdf"
else "image"
),
"source": {
"data": attachment.base64_content(),
"media_type": attachment.resolve_type(),
"type": "base64",
},
}
for attachment in prompt.attachments
]
content.append({"type": "text", "text": prompt.prompt})
messages.append(
{
"role": "user",
"content": content,
}
)
else:
messages.append({"role": "user", "content": prompt.prompt})
return messages
def build_kwargs(self, prompt, conversation):
kwargs = {
"model": self.claude_model_id,
"messages": self.build_messages(prompt, conversation),
"max_tokens": prompt.options.max_tokens,
}
if prompt.options.user_id:
kwargs["metadata"] = {"user_id": prompt.options.user_id}
if prompt.options.top_p:
kwargs["top_p"] = prompt.options.top_p
else:
kwargs["temperature"] = prompt.options.temperature
if prompt.options.top_k:
kwargs["top_k"] = prompt.options.top_k
if prompt.system:
kwargs["system"] = prompt.system
if self.extra_headers:
kwargs["extra_headers"] = self.extra_headers
return kwargs
def set_usage(self, response):
usage = response.response_json.pop("usage")
if usage:
response.set_usage(
input=usage.get("input_tokens"), output=usage.get("output_tokens")
)
def __str__(self):
return "Anthropic Messages: {}".format(self.model_id)
class ClaudeMessages(_Shared, llm.Model):
def execute(self, prompt, stream, response, conversation):
client = Anthropic(api_key=self.get_key())
kwargs = self.build_kwargs(prompt, conversation)
if stream:
with client.messages.stream(**kwargs) as stream:
for text in stream.text_stream:
yield text
# This records usage and other data:
response.response_json = stream.get_final_message().model_dump()
else:
completion = client.messages.create(**kwargs)
yield completion.content[0].text
response.response_json = completion.model_dump()
self.set_usage(response)
class ClaudeMessagesLong(ClaudeMessages):
class Options(ClaudeOptions):
max_tokens: Optional[int] = long_field
class AsyncClaudeMessages(_Shared, llm.AsyncModel):
async def execute(self, prompt, stream, response, conversation):
client = AsyncAnthropic(api_key=self.get_key())
kwargs = self.build_kwargs(prompt, conversation)
if stream:
async with client.messages.stream(**kwargs) as stream_obj:
async for text in stream_obj.text_stream:
yield text
response.response_json = (await stream_obj.get_final_message()).model_dump()
else:
completion = await client.messages.create(**kwargs)
yield completion.content[0].text
response.response_json = completion.model_dump()
self.set_usage(response)
class AsyncClaudeMessagesLong(AsyncClaudeMessages):
class Options(ClaudeOptions):
max_tokens: Optional[int] = long_field