メモリバックエンド
すべてのバックエンドは同じ MemoryService トレイトを実装しているため、機能フラグを有効にして構築することで選択します — エージェントコードは変更されません。6つあります。
概要
| バックエンド | 機能 | 永続性 | 検索 | 使用場面 |
|---|---|---|---|---|
InMemoryMemoryService | (デフォルト) | プロセスのみ | キーワード | 開発、テスト、デモ |
SqliteMemoryService | sqlite-memory | ファイル | キーワード | シングルノードアプリ、ローカル永続性 |
PostgresMemoryService | database-memory | Postgres + pgvector | ベクトル類似性 | 本番環境でのセマンティック検索 |
RedisMemoryService | redis-memory | Redis (オプション TTL) | キーワード | 高速、一時的、共有キャッシュ |
MongoMemoryService | mongodb-memory | MongoDB | キーワード / ベクトル | 既存のMongoインフラ |
Neo4jMemoryService | neo4j-memory | Neo4j | グラフ | 既存のNeo4jインフラ |
(バイテンポラル GraphMemoryService は7番目のもので、独自のページで説明されています —
ナレッジグラフ。)
InMemory — ここから開始
セットアップ不要。すべてプロセス内に存在し、終了時に消滅します。
use adk_memory::InMemoryMemoryService;
let memory = InMemoryMemoryService::new();
開発やテストスイートに最適です。トレイトが同一であるため、1行の変更で永続的なバックエンドに置き換えることができます。
SQLite — ファイル
永続的で、単一ファイル、サーバー不要。デスクトップアプリやシングルノードサービスに最適です。
use adk_memory::SqliteMemoryService;
let memory = SqliteMemoryService::new("sqlite://memory.db").await?;
// or build from an existing pool: SqliteMemoryService::from_pool(pool)
Postgres + pgvector — 本番環境のセマンティック検索
実際の類似性検索のためのバックエンドです。pgvectorに埋め込みを保存し、インデックス戦略を選択できます。
use adk_memory::{PostgresMemoryService, VectorIndexType};
let memory = PostgresMemoryService::builder(pool, embedding_provider)
.vector_index(VectorIndexType::Hnsw { m: 32, ef_construction: 128 }) // or IvfFlat / None
.build()
.await?;
VectorIndexType::Noneは厳密な(ブルートフォース)検索を行います — 小規模なセットには適しています。Hnsw(デフォルト)とIvfFlatは大規模なセットにスケールします。ベクトル検索には埋め込みプロバイダーが必要です。
Redis, MongoDB, Neo4j
すでにインフラストラクチャを実行している場合は、これらを使用してください。
use adk_memory::{RedisMemoryService, RedisMemoryConfig};
use std::time::Duration;
let memory = RedisMemoryService::new(RedisMemoryConfig {
url: "redis://localhost:6379".into(),
ttl: Some(Duration::from_secs(60 * 60 * 24 * 30)), // optional expiry
}).await?;
MongoMemoryService::new(...)とNeo4jMemoryService::new(...)は同じ形状に従います。これらはすべて、Conceptsからの(app, user, project)分離を尊重します。
Embeddings
ベクトル類似性(Postgres、オプションでMongo)を行うバックエンドは、テキストをベクトルに変換する必要があります。EmbeddingProviderを提供してください。
#[async_trait]
pub trait EmbeddingProvider: Send + Sync {
async fn embed(&self, texts: &[String]) -> Result<Vec<Vec<f32>>>;
}
キーワードバックエンド(InMemory、SQLite、Redis)は必要ありません。インデックスを作成する埋め込みモデルと、クエリに使用するモデルを一致させてください。
Migrations
SQLをバックエンドとするサービスは、スキーマを作成およびアップグレードするためにバージョン管理された冪等なマイグレーションを実行するため、新しいバージョンをデプロイする際に手動でのDDLは不要です。起動時にサービスのmigrate()を呼び出すか(または構築時に処理させるか)してください。
選択
- 構築 / テスト → InMemory。
- 単一ノードで永続化したい場合 → SQLite。
- 大規模なセマンティック検索 → Postgres + pgvector。
- すでにRedis / Mongo / Neo4jを実行している場合 → そのバックエンド。
- トランスクリプトではなく、ユーザーのクエリ可能なモデルが必要な場合 →
GraphMemoryService。
次へ: ナレッジグラフ →