Skip to content

Commit b5ecda8

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: GenAI SDK client(memory): Add Memory Topic labels to Memory
PiperOrigin-RevId: 821859582
1 parent f84739c commit b5ecda8

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

tests/unit/vertexai/genai/replays/test_create_agent_engine.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818

1919
from tests.unit.vertexai.genai.replays import pytest_helper
20+
from vertexai._genai import types
2021

2122

2223
def test_create_config_lightweight(client):
@@ -54,6 +55,29 @@ def test_create_with_context_spec(client):
5455
parent = f"projects/{project}/locations/{location}"
5556
generation_model = f"{parent}/publishers/google/models/gemini-2.0-flash-001"
5657
embedding_model = f"{parent}/publishers/google/models/text-embedding-005"
58+
customization_config = {
59+
"memory_topics": [
60+
{"managed_memory_topic": {"managed_topic_enum": "USER_PREFERENCES"}}
61+
],
62+
"generate_memories_examples": [
63+
{
64+
"conversation_source": {
65+
"events": [
66+
{"content": {"role": "user", "parts": [{"text": "Hello"}]}}
67+
]
68+
},
69+
"generatedMemories": [
70+
{
71+
"fact": "I like to say hello.",
72+
"topics": [{"managed_memory_topic": "USER_PREFERENCES"}],
73+
}
74+
],
75+
}
76+
],
77+
}
78+
memory_bank_customization_config = types.MemoryBankCustomizationConfig(
79+
**customization_config
80+
)
5781

5882
agent_engine = client.agent_engines.create(
5983
config={
@@ -64,6 +88,7 @@ def test_create_with_context_spec(client):
6488
"embedding_model": embedding_model,
6589
},
6690
"ttl_config": {"default_ttl": "120s"},
91+
"customization_configs": [memory_bank_customization_config],
6792
},
6893
},
6994
"http_options": {"api_version": "v1beta1"},
@@ -76,6 +101,9 @@ def test_create_with_context_spec(client):
76101
memory_bank_config.similarity_search_config.embedding_model == embedding_model
77102
)
78103
assert memory_bank_config.ttl_config.default_ttl == "120s"
104+
assert memory_bank_config.customization_configs == [
105+
memory_bank_customization_config
106+
]
79107
# Clean up resources.
80108
client.agent_engines.delete(name=agent_engine.api_resource.name, force=True)
81109

vertexai/_genai/memories.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ def _AgentEngineMemoryConfig_to_vertex(
7272
getv(from_object, ["disable_memory_revisions"]),
7373
)
7474

75+
if getv(from_object, ["topics"]) is not None:
76+
setv(
77+
parent_object, ["topics"], [item for item in getv(from_object, ["topics"])]
78+
)
79+
7580
return to_object
7681

7782

@@ -365,6 +370,11 @@ def _UpdateAgentEngineMemoryConfig_to_vertex(
365370
getv(from_object, ["disable_memory_revisions"]),
366371
)
367372

373+
if getv(from_object, ["topics"]) is not None:
374+
setv(
375+
parent_object, ["topics"], [item for item in getv(from_object, ["topics"])]
376+
)
377+
368378
if getv(from_object, ["update_mask"]) is not None:
369379
setv(
370380
parent_object, ["_query", "updateMask"], getv(from_object, ["update_mask"])

vertexai/_genai/types.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5330,6 +5330,30 @@ class MemoryBankCustomizationConfigGenerateMemoriesExampleConversationSourceDict
53305330
]
53315331

53325332

5333+
class MemoryTopicId(_common.BaseModel):
5334+
"""The topic ID for a memory."""
5335+
5336+
custom_memory_topic_label: Optional[str] = Field(
5337+
default=None, description="""Optional. The custom memory topic label."""
5338+
)
5339+
managed_memory_topic: Optional[ManagedTopicEnum] = Field(
5340+
default=None, description="""Optional. The managed memory topic."""
5341+
)
5342+
5343+
5344+
class MemoryTopicIdDict(TypedDict, total=False):
5345+
"""The topic ID for a memory."""
5346+
5347+
custom_memory_topic_label: Optional[str]
5348+
"""Optional. The custom memory topic label."""
5349+
5350+
managed_memory_topic: Optional[ManagedTopicEnum]
5351+
"""Optional. The managed memory topic."""
5352+
5353+
5354+
MemoryTopicIdOrDict = Union[MemoryTopicId, MemoryTopicIdDict]
5355+
5356+
53335357
class MemoryBankCustomizationConfigGenerateMemoriesExampleGeneratedMemory(
53345358
_common.BaseModel
53355359
):
@@ -5338,6 +5362,10 @@ class MemoryBankCustomizationConfigGenerateMemoriesExampleGeneratedMemory(
53385362
fact: Optional[str] = Field(
53395363
default=None, description="""Required. The fact to generate a memory from."""
53405364
)
5365+
topics: Optional[list[MemoryTopicId]] = Field(
5366+
default=None,
5367+
description="""Optional. The list of topics that the memory should be associated with. For example, use `custom_memory_topic_label = "jargon"` if the extracted memory is an example of memory extraction for the custom topic `jargon`.""",
5368+
)
53415369

53425370

53435371
class MemoryBankCustomizationConfigGenerateMemoriesExampleGeneratedMemoryDict(
@@ -5348,6 +5376,9 @@ class MemoryBankCustomizationConfigGenerateMemoriesExampleGeneratedMemoryDict(
53485376
fact: Optional[str]
53495377
"""Required. The fact to generate a memory from."""
53505378

5379+
topics: Optional[list[MemoryTopicIdDict]]
5380+
"""Optional. The list of topics that the memory should be associated with. For example, use `custom_memory_topic_label = "jargon"` if the extracted memory is an example of memory extraction for the custom topic `jargon`."""
5381+
53515382

53525383
MemoryBankCustomizationConfigGenerateMemoriesExampleGeneratedMemoryOrDict = Union[
53535384
MemoryBankCustomizationConfigGenerateMemoriesExampleGeneratedMemory,
@@ -6432,6 +6463,9 @@ class AgentEngineMemoryConfig(_common.BaseModel):
64326463
default=None,
64336464
description="""Optional. Input only. If true, no revision will be created for this request.""",
64346465
)
6466+
topics: Optional[list[MemoryTopicId]] = Field(
6467+
default=None, description="""Optional. The topics of the memory."""
6468+
)
64356469

64366470

64376471
class AgentEngineMemoryConfigDict(TypedDict, total=False):
@@ -6466,6 +6500,9 @@ class AgentEngineMemoryConfigDict(TypedDict, total=False):
64666500
disable_memory_revisions: Optional[bool]
64676501
"""Optional. Input only. If true, no revision will be created for this request."""
64686502

6503+
topics: Optional[list[MemoryTopicIdDict]]
6504+
"""Optional. The topics of the memory."""
6505+
64696506

64706507
AgentEngineMemoryConfigOrDict = Union[
64716508
AgentEngineMemoryConfig, AgentEngineMemoryConfigDict
@@ -6573,6 +6610,9 @@ class Memory(_common.BaseModel):
65736610
default=None,
65746611
description="""Output only. Timestamp when this Memory was most recently updated.""",
65756612
)
6613+
topics: Optional[list[MemoryTopicId]] = Field(
6614+
default=None, description="""Optional. The Topics of the Memory."""
6615+
)
65766616

65776617

65786618
class MemoryDict(TypedDict, total=False):
@@ -6614,6 +6654,9 @@ class MemoryDict(TypedDict, total=False):
66146654
update_time: Optional[datetime.datetime]
66156655
"""Output only. Timestamp when this Memory was most recently updated."""
66166656

6657+
topics: Optional[list[MemoryTopicIdDict]]
6658+
"""Optional. The Topics of the Memory."""
6659+
66176660

66186661
MemoryOrDict = Union[Memory, MemoryDict]
66196662

@@ -6840,6 +6883,10 @@ class GenerateMemoriesRequestDirectMemoriesSourceDirectMemory(_common.BaseModel)
68406883
default=None,
68416884
description="""Required. The fact to consolidate with existing memories.""",
68426885
)
6886+
topics: Optional[list[MemoryTopicId]] = Field(
6887+
default=None,
6888+
description="""Optional. The topics that the consolidated memories should be associated with.""",
6889+
)
68436890

68446891

68456892
class GenerateMemoriesRequestDirectMemoriesSourceDirectMemoryDict(
@@ -6850,6 +6897,9 @@ class GenerateMemoriesRequestDirectMemoriesSourceDirectMemoryDict(
68506897
fact: Optional[str]
68516898
"""Required. The fact to consolidate with existing memories."""
68526899

6900+
topics: Optional[list[MemoryTopicIdDict]]
6901+
"""Optional. The topics that the consolidated memories should be associated with."""
6902+
68536903

68546904
GenerateMemoriesRequestDirectMemoriesSourceDirectMemoryOrDict = Union[
68556905
GenerateMemoriesRequestDirectMemoriesSourceDirectMemory,
@@ -7683,6 +7733,9 @@ class UpdateAgentEngineMemoryConfig(_common.BaseModel):
76837733
default=None,
76847734
description="""Optional. Input only. If true, no revision will be created for this request.""",
76857735
)
7736+
topics: Optional[list[MemoryTopicId]] = Field(
7737+
default=None, description="""Optional. The topics of the memory."""
7738+
)
76867739
update_mask: Optional[str] = Field(
76877740
default=None,
76887741
description="""The update mask to apply. For the `FieldMask` definition, see
@@ -7722,6 +7775,9 @@ class UpdateAgentEngineMemoryConfigDict(TypedDict, total=False):
77227775
disable_memory_revisions: Optional[bool]
77237776
"""Optional. Input only. If true, no revision will be created for this request."""
77247777

7778+
topics: Optional[list[MemoryTopicIdDict]]
7779+
"""Optional. The topics of the memory."""
7780+
77257781
update_mask: Optional[str]
77267782
"""The update mask to apply. For the `FieldMask` definition, see
77277783
https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask."""

0 commit comments

Comments
 (0)