Skip to content

Commit 629717b

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: Add metadata and metadata filtering to memories
PiperOrigin-RevId: 854287666
1 parent b814aab commit 629717b

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

vertexai/_genai/memories.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def _AgentEngineMemoryConfig_to_vertex(
7777
parent_object, ["topics"], [item for item in getv(from_object, ["topics"])]
7878
)
7979

80+
if getv(from_object, ["metadata"]) is not None:
81+
setv(parent_object, ["metadata"], getv(from_object, ["metadata"]))
82+
8083
return to_object
8184

8285

@@ -153,6 +156,16 @@ def _GenerateAgentEngineMemoriesConfig_to_vertex(
153156
getv(from_object, ["disable_memory_revisions"]),
154157
)
155158

159+
if getv(from_object, ["metadata"]) is not None:
160+
setv(parent_object, ["metadata"], getv(from_object, ["metadata"]))
161+
162+
if getv(from_object, ["metadata_merge_strategy"]) is not None:
163+
setv(
164+
parent_object,
165+
["metadataMergeStrategy"],
166+
getv(from_object, ["metadata_merge_strategy"]),
167+
)
168+
156169
return to_object
157170

158171

@@ -316,6 +329,13 @@ def _RetrieveAgentEngineMemoriesConfig_to_vertex(
316329
if getv(from_object, ["filter"]) is not None:
317330
setv(parent_object, ["filter"], getv(from_object, ["filter"]))
318331

332+
if getv(from_object, ["filter_groups"]) is not None:
333+
setv(
334+
parent_object,
335+
["filterGroups"],
336+
[item for item in getv(from_object, ["filter_groups"])],
337+
)
338+
319339
return to_object
320340

321341

@@ -413,6 +433,9 @@ def _UpdateAgentEngineMemoryConfig_to_vertex(
413433
parent_object, ["topics"], [item for item in getv(from_object, ["topics"])]
414434
)
415435

436+
if getv(from_object, ["metadata"]) is not None:
437+
setv(parent_object, ["metadata"], getv(from_object, ["metadata"]))
438+
416439
if getv(from_object, ["update_mask"]) is not None:
417440
setv(
418441
parent_object, ["_query", "updateMask"], getv(from_object, ["update_mask"])

vertexai/_genai/types/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,17 @@
578578
from .common import MemoryBankCustomizationConfigMemoryTopicManagedMemoryTopicOrDict
579579
from .common import MemoryBankCustomizationConfigMemoryTopicOrDict
580580
from .common import MemoryBankCustomizationConfigOrDict
581+
from .common import MemoryConjunctionFilter
582+
from .common import MemoryConjunctionFilterDict
583+
from .common import MemoryConjunctionFilterOrDict
581584
from .common import MemoryDict
585+
from .common import MemoryFilter
586+
from .common import MemoryFilterDict
587+
from .common import MemoryFilterOrDict
588+
from .common import MemoryMetadataMergeStrategy
589+
from .common import MemoryMetadataValue
590+
from .common import MemoryMetadataValueDict
591+
from .common import MemoryMetadataValueOrDict
582592
from .common import MemoryOrDict
583593
from .common import MemoryRevision
584594
from .common import MemoryRevisionDict
@@ -613,6 +623,7 @@
613623
from .common import ObservabilityEvalCase
614624
from .common import ObservabilityEvalCaseDict
615625
from .common import ObservabilityEvalCaseOrDict
626+
from .common import Operator
616627
from .common import OptimizeConfig
617628
from .common import OptimizeConfigDict
618629
from .common import OptimizeConfigOrDict
@@ -1523,6 +1534,15 @@
15231534
"RetrieveMemoriesRequestSimpleRetrievalParams",
15241535
"RetrieveMemoriesRequestSimpleRetrievalParamsDict",
15251536
"RetrieveMemoriesRequestSimpleRetrievalParamsOrDict",
1537+
"MemoryMetadataValue",
1538+
"MemoryMetadataValueDict",
1539+
"MemoryMetadataValueOrDict",
1540+
"MemoryFilter",
1541+
"MemoryFilterDict",
1542+
"MemoryFilterOrDict",
1543+
"MemoryConjunctionFilter",
1544+
"MemoryConjunctionFilterDict",
1545+
"MemoryConjunctionFilterOrDict",
15261546
"RetrieveAgentEngineMemoriesConfig",
15271547
"RetrieveAgentEngineMemoriesConfigDict",
15281548
"RetrieveAgentEngineMemoriesConfigOrDict",
@@ -1909,6 +1929,7 @@
19091929
"IdentityType",
19101930
"AgentServerMode",
19111931
"ManagedTopicEnum",
1932+
"Operator",
19121933
"Language",
19131934
"MachineConfig",
19141935
"State",
@@ -1917,6 +1938,7 @@
19171938
"RubricContentType",
19181939
"EvaluationRunState",
19191940
"OptimizeTarget",
1941+
"MemoryMetadataMergeStrategy",
19201942
"GenerateMemoriesResponseGeneratedMemoryAction",
19211943
"PromptOptimizerMethod",
19221944
"PromptData",

vertexai/_genai/types/common.py

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,19 @@ class ManagedTopicEnum(_common.CaseInSensitiveEnum):
243243
"""Information that the user explicitly requested to remember or forget."""
244244

245245

246+
class Operator(_common.CaseInSensitiveEnum):
247+
"""Operator to apply to the filter. If not set, then EQUAL will be used."""
248+
249+
OPERATOR_UNSPECIFIED = "OPERATOR_UNSPECIFIED"
250+
"""Unspecified operator. Defaults to EQUAL."""
251+
EQUAL = "EQUAL"
252+
"""Equal to."""
253+
GREATER_THAN = "GREATER_THAN"
254+
"""Greater than."""
255+
LESS_THAN = "LESS_THAN"
256+
"""Less than."""
257+
258+
246259
class Language(_common.CaseInSensitiveEnum):
247260
"""The coding language supported in this environment."""
248261

@@ -345,6 +358,19 @@ class OptimizeTarget(_common.CaseInSensitiveEnum):
345358
"""The prompt optimizer based on user provided examples with target responses."""
346359

347360

361+
class MemoryMetadataMergeStrategy(_common.CaseInSensitiveEnum):
362+
"""The strategy to use when applying metadata to existing memories during consolidation."""
363+
364+
METADATA_MERGE_STRATEGY_UNSPECIFIED = "METADATA_MERGE_STRATEGY_UNSPECIFIED"
365+
"""The metadata merge strategy is unspecified."""
366+
OVERWRITE = "OVERWRITE"
367+
"""Replace the metadata of the updated memories with the new metadata."""
368+
MERGE = "MERGE"
369+
"""Append new metadata to the existing metadata. If there are duplicate keys, the existing values will be overwritten."""
370+
REQUIRE_EXACT_MATCH = "REQUIRE_EXACT_MATCH"
371+
"""Restrict consolidation to memories that have exactly the same metadata as the request. If a memory doesn't have the same metadata, it is not eligible for consolidation."""
372+
373+
348374
class GenerateMemoriesResponseGeneratedMemoryAction(_common.CaseInSensitiveEnum):
349375
"""The action to take."""
350376

@@ -6622,6 +6648,10 @@ class AgentEngineMemoryConfig(_common.BaseModel):
66226648
topics: Optional[list[MemoryTopicId]] = Field(
66236649
default=None, description="""Optional. The topics of the memory."""
66246650
)
6651+
metadata: Optional[dict[str, "MemoryMetadataValue"]] = Field(
6652+
default=None,
6653+
description="""Optional. User-provided metadata for the Memory. This information was provided when creating, updating, or generating the Memory. It was not generated by Memory Bank.""",
6654+
)
66256655

66266656

66276657
class AgentEngineMemoryConfigDict(TypedDict, total=False):
@@ -6659,6 +6689,9 @@ class AgentEngineMemoryConfigDict(TypedDict, total=False):
66596689
topics: Optional[list[MemoryTopicIdDict]]
66606690
"""Optional. The topics of the memory."""
66616691

6692+
metadata: Optional[dict[str, "MemoryMetadataValueDict"]]
6693+
"""Optional. User-provided metadata for the Memory. This information was provided when creating, updating, or generating the Memory. It was not generated by Memory Bank."""
6694+
66626695

66636696
AgentEngineMemoryConfigOrDict = Union[
66646697
AgentEngineMemoryConfig, AgentEngineMemoryConfigDict
@@ -6769,6 +6802,10 @@ class Memory(_common.BaseModel):
67696802
topics: Optional[list[MemoryTopicId]] = Field(
67706803
default=None, description="""Optional. The Topics of the Memory."""
67716804
)
6805+
metadata: Optional[dict[str, "MemoryMetadataValue"]] = Field(
6806+
default=None,
6807+
description="""Optional. User-provided metadata for the Memory. This information was provided when creating, updating, or generating the Memory. It was not generated by Memory Bank.""",
6808+
)
67726809

67736810

67746811
class MemoryDict(TypedDict, total=False):
@@ -6813,6 +6850,9 @@ class MemoryDict(TypedDict, total=False):
68136850
topics: Optional[list[MemoryTopicIdDict]]
68146851
"""Optional. The Topics of the Memory."""
68156852

6853+
metadata: Optional[dict[str, "MemoryMetadataValueDict"]]
6854+
"""Optional. User-provided metadata for the Memory. This information was provided when creating, updating, or generating the Memory. It was not generated by Memory Bank."""
6855+
68166856

68176857
MemoryOrDict = Union[Memory, MemoryDict]
68186858

@@ -7124,6 +7164,14 @@ class GenerateAgentEngineMemoriesConfig(_common.BaseModel):
71247164
default=None,
71257165
description="""Optional. Input only. If true, no revisions will be created for this request.""",
71267166
)
7167+
metadata: Optional[dict[str, "MemoryMetadataValue"]] = Field(
7168+
default=None,
7169+
description="""Optional. User-provided metadata for the generated memories. This is not generated by Memory Bank.""",
7170+
)
7171+
metadata_merge_strategy: Optional[MemoryMetadataMergeStrategy] = Field(
7172+
default=None,
7173+
description="""Optional. The strategy to use when applying metadata to existing memories.""",
7174+
)
71277175

71287176

71297177
class GenerateAgentEngineMemoriesConfigDict(TypedDict, total=False):
@@ -7155,6 +7203,12 @@ class GenerateAgentEngineMemoriesConfigDict(TypedDict, total=False):
71557203
disable_memory_revisions: Optional[bool]
71567204
"""Optional. Input only. If true, no revisions will be created for this request."""
71577205

7206+
metadata: Optional[dict[str, "MemoryMetadataValueDict"]]
7207+
"""Optional. User-provided metadata for the generated memories. This is not generated by Memory Bank."""
7208+
7209+
metadata_merge_strategy: Optional[MemoryMetadataMergeStrategy]
7210+
"""Optional. The strategy to use when applying metadata to existing memories."""
7211+
71587212

71597213
GenerateAgentEngineMemoriesConfigOrDict = Union[
71607214
GenerateAgentEngineMemoriesConfig, GenerateAgentEngineMemoriesConfigDict
@@ -7612,6 +7666,95 @@ class RetrieveMemoriesRequestSimpleRetrievalParamsDict(TypedDict, total=False):
76127666
]
76137667

76147668

7669+
class MemoryMetadataValue(_common.BaseModel):
7670+
"""Memory metadata."""
7671+
7672+
timestamp_value: Optional[datetime.datetime] = Field(
7673+
default=None,
7674+
description="""Timestamp value. When filtering on timestamp values, only the seconds field will be compared.""",
7675+
)
7676+
double_value: Optional[float] = Field(default=None, description="""Double value.""")
7677+
bool_value: Optional[bool] = Field(default=None, description="""Boolean value.""")
7678+
string_value: Optional[str] = Field(default=None, description="""String value.""")
7679+
7680+
7681+
class MemoryMetadataValueDict(TypedDict, total=False):
7682+
"""Memory metadata."""
7683+
7684+
timestamp_value: Optional[datetime.datetime]
7685+
"""Timestamp value. When filtering on timestamp values, only the seconds field will be compared."""
7686+
7687+
double_value: Optional[float]
7688+
"""Double value."""
7689+
7690+
bool_value: Optional[bool]
7691+
"""Boolean value."""
7692+
7693+
string_value: Optional[str]
7694+
"""String value."""
7695+
7696+
7697+
MemoryMetadataValueOrDict = Union[MemoryMetadataValue, MemoryMetadataValueDict]
7698+
7699+
7700+
class MemoryFilter(_common.BaseModel):
7701+
"""Filter to apply when retrieving memories."""
7702+
7703+
op: Optional[Operator] = Field(
7704+
default=None,
7705+
description="""Operator to apply to the filter. If not set, then EQUAL will be used.""",
7706+
)
7707+
negate: Optional[bool] = Field(
7708+
default=None, description="""If true, the filter will be negated."""
7709+
)
7710+
key: Optional[str] = Field(
7711+
default=None,
7712+
description="""Key of the filter. For example, "author" would apply to `metadata` entries with the key "author".""",
7713+
)
7714+
value: Optional[MemoryMetadataValue] = Field(
7715+
default=None, description="""Value to compare to."""
7716+
)
7717+
7718+
7719+
class MemoryFilterDict(TypedDict, total=False):
7720+
"""Filter to apply when retrieving memories."""
7721+
7722+
op: Optional[Operator]
7723+
"""Operator to apply to the filter. If not set, then EQUAL will be used."""
7724+
7725+
negate: Optional[bool]
7726+
"""If true, the filter will be negated."""
7727+
7728+
key: Optional[str]
7729+
"""Key of the filter. For example, "author" would apply to `metadata` entries with the key "author"."""
7730+
7731+
value: Optional[MemoryMetadataValueDict]
7732+
"""Value to compare to."""
7733+
7734+
7735+
MemoryFilterOrDict = Union[MemoryFilter, MemoryFilterDict]
7736+
7737+
7738+
class MemoryConjunctionFilter(_common.BaseModel):
7739+
"""The conjunction filter for memories."""
7740+
7741+
filters: Optional[list[MemoryFilter]] = Field(
7742+
default=None, description="""Filters that will combined using AND logic."""
7743+
)
7744+
7745+
7746+
class MemoryConjunctionFilterDict(TypedDict, total=False):
7747+
"""The conjunction filter for memories."""
7748+
7749+
filters: Optional[list[MemoryFilterDict]]
7750+
"""Filters that will combined using AND logic."""
7751+
7752+
7753+
MemoryConjunctionFilterOrDict = Union[
7754+
MemoryConjunctionFilter, MemoryConjunctionFilterDict
7755+
]
7756+
7757+
76157758
class RetrieveAgentEngineMemoriesConfig(_common.BaseModel):
76167759
"""Config for retrieving memories."""
76177760

@@ -7629,6 +7772,23 @@ class RetrieveAgentEngineMemoriesConfig(_common.BaseModel):
76297772
* `update_time`
76307773
""",
76317774
)
7775+
filter_groups: Optional[list[MemoryConjunctionFilter]] = Field(
7776+
default=None,
7777+
description="""Metadata filters that will be applied to the retrieved memories'
7778+
`metadata` using OR logic. Filters are defined using disjunctive normal
7779+
form (OR of ANDs).
7780+
7781+
For example:
7782+
`filter_groups: [{filters: [{key: "author", value: {string_value: "agent
7783+
`123"}, op: EQUAL}]}, {filters: [{key: "label", value: {string_value:
7784+
"travel"}, op: EQUAL}, {key: "author", value: {string_value: "agent 321"},
7785+
op: EQUAL}]}]`
7786+
7787+
would be equivalent to the logical expression:
7788+
`(metadata.author = "agent 123" OR (metadata.label = "travel" AND
7789+
metadata.author = "agent 321"))`.
7790+
""",
7791+
)
76327792

76337793

76347794
class RetrieveAgentEngineMemoriesConfigDict(TypedDict, total=False):
@@ -7647,6 +7807,22 @@ class RetrieveAgentEngineMemoriesConfigDict(TypedDict, total=False):
76477807
* `update_time`
76487808
"""
76497809

7810+
filter_groups: Optional[list[MemoryConjunctionFilterDict]]
7811+
"""Metadata filters that will be applied to the retrieved memories'
7812+
`metadata` using OR logic. Filters are defined using disjunctive normal
7813+
form (OR of ANDs).
7814+
7815+
For example:
7816+
`filter_groups: [{filters: [{key: "author", value: {string_value: "agent
7817+
`123"}, op: EQUAL}]}, {filters: [{key: "label", value: {string_value:
7818+
"travel"}, op: EQUAL}, {key: "author", value: {string_value: "agent 321"},
7819+
op: EQUAL}]}]`
7820+
7821+
would be equivalent to the logical expression:
7822+
`(metadata.author = "agent 123" OR (metadata.label = "travel" AND
7823+
metadata.author = "agent 321"))`.
7824+
"""
7825+
76507826

76517827
RetrieveAgentEngineMemoriesConfigOrDict = Union[
76527828
RetrieveAgentEngineMemoriesConfig, RetrieveAgentEngineMemoriesConfigDict
@@ -7913,6 +8089,10 @@ class UpdateAgentEngineMemoryConfig(_common.BaseModel):
79138089
topics: Optional[list[MemoryTopicId]] = Field(
79148090
default=None, description="""Optional. The topics of the memory."""
79158091
)
8092+
metadata: Optional[dict[str, MemoryMetadataValue]] = Field(
8093+
default=None,
8094+
description="""Optional. User-provided metadata for the Memory. This information was provided when creating, updating, or generating the Memory. It was not generated by Memory Bank.""",
8095+
)
79168096
update_mask: Optional[str] = Field(
79178097
default=None,
79188098
description="""The update mask to apply. For the `FieldMask` definition, see
@@ -7955,6 +8135,9 @@ class UpdateAgentEngineMemoryConfigDict(TypedDict, total=False):
79558135
topics: Optional[list[MemoryTopicIdDict]]
79568136
"""Optional. The topics of the memory."""
79578137

8138+
metadata: Optional[dict[str, MemoryMetadataValueDict]]
8139+
"""Optional. User-provided metadata for the Memory. This information was provided when creating, updating, or generating the Memory. It was not generated by Memory Bank."""
8140+
79588141
update_mask: Optional[str]
79598142
"""The update mask to apply. For the `FieldMask` definition, see
79608143
https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask."""

0 commit comments

Comments
 (0)