-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Feat agent engine samples #13738
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
base: main
Are you sure you want to change the base?
Feat agent engine samples #13738
Changes from all commits
0276892
b4833fd
ac539fd
6d8098b
969cc4a
3af9cc5
b1209f1
137aea6
784aad6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # 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. | ||
|
|
||
| # [START generativeaionvertexai_create_agent_engine] | ||
| import vertexai | ||
|
|
||
|
|
||
| def create_agent_engine_with_memorybank_config( | ||
| project_id: str, | ||
| location: str, | ||
| model: str = "gemini-3-flash", | ||
| ) -> object: | ||
| """Creates an Agent Engine instance with Memory Bank configuration.""" | ||
| vertexai.init(project=project_id, location=location) | ||
| client = vertexai.Client() | ||
|
|
||
| agent_engine = client.agent_engines.create( | ||
| config={ | ||
| "context_spec": { | ||
| "memory_bank_config": { | ||
| "generation_config": { | ||
| "model": f"projects/{project_id}/locations/{location}/publishers/google/models/{model}" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| print(f"Created Agent Engine: {agent_engine.api_resource.name}") | ||
| return agent_engine | ||
| # [END generativeaionvertexai_create_agent_engine] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # 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. | ||
|
|
||
| # [START generativeaionvertexai_delete_agent_engine] | ||
| import vertexai | ||
|
|
||
|
|
||
| def delete_agent_engine(project_id: str, location: str, agent_engine_id: str) -> None: | ||
| """Deletes an Agent Engine instance.""" | ||
| vertexai.init(project=project_id, location=location) | ||
| client = vertexai.Client() | ||
|
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you update these two lines to |
||
|
|
||
| client.agent_engines.delete(name=agent_engine_id) | ||
| print(f"Deleted Agent Engine: {agent_engine_id}") | ||
| # [END generativeaionvertexai_delete_agent_engine] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # 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. | ||
|
|
||
| # [START generativeaionvertexai_generate_memories] | ||
| import vertexai | ||
|
|
||
|
|
||
| def generate_memories(project_id: str, location: str, agent_engine_name: str) -> object: | ||
| """Generates memories for a specific Agent Engine instance.""" | ||
| vertexai.init(project=project_id, location=location) | ||
| client = vertexai.Client() | ||
|
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you update these two lines to |
||
|
|
||
| # agent_engine_name format: 'projects/{project}/locations/{location}/reasoningEngines/{id}' | ||
| response = client.agent_engines.memories.generate( | ||
| name=agent_engine_name | ||
| ) | ||
| print("Memories generated successfully.") | ||
| return response | ||
| # [END generativeaionvertexai_generate_memories] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # 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. | ||
|
|
||
| import os | ||
|
|
||
| from typing import Generator | ||
|
|
||
| import pytest | ||
|
|
||
| import create_agent_engine | ||
| import delete_agent_engine | ||
| import generate_memories | ||
|
|
||
| PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
| LOCATION = "us-central1" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def agent_engine_id() -> Generator[str, None, None]: | ||
| """Creates a test Agent Engine and yields its ID, ensuring cleanup.""" | ||
| if not PROJECT_ID: | ||
| pytest.skip("GOOGLE_CLOUD_PROJECT not set") | ||
|
|
||
| print("Creating Agent Engine...") | ||
| engine_name = None | ||
| try: | ||
| engine = create_agent_engine.create_agent_engine_with_memorybank_config(PROJECT_ID, LOCATION) | ||
| engine_name = engine.api_resource.name | ||
| yield engine_name | ||
| except Exception as e: | ||
| pytest.skip(f"Failed to create agent engine: {e}") | ||
| finally: | ||
| # This 'finally' block ensures cleanup even if tests fail | ||
| if engine_name: | ||
| print(f"Cleaning up: {engine_name}") | ||
| try: | ||
| delete_agent_engine.delete_agent_engine(PROJECT_ID, LOCATION, engine_name) | ||
| except Exception: | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not PROJECT_ID, reason="GOOGLE_CLOUD_PROJECT not set") | ||
| def test_create_agent_engine(agent_engine_id: str) -> None: | ||
| assert agent_engine_id | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not PROJECT_ID, reason="GOOGLE_CLOUD_PROJECT not set") | ||
| def test_generate_memories(agent_engine_id: str) -> None: | ||
| if not agent_engine_id: | ||
| pytest.skip("Agent Engine not created") | ||
| response = generate_memories.generate_memories(PROJECT_ID, LOCATION, agent_engine_id) | ||
| assert response | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not PROJECT_ID, reason="GOOGLE_CLOUD_PROJECT not set") | ||
| def test_delete_agent_engine() -> None: | ||
| """Tests that an agent engine can be deleted.""" | ||
| # Create a fresh one just to test the delete function | ||
| engine = create_agent_engine.create_agent_engine_with_memorybank_config(PROJECT_ID, LOCATION) | ||
| assert engine, "Failed to create engine for deletion test" | ||
|
|
||
| # Call your delete function and ensure it doesn't crash | ||
| delete_agent_engine.delete_agent_engine( | ||
| PROJECT_ID, LOCATION, engine.api_resource.name | ||
| ) | ||
|
|
||
|
|
||
| # Simplified test that just checks imports and structural correctness without calling API | ||
| def test_imports() -> None: | ||
| assert create_agent_engine.create_agent_engine_with_memorybank_config | ||
| assert generate_memories.generate_memories | ||
| assert delete_agent_engine.delete_agent_engine |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you update these two lines to:
?