GeezerVison is showing github.com — feat: Add memory related methods to AdkApp · googleapis/python-aiplatform@c49c52b
File treeExpand file treeCollapse file tree Open diff view settings vertexai/preview/reasoning_engines/templates
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: tests/unit/vertex_adk/test_reasoning_engine_templates_adk.py +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -370,6 +370,40 @@ def test_delete_session(self): | 370 | 370 | response0 = app.list_sessions(user_id="test_user_id") | 371 | 371 | assert not response0.sessions | 372 | 372 |
| | 373 | + @pytest.mark.asyncio | | 374 | + async def test_async_generate_memories(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_generate_memories(source=session) | | 386 | + assert app._tmpl_attrs.get("memory_service") is not None | | 387 | + | | 388 | + @pytest.mark.asyncio | | 389 | + async def test_async_search_memories(self): | | 390 | + app = reasoning_engines.AdkApp( | | 391 | + agent=Agent(name="test_agent", model=_TEST_MODEL) | | 392 | + ) | | 393 | + assert app._tmpl_attrs.get("memory_service") is None | | 394 | + session = app.create_session(user_id="test_user_id") | | 395 | + list(app.stream_query( | | 396 | + user_id="test_user_id", | | 397 | + session_id=session.id, | | 398 | + message="My cat's name is Garfield", | | 399 | + )) | | 400 | + await app.async_generate_memories(source=session) | | 401 | + await app.search_memories( | | 402 | + user_id="test_user_id", | | 403 | + query="What is my cat's name", | | 404 | + ) | | 405 | + assert app._tmpl_attrs.get("memory_service") is not None | | 406 | + | 373 | 407 | @pytest.mark.usefixtures("caplog") | 374 | 408 | def test_enable_tracing( | 375 | 409 | self, | |
Collapse file Copy file name to clipboardExpand all lines: vertexai/preview/reasoning_engines/templates/adk.py +40Lines changed: 40 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -961,6 +961,44 @@ def _asyncio_thread_main(): | 961 | 961 | if isinstance(outcome, RuntimeError): | 962 | 962 | raise outcome from None | 963 | 963 |
| | 964 | + async def async_generate_memories(self, *, source: Any): | | 965 | + """Generates memories. | | 966 | +
| | 967 | + Args: | | 968 | + source (Union[Session, Dict[str, Any]]): | | 969 | + Required. The source (e.g. session) for generating memories. | | 970 | + """ | | 971 | + from google.adk.sessions.session import Session | | 972 | + | | 973 | + if isinstance(source, Dict): | | 974 | + source = Session.model_validate(source) | | 975 | + elif not isinstance(source, Session): | | 976 | + raise TypeError("source must be a Session object.") | | 977 | + | | 978 | + if not self._tmpl_attrs.get("memory_service"): | | 979 | + self.set_up() | | 980 | + return await self._tmpl_attrs.get("memory_service").add_session_to_memory( | | 981 | + session=source, | | 982 | + ) | | 983 | + | | 984 | + async def async_search_memories(self, *, user_id: str, query: str): | | 985 | + """Searches memories for the given user. | | 986 | +
| | 987 | + Args: | | 988 | + user_id: The id of the user. | | 989 | + query: The query to match the memories on. | | 990 | +
| | 991 | + Returns: | | 992 | + A SearchMemoryResponse containing the matching memories. | | 993 | + """ | | 994 | + if not self._tmpl_attrs.get("memory_service"): | | 995 | + self.set_up() | | 996 | + return await self._tmpl_attrs.get("memory_service").search_memory( | | 997 | + app_name=self._tmpl_attrs.get("app_name"), | | 998 | + user_id=user_id, | | 999 | + query=query, | | 1000 | + ) | | 1001 | + | 964 | 1002 | def register_operations(self) -> Dict[str, List[str]]: | 965 | 1003 | """Registers the operations of the ADK application.""" | 966 | 1004 | return { | @@ -975,6 +1013,8 @@ def register_operations(self) -> Dict[str, List[str]]: | 975 | 1013 | "async_list_sessions", | 976 | 1014 | "async_create_session", | 977 | 1015 | "async_delete_session", | | 1016 | + "async_generate_memories", | | 1017 | + "async_search_memories", | 978 | 1018 | ], | 979 | 1019 | "stream": ["stream_query", "streaming_agent_run_with_events"], | 980 | 1020 | "async_stream": ["async_stream_query"], | |
|