GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: Add memory related methods to AdkApp · googleapis/python-aiplatform@e92a4ef

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • tests/unit/vertex_adk
    • test_reasoning_engine_templates_adk.py
  • vertexai/preview/reasoning_engines/templates
    • adk.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎tests/unit/vertex_adk/test_reasoning_engine_templates_adk.py‎

Copy file name to clipboardExpand all lines: tests/unit/vertex_adk/test_reasoning_engine_templates_adk.py
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,91 @@ def test_delete_session(self):
370370
response0 = app.list_sessions(user_id="test_user_id")
371371
assert not response0.sessions
372372

373+
@pytest.mark.asyncio
374+
async def test_async_add_session_to_memory(self):
375+
app = reasoning_engines.AdkApp(
376+
agent=Agent(name="test_agent", model=_TEST_MODEL)
377+
)
378+
assert app._tmpl_attrs.get("memory_service") is None
379+
session = app.create_session(user_id="test_user_id")
380+
list(app.stream_query(
381+
user_id="test_user_id",
382+
session_id=session.id,
383+
message="My cat's name is Garfield",
384+
))
385+
await app.async_add_session_to_memory(session=app.get_session(
386+
user_id="test_user_id",
387+
session_id=session.id,
388+
))
389+
response = await app.async_search_memory(
390+
user_id="test_user_id",
391+
query="What is my cat's name",
392+
)
393+
assert len(response.memories) >= 1
394+
395+
@pytest.mark.asyncio
396+
async def test_async_add_session_to_memory_dict(self):
397+
app = reasoning_engines.AdkApp(
398+
agent=Agent(name="test_agent", model=_TEST_MODEL)
399+
)
400+
await app.async_add_session_to_memory(session={
401+
"id": "ca18c25a-644b-4e13-9b24-78c150ec3eb9",
402+
"app_name": "default-app-name",
403+
"user_id": "u_458",
404+
"events": [
405+
{
406+
"author": "user",
407+
"content": {
408+
"parts": [{"text": "My cat's name is Garfield"}],
409+
"role": "user",
410+
},
411+
},
412+
{
413+
"author": "my_personal_agent",
414+
"content": {
415+
"parts": [{"text": "Okay, good to know!"}],
416+
"role": "model",
417+
},
418+
},
419+
],
420+
})
421+
response = await app.async_search_memory(
422+
user_id="test_user_id",
423+
query="What is my cat's name",
424+
)
425+
assert len(response.memories) >= 1
426+
427+
@pytest.mark.asyncio
428+
async def test_async_search_memory(self):
429+
app = reasoning_engines.AdkApp(
430+
agent=Agent(name="test_agent", model=_TEST_MODEL)
431+
)
432+
response = await app.async_search_memory(
433+
user_id="test_user_id",
434+
query="What is my cat's name",
435+
)
436+
assert not response.memories
437+
await app.async_add_session_to_memory(session={
438+
"id": "ca18c25a-644b-4e13-9b24-78c150ec3eb9",
439+
"app_name": "default-app-name",
440+
"user_id": "u_458",
441+
"events": [
442+
{"content": {
443+
"parts": [{"text": "My cat's name is Garfield"}],
444+
"role": "user",
445+
}},
446+
{"content": {
447+
"parts": [{"text": "Okay, good to know!"}],
448+
"role": "model",
449+
}},
450+
],
451+
})
452+
response = await app.async_search_memory(
453+
user_id="test_user_id",
454+
query="What is my cat's name",
455+
)
456+
assert len(response.memories) >= 1
457+
373458
@pytest.mark.usefixtures("caplog")
374459
def test_enable_tracing(
375460
self,
Collapse file

‎vertexai/preview/reasoning_engines/templates/adk.py‎

Copy file name to clipboardExpand all lines: vertexai/preview/reasoning_engines/templates/adk.py
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
except (ImportError, AttributeError):
5252
BaseSessionService = Any
5353

54+
try:
55+
from google.adk.sessions.session import Session
56+
57+
Session = Session
58+
except (ImportError, AttributeError):
59+
Session = Any
60+
5461
try:
5562
from google.adk.artifacts import BaseArtifactService
5663

@@ -961,6 +968,53 @@ def _asyncio_thread_main():
961968
if isinstance(outcome, RuntimeError):
962969
raise outcome from None
963970

971+
async def async_add_session_to_memory(
972+
self,
973+
*,
974+
session: Union["Session", Dict[str, Any]],
975+
):
976+
"""Generates memories.
977+
978+
Args:
979+
session (Union[Session, Dict[str, Any]]):
980+
Required. The session to use for generating memories.
981+
"""
982+
from google.adk.sessions.session import Session
983+
984+
if isinstance(session, Dict):
985+
session = Session.model_validate(session)
986+
elif not isinstance(session, Session):
987+
raise TypeError("session must be a Session object.")
988+
if not session.events:
989+
# Get the latest version of the session in case it was updated.
990+
session = await self.async_get_session(
991+
user_id=session.user_id,
992+
session_id=session.id,
993+
)
994+
if not self._tmpl_attrs.get("memory_service"):
995+
self.set_up()
996+
return await self._tmpl_attrs.get("memory_service").add_session_to_memory(
997+
session=session,
998+
)
999+
1000+
async def async_search_memory(self, *, user_id: str, query: str):
1001+
"""Searches memories for the given user.
1002+
1003+
Args:
1004+
user_id: The id of the user.
1005+
query: The query to match the memories on.
1006+
1007+
Returns:
1008+
A SearchMemoryResponse containing the matching memories.
1009+
"""
1010+
if not self._tmpl_attrs.get("memory_service"):
1011+
self.set_up()
1012+
return await self._tmpl_attrs.get("memory_service").search_memory(
1013+
app_name=self._tmpl_attrs.get("app_name"),
1014+
user_id=user_id,
1015+
query=query,
1016+
)
1017+
9641018
def register_operations(self) -> Dict[str, List[str]]:
9651019
"""Registers the operations of the ADK application."""
9661020
return {
@@ -975,6 +1029,8 @@ def register_operations(self) -> Dict[str, List[str]]:
9751029
"async_list_sessions",
9761030
"async_create_session",
9771031
"async_delete_session",
1032+
"async_add_session_to_memory",
1033+
"async_search_memory",
9781034
],
9791035
"stream": ["stream_query", "streaming_agent_run_with_events"],
9801036
"async_stream": ["async_stream_query"],

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.