Repository navigation
Expand file tree
/
Copy path_client.py
More file actions
432 lines (362 loc) · 17.4 KB
/
Copy path_client.py
File metadata and controls
432 lines (362 loc) · 17.4 KB
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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
from __future__ import annotations
import os
from typing import TYPE_CHECKING, Any, Union, Mapping, TypeVar, Sequence
from typing_extensions import Self, override
import httpx2
from .._env import with_environment_headers
from ._auth import load_auth, refresh_auth
from ._beta import Beta, AsyncBeta
from ..._types import Omit, NotGiven, OpenTelemetryClientOption, not_given
from ..._utils import is_dict, asyncify, is_given, path_template, get_async_library
from ..._client import _keep_client_header_params
from ..._compat import model_copy, typed_cached_property
from ..._models import FinalRequestOptions
from ..._version import __version__
from ..._constants import ANTHROPIC_API_VERSION
from ..._streaming import Stream, AsyncStream
from ..._exceptions import AnthropicError
from ..._middleware import MiddlewareInput
from ..._base_client import (
DEFAULT_MAX_RETRIES,
BaseClient,
SyncAPIClient,
AsyncAPIClient,
)
from .._platform_client import copy_platform_client
from ...resources.messages import Messages, AsyncMessages
if TYPE_CHECKING:
from google.auth.credentials import Credentials as GoogleCredentials # type: ignore
_HttpxClientT = TypeVar("_HttpxClientT", bound=Union[httpx2.Client, httpx2.AsyncClient])
_DefaultStreamT = TypeVar("_DefaultStreamT", bound=Union[Stream[Any], AsyncStream[Any]])
class BaseVertexClient(BaseClient[_HttpxClientT, _DefaultStreamT]):
_gen_ai_provider_name = "gcp.vertex_ai"
_otel_platform = "vertex"
@typed_cached_property
def region(self) -> str:
raise RuntimeError("region not set")
@typed_cached_property
def project_id(self) -> str | None:
project_id = os.environ.get("ANTHROPIC_VERTEX_PROJECT_ID")
if project_id:
return project_id
return None
class AnthropicVertex(BaseVertexClient[httpx2.Client, Stream[Any]], SyncAPIClient):
messages: Messages
beta: Beta
def __init__(
self,
*,
region: str | NotGiven = not_given,
project_id: str | NotGiven = not_given,
access_token: str | None = None,
credentials: GoogleCredentials | None = None,
base_url: str | httpx2.URL | None = None,
timeout: float | httpx2.Timeout | None | NotGiven = not_given,
max_retries: int = DEFAULT_MAX_RETRIES,
default_headers: Mapping[str, str] | None = None,
default_query: Mapping[str, object] | None = None,
# OpenTelemetry tracing of this client's API calls. `False` disables the spans, and
# `traces.content_mode` set to `"content"` adds the content of requests and responses to them.
open_telemetry: OpenTelemetryClientOption = None,
# Configure a custom httpx client. See the [httpx documentation](https://www.python-httpx.org/api/#client) for more details.
http_client: httpx2.Client | None = None,
middleware: Sequence[MiddlewareInput] | None = None,
_strict_response_validation: bool = False,
) -> None:
if not is_given(region):
region = os.environ.get("CLOUD_ML_REGION", not_given)
if not is_given(region):
raise ValueError(
"No region was given. The client should be instantiated with the `region` argument or the `CLOUD_ML_REGION` environment variable should be set."
)
if region == "":
raise ValueError(f"Expected a non-empty value for `region` but received {region!r}")
if base_url is None:
base_url = os.environ.get("ANTHROPIC_VERTEX_BASE_URL")
if base_url is None:
if region == "global":
base_url = "https://aiplatform.googleapis.com/v1"
elif region == "us":
base_url = "https://aiplatform.us.rep.googleapis.com/v1"
elif region == "eu":
base_url = "https://aiplatform.eu.rep.googleapis.com/v1"
else:
base_url = f"https://{region}-aiplatform.googleapis.com/v1"
super().__init__(
version=__version__,
base_url=base_url,
timeout=timeout,
max_retries=max_retries,
open_telemetry=open_telemetry,
custom_headers=with_environment_headers(default_headers),
custom_query=default_query,
http_client=http_client,
middleware=middleware,
_strict_response_validation=_strict_response_validation,
)
if is_given(project_id):
self.project_id = project_id
self.region = region
self.access_token = access_token
self.credentials = credentials
self._default_stream_cls = Stream
self.messages = Messages(self)
self.beta = Beta(self)
@property
@override
def default_headers(self) -> dict[str, str | Omit]:
return {
**super().default_headers,
"X-Stainless-Async": "false",
**self._custom_headers,
}
@override
def _prepare_options(self, options: FinalRequestOptions) -> FinalRequestOptions:
return _prepare_options(options, project_id=self.project_id, region=self.region)
@override
def _prepare_request(self, request: httpx2.Request) -> None:
if request.headers.get("Authorization"):
# already authenticated, nothing for us to do
return
request.headers["Authorization"] = f"Bearer {self._ensure_access_token()}"
def _ensure_access_token(self) -> str:
if self.access_token is not None:
return self.access_token
if not self.credentials:
self.credentials, project_id = load_auth(project_id=self.project_id)
if not self.project_id:
self.project_id = project_id
if self.credentials.expired or not self.credentials.token:
refresh_auth(self.credentials)
if not self.credentials.token:
raise RuntimeError("Could not resolve API token from the environment")
assert isinstance(self.credentials.token, str)
return self.credentials.token
def copy(
self,
*,
region: str | NotGiven = not_given,
project_id: str | NotGiven = not_given,
access_token: str | None = None,
credentials: GoogleCredentials | None = None,
base_url: str | httpx2.URL | None = None,
timeout: float | httpx2.Timeout | None | NotGiven = not_given,
http_client: httpx2.Client | None = None,
max_retries: int | NotGiven = not_given,
open_telemetry: OpenTelemetryClientOption = None,
default_headers: Mapping[str, str] | None = None,
set_default_headers: Mapping[str, str] | None = None,
default_query: Mapping[str, object] | None = None,
set_default_query: Mapping[str, object] | None = None,
middleware: Sequence[MiddlewareInput] | None | NotGiven = not_given,
_extra_kwargs: Mapping[str, Any] = {},
) -> Self:
"""
Create a new client instance re-using the same options given to the current client with optional overriding.
"""
return copy_platform_client(
self,
region=region if is_given(region) else self.region,
project_id=project_id if is_given(project_id) else self.project_id or not_given,
access_token=access_token or self.access_token,
credentials=credentials or self.credentials,
base_url=base_url or self.base_url,
timeout=timeout,
http_client=http_client,
max_retries=max_retries,
open_telemetry=open_telemetry,
default_headers=default_headers,
set_default_headers=set_default_headers,
default_query=default_query,
set_default_query=set_default_query,
middleware=middleware,
**_extra_kwargs,
)
# Alias for `copy` for nicer inline usage, e.g.
# client.with_options(timeout=10).foo.create(...)
with_options = copy
def with_middleware(self, *middleware: MiddlewareInput) -> Self:
"""A new client with the given middleware appended after this client's middleware.
Convenience for applying extra middleware to a single request:
```py
client.with_middleware(my_middleware).messages.create(...)
```
"""
return self.copy(middleware=[*self._middleware, *middleware])
class AsyncAnthropicVertex(BaseVertexClient[httpx2.AsyncClient, AsyncStream[Any]], AsyncAPIClient):
messages: AsyncMessages
beta: AsyncBeta
def __init__(
self,
*,
region: str | NotGiven = not_given,
project_id: str | NotGiven = not_given,
access_token: str | None = None,
credentials: GoogleCredentials | None = None,
base_url: str | httpx2.URL | None = None,
timeout: float | httpx2.Timeout | None | NotGiven = not_given,
max_retries: int = DEFAULT_MAX_RETRIES,
default_headers: Mapping[str, str] | None = None,
default_query: Mapping[str, object] | None = None,
# OpenTelemetry tracing of this client's API calls. `False` disables the spans, and
# `traces.content_mode` set to `"content"` adds the content of requests and responses to them.
open_telemetry: OpenTelemetryClientOption = None,
# Configure a custom httpx client. See the [httpx documentation](https://www.python-httpx.org/api/#client) for more details.
http_client: httpx2.AsyncClient | None = None,
middleware: Sequence[MiddlewareInput] | None = None,
_strict_response_validation: bool = False,
) -> None:
if not is_given(region):
region = os.environ.get("CLOUD_ML_REGION", not_given)
if not is_given(region):
raise ValueError(
"No region was given. The client should be instantiated with the `region` argument or the `CLOUD_ML_REGION` environment variable should be set."
)
if region == "":
raise ValueError(f"Expected a non-empty value for `region` but received {region!r}")
if base_url is None:
base_url = os.environ.get("ANTHROPIC_VERTEX_BASE_URL")
if base_url is None:
if region == "global":
base_url = "https://aiplatform.googleapis.com/v1"
elif region == "us":
base_url = "https://aiplatform.us.rep.googleapis.com/v1"
elif region == "eu":
base_url = "https://aiplatform.eu.rep.googleapis.com/v1"
else:
base_url = f"https://{region}-aiplatform.googleapis.com/v1"
super().__init__(
version=__version__,
base_url=base_url,
timeout=timeout,
max_retries=max_retries,
open_telemetry=open_telemetry,
custom_headers=with_environment_headers(default_headers),
custom_query=default_query,
http_client=http_client,
middleware=middleware,
_strict_response_validation=_strict_response_validation,
)
if is_given(project_id):
self.project_id = project_id
self.region = region
self.access_token = access_token
self.credentials = credentials
self._default_stream_cls = AsyncStream
self.messages = AsyncMessages(self)
self.beta = AsyncBeta(self)
@property
@override
def default_headers(self) -> dict[str, str | Omit]:
return {
**super().default_headers,
"X-Stainless-Async": f"async:{get_async_library()}",
**self._custom_headers,
}
@override
async def _prepare_options(self, options: FinalRequestOptions) -> FinalRequestOptions:
return _prepare_options(options, project_id=self.project_id, region=self.region)
@override
async def _prepare_request(self, request: httpx2.Request) -> None:
if request.headers.get("Authorization"):
# already authenticated, nothing for us to do
return
request.headers["Authorization"] = f"Bearer {await self._ensure_access_token()}"
async def _ensure_access_token(self) -> str:
if self.access_token is not None:
return self.access_token
if not self.credentials:
self.credentials, project_id = await asyncify(load_auth)(project_id=self.project_id)
if not self.project_id:
self.project_id = project_id
if self.credentials.expired or not self.credentials.token:
await asyncify(refresh_auth)(self.credentials)
if not self.credentials.token:
raise RuntimeError("Could not resolve API token from the environment")
assert isinstance(self.credentials.token, str)
return self.credentials.token
def copy(
self,
*,
region: str | NotGiven = not_given,
project_id: str | NotGiven = not_given,
access_token: str | None = None,
credentials: GoogleCredentials | None = None,
base_url: str | httpx2.URL | None = None,
timeout: float | httpx2.Timeout | None | NotGiven = not_given,
http_client: httpx2.AsyncClient | None = None,
max_retries: int | NotGiven = not_given,
open_telemetry: OpenTelemetryClientOption = None,
default_headers: Mapping[str, str] | None = None,
set_default_headers: Mapping[str, str] | None = None,
default_query: Mapping[str, object] | None = None,
set_default_query: Mapping[str, object] | None = None,
middleware: Sequence[MiddlewareInput] | None | NotGiven = not_given,
_extra_kwargs: Mapping[str, Any] = {},
) -> Self:
"""
Create a new client instance re-using the same options given to the current client with optional overriding.
"""
return copy_platform_client(
self,
region=region if is_given(region) else self.region,
project_id=project_id if is_given(project_id) else self.project_id or not_given,
access_token=access_token or self.access_token,
credentials=credentials or self.credentials,
base_url=base_url or self.base_url,
timeout=timeout,
http_client=http_client,
max_retries=max_retries,
open_telemetry=open_telemetry,
default_headers=default_headers,
set_default_headers=set_default_headers,
default_query=default_query,
set_default_query=set_default_query,
middleware=middleware,
**_extra_kwargs,
)
# Alias for `copy` for nicer inline usage, e.g.
# client.with_options(timeout=10).foo.create(...)
with_options = copy
def with_middleware(self, *middleware: MiddlewareInput) -> Self:
"""A new client with the given middleware appended after this client's middleware.
Convenience for applying extra middleware to a single request:
```py
client.with_middleware(my_middleware).messages.create(...)
```
"""
return self.copy(middleware=[*self._middleware, *middleware])
def _prepare_options(input_options: FinalRequestOptions, *, project_id: str | None, region: str) -> FinalRequestOptions:
options = model_copy(_keep_client_header_params(input_options), deep=True)
if is_dict(options.json_data):
options.json_data.setdefault("anthropic_version", ANTHROPIC_API_VERSION)
if options.url in {"/v1/messages", "/v1/messages?beta=true"} and options.method == "post":
if project_id is None:
raise RuntimeError(
"No project_id was given and it could not be resolved from credentials. The client should be instantiated with the `project_id` argument or the `ANTHROPIC_VERTEX_PROJECT_ID` environment variable should be set."
)
if not is_dict(options.json_data):
raise RuntimeError("Expected json data to be a dictionary for post /v1/messages")
model = options.json_data.pop("model")
stream = options.json_data.get("stream", False)
specifier = "streamRawPredict" if stream else "rawPredict"
path = "/projects/{project_id}/locations/{region}/publishers/anthropic/models/{model}:{specifier}"
url = request_path(path, project_id=project_id, region=region, model=model, specifier=specifier)
parent, _, model_segment = url.rpartition("/")
# A server that drops `;` and the rest of its segment would drop the specifier.
options.url = f"{parent}/{model_segment.replace(';', '%3B')}"
if options.url in {"/v1/messages/count_tokens", "/v1/messages/count_tokens?beta=true"} and options.method == "post":
if project_id is None:
raise RuntimeError(
"No project_id was given and it could not be resolved from credentials. The client should be instantiated with the `project_id` argument or the `ANTHROPIC_VERTEX_PROJECT_ID` environment variable should be set."
)
path = "/projects/{project_id}/locations/{region}/publishers/anthropic/models/count-tokens:rawPredict"
options.url = request_path(path, project_id=project_id, region=region)
if options.url.startswith("/v1/messages/batches"):
raise AnthropicError("The Batch API is not supported in the Vertex client yet")
return options
def request_path(template: str, /, **values: object) -> str:
for name, value in values.items():
if value == "":
raise ValueError(f"Expected a non-empty value for `{name}` but received {value!r}")
return path_template(template, **values)