Skip to Content
リファレンスメモリメモリクラス

Memory クラスリファレンス

Memory クラスは、Mastraで会話履歴とスレッドベースのメッセージストレージを管理するための堅牢なシステムを提供します。会話の永続的な保存、セマンティック検索機能、効率的なメッセージ取得を可能にします。会話履歴のためのストレージプロバイダーを設定する必要があり、セマンティックリコールを有効にする場合は、ベクトルストアとエンベッダーも提供する必要があります。

基本的な使い方

import { Memory } from "@mastra/memory"; import { Agent } from "@mastra/core/agent"; const agent = new Agent({ memory: new Memory(), ...otherOptions, });

カスタム設定

import { Memory } from "@mastra/memory"; import { LibSQLStore, LibSQLVector } from "@mastra/libsql"; import { Agent } from "@mastra/core/agent"; const memory = new Memory({ // Optional storage configuration - libsql will be used by default storage: new LibSQLStore({ url: "file:./memory.db", }), // Optional vector database for semantic search vector: new LibSQLVector({ url: "file:./vector.db", }), // Memory configuration options options: { // Number of recent messages to include lastMessages: 20, // Semantic search configuration semanticRecall: { topK: 3, // Number of similar messages to retrieve messageRange: { // Messages to include around each result before: 2, after: 1, }, }, // Working memory configuration workingMemory: { enabled: true, template: ` # User - First Name: - Last Name: `, }, }, }); const agent = new Agent({ memory, ...otherOptions, });

ワーキングメモリ

ワーキングメモリ機能を利用すると、エージェントは会話をまたいで情報を保持できます。有効にすると、Memory クラスは専用のツールコールを使ってワーキングメモリの更新を自動的に管理します。

設定例:

const memory = new Memory({ options: { workingMemory: { enabled: true, template: "# User\n- **First Name**:\n- **Last Name**:", }, }, });

テンプレートが指定されていない場合、Memory クラスはデフォルトのテンプレートを使用します。これには、ユーザー情報、好み、目標、その他のコンテキスト情報のフィールドが Markdown 形式で含まれています。詳細な使用例やベストプラクティスについては、ワーキングメモリガイドをご覧ください。

embedder

semanticRecall を有効にする場合、埋め込みモデルが必要です。

一つの選択肢として @mastra/fastembed を利用できます。これは FastEmbed を使ったオンデバイス/ローカル埋め込みモデルを提供します。このモデルはローカルで動作し、API キーやネットワークリクエストは不要です。

利用するには、まずパッケージをインストールしてください:

npm install @mastra/fastembed

次に、Memory インスタンスで設定します:

import { Memory } from "@mastra/memory"; import { fastembed } from "@mastra/fastembed"; import { Agent } from "@mastra/core/agent"; const agent = new Agent({ memory: new Memory({ embedder: fastembed, // ... other memory config }), });

なお、プロジェクトのデプロイ先によっては、FastEmbed の大きな内部依存関係のためにデプロイできない場合があります。

代わりに、OpenAI のような API ベースの embedder も利用できます(この問題は発生しません):

import { Memory } from "@mastra/memory"; import { openai } from "@ai-sdk/openai"; import { Agent } from "@mastra/core/agent"; const agent = new Agent({ memory: new Memory({ embedder: openai.embedding("text-embedding-3-small"), }), });

Mastra は Vercel AI SDK を通じて多くの埋め込みモデルをサポートしており、OpenAI、Google、Mistral、Cohere などのオプションも利用できます。

パラメータ

storage?:

MastraStorage
メモリデータを永続化するためのストレージ実装

vector?:

MastraVector
セマンティック検索機能のためのベクターストア

embedder?:

EmbeddingModel
ベクター埋め込みのためのEmbedderインスタンス。セマンティックリコールが有効な場合に必要

options?:

MemoryConfig
一般的なメモリ設定オプション

options

lastMessages?:

number | false
= 10
取得する最新メッセージの数。無効にするにはfalseに設定します。

semanticRecall?:

boolean | SemanticRecallConfig
= false
メッセージ履歴でのセマンティック検索を有効にします。ベクターストアが提供されている場合は自動的に有効になります。

topK?:

number
= 2
セマンティック検索を使用する際に取得する類似メッセージの数

messageRange?:

number | { before: number; after: number }
= 2
セマンティック検索結果の周辺に含めるメッセージの範囲

scope?:

'thread' | 'resource'
= 'thread'
セマンティック検索のスコープ。'thread'は現在のスレッド内のみを検索します(デフォルト)。'resource'は指定されたresourceIdのすべてのスレッドを横断して検索し、エージェントがユーザーの過去の会話から情報を思い出すことを可能にします。'resource'スコープは現在LibSQL、Postgres、Upstashストレージアダプターでサポートされています。

workingMemory?:

{ enabled: boolean; template?: string }
= { enabled: false, template: '# User Information\n- **First Name**:\n- **Last Name**:\n...' }
会話を通じてユーザー情報を永続的に保存できるワーキングメモリ機能の設定。ワーキングメモリはMarkdown形式を使用して、継続的に関連する情報を構造化して保存します。

threads?:

{ generateTitle?: boolean }
= { generateTitle: false }
メモリスレッド作成に関する設定。`generateTitle`を有効にすると、ユーザーの最初のメッセージのLLM要約からthread.titleが生成されます。

関連項目