Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions contributing/samples/memory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Memory Sample

This sample demonstrates memory usage with ADK. The included `main.py` uses
in-memory services for simplicity. For persistent local memory, use SQLite.

## Persistent Local Memory (SQLite)

Programmatic usage:

```python
from google.adk.memory.sqlite_memory_service import SqliteMemoryService
from google.adk.runners import Runner
from google.adk.sessions.in_memory_session_service import InMemorySessionService

memory_service = SqliteMemoryService(db_path="memory.db")
runner = Runner(
app_name="my_app",
agent=agent.root_agent,
session_service=InMemorySessionService(),
memory_service=memory_service,
)
```

CLI usage (supported for `adk web` and `adk api_server`):

```bash
adk web path/to/agents_dir --memory_service_uri=sqlite:///memory.db
```

Notes:
- `sqlite:///memory.db` uses a relative path.
- `sqlite:////abs/path/memory.db` uses an absolute path.
- `adk run` currently ignores `--memory_service_uri`.
14 changes: 14 additions & 0 deletions src/google/adk/cli/service_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,22 @@ def agentengine_memory_factory(uri: str, **kwargs):
)
return VertexAiMemoryBankService(**params)

def sqlite_memory_factory(uri: str, **kwargs):
from ..memory.sqlite_memory_service import SqliteMemoryService

parsed = urlparse(uri)
db_path = parsed.path
if not db_path:
db_path = ":memory:"
elif db_path.startswith("/"):
db_path = db_path[1:]
Comment on lines +327 to +328
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for handling db_path when it starts with / is to remove the leading slash. This implies that absolute paths are expected to be provided without the leading slash when passed to SqliteMemoryService. While this works, it might be more intuitive to handle both sqlite:///path/to/db and sqlite:////abs/path/to/db (which urlparse would parse as /abs/path/to/db) consistently by ensuring db_path is correctly interpreted as an absolute path if it starts with / after parsing, rather than stripping it. However, given the current SqliteMemoryService expects db_path to be a str or Path and handles Path objects correctly, stripping the leading slash might be fine if SqliteMemoryService internally resolves relative paths from the current working directory or if the db_path is always expected to be relative unless explicitly handled as absolute by the caller.

kwargs_copy = kwargs.copy()
kwargs_copy.pop("agents_dir", None)
return SqliteMemoryService(db_path=db_path, **kwargs_copy)

registry.register_memory_service("rag", rag_memory_factory)
registry.register_memory_service("agentengine", agentengine_memory_factory)
registry.register_memory_service("sqlite", sqlite_memory_factory)


def _load_gcp_config(
Expand Down
2 changes: 2 additions & 0 deletions src/google/adk/memory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

from .base_memory_service import BaseMemoryService
from .in_memory_memory_service import InMemoryMemoryService
from .sqlite_memory_service import SqliteMemoryService
from .vertex_ai_memory_bank_service import VertexAiMemoryBankService

logger = logging.getLogger('google_adk.' + __name__)

__all__ = [
'BaseMemoryService',
'InMemoryMemoryService',
'SqliteMemoryService',
'VertexAiMemoryBankService',
]

Expand Down
Loading
Loading