GeezerVison is showing github.com — feat: Add memory related methods to AdkApp · googleapis/python-aiplatform@e92a4ef
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 +85Lines changed: 85 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -370,6 +370,91 @@ 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_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 | + | 373 | 458 | @pytest.mark.usefixtures("caplog") | 374 | 459 | def test_enable_tracing( | 375 | 460 | self, | |
Collapse file Copy file name to clipboardExpand all lines: vertexai/preview/reasoning_engines/templates/adk.py +56Lines changed: 56 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 51 | 51 | except (ImportError, AttributeError): | 52 | 52 | BaseSessionService = Any | 53 | 53 |
| | 54 | + try: | | 55 | + from google.adk.sessions.session import Session | | 56 | + | | 57 | + Session = Session | | 58 | + except (ImportError, AttributeError): | | 59 | + Session = Any | | 60 | + | 54 | 61 | try: | 55 | 62 | from google.adk.artifacts import BaseArtifactService | 56 | 63 |
| @@ -961,6 +968,53 @@ def _asyncio_thread_main(): | 961 | 968 | if isinstance(outcome, RuntimeError): | 962 | 969 | raise outcome from None | 963 | 970 |
| | 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 | + | 964 | 1018 | def register_operations(self) -> Dict[str, List[str]]: | 965 | 1019 | """Registers the operations of the ADK application.""" | 966 | 1020 | return { | @@ -975,6 +1029,8 @@ def register_operations(self) -> Dict[str, List[str]]: | 975 | 1029 | "async_list_sessions", | 976 | 1030 | "async_create_session", | 977 | 1031 | "async_delete_session", | | 1032 | + "async_add_session_to_memory", | | 1033 | + "async_search_memory", | 978 | 1034 | ], | 979 | 1035 | "stream": ["stream_query", "streaming_agent_run_with_events"], | 980 | 1036 | "async_stream": ["async_stream_query"], | |
|