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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 | class AnthropicMultiModal(MultiModalLLM):
model: str = Field(description="The Multi-Modal model to use from Anthropic.")
temperature: float = Field(description="The temperature to use for sampling.")
max_tokens: Optional[int] = Field(
description=" The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt",
gt=0,
)
context_window: Optional[int] = Field(
description="The maximum number of context tokens for the model.",
gt=0,
)
max_retries: int = Field(
default=3,
description="Maximum number of retries.",
ge=0,
)
timeout: float = Field(
default=60.0,
description="The timeout, in seconds, for API requests.",
ge=0,
)
api_key: str = Field(
default=None, description="The Anthropic API key.", exclude=True
)
system_prompt: str = Field(default="", description="System Prompt.")
api_base: str = Field(default=None, description="The base URL for Anthropic API.")
api_version: str = Field(description="The API version for Anthropic API.")
additional_kwargs: Dict[str, Any] = Field(
default_factory=dict, description="Additional kwargs for the Anthropic API."
)
default_headers: Optional[Dict[str, str]] = Field(
default=None, description="The default headers for API requests."
)
_messages_to_prompt: Callable = PrivateAttr()
_completion_to_prompt: Callable = PrivateAttr()
_client: Anthropic = PrivateAttr()
_aclient: AsyncAnthropic = PrivateAttr()
_http_client: Optional[httpx.Client] = PrivateAttr()
def __init__(
self,
model: str = "claude-3-opus-20240229",
temperature: float = DEFAULT_TEMPERATURE,
max_tokens: Optional[int] = 300,
additional_kwargs: Optional[Dict[str, Any]] = None,
context_window: Optional[int] = DEFAULT_CONTEXT_WINDOW,
max_retries: int = 3,
timeout: float = 60.0,
api_key: Optional[str] = None,
api_base: Optional[str] = None,
api_version: Optional[str] = None,
messages_to_prompt: Optional[Callable] = None,
completion_to_prompt: Optional[Callable] = None,
callback_manager: Optional[CallbackManager] = None,
default_headers: Optional[Dict[str, str]] = None,
http_client: Optional[httpx.Client] = None,
system_prompt: Optional[str] = "",
**kwargs: Any,
) -> None:
api_key, api_base, api_version = resolve_anthropic_credentials(
api_key=api_key,
api_base=api_base,
api_version=api_version,
)
super().__init__(
model=model,
temperature=temperature,
max_tokens=max_tokens,
additional_kwargs=additional_kwargs or {},
context_window=context_window,
max_retries=max_retries,
timeout=timeout,
api_key=api_key,
api_base=api_base,
api_version=api_version,
callback_manager=callback_manager,
default_headers=default_headers,
system_promt=system_prompt,
**kwargs,
)
self._messages_to_prompt = messages_to_prompt or generic_messages_to_prompt
self._completion_to_prompt = completion_to_prompt or (lambda x: x)
self._http_client = http_client
self._client, self._aclient = self._get_clients(**kwargs)
def _get_clients(self, **kwargs: Any) -> Tuple[Anthropic, AsyncAnthropic]:
client = Anthropic(**self._get_credential_kwargs())
aclient = AsyncAnthropic(**self._get_credential_kwargs())
return client, aclient
@classmethod
def class_name(cls) -> str:
return "anthropic_multi_modal_llm"
@property
def metadata(self) -> MultiModalLLMMetadata:
"""Multi Modal LLM metadata."""
return MultiModalLLMMetadata(
num_output=self.max_tokens or DEFAULT_NUM_OUTPUTS,
model_name=self.model,
)
def _get_credential_kwargs(self, **kwargs: Any) -> Dict[str, Any]:
credential_kwargs = {
"api_key": self.api_key,
"base_url": self.api_base,
"max_retries": self.max_retries,
"timeout": self.timeout,
**kwargs,
}
if self.default_headers:
credential_kwargs["default_headers"] = self.default_headers
return credential_kwargs
def _get_multi_modal_chat_messages(
self,
prompt: str,
role: str,
image_documents: Sequence[ImageNode],
**kwargs: Any,
) -> List[Dict]:
return generate_anthropic_multi_modal_chat_message(
prompt=prompt,
role=role,
image_documents=image_documents,
)
# Model Params for Anthropic Multi Modal model.
def _get_model_kwargs(self, **kwargs: Any) -> Dict[str, Any]:
if self.model not in ANTHROPIC_MULTI_MODAL_MODELS:
raise ValueError(
f"Invalid model {self.model}. "
f"Available models are: {list(ANTHROPIC_MULTI_MODAL_MODELS.keys())}"
)
base_kwargs = {"model": self.model, "temperature": self.temperature, **kwargs}
if self.max_tokens is not None:
base_kwargs["max_tokens"] = self.max_tokens
return {**base_kwargs, **self.additional_kwargs}
def _get_response_token_counts(self, raw_response: Any) -> dict:
"""Get the token usage reported by the response."""
if not isinstance(raw_response, dict):
return {}
usage = raw_response.get("usage", {})
# NOTE: other model providers that use the Anthropic client may not report usage
if usage is None:
return {}
return {
"prompt_tokens": usage.get("prompt_tokens", 0),
"completion_tokens": usage.get("completion_tokens", 0),
"total_tokens": usage.get("total_tokens", 0),
}
def _complete(
self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
) -> CompletionResponse:
all_kwargs = self._get_model_kwargs(**kwargs)
message_dict = self._get_multi_modal_chat_messages(
prompt=prompt, role=MessageRole.USER, image_documents=image_documents
)
response = self._client.messages.create(
messages=message_dict,
system=self.system_prompt,
stream=False,
**all_kwargs,
)
return CompletionResponse(
text=response.content[0].text,
raw=response,
additional_kwargs=self._get_response_token_counts(response),
)
def _stream_complete(
self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
) -> CompletionResponseGen:
all_kwargs = self._get_model_kwargs(**kwargs)
message_dict = self._get_multi_modal_chat_messages(
prompt=prompt, role=MessageRole.USER, image_documents=image_documents
)
def gen() -> CompletionResponseGen:
text = ""
for response in self._client.messages.create(
messages=message_dict,
stream=True,
system=self.system_prompt,
**all_kwargs,
):
if isinstance(response, ContentBlockDeltaEvent):
# update using deltas
content_delta = response.delta.text or ""
text += content_delta
yield CompletionResponse(
delta=content_delta,
text=text,
raw=response,
additional_kwargs=self._get_response_token_counts(response),
)
return gen()
def complete(
self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
) -> CompletionResponse:
return self._complete(prompt, image_documents, **kwargs)
def stream_complete(
self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
) -> CompletionResponseGen:
return self._stream_complete(prompt, image_documents, **kwargs)
def chat(
self,
**kwargs: Any,
) -> Any:
raise NotImplementedError("This function is not yet implemented.")
def stream_chat(
self,
**kwargs: Any,
) -> Any:
raise NotImplementedError("This function is not yet implemented.")
# ===== Async Endpoints =====
async def _acomplete(
self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
) -> CompletionResponse:
all_kwargs = self._get_model_kwargs(**kwargs)
message_dict = self._get_multi_modal_chat_messages(
prompt=prompt, role=MessageRole.USER, image_documents=image_documents
)
response = await self._aclient.messages.create(
messages=message_dict,
stream=False,
system=self.system_prompt,
**all_kwargs,
)
return CompletionResponse(
text=response.content[0].text,
raw=response,
additional_kwargs=self._get_response_token_counts(response),
)
async def acomplete(
self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
) -> CompletionResponse:
return await self._acomplete(prompt, image_documents, **kwargs)
async def _astream_complete(
self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
) -> CompletionResponseAsyncGen:
all_kwargs = self._get_model_kwargs(**kwargs)
message_dict = self._get_multi_modal_chat_messages(
prompt=prompt, role=MessageRole.USER, image_documents=image_documents
)
async def gen() -> CompletionResponseAsyncGen:
text = ""
async for response in await self._aclient.messages.create(
messages=message_dict,
stream=True,
system=self.system_prompt,
**all_kwargs,
):
if isinstance(response, ContentBlockDeltaEvent):
# update using deltas
content_delta = response.delta.text or ""
text += content_delta
yield CompletionResponse(
delta=content_delta,
text=text,
raw=response,
additional_kwargs=self._get_response_token_counts(response),
)
return gen()
async def astream_complete(
self, prompt: str, image_documents: Sequence[ImageNode], **kwargs: Any
) -> CompletionResponseAsyncGen:
return await self._astream_complete(prompt, image_documents, **kwargs)
async def achat(self, **kwargs: Any) -> Any:
raise NotImplementedError("This function is not yet implemented.")
async def astream_chat(self, **kwargs: Any) -> Any:
raise NotImplementedError("This function is not yet implemented.")
|