Skip to content

Commit bfc4c70

Browse files
Add Exmaples for how to use A2UI on Gemini Enterprise
1 parent 0fd7240 commit bfc4c70

File tree

21 files changed

+4594
-0
lines changed

21 files changed

+4594
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# A2UI on Gemini Enterprise
2+
3+
This folder contains examples and scripts to develop and use A2UI agents on **Gemini Enterprise**.
4+
5+
## Folder Structure
6+
7+
This directory is organized into two sub-folders depending the deployment methods:
8+
9+
- `cloud_run`: Contains scripts and configuration to deploy an agent to **Cloud Run**. This is suitable for building and deploying AI agents quickly, leveraging its speed and simplicity.
10+
- Starts with `cloud_run/README.md`.
11+
12+
- `agent_engine`: Contains code to deploy to **Vertex AI Agent Engine**. Agent Engine provides additional features like agent context management, agent evaluation, agent lifecycle management, model-based conversation quality monitoring, and model tuning with context data.
13+
- Starts with `agent_engine/README.md`.
14+
15+
## Shared Resources
16+
17+
- `a2ui_schema.py`: Contains the JSON schema for A2UI messages, used for validation during development and at runtime.
18+
- `a2ui_examples.py`: Provides several complete A2UI payload examples, such as Contact Cards and Action Confirmation modals.
19+
- `agent_executor.py`: A base implementation of an A2A (Agent-to-Agent) executor that handles A2UI validation and response formatting.
20+
21+
## Registration in Gemini Enterprise
22+
23+
Regardless of the deployment method, agents must be registered with Gemini Enterprise. This involves:
24+
1. **Defining an A2A Agent Card**: Describing the agent's skills, name, and capabilities (including the **A2UI extension**).
25+
2. **Configuring Authorization**: Setting up OAuth2 or other authentication mechanisms to allow the agent to talk to secure services. This is optional for Cloud Run deployment but **mandatory for Agent Engine deployment**.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
PROJECT_ID=<Your Google Cloud Project ID>
2+
LOCATION=<Your Google Cloud Location, for example: us-central1>
3+
STORAGE_BUCKET=<Your GCS Bucket starting with gs://>
4+
GEMINI_ENTERPRISE_APP_ID=<Your Gemini Enterprise App ID. You can create a new App or use an existing one.>
5+
AGENT_AUTHORIZATION=<Your Agent Authorization. See details in README.md>
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
# Register an A2UI Agent deployed on Vertex AI Agent Engine with Gemini Enterprise
2+
3+
This sample demonstrates how to deploy an A2UI Agent on **Agent Engine** and
4+
register on **Gemini Enterprise**.
5+
6+
## Overview
7+
8+
High level steps:
9+
10+
- Config Authorization
11+
- Setup environment variables
12+
- Develop A2UI agent with ADK + A2A (sample code provided)
13+
- Deploy the agent to Agent Engine
14+
- Register the agent on Gemini Enterprise
15+
16+
## Config Authorization
17+
18+
Check **Before you begin** section and follow **Obtain authorization details**
19+
section on
20+
[Register and manage A2A agents](https://docs.cloud.google.com/gemini/enterprise/docs/register-and-manage-an-a2a-agent).
21+
Download JSON that looks like:
22+
23+
```
24+
{
25+
"web": {
26+
"client_id": "<client id>",
27+
"project_id": "<Google Cloud project id>",
28+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
29+
"token_uri": "https://oauth2.googleapis.com/token",
30+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
31+
"client_secret": "<secret>",
32+
"redirect_uris": [
33+
"https://vertexaisearch.cloud.google.com/oauth-redirect",
34+
"https://vertexaisearch.cloud.google.com/static/oauth/oauth.html"
35+
]
36+
}
37+
}
38+
```
39+
40+
NOTE: For this deployment, you can skip the rest of **Register and manage A2A
41+
agents** page.
42+
43+
Replace **YOUR_CLIENT_ID** with `client_id` which can be found in the downloaded
44+
json and save the following as **authorizationUri**
45+
46+
```
47+
https://accounts.google.com/o/oauth2/v2/auth?client_id=<YOUR_CLIENT_ID>&redirect_uri=https%3A%2F%2Fvertexaisearch.cloud.google.com%2Fstatic%2Foauth%2Foauth.html&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform&include_granted_scopes=true&response_type=code&access_type=offline&prompt=consent
48+
```
49+
50+
In your console, run this:
51+
52+
```
53+
curl -X POST \
54+
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
55+
-H "Content-Type: application/json" \
56+
-H "X-Goog-User-Project: <YOUR_PROJECT_ID>" \
57+
"https://<ENDPOINT_LOCATION>-discoveryengine.googleapis.com/v1alpha/projects/<YOUR_PROJECT_ID>/locations/<LOCATION>/authorizations?authorizationId=<AUTH_ID>" \
58+
-d '{
59+
"name": "projects/<YOUR_PROJECT_ID>/locations/<LOCATION>/authorizations/<AUTH_ID>",
60+
"serverSideOauth2": {
61+
"clientId": "<OAUTH_CLIENT_ID>",
62+
"clientSecret": "<OAUTH_CLIENT_SECRET>",
63+
"authorizationUri": "<OAUTH_AUTH_URI>",
64+
"tokenUri": "<OAUTH_TOKEN_URI>"
65+
}
66+
}'
67+
'
68+
```
69+
70+
Replace the following:
71+
72+
- **YOUR_PROJECT_ID**: the ID of your project. There are 3 occasions
73+
- **ENDPOINT_LOCATION**: the multi-region for your API request. Specify one of
74+
the following values:
75+
- *us* for the US multi-region
76+
- *eu* for the EU multi-region
77+
- *global* for the Global location
78+
- **LOCATION**: the multi-region of your data store: *global*, *us*, or *eu*.
79+
There are 2 occasions
80+
- **AUTH_ID**: The ID of the authorization resource. This is an arbitrary
81+
alphanumeric ID that you define. You need to reference this ID later when
82+
registering an Agent that requires OAuth support. There are 2 occasions.
83+
- **OAUTH_CLIENT_ID**: copy `client_id` from the downloaded JSON.
84+
- **OAUTH_CLIENT_SECRET**: copy `client_secret` from the downloaded JSON.
85+
- **OAUTH_AUTH_URI**: the value of **authorizationUri**. See above.
86+
- **OAUTH_TOKEN_URI**: copy `token_uri` from the downloaded JSON.
87+
88+
NOTE: if `$(gcloud auth print-access-token)` does not work for you, replace it
89+
with `$(gcloud auth application-default print-access-token)` and try again.
90+
91+
As result, you will get **AGENT_AUTHORIZATION** like this:
92+
93+
```
94+
projects/PROJECT_NUMBER/locations/global/authorizations/<AUTH_ID>
95+
```
96+
97+
The value will be used as an environment variable described below.
98+
99+
NOTE: if you already have an agent that is deployed to Agent Engine, skip to
100+
**Manually Register An Agent** section.
101+
102+
## Setup Environment Variables
103+
104+
1. **Copy `.env.example`:** Duplicate the `.env.example` file and rename it to
105+
`.env`.
106+
- `cd /path/to/a2ui_on_agentengine`
107+
- `cp .env.example .env`
108+
2. **Fill `.env`:** Update the `.env` file with your specific Google Cloud
109+
project details:
110+
* `PROJECT_ID`: Your Google Cloud Project ID.
111+
* `LOCATION`: The Google Cloud region you want to deploy the agent in (e.g.,
112+
`us-central1`). This location is **not** the same as the *location* used
113+
in the command above.
114+
* `STORAGE_BUCKET`: A Google Cloud Storage bucket name for staging. It
115+
starts with **"gs://"**.
116+
* `GEMINI_ENTERPRISE_APP_ID`: Your Gemini Enterprise Application ID. You
117+
can create a new App or use an existing one on Google Cloud Gemini Enterprise.
118+
* `AGENT_AUTHORIZATION`: the value **AGENT_AUTHORIZATION** obtained above.
119+
120+
## Running the Script
121+
122+
The `main.py` script performs the following actions:
123+
124+
1. Initializes the Vertex AI client.
125+
2. Defines a sample "Contact Card Agent" skill and creates an agent card.
126+
3. Creates a local `A2aAgent` instance.
127+
4. Deploys the agent to Vertex AI Agent Engine (`client.agent_engines.create`).
128+
5. Fetches the deployed agent's card.
129+
6. Registers the agent on Gemini Enterprise using the Discovery Engine API.
130+
131+
To run the script using `uv`:
132+
133+
1. **Navigate to the script directory:**
134+
- `cd /path/to/a2ui_on_agentengine`
135+
2. **Create and activate a virtual environment:**
136+
- `uv venv source`
137+
- `.venv/bin/activate`
138+
3. **Install dependencies:**
139+
- `uv sync`
140+
4. **Run the script:**
141+
- `uv run main.py`
142+
- It may take 5-10 minutes to finish.
143+
144+
## Customization
145+
146+
To build your own agent, you will need to:
147+
148+
* Implement your agent's logic, by modifying or replacing `agent_executor.py`
149+
and the `AdkAgentToA2AExecutor` class.
150+
* Adjust the `agent_name`, `display_name`, and `description` when calling
151+
`_register_agent_on_gemini_enterprise` in `main.py`.
152+
153+
## Manually Register An Agent
154+
155+
If you have an Agent that is already deployed to Agent Engine, you can manually
156+
register it on Gemini Enterprise without running "main.py" script.
157+
158+
1. Complete **Config Authorization** section above.
159+
2. Open Google Cloud **Gemini Enterprise**.
160+
3. Click on the **App** you want to register your agent.
161+
- If you don't see the app being listed, click **Edit** to switch location
162+
4. Select **Agents** from the left nav bar.
163+
5. Click **Add agent** and select **Add** on **A2A** card.
164+
6. Copy this following JSON to the "Agent Card JSON" input box.
165+
166+
```
167+
{
168+
"name": "Test Contact Card Agent",
169+
"url": "https://<LOCATION>-aiplatform.googleapis.com/v1beta1/<RESOURCE_NAME>/a2a",
170+
"description": "A helpful assistant agent that can find contact card s.",
171+
"skills": [
172+
{
173+
"description": "A helpful assistant agent that can find contact cards.",
174+
"tags": [
175+
"Contact-Card"
176+
],
177+
"name": "Contact Card Agent",
178+
"examples": [
179+
"Who is John Doe?",
180+
"List all contact cards."
181+
],
182+
"id": "contact_card_agent"
183+
}
184+
],
185+
"version": "1.0.0",
186+
"capabilities": {
187+
"streaming": false,
188+
"extensions": [
189+
{
190+
"uri": "https://a2ui.org/a2a-extension/a2ui/v0.8",
191+
"description": "Ability to render A2UI",
192+
"required": false,
193+
"params": {
194+
"supportedCatalogIds": [
195+
"https://a2ui.org/specification/v0_8/standard_catalog_definition.json"
196+
]
197+
}
198+
}
199+
]
200+
},
201+
"protocolVersion": "0.3.0",
202+
"defaultOutputModes": [
203+
"application/json"
204+
],
205+
"defaultInputModes": [
206+
"text/plain"
207+
],
208+
"supportsAuthenticatedExtendedCard": true,
209+
"preferredTransport": "HTTP+JSON"
210+
}
211+
```
212+
213+
Replace **LOCATION** and **RESOURCE_NAME**.
214+
215+
- LOCATION is where you deploy your agent. For example; us-central1.
216+
- RESOURCE_NAME can be found on Google Cloud **Agent Engine**: click the agent;
217+
click **Service Configuration**; select **Deployment details**; copy
218+
**Resource name**.
219+
220+
Update *name*, *description*, *skills*, *version* as needed. Leave other
221+
values unchanged.
222+
223+
7. Click **Preview Agent Details**
224+
225+
8. Click **Next**
226+
227+
9. Fill the **Agent authorization** form:
228+
229+
- Copy `client_id`, `client_secret`, `token_uri` from the downloaded JSON.
230+
- Copy **authorizationUri** value from the above to **Authorization URL**.
231+
- Leave **Scopes** field empty.
232+
- Click **Finish**
233+
234+
## Test Your Agent
235+
236+
1. Open Google Cloud Console and search for **"Gemini Enterprise"** and click on it.
237+
2. Open the project you used in the above setting.
238+
3. Click on the **App** you used to register your agent.
239+
- If you don't see your app being listed, click **"Edit"** to switch
240+
location
241+
4. Select **"Agents"** from the left nav bar.
242+
5. Click the three-dot button on the **"Actions"** column and select
243+
**"Previwe"** menu.
244+
6. It will open Gemini Enterprise Agent page.
245+
7. Try queries like *"Find contact card of Sarah"*.
246+
- If this is the first time you start a chat with the agent, it will ask
247+
for manual authorization.
248+
8. You should see a Contact Card being rendered on Gemini Enterprise.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This file makes this directory a Python package

0 commit comments

Comments
 (0)