View a markdown version of this page

エージェントメモリのベクトルストアとして ElastiCache for Valkey を設定する - Amazon ElastiCache

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

エージェントメモリのベクトルストアとして ElastiCache for Valkey を設定する

次のチュートリアルでは、Mem0 と ElastiCache for Valkey をベクトルストアとして使用してメモリ対応 AI エージェントを構築する方法を示します。

ステップ 1: メモリなしで基本エージェントを作成する

まず、Strands Agents をインストールし、基本的なエージェントを作成します。

pip install strands-agents strands-agents-tools strands-agents-builder

ウェブブラウジング用の HTTP ツールを使用して基本的なエージェントを初期化します。

from strands import Agent from strands.tools import http_request # Initialize agent with access to the tool to browse the web agent = Agent(tools=[http_request]) # Format messages as expected by Strands formatted_messages = [ { "role": "user", "content": [{"text": "What is the URL for the project mem0 and its most important metrics?"}] } ] result = agent(formatted_messages)

メモリがない場合、エージェントはリクエストごとに同じ調査タスクを繰り返し実行します。テストでは、エージェントは 3 つのツール呼び出しを実行してリクエストに応答します。約 70,000 トークンを使用し、完了までに 9 秒以上かかります。

ステップ 2: ElastiCache for Valkey で Mem0 を設定する

Valkey ベクトルストアコネクタを使用して Mem0 ライブラリをインストールします。

pip install mem0ai "mem0ai[vector_stores]"

Valkey をベクトルストアとして設定します。ElastiCache for Valkey は、バージョン 8.2 以降のベクトル検索機能をサポートしています。

from mem0 import Memory # Configure Mem0 with ElastiCache for Valkey config = { "vector_store": { "provider": "valkey", "config": { "valkey_url": "your-elasticache-cluster.cache.amazonaws.com:6379", "index_name": "agent_memory", "embedding_model_dims": 1024, "index_type": "flat" } } } m = Memory.from_config(config)

your-elasticache-cluster.cache.amazonaws.com を ElastiCache クラスターのエンドポイントに置き換えます。クラスターエンドポイントを検索する手順については、ElastiCache クラスターへのアクセス」を参照してください。

ステップ 3: エージェントにメモリツールを追加する

エージェントが情報の保存と取得に使用できるメモリツールを作成します。@tool デコレータは、通常の Python 関数をエージェントが呼び出すことができるツールに変換します。

from strands import Agent, tool from strands.tools import http_request @tool def store_memory_tool(information: str, user_id: str = "user") -> str: """Store important information in long-term memory.""" memory_message = [{"role": "user", "content": information}] # Create new memories using Mem0 and store them in Valkey m.add(memory_message, user_id=user_id) return f"Stored: {information}" @tool def search_memory_tool(query: str, user_id: str = "user") -> str: """Search stored memories for relevant information.""" # Search memories using Mem0 stored in Valkey results = m.search(query, user_id=user_id) if results['results']: return "\n".join([r['memory'] for r in results['results']]) return "No memories found" # Initialize Strands agent with memory tools agent = Agent(tools=[http_request, store_memory_tool, search_memory_tool])

ステップ 4: メモリ対応エージェントをテストする

メモリを有効にすると、エージェントはインタラクションから情報を保存し、後続のリクエストで取得します。

# First request - agent searches the web and stores results in memory formatted_messages = [ { "role": "user", "content": [{"text": "What is the URL for the project mem0 and its most important metrics?"}] } ] result = agent(formatted_messages) # Second request (same question) - agent retrieves from memory result = agent(formatted_messages)

2 番目のリクエストでは、エージェントはウェブツール呼び出しを行うのではなく、メモリから情報を取得します。テストでは、トークンの使用量が約 70,000 から 6,300 (12 倍の削減) に削減され、応答時間が 9.25 秒から 2 秒 (3 倍以上高速) に向上しました。

内部での仕組み

次の表は、Mem0 が内部的に ElastiCache でエージェントメモリを実装するために使用する Valkey コマンドを示しています。Mem0 は API を通じてこれらのコマンドを抽象化します。正確なスキーマとキーの命名はMem0 のバージョンと設定によって異なる場合があります。

運用 Valkey コマンド 説明
ベクトルインデックスを作成する FT.CREATE agent_memory SCHEMA embedding VECTOR HNSW 6 TYPE FLOAT32 DIM 1024 DISTANCE_METRIC COSINE セマンティックメモリ検索用のベクトルインデックスを作成します
メモリの保存 HSET mem:{id} memory "..." embedding [bytes] user_id "user_123" created_at "..." ベクトル埋め込みでメモリを保存します
思い出を検索する FT.SEARCH agent_memory "*=>[KNN 5 @embedding $query_vec]" PARAMS 2 query_vec [bytes] DIALECT 2 最も意味的に類似した記憶を見つける
有効期限を設定する EXPIRE mem:{id} 86400 メモリエントリの TTL を設定します