Skip to Content
メモリMem0を使用したメモリ

Mem0を使ったメモリ

この例では、カスタムツールを通じてMem0をメモリバックエンドとして使用するMastraのエージェントシステムの使い方を説明します。

セットアップ

まず、Mem0インテグレーションを設定し、情報を記憶・想起するためのツールを作成します。

import { Mem0Integration } from "@mastra/mem0"; import { createTool } from "@mastra/core/tools"; import { Agent } from "@mastra/core/agent"; import { openai } from "@ai-sdk/openai"; import { z } from "zod"; // Initialize Mem0 integration const mem0 = new Mem0Integration({ config: { apiKey: process.env.MEM0_API_KEY || "", user_id: "alice", // Unique user identifier }, }); // Create memory tools const mem0RememberTool = createTool({ id: "Mem0-remember", description: "Remember your agent memories that you've previously saved using the Mem0-memorize tool.", inputSchema: z.object({ question: z .string() .describe("Question used to look up the answer in saved memories."), }), outputSchema: z.object({ answer: z.string().describe("Remembered answer"), }), execute: async ({ context }) => { console.log(`Searching memory "${context.question}"`); const memory = await mem0.searchMemory(context.question); console.log(`\nFound memory "${memory}"\n`); return { answer: memory, }; }, }); const mem0MemorizeTool = createTool({ id: "Mem0-memorize", description: "Save information to mem0 so you can remember it later using the Mem0-remember tool.", inputSchema: z.object({ statement: z.string().describe("A statement to save into memory"), }), execute: async ({ context }) => { console.log(`\nCreating memory "${context.statement}"\n`); // To reduce latency, memories can be saved async without blocking tool execution void mem0.createMemory(context.statement).then(() => { console.log(`\nMemory "${context.statement}" saved.\n`); }); return { success: true }; }, }); // Create an agent with memory tools const mem0Agent = new Agent({ name: "Mem0 Agent", instructions: ` You are a helpful assistant that has the ability to memorize and remember facts using Mem0. Use the Mem0-memorize tool to save important information that might be useful later. Use the Mem0-remember tool to recall previously saved information when answering questions. `, model: openai("gpt-4o"), tools: { mem0RememberTool, mem0MemorizeTool }, });

環境設定

環境変数にMem0 APIキーを設定してください:

MEM0_API_KEY=your-mem0-api-key

Mem0 APIキーは、app.mem0.aiでサインアップして新しいプロジェクトを作成することで取得できます。

使用例

import { randomUUID } from "crypto"; // Start a conversation const threadId = randomUUID(); // Ask the agent to remember some information const response1 = await mem0Agent.text( "Please remember that I prefer vegetarian meals and I'm allergic to nuts. Also, I live in San Francisco.", { threadId, }, ); // Ask about different topics const response2 = await mem0Agent.text( "I'm planning a dinner party for 6 people next weekend. Can you suggest a menu?", { threadId, }, ); // Later, ask the agent to recall information const response3 = await mem0Agent.text( "What do you remember about my dietary preferences?", { threadId, }, ); // Ask about location-specific information const response4 = await mem0Agent.text( "Recommend some local restaurants for my dinner party based on what you know about me.", { threadId, }, );

主な機能

Mem0統合には以下のような利点があります:

  1. 自動メモリ管理: Mem0がメモリの保存、インデックス化、検索を知的に処理します
  2. セマンティック検索: エージェントは完全一致だけでなく、セマンティックな類似性に基づいて関連するメモリを見つけることができます
  3. ユーザー固有のメモリ: 各user_idが個別のメモリ空間を維持します
  4. 非同期保存: メモリはバックグラウンドで保存され、応答遅延を削減します
  5. 長期持続性: メモリは会話やセッションを跨いで持続します

ツールベースのアプローチ

Mastraの組み込みメモリシステムとは異なり、この例では以下のようなツールベースのアプローチを使用します:

  • エージェントはMem0-memorizeツールを使用して情報を保存するタイミングを決定します
  • エージェントはMem0-rememberツールを使用して関連するメモリを積極的に検索できます
  • これにより、エージェントはメモリ操作をより制御でき、メモリの使用が透明になります

ベストプラクティス

  1. 明確な指示: いつ情報を記憶し、思い出すべきかについて、エージェントに明確な指示を提供する
  2. ユーザー識別: 異なるユーザーに対して別々のメモリ空間を維持するために、一貫したuser_id値を使用する
  3. 説明的な文: メモリを保存する際は、後で検索しやすい説明的な文を使用する
  4. メモリのクリーンアップ: 古いまたは無関係なメモリの定期的なクリーンアップの実装を検討する

この例では、会話を通じてユーザーについての情報を学習し記憶できるインテリジェントなエージェントを作成する方法を示しており、時間の経過とともにインタラクションをよりパーソナライズされ、文脈に応じたものにします。