agent-starter-pack 0.18.1__py3-none-any.whl → 0.19.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of agent-starter-pack might be problematic. Click here for more details.
- agent_starter_pack/agents/adk_a2a_base/.template/templateconfig.yaml +22 -0
- agent_starter_pack/agents/adk_a2a_base/README.md +22 -0
- agent_starter_pack/agents/adk_a2a_base/app/__init__.py +17 -0
- agent_starter_pack/agents/adk_a2a_base/app/agent.py +70 -0
- agent_starter_pack/agents/adk_a2a_base/notebooks/adk_a2a_app_testing.ipynb +600 -0
- agent_starter_pack/agents/adk_a2a_base/notebooks/evaluating_adk_agent.ipynb +1535 -0
- agent_starter_pack/agents/adk_a2a_base/tests/integration/test_agent.py +58 -0
- agent_starter_pack/base_template/.gitignore +1 -1
- agent_starter_pack/base_template/Makefile +11 -11
- agent_starter_pack/base_template/README.md +1 -1
- agent_starter_pack/base_template/{% if cookiecutter.cicd_runner == 'github_actions' %}.github{% else %}unused_github{% endif %}/workflows/deploy-to-prod.yaml +10 -2
- agent_starter_pack/base_template/{% if cookiecutter.cicd_runner == 'github_actions' %}.github{% else %}unused_github{% endif %}/workflows/staging.yaml +26 -5
- agent_starter_pack/base_template/{% if cookiecutter.cicd_runner == 'google_cloud_build' %}.cloudbuild{% else %}unused_.cloudbuild{% endif %}/deploy-to-prod.yaml +18 -3
- agent_starter_pack/base_template/{% if cookiecutter.cicd_runner == 'google_cloud_build' %}.cloudbuild{% else %}unused_.cloudbuild{% endif %}/staging.yaml +34 -3
- agent_starter_pack/cli/utils/cicd.py +20 -4
- agent_starter_pack/cli/utils/register_gemini_enterprise.py +79 -84
- agent_starter_pack/cli/utils/template.py +2 -0
- agent_starter_pack/deployment_targets/agent_engine/tests/integration/test_agent_engine_app.py +104 -2
- agent_starter_pack/deployment_targets/agent_engine/tests/load_test/load_test.py +144 -0
- agent_starter_pack/deployment_targets/agent_engine/tests/{% if cookiecutter.is_adk_a2a %}helpers.py{% else %}unused_helpers.py{% endif %} +138 -0
- agent_starter_pack/deployment_targets/agent_engine/{{cookiecutter.agent_directory}}/agent_engine_app.py +88 -4
- agent_starter_pack/deployment_targets/agent_engine/{{cookiecutter.agent_directory}}/utils/deployment.py +4 -0
- agent_starter_pack/deployment_targets/cloud_run/Dockerfile +3 -0
- agent_starter_pack/deployment_targets/cloud_run/deployment/terraform/dev/service.tf +7 -0
- agent_starter_pack/deployment_targets/cloud_run/deployment/terraform/service.tf +16 -2
- agent_starter_pack/deployment_targets/cloud_run/tests/integration/test_server_e2e.py +218 -1
- agent_starter_pack/deployment_targets/cloud_run/tests/load_test/README.md +2 -2
- agent_starter_pack/deployment_targets/cloud_run/tests/load_test/load_test.py +51 -4
- agent_starter_pack/deployment_targets/cloud_run/{{cookiecutter.agent_directory}}/server.py +66 -0
- agent_starter_pack/resources/locks/uv-adk_a2a_base-agent_engine.lock +4224 -0
- agent_starter_pack/resources/locks/uv-adk_a2a_base-cloud_run.lock +4819 -0
- agent_starter_pack/resources/locks/uv-adk_base-agent_engine.lock +230 -236
- agent_starter_pack/resources/locks/uv-adk_base-cloud_run.lock +290 -296
- agent_starter_pack/resources/locks/uv-adk_live-agent_engine.lock +230 -236
- agent_starter_pack/resources/locks/uv-adk_live-cloud_run.lock +290 -296
- agent_starter_pack/resources/locks/uv-agentic_rag-agent_engine.lock +234 -239
- agent_starter_pack/resources/locks/uv-agentic_rag-cloud_run.lock +294 -299
- agent_starter_pack/resources/locks/uv-crewai_coding_crew-agent_engine.lock +221 -228
- agent_starter_pack/resources/locks/uv-crewai_coding_crew-cloud_run.lock +279 -286
- agent_starter_pack/resources/locks/uv-langgraph_base_react-agent_engine.lock +226 -233
- agent_starter_pack/resources/locks/uv-langgraph_base_react-cloud_run.lock +298 -305
- {agent_starter_pack-0.18.1.dist-info → agent_starter_pack-0.19.0.dist-info}/METADATA +2 -1
- {agent_starter_pack-0.18.1.dist-info → agent_starter_pack-0.19.0.dist-info}/RECORD +46 -36
- {agent_starter_pack-0.18.1.dist-info → agent_starter_pack-0.19.0.dist-info}/WHEEL +0 -0
- {agent_starter_pack-0.18.1.dist-info → agent_starter_pack-0.19.0.dist-info}/entry_points.txt +0 -0
- {agent_starter_pack-0.18.1.dist-info → agent_starter_pack-0.19.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cells": [
|
|
3
|
+
{
|
|
4
|
+
"cell_type": "markdown",
|
|
5
|
+
"metadata": {},
|
|
6
|
+
"source": [
|
|
7
|
+
"# 🧪 ADK with A2A Application Testing\n",
|
|
8
|
+
"\n",
|
|
9
|
+
"This notebook demonstrates how to test an ADK (Agent Development Kit) application that implements the Agent2Agent (A2A) protocol.\n",
|
|
10
|
+
"It covers both local and remote testing, both with Agent Engine and Cloud Run."
|
|
11
|
+
]
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"cell_type": "markdown",
|
|
15
|
+
"metadata": {},
|
|
16
|
+
"source": [
|
|
17
|
+
"## Install dependencies"
|
|
18
|
+
]
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"cell_type": "code",
|
|
22
|
+
"execution_count": null,
|
|
23
|
+
"metadata": {},
|
|
24
|
+
"outputs": [],
|
|
25
|
+
"source": [
|
|
26
|
+
"!pip install google-cloud-aiplatform a2a-sdk --upgrade"
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"cell_type": "code",
|
|
31
|
+
"execution_count": null,
|
|
32
|
+
"metadata": {},
|
|
33
|
+
"outputs": [],
|
|
34
|
+
"source": [
|
|
35
|
+
"# Uncomment the following lines if you're not using the virtual environment created by uv\n",
|
|
36
|
+
"# import sys\n",
|
|
37
|
+
"\n",
|
|
38
|
+
"# sys.path.append(\"../\")"
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"cell_type": "markdown",
|
|
43
|
+
"metadata": {},
|
|
44
|
+
"source": [
|
|
45
|
+
"### Import libraries"
|
|
46
|
+
]
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"cell_type": "code",
|
|
50
|
+
"execution_count": null,
|
|
51
|
+
"metadata": {},
|
|
52
|
+
"outputs": [],
|
|
53
|
+
"source": [
|
|
54
|
+
"# ruff: noqa\n",
|
|
55
|
+
"import asyncio\n",
|
|
56
|
+
"import json\n",
|
|
57
|
+
"import os\n",
|
|
58
|
+
"import requests\n",
|
|
59
|
+
"import uuid\n",
|
|
60
|
+
"\n",
|
|
61
|
+
"import vertexai\n",
|
|
62
|
+
"from a2a.types import (\n",
|
|
63
|
+
" Message,\n",
|
|
64
|
+
" MessageSendParams,\n",
|
|
65
|
+
" Part,\n",
|
|
66
|
+
" Role,\n",
|
|
67
|
+
" SendStreamingMessageRequest,\n",
|
|
68
|
+
" TextPart,\n",
|
|
69
|
+
")\n",
|
|
70
|
+
"from IPython.display import Markdown, display\n",
|
|
71
|
+
"from google.adk.artifacts import InMemoryArtifactService\n",
|
|
72
|
+
"from google.adk.sessions import InMemorySessionService\n",
|
|
73
|
+
"\n",
|
|
74
|
+
"from app.agent_engine_app import AgentEngineApp\n",
|
|
75
|
+
"from tests.helpers import (\n",
|
|
76
|
+
" build_get_request,\n",
|
|
77
|
+
" build_post_request,\n",
|
|
78
|
+
" poll_task_completion,\n",
|
|
79
|
+
")"
|
|
80
|
+
]
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
"cell_type": "markdown",
|
|
84
|
+
"metadata": {},
|
|
85
|
+
"source": [
|
|
86
|
+
"### Initialize Vertex AI Client"
|
|
87
|
+
]
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
"cell_type": "code",
|
|
91
|
+
"execution_count": null,
|
|
92
|
+
"metadata": {},
|
|
93
|
+
"outputs": [],
|
|
94
|
+
"source": [
|
|
95
|
+
"# Initialize the Vertex AI client\n",
|
|
96
|
+
"LOCATION = \"us-central1\"\n",
|
|
97
|
+
"\n",
|
|
98
|
+
"client = vertexai.Client(\n",
|
|
99
|
+
" location=LOCATION,\n",
|
|
100
|
+
")"
|
|
101
|
+
]
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
"cell_type": "markdown",
|
|
105
|
+
"metadata": {},
|
|
106
|
+
"source": [
|
|
107
|
+
"## If you are using Agent Engine\n",
|
|
108
|
+
"See more documentation at [Agent Engine Overview](https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/overview)"
|
|
109
|
+
]
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
"cell_type": "markdown",
|
|
113
|
+
"metadata": {},
|
|
114
|
+
"source": [
|
|
115
|
+
"### Remote Testing"
|
|
116
|
+
]
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
"cell_type": "code",
|
|
120
|
+
"execution_count": null,
|
|
121
|
+
"metadata": {},
|
|
122
|
+
"outputs": [],
|
|
123
|
+
"source": [
|
|
124
|
+
"# Set to None to auto-detect from ./deployment_metadata.json, or specify manually\n",
|
|
125
|
+
"# \"projects/PROJECT_ID/locations/us-central1/reasoningEngines/ENGINE_ID\"\n",
|
|
126
|
+
"REASONING_ENGINE_ID = None\n",
|
|
127
|
+
"\n",
|
|
128
|
+
"if REASONING_ENGINE_ID is None:\n",
|
|
129
|
+
" try:\n",
|
|
130
|
+
" with open(\"../deployment_metadata.json\") as f:\n",
|
|
131
|
+
" metadata = json.load(f)\n",
|
|
132
|
+
" REASONING_ENGINE_ID = metadata.get(\"remote_agent_engine_id\")\n",
|
|
133
|
+
" except (FileNotFoundError, json.JSONDecodeError):\n",
|
|
134
|
+
" pass\n",
|
|
135
|
+
"\n",
|
|
136
|
+
"print(f\"Using REASONING_ENGINE_ID: {REASONING_ENGINE_ID}\")\n",
|
|
137
|
+
"\n",
|
|
138
|
+
"# Extract project_id, location, and engine_id from REASONING_ENGINE_ID\n",
|
|
139
|
+
"parts = REASONING_ENGINE_ID.split(\"/\")\n",
|
|
140
|
+
"project_id = parts[1]\n",
|
|
141
|
+
"location = parts[3]\n",
|
|
142
|
+
"engine_id = parts[5]\n",
|
|
143
|
+
"\n",
|
|
144
|
+
"# Construct API endpoints\n",
|
|
145
|
+
"base_url = f\"https://{location}-aiplatform.googleapis.com\"\n",
|
|
146
|
+
"a2a_base_path = f\"/v1beta1/projects/{project_id}/locations/{location}/reasoningEngines/{engine_id}/a2a/v1\"\n",
|
|
147
|
+
"\n",
|
|
148
|
+
"print(f\"Base URL: {base_url}\")\n",
|
|
149
|
+
"print(f\"A2A base path: {a2a_base_path}\")"
|
|
150
|
+
]
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"cell_type": "markdown",
|
|
154
|
+
"metadata": {},
|
|
155
|
+
"source": [
|
|
156
|
+
"#### Fetch Agent Card"
|
|
157
|
+
]
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
"cell_type": "code",
|
|
161
|
+
"execution_count": null,
|
|
162
|
+
"metadata": {},
|
|
163
|
+
"outputs": [],
|
|
164
|
+
"source": [
|
|
165
|
+
"# Fetch agent card using REST API\n",
|
|
166
|
+
"import google.auth\n",
|
|
167
|
+
"import google.auth.transport.requests\n",
|
|
168
|
+
"\n",
|
|
169
|
+
"# Get authentication token\n",
|
|
170
|
+
"creds, project = google.auth.default()\n",
|
|
171
|
+
"auth_req = google.auth.transport.requests.Request()\n",
|
|
172
|
+
"creds.refresh(auth_req)\n",
|
|
173
|
+
"\n",
|
|
174
|
+
"headers = {\"Content-Type\": \"application/json\", \"Authorization\": f\"Bearer {creds.token}\"}\n",
|
|
175
|
+
"\n",
|
|
176
|
+
"# GET request to fetch agent card\n",
|
|
177
|
+
"response = requests.get(\n",
|
|
178
|
+
" f\"{base_url}{a2a_base_path}/card\",\n",
|
|
179
|
+
" headers=headers,\n",
|
|
180
|
+
")\n",
|
|
181
|
+
"\n",
|
|
182
|
+
"print(f\"Response status code: {response.status_code}\")\n",
|
|
183
|
+
"\n",
|
|
184
|
+
"if response.status_code == 200:\n",
|
|
185
|
+
" remote_a2a_agent_card = response.json()\n",
|
|
186
|
+
" print(f\"Agent: {remote_a2a_agent_card.get('name')}\")\n",
|
|
187
|
+
" print(f\"URL: {remote_a2a_agent_card.get('url')}\")\n",
|
|
188
|
+
" print(\n",
|
|
189
|
+
" f\"Skills: {[s.get('description') for s in remote_a2a_agent_card.get('skills', [])]}\"\n",
|
|
190
|
+
" )\n",
|
|
191
|
+
" print(f\"Protocol Version: {remote_a2a_agent_card.get('protocolVersion')}\")\n",
|
|
192
|
+
"else:\n",
|
|
193
|
+
" print(f\"Error: {response.text}\")"
|
|
194
|
+
]
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
"cell_type": "markdown",
|
|
198
|
+
"metadata": {},
|
|
199
|
+
"source": [
|
|
200
|
+
"#### Send Message"
|
|
201
|
+
]
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
"cell_type": "code",
|
|
205
|
+
"execution_count": null,
|
|
206
|
+
"metadata": {},
|
|
207
|
+
"outputs": [],
|
|
208
|
+
"source": [
|
|
209
|
+
"# Send the message using A2A REST API\n",
|
|
210
|
+
"import google.auth\n",
|
|
211
|
+
"import google.auth.transport.requests\n",
|
|
212
|
+
"\n",
|
|
213
|
+
"# Get authentication token\n",
|
|
214
|
+
"creds, project = google.auth.default()\n",
|
|
215
|
+
"auth_req = google.auth.transport.requests.Request()\n",
|
|
216
|
+
"creds.refresh(auth_req)\n",
|
|
217
|
+
"\n",
|
|
218
|
+
"headers = {\"Content-Type\": \"application/json\", \"Authorization\": f\"Bearer {creds.token}\"}\n",
|
|
219
|
+
"\n",
|
|
220
|
+
"data = {\n",
|
|
221
|
+
" \"message\": {\n",
|
|
222
|
+
" \"messageId\": f\"msg-{os.urandom(8).hex()}\",\n",
|
|
223
|
+
" \"content\": [{\"text\": \"What is the weather in New York?\"}],\n",
|
|
224
|
+
" \"role\": \"ROLE_USER\",\n",
|
|
225
|
+
" }\n",
|
|
226
|
+
"}\n",
|
|
227
|
+
"\n",
|
|
228
|
+
"# Send POST request to message:send endpoint\n",
|
|
229
|
+
"response = requests.post(\n",
|
|
230
|
+
" f\"{base_url}{a2a_base_path}/message:send\",\n",
|
|
231
|
+
" headers=headers,\n",
|
|
232
|
+
" json=data,\n",
|
|
233
|
+
")\n",
|
|
234
|
+
"\n",
|
|
235
|
+
"print(f\"Response status code: {response.status_code}\")\n",
|
|
236
|
+
"\n",
|
|
237
|
+
"if response.status_code == 200:\n",
|
|
238
|
+
" response_data = response.json()\n",
|
|
239
|
+
" task_id = response_data[\"task\"][\"id\"]\n",
|
|
240
|
+
" print(f\"Task started: {task_id}\")\n",
|
|
241
|
+
"else:\n",
|
|
242
|
+
" print(f\"Error: {response.text}\")"
|
|
243
|
+
]
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
"cell_type": "markdown",
|
|
247
|
+
"metadata": {},
|
|
248
|
+
"source": [
|
|
249
|
+
"#### Poll for response"
|
|
250
|
+
]
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
"cell_type": "code",
|
|
254
|
+
"execution_count": null,
|
|
255
|
+
"metadata": {},
|
|
256
|
+
"outputs": [],
|
|
257
|
+
"source": [
|
|
258
|
+
"# Poll for task completion using REST API\n",
|
|
259
|
+
"max_attempts = 30\n",
|
|
260
|
+
"for attempt in range(max_attempts):\n",
|
|
261
|
+
" poll_response = requests.get(\n",
|
|
262
|
+
" f\"{base_url}{a2a_base_path}/tasks/{task_id}\",\n",
|
|
263
|
+
" headers=headers,\n",
|
|
264
|
+
" )\n",
|
|
265
|
+
"\n",
|
|
266
|
+
" if poll_response.status_code != 200:\n",
|
|
267
|
+
" print(f\"Poll failed with status code: {poll_response.status_code}\")\n",
|
|
268
|
+
" break\n",
|
|
269
|
+
"\n",
|
|
270
|
+
" result = poll_response.json()\n",
|
|
271
|
+
" task_state = result.get(\"status\", {}).get(\"state\")\n",
|
|
272
|
+
" print(f\"Attempt {attempt + 1}: {task_state}\")\n",
|
|
273
|
+
"\n",
|
|
274
|
+
" if task_state == \"TASK_STATE_COMPLETED\":\n",
|
|
275
|
+
" print(\"Task completed!\")\n",
|
|
276
|
+
" break\n",
|
|
277
|
+
" elif task_state in [\"TASK_STATE_FAILED\", \"TASK_STATE_CANCELLED\"]:\n",
|
|
278
|
+
" print(f\"Task failed: {result}\")\n",
|
|
279
|
+
" break\n",
|
|
280
|
+
"\n",
|
|
281
|
+
" await asyncio.sleep(1)\n",
|
|
282
|
+
"\n",
|
|
283
|
+
"# Extract and display artifacts\n",
|
|
284
|
+
"if \"artifacts\" in result and result[\"artifacts\"]:\n",
|
|
285
|
+
" for artifact in result[\"artifacts\"]:\n",
|
|
286
|
+
" if artifact.get(\"parts\"):\n",
|
|
287
|
+
" for part in artifact[\"parts\"]:\n",
|
|
288
|
+
" if \"text\" in part:\n",
|
|
289
|
+
" display(Markdown(f\"**Answer**:\\\\n {part['text']}\"))\n",
|
|
290
|
+
" else:\n",
|
|
291
|
+
" print(\"Could not extract text from artifact parts.\")\n",
|
|
292
|
+
"else:\n",
|
|
293
|
+
" print(\"No artifacts found in result\")"
|
|
294
|
+
]
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
"cell_type": "markdown",
|
|
298
|
+
"metadata": {},
|
|
299
|
+
"source": [
|
|
300
|
+
"### Local Testing\n",
|
|
301
|
+
"\n",
|
|
302
|
+
"You can import directly the AgentEngineApp class within your environment. \n",
|
|
303
|
+
"To run the agent locally, follow these steps:\n",
|
|
304
|
+
"1. Make sure all required packages are installed in your environment\n",
|
|
305
|
+
"2. The recommended approach is to use the same virtual environment created by the 'uv' tool\n",
|
|
306
|
+
"3. You can set up this environment by running 'make install' from your agent's root directory\n",
|
|
307
|
+
"4. Then select this kernel (.venv folder in your project) in your Jupyter notebook to ensure all dependencies are available"
|
|
308
|
+
]
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
"cell_type": "code",
|
|
312
|
+
"execution_count": null,
|
|
313
|
+
"metadata": {},
|
|
314
|
+
"outputs": [],
|
|
315
|
+
"source": [
|
|
316
|
+
"local_agent_engine = await AgentEngineApp.create(\n",
|
|
317
|
+
" artifact_service_builder=lambda: InMemoryArtifactService(),\n",
|
|
318
|
+
" session_service_builder=lambda: InMemorySessionService(),\n",
|
|
319
|
+
")\n",
|
|
320
|
+
"local_agent_engine.set_up()"
|
|
321
|
+
]
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
"cell_type": "markdown",
|
|
325
|
+
"metadata": {},
|
|
326
|
+
"source": [
|
|
327
|
+
"#### Verify Custom Method is Registered"
|
|
328
|
+
]
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
"cell_type": "code",
|
|
332
|
+
"execution_count": null,
|
|
333
|
+
"metadata": {},
|
|
334
|
+
"outputs": [],
|
|
335
|
+
"source": [
|
|
336
|
+
"test = local_agent_engine.register_operations()\n",
|
|
337
|
+
"print(test)"
|
|
338
|
+
]
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
"cell_type": "markdown",
|
|
342
|
+
"metadata": {},
|
|
343
|
+
"source": [
|
|
344
|
+
"#### Fetch Agent Card"
|
|
345
|
+
]
|
|
346
|
+
},
|
|
347
|
+
{
|
|
348
|
+
"cell_type": "code",
|
|
349
|
+
"execution_count": null,
|
|
350
|
+
"metadata": {},
|
|
351
|
+
"outputs": [],
|
|
352
|
+
"source": [
|
|
353
|
+
"request = build_get_request(None)\n",
|
|
354
|
+
"response = await local_agent_engine.handle_authenticated_agent_card(\n",
|
|
355
|
+
" request=request, context=None\n",
|
|
356
|
+
")\n",
|
|
357
|
+
"print(response)"
|
|
358
|
+
]
|
|
359
|
+
},
|
|
360
|
+
{
|
|
361
|
+
"cell_type": "markdown",
|
|
362
|
+
"metadata": {},
|
|
363
|
+
"source": [
|
|
364
|
+
"#### Send Message"
|
|
365
|
+
]
|
|
366
|
+
},
|
|
367
|
+
{
|
|
368
|
+
"cell_type": "code",
|
|
369
|
+
"execution_count": null,
|
|
370
|
+
"metadata": {},
|
|
371
|
+
"outputs": [],
|
|
372
|
+
"source": [
|
|
373
|
+
"message_data = {\n",
|
|
374
|
+
" \"message\": {\n",
|
|
375
|
+
" \"messageId\": f\"msg-{os.urandom(8).hex()}\",\n",
|
|
376
|
+
" \"content\": [{\"text\": \"What is the weather in New York?\"}],\n",
|
|
377
|
+
" \"role\": \"ROLE_USER\",\n",
|
|
378
|
+
" },\n",
|
|
379
|
+
"}\n",
|
|
380
|
+
"\n",
|
|
381
|
+
"request = build_post_request(message_data)\n",
|
|
382
|
+
"\n",
|
|
383
|
+
"response = await local_agent_engine.on_message_send(request=request, context=None)\n",
|
|
384
|
+
"print(response)"
|
|
385
|
+
]
|
|
386
|
+
},
|
|
387
|
+
{
|
|
388
|
+
"cell_type": "markdown",
|
|
389
|
+
"metadata": {},
|
|
390
|
+
"source": [
|
|
391
|
+
"#### Poll for response"
|
|
392
|
+
]
|
|
393
|
+
},
|
|
394
|
+
{
|
|
395
|
+
"cell_type": "code",
|
|
396
|
+
"execution_count": null,
|
|
397
|
+
"metadata": {},
|
|
398
|
+
"outputs": [],
|
|
399
|
+
"source": [
|
|
400
|
+
"task_id = response[\"task\"][\"id\"]\n",
|
|
401
|
+
"print(f\"The Task ID is: {task_id}\")\n",
|
|
402
|
+
"\n",
|
|
403
|
+
"# Poll for completion using helper\n",
|
|
404
|
+
"final_response = await poll_task_completion(local_agent_engine, task_id)\n",
|
|
405
|
+
"\n",
|
|
406
|
+
"# Extract and display artifacts\n",
|
|
407
|
+
"for artifact in final_response[\"artifacts\"]:\n",
|
|
408
|
+
" if artifact[\"parts\"] and \"text\" in artifact[\"parts\"][0]:\n",
|
|
409
|
+
" display(Markdown(f\"**Answer**:\\n {artifact['parts'][0]['text']}\"))\n",
|
|
410
|
+
" else:\n",
|
|
411
|
+
" print(\"Could not extract text from artifact parts.\")"
|
|
412
|
+
]
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
"cell_type": "markdown",
|
|
416
|
+
"metadata": {},
|
|
417
|
+
"source": [
|
|
418
|
+
"#### Register Feedback"
|
|
419
|
+
]
|
|
420
|
+
},
|
|
421
|
+
{
|
|
422
|
+
"cell_type": "code",
|
|
423
|
+
"execution_count": null,
|
|
424
|
+
"metadata": {},
|
|
425
|
+
"outputs": [],
|
|
426
|
+
"source": [
|
|
427
|
+
"local_agent_engine.register_feedback(\n",
|
|
428
|
+
" feedback={\n",
|
|
429
|
+
" \"score\": 5,\n",
|
|
430
|
+
" \"text\": \"Great response!\",\n",
|
|
431
|
+
" \"invocation_id\": \"test-invocation-123\",\n",
|
|
432
|
+
" \"user_id\": \"test\",\n",
|
|
433
|
+
" }\n",
|
|
434
|
+
")"
|
|
435
|
+
]
|
|
436
|
+
},
|
|
437
|
+
{
|
|
438
|
+
"cell_type": "markdown",
|
|
439
|
+
"metadata": {},
|
|
440
|
+
"source": [
|
|
441
|
+
"## If you are using Cloud Run"
|
|
442
|
+
]
|
|
443
|
+
},
|
|
444
|
+
{
|
|
445
|
+
"cell_type": "markdown",
|
|
446
|
+
"metadata": {},
|
|
447
|
+
"source": [
|
|
448
|
+
"### Remote Testing\n",
|
|
449
|
+
"\n",
|
|
450
|
+
"For more information about authenticating HTTPS requests to Cloud Run services, see:\n",
|
|
451
|
+
"[Cloud Run Authentication Documentation](https://cloud.google.com/run/docs/triggering/https-request)\n",
|
|
452
|
+
"\n",
|
|
453
|
+
"Remote testing involves using a deployed service URL instead of localhost.\n",
|
|
454
|
+
"\n",
|
|
455
|
+
"Authentication is handled using GCP identity tokens instead of local credentials."
|
|
456
|
+
]
|
|
457
|
+
},
|
|
458
|
+
{
|
|
459
|
+
"cell_type": "code",
|
|
460
|
+
"execution_count": null,
|
|
461
|
+
"metadata": {},
|
|
462
|
+
"outputs": [],
|
|
463
|
+
"source": [
|
|
464
|
+
"ID_TOKEN = get_ipython().getoutput(\"gcloud auth print-identity-token -q\")[0]"
|
|
465
|
+
]
|
|
466
|
+
},
|
|
467
|
+
{
|
|
468
|
+
"cell_type": "code",
|
|
469
|
+
"execution_count": null,
|
|
470
|
+
"metadata": {},
|
|
471
|
+
"outputs": [],
|
|
472
|
+
"source": [
|
|
473
|
+
"SERVICE_URL = \"YOUR_SERVICE_URL_HERE\" # Replace with your Cloud Run service URL"
|
|
474
|
+
]
|
|
475
|
+
},
|
|
476
|
+
{
|
|
477
|
+
"cell_type": "markdown",
|
|
478
|
+
"metadata": {},
|
|
479
|
+
"source": [
|
|
480
|
+
"Send a message using A2A protocol"
|
|
481
|
+
]
|
|
482
|
+
},
|
|
483
|
+
{
|
|
484
|
+
"cell_type": "code",
|
|
485
|
+
"execution_count": null,
|
|
486
|
+
"metadata": {},
|
|
487
|
+
"outputs": [],
|
|
488
|
+
"source": [
|
|
489
|
+
"# Create A2A message request\n",
|
|
490
|
+
"message = Message(\n",
|
|
491
|
+
" message_id=f\"msg-user-{uuid.uuid4()}\",\n",
|
|
492
|
+
" role=Role.user,\n",
|
|
493
|
+
" parts=[Part(root=TextPart(text=\"Hello! Weather in New York?\"))],\n",
|
|
494
|
+
")\n",
|
|
495
|
+
"\n",
|
|
496
|
+
"request = SendStreamingMessageRequest(\n",
|
|
497
|
+
" id=f\"req-{uuid.uuid4()}\",\n",
|
|
498
|
+
" params=MessageSendParams(message=message),\n",
|
|
499
|
+
")\n",
|
|
500
|
+
"\n",
|
|
501
|
+
"# Set up headers with authentication\n",
|
|
502
|
+
"headers = {\"Content-Type\": \"application/json\", \"Authorization\": f\"Bearer {ID_TOKEN}\"}\n",
|
|
503
|
+
"\n",
|
|
504
|
+
"# Send the streaming request to the A2A endpoint\n",
|
|
505
|
+
"response = requests.post(\n",
|
|
506
|
+
" f\"{SERVICE_URL}/a2a/app/\",\n",
|
|
507
|
+
" headers=headers,\n",
|
|
508
|
+
" json=request.model_dump(mode=\"json\", exclude_none=True),\n",
|
|
509
|
+
" stream=True,\n",
|
|
510
|
+
" timeout=60,\n",
|
|
511
|
+
")\n",
|
|
512
|
+
"\n",
|
|
513
|
+
"print(f\"Response status code: {response.status_code}\")\n",
|
|
514
|
+
"\n",
|
|
515
|
+
"# Parse streaming A2A responses\n",
|
|
516
|
+
"for line in response.iter_lines():\n",
|
|
517
|
+
" if line:\n",
|
|
518
|
+
" line_str = line.decode(\"utf-8\")\n",
|
|
519
|
+
" if line_str.startswith(\"data: \"):\n",
|
|
520
|
+
" event_json = line_str[6:]\n",
|
|
521
|
+
" event = json.loads(event_json)\n",
|
|
522
|
+
" print(f\"Received event: {event}\")"
|
|
523
|
+
]
|
|
524
|
+
},
|
|
525
|
+
{
|
|
526
|
+
"cell_type": "markdown",
|
|
527
|
+
"metadata": {},
|
|
528
|
+
"source": [
|
|
529
|
+
"### Local Testing\n",
|
|
530
|
+
"\n",
|
|
531
|
+
"> You can run the application locally via the `make local-backend` command.\n",
|
|
532
|
+
"\n",
|
|
533
|
+
"Send a message to the local backend service using the A2A protocol and receive a streaming response."
|
|
534
|
+
]
|
|
535
|
+
},
|
|
536
|
+
{
|
|
537
|
+
"cell_type": "code",
|
|
538
|
+
"execution_count": null,
|
|
539
|
+
"metadata": {},
|
|
540
|
+
"outputs": [],
|
|
541
|
+
"source": [
|
|
542
|
+
"# Create A2A message request\n",
|
|
543
|
+
"message = Message(\n",
|
|
544
|
+
" message_id=f\"msg-user-{uuid.uuid4()}\",\n",
|
|
545
|
+
" role=Role.user,\n",
|
|
546
|
+
" parts=[Part(root=TextPart(text=\"Hello! Weather in New York?\"))],\n",
|
|
547
|
+
")\n",
|
|
548
|
+
"\n",
|
|
549
|
+
"request = SendStreamingMessageRequest(\n",
|
|
550
|
+
" id=f\"req-{uuid.uuid4()}\",\n",
|
|
551
|
+
" params=MessageSendParams(message=message),\n",
|
|
552
|
+
")\n",
|
|
553
|
+
"\n",
|
|
554
|
+
"# Set up headers\n",
|
|
555
|
+
"headers = {\"Content-Type\": \"application/json\"}\n",
|
|
556
|
+
"\n",
|
|
557
|
+
"# Send the streaming request to the local A2A endpoint\n",
|
|
558
|
+
"response = requests.post(\n",
|
|
559
|
+
" \"http://127.0.0.1:8000/a2a/app/\",\n",
|
|
560
|
+
" headers=headers,\n",
|
|
561
|
+
" json=request.model_dump(mode=\"json\", exclude_none=True),\n",
|
|
562
|
+
" stream=True,\n",
|
|
563
|
+
" timeout=60,\n",
|
|
564
|
+
")\n",
|
|
565
|
+
"\n",
|
|
566
|
+
"print(f\"Response status code: {response.status_code}\")\n",
|
|
567
|
+
"\n",
|
|
568
|
+
"# Parse streaming A2A responses\n",
|
|
569
|
+
"for line in response.iter_lines():\n",
|
|
570
|
+
" if line:\n",
|
|
571
|
+
" line_str = line.decode(\"utf-8\")\n",
|
|
572
|
+
" if line_str.startswith(\"data: \"):\n",
|
|
573
|
+
" event_json = line_str[6:]\n",
|
|
574
|
+
" event = json.loads(event_json)\n",
|
|
575
|
+
" print(f\"Received event: {event}\")"
|
|
576
|
+
]
|
|
577
|
+
}
|
|
578
|
+
],
|
|
579
|
+
"metadata": {
|
|
580
|
+
"kernelspec": {
|
|
581
|
+
"display_name": "myagent-1761660603",
|
|
582
|
+
"language": "python",
|
|
583
|
+
"name": "python3"
|
|
584
|
+
},
|
|
585
|
+
"language_info": {
|
|
586
|
+
"codemirror_mode": {
|
|
587
|
+
"name": "ipython",
|
|
588
|
+
"version": 3
|
|
589
|
+
},
|
|
590
|
+
"file_extension": ".py",
|
|
591
|
+
"mimetype": "text/x-python",
|
|
592
|
+
"name": "python",
|
|
593
|
+
"nbconvert_exporter": "python",
|
|
594
|
+
"pygments_lexer": "ipython3",
|
|
595
|
+
"version": "3.12.9"
|
|
596
|
+
}
|
|
597
|
+
},
|
|
598
|
+
"nbformat": 4,
|
|
599
|
+
"nbformat_minor": 2
|
|
600
|
+
}
|