-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathvertex.py
More file actions
111 lines (95 loc) · 3.31 KB
/
Copy pathvertex.py
File metadata and controls
111 lines (95 loc) · 3.31 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
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Example demonstrating Vertex AI authentication in Google Antigravity SDK.
This example demonstrates how to connect to Vertex AI using either:
1. Express Mode: API key authentication (vertex=True, api_key="...").
2. Standard Mode: Google Cloud project and location (vertex=True, project="...",
location="...").
To run:
# Option 1: Express Mode (API Key)
VERTEX_API_KEY="your-api-key" python vertex.py
# Option 2: Standard Mode (Project & Location with ADC)
GOOGLE_CLOUD_PROJECT="your-project" python vertex.py
Criteria for correct script performance:
1. The script exits cleanly with return code 0 (no unhandled exceptions).
2. The agent produces a non-empty text response.
"""
import argparse
import asyncio
import os
from google.antigravity import Agent
from google.antigravity import LocalAgentConfig
async def main() -> None:
parser = argparse.ArgumentParser(
description="Vertex AI authentication example."
)
parser.add_argument(
"--api_key",
default=None,
help="API key for Vertex Express Mode (defaults to VERTEX_API_KEY).",
)
parser.add_argument(
"--project",
default=None,
help="Google Cloud project ID (defaults to GOOGLE_CLOUD_PROJECT).",
)
parser.add_argument(
"--location",
default=None,
help=(
"Google Cloud location (defaults to GOOGLE_CLOUD_LOCATION or"
" us-central1)."
),
)
args = parser.parse_args()
api_key = args.api_key or os.environ.get("VERTEX_API_KEY")
project = args.project or os.environ.get("GOOGLE_CLOUD_PROJECT")
location = (
args.location or os.environ.get("GOOGLE_CLOUD_LOCATION") or "us-central1"
)
if api_key and project:
raise ValueError(
"Cannot specify both api_key (Express Mode) and project (Standard"
" Mode). They are mutually exclusive."
)
if api_key:
print("Authenticating via Vertex AI Express Mode (API Key)...")
config = LocalAgentConfig(
vertex=True,
api_key=api_key,
)
elif project:
print(
f"Authenticating via Vertex AI Standard Mode (Project: {project},"
f" Location: {location})..."
)
config = LocalAgentConfig(
vertex=True,
project=project,
location=location,
)
else:
raise ValueError(
"No Vertex AI credentials provided.\n"
"Provide an API key via --api_key or VERTEX_API_KEY (Express Mode),\n"
"or provide project/location via --project/--location or"
" GOOGLE_CLOUD_PROJECT (Standard Mode)."
)
async with Agent(config) as agent:
prompt = "Tell me a software engineering joke."
print(f" User: {prompt}")
response = await agent.chat(prompt)
print(f" Agent: {await response.text()}\n")
if __name__ == "__main__":
asyncio.run(main())