Upstash を使ったメモリ
この例では、ストレージのバックエンドとして Upstash を使用して、Mastra のメモリシステムを利用する方法を紹介します。
前提条件
この例では openai
モデルを使用し、Upstash Redis と Upstash Vector の両サービスが必要です。以下を .env
ファイルに追加してください:
.env
OPENAI_API_KEY=<your-api-key>
UPSTASH_REDIS_REST_URL=<your-redis-url>
UPSTASH_REDIS_REST_TOKEN=<your-redis-token>
UPSTASH_VECTOR_REST_URL=<your-vector-index-url>
UPSTASH_VECTOR_REST_TOKEN=<your-vector-index-token>
upstash.com にサインアップし、Redis と Vector のデータベースをそれぞれ作成すると、Upstash の認証情報を取得できます。
次のパッケージをインストールしてください:
npm install @mastra/upstash
エージェントにメモリを追加する
エージェントに Upstash のメモリを追加するには、Memory
クラスを使い、UpstashStore
で新しい storage
キーを、UpstashVector
で新しい vector
キーをそれぞれ作成します。設定はリモートサービスにもローカル環境にも向けられます。
src/mastra/agents/example-upstash-agent.ts
import { Memory } from "@mastra/memory";
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
import { UpstashStore } from "@mastra/upstash";
export const upstashAgent = new Agent({
name: "upstash-agent",
instructions: "You are an AI agent with the ability to automatically recall memories from previous interactions.",
model: openai("gpt-4o"),
memory: new Memory({
storage: new UpstashStore({
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!
}),
options: {
threads: {
generateTitle: true
}
}
})
});
fastembed を使ったローカル埋め込み
埋め込みは、メモリの semanticRecall
が(キーワードではなく)意味に基づいて関連メッセージを検索するために用いる数値ベクトルです。このセットアップでは、ベクトル埋め込みの生成に @mastra/fastembed
を使用します。
まずは fastembed
をインストールします:
npm install @mastra/fastembed
次をエージェントに追加します:
src/mastra/agents/example-upstash-agent.ts
import { Memory } from "@mastra/memory";
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
import { UpstashStore, UpstashVector } from "@mastra/upstash";
import { fastembed } from "@mastra/fastembed";
export const upstashAgent = new Agent({
name: "upstash-agent",
instructions: "You are an AI agent with the ability to automatically recall memories from previous interactions.",
model: openai("gpt-4o"),
memory: new Memory({
storage: new UpstashStore({
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!
}),
vector: new UpstashVector({
url: process.env.UPSTASH_VECTOR_REST_URL!,
token: process.env.UPSTASH_VECTOR_REST_TOKEN!
}),
embedder: fastembed,
options: {
lastMessages: 10,
semanticRecall: {
topK: 3,
messageRange: 2
}
}
})
});
使用例
このリクエストのリコール範囲を絞るには、memoryOptions
を使用します。lastMessages: 5
を設定して直近メッセージに基づくリコールを制限し、semanticRecall
を使って、各一致の前後の文脈として messageRange: 2
の隣接メッセージを含めつつ、最も関連性の高い topK: 3
件のメッセージを取得します。
src/test-upstash-agent.ts
import "dotenv/config";
import { mastra } from "./mastra";
const threadId = "123";
const resourceId = "user-456";
const agent = mastra.getAgent("upstashAgent");
const message = await agent.stream("My name is Mastra", {
memory: {
thread: threadId,
resource: resourceId
}
});
await message.textStream.pipeTo(new WritableStream());
const stream = await agent.stream("What's my name?", {
memory: {
thread: threadId,
resource: resourceId
},
memoryOptions: {
lastMessages: 5,
semanticRecall: {
topK: 3,
messageRange: 2
}
}
});
for await (const chunk of stream.textStream) {
process.stdout.write(chunk);
}