Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vertex): add credentials argument #542

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/anthropic/lib/vertex/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def __init__(
proxies: ProxiesTypes | None = None,
# See httpx documentation for [limits](https://www.python-httpx.org/advanced/#pool-limit-configuration)
connection_pool_limits: httpx.Limits | None = None,
credentials: GoogleCredentials | None = None,
_strict_response_validation: bool = False,
) -> None:
if not is_given(region):
Expand Down Expand Up @@ -161,7 +162,7 @@ def __init__(

self.region = region
self.access_token = access_token
self._credentials: GoogleCredentials | None = None
self.credentials = credentials

self.messages = Messages(self)

Expand All @@ -179,18 +180,18 @@ 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.credentials:
self.credentials, project_id = load_auth(project_id=self.project_id)
if not self.project_id:
self.project_id = project_id
else:
refresh_auth(self._credentials)
refresh_auth(self.credentials)

if not self._credentials.token:
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
assert isinstance(self.credentials.token, str)
return self.credentials.token


class AsyncAnthropicVertex(BaseVertexClient[httpx.AsyncClient, AsyncStream[Any]], AsyncAPIClient):
Expand All @@ -215,6 +216,7 @@ def __init__(
proxies: ProxiesTypes | None = None,
# See httpx documentation for [limits](https://www.python-httpx.org/advanced/#pool-limit-configuration)
connection_pool_limits: httpx.Limits | None = None,
credentials: GoogleCredentials | None = None,
_strict_response_validation: bool = False,
) -> None:
if not is_given(region):
Expand Down Expand Up @@ -248,7 +250,7 @@ def __init__(

self.region = region
self.access_token = access_token
self._credentials: GoogleCredentials | None = None
self.credentials = credentials

self.messages = AsyncMessages(self)

Expand All @@ -266,15 +268,15 @@ 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.credentials:
self.credentials, project_id = await asyncify(load_auth)(project_id=self.project_id)
if not self.project_id:
self.project_id = project_id
else:
await asyncify(refresh_auth)(self._credentials)
await asyncify(refresh_auth)(self.credentials)

if not self._credentials.token:
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
assert isinstance(self.credentials.token, str)
return self.credentials.token