@mastra/mcp-docs-server 0.0.0-default-storage-virtual-file-20250410035748
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.docs/organized/changelogs/%40mastra%2Fastra.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +131 -0
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +80 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Ffirecrawl.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fgithub.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fmem0.md +166 -0
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Frag.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fragie.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-azure.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-deepgram.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-elevenlabs.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-google.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-ibm.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-murf.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-openai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-playai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-replicate.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-speechify.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +220 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +220 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +299 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +302 -0
- package/.docs/organized/changelogs/create-mastra.md +302 -0
- package/.docs/organized/changelogs/mastra.md +302 -0
- package/.docs/organized/code-examples/agent-network.md +282 -0
- package/.docs/organized/code-examples/agent.md +390 -0
- package/.docs/organized/code-examples/ai-sdk-useChat.md +378 -0
- package/.docs/organized/code-examples/assistant-ui.md +37 -0
- package/.docs/organized/code-examples/bird-checker-with-express.md +235 -0
- package/.docs/organized/code-examples/bird-checker-with-nextjs-and-eval.md +360 -0
- package/.docs/organized/code-examples/bird-checker-with-nextjs.md +250 -0
- package/.docs/organized/code-examples/crypto-chatbot.md +96 -0
- package/.docs/organized/code-examples/fireworks-r1.md +159 -0
- package/.docs/organized/code-examples/memory-todo-agent.md +164 -0
- package/.docs/organized/code-examples/memory-with-context.md +167 -0
- package/.docs/organized/code-examples/memory-with-libsql.md +204 -0
- package/.docs/organized/code-examples/memory-with-mem0.md +121 -0
- package/.docs/organized/code-examples/memory-with-pg.md +224 -0
- package/.docs/organized/code-examples/memory-with-upstash.md +268 -0
- package/.docs/organized/code-examples/quick-start.md +129 -0
- package/.docs/organized/code-examples/stock-price-tool.md +124 -0
- package/.docs/organized/code-examples/weather-agent.md +353 -0
- package/.docs/organized/code-examples/workflow-ai-recruiter.md +159 -0
- package/.docs/organized/code-examples/workflow-with-inline-steps.md +111 -0
- package/.docs/organized/code-examples/workflow-with-memory.md +393 -0
- package/.docs/organized/code-examples/workflow-with-separate-steps.md +131 -0
- package/.docs/raw/agents/adding-tools.mdx +239 -0
- package/.docs/raw/agents/adding-voice.mdx +175 -0
- package/.docs/raw/agents/agent-memory.mdx +62 -0
- package/.docs/raw/agents/mcp-guide.mdx +192 -0
- package/.docs/raw/agents/overview.mdx +303 -0
- package/.docs/raw/community/discord.mdx +12 -0
- package/.docs/raw/community/licensing.mdx +63 -0
- package/.docs/raw/deployment/client.mdx +120 -0
- package/.docs/raw/deployment/deployment.mdx +119 -0
- package/.docs/raw/deployment/server.mdx +276 -0
- package/.docs/raw/evals/custom-eval.mdx +22 -0
- package/.docs/raw/evals/overview.mdx +95 -0
- package/.docs/raw/evals/running-in-ci.mdx +81 -0
- package/.docs/raw/evals/textual-evals.mdx +54 -0
- package/.docs/raw/faq/index.mdx +63 -0
- package/.docs/raw/frameworks/ai-sdk.mdx +296 -0
- package/.docs/raw/frameworks/next-js.mdx +238 -0
- package/.docs/raw/getting-started/installation.mdx +436 -0
- package/.docs/raw/getting-started/mcp-docs-server.mdx +141 -0
- package/.docs/raw/getting-started/project-structure.mdx +80 -0
- package/.docs/raw/index.mdx +22 -0
- package/.docs/raw/integrations/index.mdx +213 -0
- package/.docs/raw/local-dev/add-to-existing-project.mdx +48 -0
- package/.docs/raw/local-dev/creating-a-new-project.mdx +54 -0
- package/.docs/raw/local-dev/mastra-dev.mdx +108 -0
- package/.docs/raw/memory/memory-processors.mdx +131 -0
- package/.docs/raw/memory/overview.mdx +119 -0
- package/.docs/raw/memory/semantic-recall.mdx +122 -0
- package/.docs/raw/memory/working-memory.mdx +87 -0
- package/.docs/raw/observability/logging.mdx +38 -0
- package/.docs/raw/observability/nextjs-tracing.mdx +108 -0
- package/.docs/raw/observability/tracing.mdx +115 -0
- package/.docs/raw/rag/chunking-and-embedding.mdx +156 -0
- package/.docs/raw/rag/overview.mdx +85 -0
- package/.docs/raw/rag/retrieval.mdx +365 -0
- package/.docs/raw/rag/vector-databases.mdx +340 -0
- package/.docs/raw/reference/agents/createTool.mdx +229 -0
- package/.docs/raw/reference/agents/generate.mdx +327 -0
- package/.docs/raw/reference/agents/getAgent.mdx +54 -0
- package/.docs/raw/reference/agents/stream.mdx +362 -0
- package/.docs/raw/reference/cli/build.mdx +48 -0
- package/.docs/raw/reference/cli/deploy.mdx +22 -0
- package/.docs/raw/reference/cli/dev.mdx +134 -0
- package/.docs/raw/reference/cli/init.mdx +43 -0
- package/.docs/raw/reference/client-js/agents.mdx +107 -0
- package/.docs/raw/reference/client-js/error-handling.mdx +38 -0
- package/.docs/raw/reference/client-js/logs.mdx +24 -0
- package/.docs/raw/reference/client-js/memory.mdx +97 -0
- package/.docs/raw/reference/client-js/telemetry.mdx +20 -0
- package/.docs/raw/reference/client-js/tools.mdx +44 -0
- package/.docs/raw/reference/client-js/vectors.mdx +79 -0
- package/.docs/raw/reference/client-js/workflows.mdx +136 -0
- package/.docs/raw/reference/core/mastra-class.mdx +232 -0
- package/.docs/raw/reference/deployer/cloudflare.mdx +176 -0
- package/.docs/raw/reference/deployer/deployer.mdx +159 -0
- package/.docs/raw/reference/deployer/netlify.mdx +88 -0
- package/.docs/raw/reference/deployer/vercel.mdx +97 -0
- package/.docs/raw/reference/evals/answer-relevancy.mdx +186 -0
- package/.docs/raw/reference/evals/bias.mdx +186 -0
- package/.docs/raw/reference/evals/completeness.mdx +174 -0
- package/.docs/raw/reference/evals/content-similarity.mdx +183 -0
- package/.docs/raw/reference/evals/context-position.mdx +190 -0
- package/.docs/raw/reference/evals/context-precision.mdx +189 -0
- package/.docs/raw/reference/evals/context-relevancy.mdx +188 -0
- package/.docs/raw/reference/evals/contextual-recall.mdx +191 -0
- package/.docs/raw/reference/evals/faithfulness.mdx +193 -0
- package/.docs/raw/reference/evals/hallucination.mdx +219 -0
- package/.docs/raw/reference/evals/keyword-coverage.mdx +176 -0
- package/.docs/raw/reference/evals/prompt-alignment.mdx +238 -0
- package/.docs/raw/reference/evals/summarization.mdx +205 -0
- package/.docs/raw/reference/evals/textual-difference.mdx +161 -0
- package/.docs/raw/reference/evals/tone-consistency.mdx +181 -0
- package/.docs/raw/reference/evals/toxicity.mdx +165 -0
- package/.docs/raw/reference/index.mdx +8 -0
- package/.docs/raw/reference/memory/Memory.mdx +212 -0
- package/.docs/raw/reference/memory/createThread.mdx +95 -0
- package/.docs/raw/reference/memory/getThreadById.mdx +46 -0
- package/.docs/raw/reference/memory/getThreadsByResourceId.mdx +48 -0
- package/.docs/raw/reference/memory/query.mdx +167 -0
- package/.docs/raw/reference/networks/agent-network.mdx +159 -0
- package/.docs/raw/reference/observability/create-logger.mdx +106 -0
- package/.docs/raw/reference/observability/logger.mdx +55 -0
- package/.docs/raw/reference/observability/otel-config.mdx +120 -0
- package/.docs/raw/reference/observability/providers/braintrust.mdx +40 -0
- package/.docs/raw/reference/observability/providers/dash0.mdx +40 -0
- package/.docs/raw/reference/observability/providers/index.mdx +16 -0
- package/.docs/raw/reference/observability/providers/laminar.mdx +41 -0
- package/.docs/raw/reference/observability/providers/langfuse.mdx +51 -0
- package/.docs/raw/reference/observability/providers/langsmith.mdx +48 -0
- package/.docs/raw/reference/observability/providers/langwatch.mdx +45 -0
- package/.docs/raw/reference/observability/providers/new-relic.mdx +40 -0
- package/.docs/raw/reference/observability/providers/signoz.mdx +40 -0
- package/.docs/raw/reference/observability/providers/traceloop.mdx +40 -0
- package/.docs/raw/reference/rag/astra.mdx +258 -0
- package/.docs/raw/reference/rag/chroma.mdx +281 -0
- package/.docs/raw/reference/rag/chunk.mdx +235 -0
- package/.docs/raw/reference/rag/document.mdx +127 -0
- package/.docs/raw/reference/rag/embeddings.mdx +160 -0
- package/.docs/raw/reference/rag/extract-params.mdx +226 -0
- package/.docs/raw/reference/rag/graph-rag.mdx +182 -0
- package/.docs/raw/reference/rag/libsql.mdx +357 -0
- package/.docs/raw/reference/rag/metadata-filters.mdx +298 -0
- package/.docs/raw/reference/rag/pg.mdx +477 -0
- package/.docs/raw/reference/rag/pinecone.mdx +281 -0
- package/.docs/raw/reference/rag/qdrant.mdx +236 -0
- package/.docs/raw/reference/rag/rerank.mdx +212 -0
- package/.docs/raw/reference/rag/turbopuffer.mdx +249 -0
- package/.docs/raw/reference/rag/upstash.mdx +247 -0
- package/.docs/raw/reference/rag/vectorize.mdx +298 -0
- package/.docs/raw/reference/storage/libsql.mdx +74 -0
- package/.docs/raw/reference/storage/postgresql.mdx +48 -0
- package/.docs/raw/reference/storage/upstash.mdx +86 -0
- package/.docs/raw/reference/tools/client.mdx +188 -0
- package/.docs/raw/reference/tools/document-chunker-tool.mdx +141 -0
- package/.docs/raw/reference/tools/graph-rag-tool.mdx +154 -0
- package/.docs/raw/reference/tools/mcp-configuration.mdx +206 -0
- package/.docs/raw/reference/tools/vector-query-tool.mdx +212 -0
- package/.docs/raw/reference/voice/composite-voice.mdx +140 -0
- package/.docs/raw/reference/voice/deepgram.mdx +164 -0
- package/.docs/raw/reference/voice/elevenlabs.mdx +216 -0
- package/.docs/raw/reference/voice/google.mdx +198 -0
- package/.docs/raw/reference/voice/mastra-voice.mdx +394 -0
- package/.docs/raw/reference/voice/murf.mdx +251 -0
- package/.docs/raw/reference/voice/openai-realtime.mdx +431 -0
- package/.docs/raw/reference/voice/openai.mdx +168 -0
- package/.docs/raw/reference/voice/playai.mdx +159 -0
- package/.docs/raw/reference/voice/sarvam.mdx +260 -0
- package/.docs/raw/reference/voice/speechify.mdx +145 -0
- package/.docs/raw/reference/voice/voice.answer.mdx +122 -0
- package/.docs/raw/reference/voice/voice.connect.mdx +124 -0
- package/.docs/raw/reference/voice/voice.listen.mdx +195 -0
- package/.docs/raw/reference/voice/voice.on.mdx +189 -0
- package/.docs/raw/reference/voice/voice.send.mdx +118 -0
- package/.docs/raw/reference/voice/voice.speak.mdx +203 -0
- package/.docs/raw/reference/workflows/after.mdx +88 -0
- package/.docs/raw/reference/workflows/afterEvent.mdx +76 -0
- package/.docs/raw/reference/workflows/commit.mdx +37 -0
- package/.docs/raw/reference/workflows/createRun.mdx +77 -0
- package/.docs/raw/reference/workflows/else.mdx +72 -0
- package/.docs/raw/reference/workflows/events.mdx +305 -0
- package/.docs/raw/reference/workflows/execute.mdx +110 -0
- package/.docs/raw/reference/workflows/if.mdx +107 -0
- package/.docs/raw/reference/workflows/resume.mdx +155 -0
- package/.docs/raw/reference/workflows/resumeWithEvent.mdx +133 -0
- package/.docs/raw/reference/workflows/snapshots.mdx +207 -0
- package/.docs/raw/reference/workflows/start.mdx +84 -0
- package/.docs/raw/reference/workflows/step-class.mdx +100 -0
- package/.docs/raw/reference/workflows/step-condition.mdx +134 -0
- package/.docs/raw/reference/workflows/step-function.mdx +92 -0
- package/.docs/raw/reference/workflows/step-options.mdx +69 -0
- package/.docs/raw/reference/workflows/step-retries.mdx +203 -0
- package/.docs/raw/reference/workflows/suspend.mdx +70 -0
- package/.docs/raw/reference/workflows/then.mdx +74 -0
- package/.docs/raw/reference/workflows/until.mdx +165 -0
- package/.docs/raw/reference/workflows/watch.mdx +118 -0
- package/.docs/raw/reference/workflows/while.mdx +168 -0
- package/.docs/raw/reference/workflows/workflow.mdx +233 -0
- package/.docs/raw/storage/overview.mdx +378 -0
- package/.docs/raw/voice/overview.mdx +135 -0
- package/.docs/raw/voice/speech-to-text.mdx +45 -0
- package/.docs/raw/voice/text-to-speech.mdx +52 -0
- package/.docs/raw/voice/voice-to-voice.mdx +310 -0
- package/.docs/raw/workflows/control-flow.mdx +778 -0
- package/.docs/raw/workflows/dynamic-workflows.mdx +236 -0
- package/.docs/raw/workflows/error-handling.mdx +183 -0
- package/.docs/raw/workflows/nested-workflows.mdx +352 -0
- package/.docs/raw/workflows/overview.mdx +167 -0
- package/.docs/raw/workflows/steps.mdx +108 -0
- package/.docs/raw/workflows/suspend-and-resume.mdx +404 -0
- package/.docs/raw/workflows/variables.mdx +313 -0
- package/LICENSE +44 -0
- package/README.md +129 -0
- package/dist/_tsup-dts-rollup.d.ts +149 -0
- package/dist/chunk-QWYMT5LP.js +194 -0
- package/dist/prepare-docs/prepare.d.ts +1 -0
- package/dist/prepare-docs/prepare.js +1 -0
- package/dist/stdio.d.ts +1 -0
- package/dist/stdio.js +518 -0
- package/package.json +60 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Recalling Relevant History
|
|
2
|
+
|
|
3
|
+
If you ask your friend what they did last weekend, they will search in their memory for events associated with "last weekend" and then tell you what they did. That's sort of like how semantic recall works in Mastra.
|
|
4
|
+
|
|
5
|
+
## How Semantic Recall Works
|
|
6
|
+
|
|
7
|
+
Semantic recall is RAG-based search that helps agents maintain context across longer interactions when messages are no longer within [recent conversation history](./overview.mdx#conversation-history).
|
|
8
|
+
|
|
9
|
+
It uses vector embeddings of messages for similarity search, integrates with various vector stores, and has configurable context windows around retrieved messages.
|
|
10
|
+
|
|
11
|
+
<br />
|
|
12
|
+
<img
|
|
13
|
+
src="/docs/semantic-recall.png"
|
|
14
|
+
alt="Diagram showing Mastra Memory semantic recall"
|
|
15
|
+
width={800}
|
|
16
|
+
/>
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
Semantic recall is enabled by default, so if you give your agent memory it will be included:
|
|
21
|
+
|
|
22
|
+
```typescript {9}
|
|
23
|
+
import { Agent } from "@mastra/core/agent";
|
|
24
|
+
import { Memory } from "@mastra/memory";
|
|
25
|
+
import { openai } from "@ai-sdk/openai";
|
|
26
|
+
|
|
27
|
+
const agent = new Agent({
|
|
28
|
+
name: "SupportAgent",
|
|
29
|
+
instructions: "You are a helpful support agent.",
|
|
30
|
+
model: openai("gpt-4o"),
|
|
31
|
+
memory: new Memory(),
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Recall configuration
|
|
36
|
+
|
|
37
|
+
The two main parameters that control semantic recall behavior are:
|
|
38
|
+
|
|
39
|
+
1. **topK**: How many semantically similar messages to retrieve
|
|
40
|
+
2. **messageRange**: How much surrounding context to include with each match
|
|
41
|
+
|
|
42
|
+
```typescript {5-6}
|
|
43
|
+
const agent = new Agent({
|
|
44
|
+
memory: new Memory({
|
|
45
|
+
options: {
|
|
46
|
+
semanticRecall: {
|
|
47
|
+
topK: 3, // Retrieve 3 most similar messages
|
|
48
|
+
messageRange: 2, // Include 2 messages before and after each match
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
}),
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Storage configuration
|
|
56
|
+
|
|
57
|
+
Semantic recall relies on a [storage and vector db](/reference/memory/Memory#parameters) to store messages and their embeddings.
|
|
58
|
+
|
|
59
|
+
```ts {8-17}
|
|
60
|
+
import { Memory } from "@mastra/memory";
|
|
61
|
+
import { Agent } from "@mastra/core/agent";
|
|
62
|
+
import { LibSQLStore } from "@mastra/core/storage/libsql";
|
|
63
|
+
import { LibSQLVector } from "@mastra/core/vector/libsql";
|
|
64
|
+
|
|
65
|
+
const agent = new Agent({
|
|
66
|
+
memory: new Memory({
|
|
67
|
+
// this is the default storage db if omitted
|
|
68
|
+
storage: new LibSQLStore({
|
|
69
|
+
config: {
|
|
70
|
+
url: "file:local.db",
|
|
71
|
+
},
|
|
72
|
+
}),
|
|
73
|
+
// this is the default vector db if omitted
|
|
74
|
+
vector: new LibSQLVector({
|
|
75
|
+
connectionUrl: "file:local.db",
|
|
76
|
+
}),
|
|
77
|
+
}),
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Storage/vector code Examples**:
|
|
82
|
+
|
|
83
|
+
- [LibSQL](/examples/memory/memory-with-libsql)
|
|
84
|
+
- [Postgres](/examples/memory/memory-with-pg)
|
|
85
|
+
- [Upstash](/examples/memory/memory-with-upstash)
|
|
86
|
+
|
|
87
|
+
### Embedder configuration
|
|
88
|
+
|
|
89
|
+
Semantic recall relies an [embedding model](/reference/memory/Memory#embedder) to convert messages into embeddings. By default Mastra uses FastEmbed but you can specify another [embedding model](https://sdk.vercel.ai/docs/ai-sdk-core/embeddings).
|
|
90
|
+
|
|
91
|
+
```ts {7}
|
|
92
|
+
import { Memory } from "@mastra/memory";
|
|
93
|
+
import { Agent } from "@mastra/core/agent";
|
|
94
|
+
import { openai } from "@ai-sdk/openai";
|
|
95
|
+
|
|
96
|
+
const agent = new Agent({
|
|
97
|
+
memory: new Memory({
|
|
98
|
+
embedder: openai.embedding("text-embedding-3-small"),
|
|
99
|
+
}),
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Disabling
|
|
104
|
+
|
|
105
|
+
There is a performance impact to using semantic recall. New messages are converted into embeddings and used to query a vector database before new messages are sent to the LLM.
|
|
106
|
+
|
|
107
|
+
Semantic recall is enabled by default but can be disabled when not needed:
|
|
108
|
+
|
|
109
|
+
```typescript {4}
|
|
110
|
+
const agent = new Agent({
|
|
111
|
+
memory: new Memory({
|
|
112
|
+
options: {
|
|
113
|
+
semanticRecall: false,
|
|
114
|
+
},
|
|
115
|
+
}),
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
You might want to disable semantic recall in scenarios like:
|
|
120
|
+
|
|
121
|
+
- When [conversation history](./getting-started.mdx#conversation-history-last-messages) provide sufficient context for the current conversation.
|
|
122
|
+
- In performance-sensitive applications, like realtime two-way audio, where the added latency of creating embeddings and running vector queries is noticeable.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import YouTube from "../../../components/youtube";
|
|
2
|
+
|
|
3
|
+
# Continuously Relevant Information
|
|
4
|
+
|
|
5
|
+
While [conversation history](/docs/memory/overview#conversation-history) and [semantic recall](./semantic-recall.mdx) help agents remember conversations, working memory allows them to maintain persistent information about users across interactions within a thread.
|
|
6
|
+
|
|
7
|
+
Think of it as the agent's active thoughts or scratchpad – the key information they keep available about the user or task. It's similar to how a person would naturally remember someone's name, preferences, or important details during a conversation.
|
|
8
|
+
|
|
9
|
+
This is useful for maintaining ongoing state that's always relevant and should always be available to the agent.
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
Here's a minimal example of setting up an agent with working memory:
|
|
14
|
+
|
|
15
|
+
```typescript {12-15}
|
|
16
|
+
import { Agent } from "@mastra/core/agent";
|
|
17
|
+
import { Memory } from "@mastra/memory";
|
|
18
|
+
import { openai } from "@ai-sdk/openai";
|
|
19
|
+
|
|
20
|
+
// Create agent with working memory enabled
|
|
21
|
+
const agent = new Agent({
|
|
22
|
+
name: "PersonalAssistant",
|
|
23
|
+
instructions: "You are a helpful personal assistant.",
|
|
24
|
+
model: openai("gpt-4o"),
|
|
25
|
+
memory: new Memory({
|
|
26
|
+
options: {
|
|
27
|
+
workingMemory: {
|
|
28
|
+
enabled: true,
|
|
29
|
+
use: "tool-call", // Recommended setting
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
}),
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## How it Works
|
|
37
|
+
|
|
38
|
+
Working memory is a block of Markdown text that the agent is able to update over time to store continuously relevant information:
|
|
39
|
+
|
|
40
|
+
<YouTube id="ik-ld_XA96s" />
|
|
41
|
+
|
|
42
|
+
## Custom Templates
|
|
43
|
+
|
|
44
|
+
Templates guide the agent on what information to track and update in working memory. While a default template is used if none is provided, you'll typically want to define a custom template tailored to your agent's specific use case to ensure it remembers the most relevant information.
|
|
45
|
+
|
|
46
|
+
Here's an example of a custom template. In this example the agent will store the users name, location, timezone, etc as soon as the user sends a message containing any of the info:
|
|
47
|
+
|
|
48
|
+
```typescript {5-28}
|
|
49
|
+
const memory = new Memory({
|
|
50
|
+
options: {
|
|
51
|
+
workingMemory: {
|
|
52
|
+
enabled: true,
|
|
53
|
+
template: `
|
|
54
|
+
# User Profile
|
|
55
|
+
|
|
56
|
+
## Personal Info
|
|
57
|
+
|
|
58
|
+
- Name:
|
|
59
|
+
- Location:
|
|
60
|
+
- Timezone:
|
|
61
|
+
|
|
62
|
+
## Preferences
|
|
63
|
+
|
|
64
|
+
- Communication Style: [e.g., Formal, Casual]
|
|
65
|
+
- Project Goal:
|
|
66
|
+
- Key Deadlines:
|
|
67
|
+
- [Deadline 1]: [Date]
|
|
68
|
+
- [Deadline 2]: [Date]
|
|
69
|
+
|
|
70
|
+
## Session State
|
|
71
|
+
|
|
72
|
+
- Last Task Discussed:
|
|
73
|
+
- Open Questions:
|
|
74
|
+
- [Question 1]
|
|
75
|
+
- [Question 2]
|
|
76
|
+
`,
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
If your agent is not properly updating working memory when you expect it to, you can add system instructions on _how_ and _when_ to use this template in your agents `instruction` setting.
|
|
83
|
+
|
|
84
|
+
## Examples
|
|
85
|
+
|
|
86
|
+
- [Streaming working memory](/examples/memory/streaming-working-memory)
|
|
87
|
+
- [Using a working memory template](/examples/memory/streaming-working-memory-advanced)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Logging | Mastra Observability Documentation"
|
|
3
|
+
description: Documentation on effective logging in Mastra, crucial for understanding application behavior and improving AI accuracy.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import Image from "next/image";
|
|
7
|
+
|
|
8
|
+
# Logging
|
|
9
|
+
|
|
10
|
+
In Mastra, logs can detail when certain functions run, what input data they receive, and how they respond.
|
|
11
|
+
|
|
12
|
+
## Basic Setup
|
|
13
|
+
|
|
14
|
+
Here's a minimal example that sets up a **console logger** at the `INFO` level. This will print out informational messages and above (i.e., `DEBUG`, `INFO`, `WARN`, `ERROR`) to the console.
|
|
15
|
+
|
|
16
|
+
```typescript filename="mastra.config.ts" showLineNumbers copy
|
|
17
|
+
import { Mastra } from "@mastra/core";
|
|
18
|
+
import { createLogger } from "@mastra/core/logger";
|
|
19
|
+
|
|
20
|
+
export const mastra = new Mastra({
|
|
21
|
+
// Other Mastra configuration...
|
|
22
|
+
logger: createLogger({
|
|
23
|
+
name: "Mastra",
|
|
24
|
+
level: "info",
|
|
25
|
+
}),
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
In this configuration:
|
|
30
|
+
|
|
31
|
+
- `name: "Mastra"` specifies the name to group logs under.
|
|
32
|
+
- `level: "info"` sets the minimum severity of logs to record.
|
|
33
|
+
|
|
34
|
+
## Configuration
|
|
35
|
+
|
|
36
|
+
- For more details on the options you can pass to `createLogger()`, see the [createLogger reference documentation](/docs/reference/observability/create-logger.mdx).
|
|
37
|
+
- Once you have a `Logger` instance, you can call its methods (e.g., `.info()`, `.warn()`, `.error()`) in the [Logger instance reference documentation](/docs/reference/observability/logger.mdx).
|
|
38
|
+
- If you want to send your logs to an external service for centralized collection, analysis, or storage, you can configure other logger types such as Upstash Redis. Consult the [createLogger reference documentation](/docs/reference/observability/create-logger.mdx) for details on parameters like `url`, `token`, and `key` when using the `UPSTASH` logger type.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Next.js Tracing | Mastra Observability Documentation"
|
|
3
|
+
description: "Set up OpenTelemetry tracing for Next.js applications"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Next.js Tracing
|
|
7
|
+
|
|
8
|
+
Next.js requires additional configuration to enable OpenTelemetry tracing.
|
|
9
|
+
|
|
10
|
+
### Step 1: Next.js Configuration
|
|
11
|
+
|
|
12
|
+
Start by enabling the instrumentation hook in your Next.js config:
|
|
13
|
+
|
|
14
|
+
```ts filename="next.config.ts" showLineNumbers copy
|
|
15
|
+
import type { NextConfig } from "next";
|
|
16
|
+
|
|
17
|
+
const nextConfig: NextConfig = {
|
|
18
|
+
experimental: {
|
|
19
|
+
instrumentationHook: true // Not required in Next.js 15+
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default nextConfig;
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Step 2: Mastra Configuration
|
|
27
|
+
|
|
28
|
+
Configure your Mastra instance:
|
|
29
|
+
|
|
30
|
+
```typescript filename="mastra.config.ts" copy
|
|
31
|
+
import { Mastra } from "@mastra/core";
|
|
32
|
+
|
|
33
|
+
export const mastra = new Mastra({
|
|
34
|
+
// ... other config
|
|
35
|
+
telemetry: {
|
|
36
|
+
serviceName: "your-project-name",
|
|
37
|
+
enabled: true
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Step 3: Configure your providers
|
|
43
|
+
|
|
44
|
+
If you're using Next.js, you have two options for setting up OpenTelemetry instrumentation:
|
|
45
|
+
|
|
46
|
+
#### Option 1: Using a Custom Exporter
|
|
47
|
+
|
|
48
|
+
The default that will work across providers is to configure a custom exporter:
|
|
49
|
+
|
|
50
|
+
1. Install the required dependencies (example using Langfuse):
|
|
51
|
+
|
|
52
|
+
```bash copy
|
|
53
|
+
npm install @opentelemetry/api langfuse-vercel
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
2. Create an instrumentation file:
|
|
57
|
+
|
|
58
|
+
```ts filename="instrumentation.ts" copy
|
|
59
|
+
import {
|
|
60
|
+
NodeSDK,
|
|
61
|
+
ATTR_SERVICE_NAME,
|
|
62
|
+
Resource,
|
|
63
|
+
} from '@mastra/core/telemetry/otel-vendor';
|
|
64
|
+
import { LangfuseExporter } from 'langfuse-vercel';
|
|
65
|
+
|
|
66
|
+
export function register() {
|
|
67
|
+
const exporter = new LangfuseExporter({
|
|
68
|
+
// ... Langfuse config
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
const sdk = new NodeSDK({
|
|
72
|
+
resource: new Resource({
|
|
73
|
+
[ATTR_SERVICE_NAME]: 'ai',
|
|
74
|
+
}),
|
|
75
|
+
traceExporter: exporter,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
sdk.start();
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
#### Option 2: Using Vercel's Otel Setup
|
|
83
|
+
|
|
84
|
+
If you're deploying to Vercel, you can use their OpenTelemetry setup:
|
|
85
|
+
|
|
86
|
+
1. Install the required dependencies:
|
|
87
|
+
|
|
88
|
+
```bash copy
|
|
89
|
+
npm install @opentelemetry/api @vercel/otel
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
2. Create an instrumentation file at the root of your project (or in the src folder if using one):
|
|
93
|
+
|
|
94
|
+
```ts filename="instrumentation.ts" copy
|
|
95
|
+
import { registerOTel } from '@vercel/otel'
|
|
96
|
+
|
|
97
|
+
export function register() {
|
|
98
|
+
registerOTel({ serviceName: 'your-project-name' })
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Summary
|
|
103
|
+
|
|
104
|
+
This setup will enable OpenTelemetry tracing for your Next.js application and Mastra operations.
|
|
105
|
+
|
|
106
|
+
For more details, see the documentation for:
|
|
107
|
+
- [Next.js Instrumentation](https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation)
|
|
108
|
+
- [Vercel OpenTelemetry](https://vercel.com/docs/observability/otel-overview/quickstart)
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Tracing | Mastra Observability Documentation"
|
|
3
|
+
description: "Set up OpenTelemetry tracing for Mastra applications"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import Image from "next/image";
|
|
7
|
+
|
|
8
|
+
# Tracing
|
|
9
|
+
|
|
10
|
+
Mastra supports the OpenTelemetry Protocol (OTLP) for tracing and monitoring your application. When telemetry is enabled, Mastra automatically traces all core primitives including agent operations, LLM interactions, tool executions, integration calls, workflow runs, and database operations. Your telemetry data can then be exported to any OTEL collector.
|
|
11
|
+
|
|
12
|
+
### Basic Configuration
|
|
13
|
+
|
|
14
|
+
Here's a simple example of enabling telemetry:
|
|
15
|
+
|
|
16
|
+
```ts filename="mastra.config.ts" showLineNumbers copy
|
|
17
|
+
export const mastra = new Mastra({
|
|
18
|
+
// ... other config
|
|
19
|
+
telemetry: {
|
|
20
|
+
serviceName: "my-app",
|
|
21
|
+
enabled: true,
|
|
22
|
+
sampling: {
|
|
23
|
+
type: "always_on",
|
|
24
|
+
},
|
|
25
|
+
export: {
|
|
26
|
+
type: "otlp",
|
|
27
|
+
endpoint: "http://localhost:4318", // SigNoz local endpoint
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Configuration Options
|
|
34
|
+
|
|
35
|
+
The telemetry config accepts these properties:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
type OtelConfig = {
|
|
39
|
+
// Name to identify your service in traces (optional)
|
|
40
|
+
serviceName?: string;
|
|
41
|
+
|
|
42
|
+
// Enable/disable telemetry (defaults to true)
|
|
43
|
+
enabled?: boolean;
|
|
44
|
+
|
|
45
|
+
// Control how many traces are sampled
|
|
46
|
+
sampling?: {
|
|
47
|
+
type: "ratio" | "always_on" | "always_off" | "parent_based";
|
|
48
|
+
probability?: number; // For ratio sampling
|
|
49
|
+
root?: {
|
|
50
|
+
probability: number; // For parent_based sampling
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// Where to send telemetry data
|
|
55
|
+
export?: {
|
|
56
|
+
type: "otlp" | "console";
|
|
57
|
+
endpoint?: string;
|
|
58
|
+
headers?: Record<string, string>;
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
See the [OtelConfig reference documentation](/docs/reference/observability/otel-config.mdx) for more details.
|
|
64
|
+
|
|
65
|
+
### Environment Variables
|
|
66
|
+
|
|
67
|
+
You can configure the OTLP endpoint and headers through environment variables:
|
|
68
|
+
|
|
69
|
+
```env filename=".env" copy
|
|
70
|
+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
|
|
71
|
+
OTEL_EXPORTER_OTLP_HEADERS=x-api-key=your-api-key
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Then in your config:
|
|
75
|
+
|
|
76
|
+
```ts filename="mastra.config.ts" showLineNumbers copy
|
|
77
|
+
export const mastra = new Mastra({
|
|
78
|
+
// ... other config
|
|
79
|
+
telemetry: {
|
|
80
|
+
serviceName: "my-app",
|
|
81
|
+
enabled: true,
|
|
82
|
+
export: {
|
|
83
|
+
type: "otlp",
|
|
84
|
+
// endpoint and headers will be picked up from env vars
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Example: SigNoz Integration
|
|
91
|
+
|
|
92
|
+
Here's what a traced agent interaction looks like in [SigNoz](https://signoz.io):
|
|
93
|
+
|
|
94
|
+
<img
|
|
95
|
+
src="/docs/signoz-telemetry-demo.png"
|
|
96
|
+
alt="Agent interaction trace showing spans, LLM calls, and tool executions"
|
|
97
|
+
style={{ maxWidth: "800px", width: "100%", margin: "8px 0" }}
|
|
98
|
+
className="nextra-image rounded-md"
|
|
99
|
+
data-zoom
|
|
100
|
+
width={800}
|
|
101
|
+
height={400}
|
|
102
|
+
/>
|
|
103
|
+
|
|
104
|
+
### Other Supported Providers
|
|
105
|
+
|
|
106
|
+
For a complete list of supported observability providers and their configuration details, see the [Observability Providers reference](../reference/observability/providers/).
|
|
107
|
+
|
|
108
|
+
### Next.js-specific Tracing steps
|
|
109
|
+
|
|
110
|
+
If you're using Next.js, you have three additional configuration steps:
|
|
111
|
+
1. Enable the instrumentation hook in `next.config.ts`
|
|
112
|
+
2. Configure Mastra telemetry settings
|
|
113
|
+
3. Set up an OpenTelemetry exporter
|
|
114
|
+
|
|
115
|
+
For implementation details, see the [Next.js Tracing](./nextjs-tracing) guide.
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Chunking and Embedding Documents | RAG | Mastra Docs
|
|
3
|
+
description: Guide on chunking and embedding documents in Mastra for efficient processing and retrieval.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Chunking and Embedding Documents
|
|
7
|
+
|
|
8
|
+
Before processing, create a MDocument instance from your content. You can initialize it from various formats:
|
|
9
|
+
|
|
10
|
+
```ts showLineNumbers copy
|
|
11
|
+
const docFromText = MDocument.fromText("Your plain text content...");
|
|
12
|
+
const docFromHTML = MDocument.fromHTML("<html>Your HTML content...</html>");
|
|
13
|
+
const docFromMarkdown = MDocument.fromMarkdown("# Your Markdown content...");
|
|
14
|
+
const docFromJSON = MDocument.fromJSON(`{ "key": "value" }`);
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Step 1: Document Processing
|
|
18
|
+
|
|
19
|
+
Use `chunk` to split documents into manageable pieces. Mastra supports multiple chunking strategies optimized for different document types:
|
|
20
|
+
|
|
21
|
+
- `recursive`: Smart splitting based on content structure
|
|
22
|
+
- `character`: Simple character-based splits
|
|
23
|
+
- `token`: Token-aware splitting
|
|
24
|
+
- `markdown`: Markdown-aware splitting
|
|
25
|
+
- `html`: HTML structure-aware splitting
|
|
26
|
+
- `json`: JSON structure-aware splitting
|
|
27
|
+
- `latex`: LaTeX structure-aware splitting
|
|
28
|
+
|
|
29
|
+
Here's an example of how to use the `recursive` strategy:
|
|
30
|
+
|
|
31
|
+
```ts showLineNumbers copy
|
|
32
|
+
const chunks = await doc.chunk({
|
|
33
|
+
strategy: "recursive",
|
|
34
|
+
size: 512,
|
|
35
|
+
overlap: 50,
|
|
36
|
+
separator: "\n",
|
|
37
|
+
extract: {
|
|
38
|
+
metadata: true, // Optionally extract metadata
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**Note:** Metadata extraction may use LLM calls, so ensure your API key is set.
|
|
44
|
+
|
|
45
|
+
We go deeper into chunking strategies in our [chunk documentation](/docs/reference/rag/chunk.mdx).
|
|
46
|
+
|
|
47
|
+
## Step 2: Embedding Generation
|
|
48
|
+
|
|
49
|
+
Transform chunks into embeddings using your preferred provider. Mastra supports many embedding providers, including OpenAI and Cohere:
|
|
50
|
+
|
|
51
|
+
### Using OpenAI
|
|
52
|
+
|
|
53
|
+
```ts showLineNumbers copy
|
|
54
|
+
import { openai } from "@ai-sdk/openai";
|
|
55
|
+
import { embedMany } from "ai";
|
|
56
|
+
|
|
57
|
+
const { embeddings } = await embedMany({
|
|
58
|
+
model: openai.embedding('text-embedding-3-small'),
|
|
59
|
+
values: chunks.map(chunk => chunk.text),
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Using Cohere
|
|
64
|
+
|
|
65
|
+
```ts showLineNumbers copy
|
|
66
|
+
import { cohere } from '@ai-sdk/cohere';
|
|
67
|
+
import { embedMany } from 'ai';
|
|
68
|
+
|
|
69
|
+
const { embeddings } = await embedMany({
|
|
70
|
+
model: cohere.embedding('embed-english-v3.0'),
|
|
71
|
+
values: chunks.map(chunk => chunk.text),
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The embedding functions return vectors, arrays of numbers representing the semantic meaning of your text, ready for similarity searches in your vector database.
|
|
76
|
+
|
|
77
|
+
### Configuring Embedding Dimensions
|
|
78
|
+
|
|
79
|
+
Embedding models typically output vectors with a fixed number of dimensions (e.g., 1536 for OpenAI's `text-embedding-3-small`).
|
|
80
|
+
Some models support reducing this dimensionality, which can help:
|
|
81
|
+
- Decrease storage requirements in vector databases
|
|
82
|
+
- Reduce computational costs for similarity searches
|
|
83
|
+
|
|
84
|
+
Here are some supported models:
|
|
85
|
+
|
|
86
|
+
OpenAI (text-embedding-3 models):
|
|
87
|
+
```ts
|
|
88
|
+
const { embeddings } = await embedMany({
|
|
89
|
+
model: openai.embedding('text-embedding-3-small', {
|
|
90
|
+
dimensions: 256 // Only supported in text-embedding-3 and later
|
|
91
|
+
}),
|
|
92
|
+
values: chunks.map(chunk => chunk.text),
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Google (text-embedding-004):
|
|
97
|
+
```ts
|
|
98
|
+
const { embeddings } = await embedMany({
|
|
99
|
+
model: google.textEmbeddingModel('text-embedding-004', {
|
|
100
|
+
outputDimensionality: 256 // Truncates excessive values from the end
|
|
101
|
+
}),
|
|
102
|
+
values: chunks.map(chunk => chunk.text),
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Example: Complete Pipeline
|
|
107
|
+
|
|
108
|
+
Here's an example showing document processing and embedding generation with both providers:
|
|
109
|
+
|
|
110
|
+
```ts showLineNumbers copy
|
|
111
|
+
import { embedMany } from "ai";
|
|
112
|
+
import { openai } from "@ai-sdk/openai";
|
|
113
|
+
import { cohere } from "@ai-sdk/cohere";
|
|
114
|
+
|
|
115
|
+
import { MDocument } from "@mastra/rag";
|
|
116
|
+
|
|
117
|
+
// Initialize document
|
|
118
|
+
const doc = MDocument.fromText(`
|
|
119
|
+
Climate change poses significant challenges to global agriculture.
|
|
120
|
+
Rising temperatures and changing precipitation patterns affect crop yields.
|
|
121
|
+
`);
|
|
122
|
+
|
|
123
|
+
// Create chunks
|
|
124
|
+
const chunks = await doc.chunk({
|
|
125
|
+
strategy: "recursive",
|
|
126
|
+
size: 256,
|
|
127
|
+
overlap: 50,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Generate embeddings with OpenAI
|
|
131
|
+
const { embeddings: openAIEmbeddings } = await embedMany({
|
|
132
|
+
model: openai.embedding('text-embedding-3-small'),
|
|
133
|
+
values: chunks.map(chunk => chunk.text),
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// OR
|
|
137
|
+
|
|
138
|
+
// Generate embeddings with Cohere
|
|
139
|
+
const { embeddings: cohereEmbeddings } = await embedMany({
|
|
140
|
+
model: cohere.embedding('embed-english-v3.0'),
|
|
141
|
+
values: chunks.map(chunk => chunk.text),
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Store embeddings in your vector database
|
|
145
|
+
await vectorStore.upsert({
|
|
146
|
+
indexName: "embeddings",
|
|
147
|
+
vectors: embeddings,
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
##
|
|
152
|
+
For more examples of different chunking strategies and embedding configurations, see:
|
|
153
|
+
|
|
154
|
+
- [Adjust Chunk Size](/docs/reference/rag/chunk.mdx#adjust-chunk-size)
|
|
155
|
+
- [Adjust Chunk Delimiters](/docs/reference/rag/chunk.mdx#adjust-chunk-delimiters)
|
|
156
|
+
- [Embed Text with Cohere](/docs/reference/rag/embeddings.mdx#using-cohere)
|