|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# pylint: disable=protected-access,bad-continuation,missing-function-docstring |
| 16 | + |
| 17 | +from tests.unit.vertexai.genai.replays import pytest_helper |
| 18 | +from vertexai._genai import types |
| 19 | + |
| 20 | + |
| 21 | +def test_generate_and_retrieve_profile(client): |
| 22 | + # TODO: Use prod once available. |
| 23 | + client._api_client._http_options.base_url = ( |
| 24 | + "https://us-central1-autopush-aiplatform.sandbox.googleapis.com" |
| 25 | + ) |
| 26 | + customization_config = {"disable_natural_language_memories": True} |
| 27 | + memory_bank_customization_config = types.MemoryBankCustomizationConfig( |
| 28 | + **customization_config |
| 29 | + ) |
| 30 | + structured_memory_config = { |
| 31 | + "scope_keys": ["user_id"], |
| 32 | + "schema_configs": [ |
| 33 | + { |
| 34 | + "id": "user-profile", |
| 35 | + "memory_schema": { |
| 36 | + "properties": { |
| 37 | + "name": {"description": "User's name", "type": "string"} |
| 38 | + }, |
| 39 | + "type": "object", |
| 40 | + }, |
| 41 | + } |
| 42 | + ], |
| 43 | + } |
| 44 | + structured_memory_config_obj = types.StructuredMemoryConfig( |
| 45 | + **structured_memory_config |
| 46 | + ) |
| 47 | + agent_engine = client.agent_engines.create( |
| 48 | + config={ |
| 49 | + "context_spec": { |
| 50 | + "memory_bank_config": { |
| 51 | + "customization_configs": [memory_bank_customization_config], |
| 52 | + "structured_memory_configs": [structured_memory_config_obj], |
| 53 | + }, |
| 54 | + }, |
| 55 | + "http_options": {"api_version": "v1beta1"}, |
| 56 | + }, |
| 57 | + ) |
| 58 | + try: |
| 59 | + agent_engine = client.agent_engines.get(name=agent_engine.api_resource.name) |
| 60 | + memory_bank_config = agent_engine.api_resource.context_spec.memory_bank_config |
| 61 | + assert memory_bank_config.customization_configs == [ |
| 62 | + memory_bank_customization_config |
| 63 | + ] |
| 64 | + assert memory_bank_config.structured_memory_configs == [ |
| 65 | + structured_memory_config_obj |
| 66 | + ] |
| 67 | + |
| 68 | + scope = {"user_id": "123"} |
| 69 | + client.agent_engines.memories.generate( |
| 70 | + name=agent_engine.api_resource.name, |
| 71 | + scope=scope, |
| 72 | + direct_contents_source={ |
| 73 | + "events": [{"content": {"parts": [{"text": "My name is Kim."}]}}] |
| 74 | + }, |
| 75 | + ) |
| 76 | + memories = list( |
| 77 | + client.agent_engines.memories.retrieve( |
| 78 | + name=agent_engine.api_resource.name, |
| 79 | + scope=scope, |
| 80 | + config={"memory_types": ["STRUCTURED_PROFILE"]}, |
| 81 | + ) |
| 82 | + ) |
| 83 | + assert len(memories) >= 1 |
| 84 | + assert memories[0].memory.structured_content is not None |
| 85 | + |
| 86 | + response = client.agent_engines.memories.retrieve_profiles( |
| 87 | + name=agent_engine.api_resource.name, scope=scope |
| 88 | + ) |
| 89 | + assert len(response.profiles) == 1 |
| 90 | + |
| 91 | + finally: |
| 92 | + # Clean up resources. |
| 93 | + client.agent_engines.delete(name=agent_engine.api_resource.name, force=True) |
| 94 | + |
| 95 | + |
| 96 | +pytestmark = pytest_helper.setup( |
| 97 | + file=__file__, |
| 98 | + globals_for_file=globals(), |
| 99 | + test_method="agent_engines.retrieve_profiles", |
| 100 | +) |
0 commit comments