@mastra/mcp-docs-server 0.0.0-commonjs-20250414101718
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 +161 -0
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +110 -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-registry-registry.md +26 -0
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fmem0.md +196 -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 +250 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +250 -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 +302 -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 +388 -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/client-side-tools.md +69 -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/mcp-registry-registry.md +63 -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 +317 -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 +215 -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 +127 -0
- package/.docs/raw/deployment/server.mdx +282 -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 +442 -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 +334 -0
- package/.docs/raw/reference/agents/getAgent.mdx +54 -0
- package/.docs/raw/reference/agents/stream.mdx +369 -0
- package/.docs/raw/reference/cli/build.mdx +55 -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 +207 -0
- package/.docs/raw/reference/deployer/deployer.mdx +159 -0
- package/.docs/raw/reference/deployer/netlify.mdx +109 -0
- package/.docs/raw/reference/deployer/vercel.mdx +117 -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 +12 -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 +207 -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 +203 -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.md +46 -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,365 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Retrieval, Semantic Search, Reranking | RAG | Mastra Docs"
|
|
3
|
+
description: Guide on retrieval processes in Mastra's RAG systems, including semantic search, filtering, and re-ranking.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { Tabs } from "nextra/components";
|
|
7
|
+
|
|
8
|
+
## Retrieval in RAG Systems
|
|
9
|
+
|
|
10
|
+
After storing embeddings, you need to retrieve relevant chunks to answer user queries.
|
|
11
|
+
|
|
12
|
+
Mastra provides flexible retrieval options with support for semantic search, filtering, and re-ranking.
|
|
13
|
+
|
|
14
|
+
## How Retrieval Works
|
|
15
|
+
|
|
16
|
+
1. The user's query is converted to an embedding using the same model used for document embeddings
|
|
17
|
+
2. This embedding is compared to stored embeddings using vector similarity
|
|
18
|
+
3. The most similar chunks are retrieved and can be optionally:
|
|
19
|
+
- Filtered by metadata
|
|
20
|
+
- Re-ranked for better relevance
|
|
21
|
+
- Processed through a knowledge graph
|
|
22
|
+
|
|
23
|
+
## Basic Retrieval
|
|
24
|
+
|
|
25
|
+
The simplest approach is direct semantic search. This method uses vector similarity to find chunks that are semantically similar to the query:
|
|
26
|
+
|
|
27
|
+
```ts showLineNumbers copy
|
|
28
|
+
import { openai } from "@ai-sdk/openai";
|
|
29
|
+
import { embed } from "ai";
|
|
30
|
+
import { PgVector } from "@mastra/pg";
|
|
31
|
+
|
|
32
|
+
// Convert query to embedding
|
|
33
|
+
const { embedding } = await embed({
|
|
34
|
+
value: "What are the main points in the article?",
|
|
35
|
+
model: openai.embedding('text-embedding-3-small'),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Query vector store
|
|
39
|
+
const pgVector = new PgVector(process.env.POSTGRES_CONNECTION_STRING);
|
|
40
|
+
const results = await pgVector.query({
|
|
41
|
+
indexName: "embeddings",
|
|
42
|
+
queryVector: embedding,
|
|
43
|
+
topK: 10,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Display results
|
|
47
|
+
console.log(results);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Results include both the text content and a similarity score:
|
|
51
|
+
|
|
52
|
+
```ts showLineNumbers copy
|
|
53
|
+
[
|
|
54
|
+
{
|
|
55
|
+
text: "Climate change poses significant challenges...",
|
|
56
|
+
score: 0.89,
|
|
57
|
+
metadata: { source: "article1.txt" }
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
text: "Rising temperatures affect crop yields...",
|
|
61
|
+
score: 0.82,
|
|
62
|
+
metadata: { source: "article1.txt" }
|
|
63
|
+
}
|
|
64
|
+
// ... more results
|
|
65
|
+
]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
For an example of how to use the basic retrieval method, see the [Retrieve Results](../../examples/rag/query/retrieve-results.mdx) example.
|
|
69
|
+
|
|
70
|
+
## Advanced Retrieval options
|
|
71
|
+
|
|
72
|
+
### Metadata Filtering
|
|
73
|
+
|
|
74
|
+
Filter results based on metadata fields to narrow down the search space. This is useful when you have documents from different sources, time periods, or with specific attributes. Mastra provides a unified MongoDB-style query syntax that works across all supported vector stores.
|
|
75
|
+
|
|
76
|
+
For detailed information about available operators and syntax, see the [Metadata Filters Reference](/reference/rag/metadata-filters).
|
|
77
|
+
|
|
78
|
+
Basic filtering examples:
|
|
79
|
+
|
|
80
|
+
```ts showLineNumbers copy
|
|
81
|
+
// Simple equality filter
|
|
82
|
+
const results = await pgVector.query({
|
|
83
|
+
indexName: "embeddings",
|
|
84
|
+
queryVector: embedding,
|
|
85
|
+
topK: 10,
|
|
86
|
+
filter: {
|
|
87
|
+
source: "article1.txt"
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Numeric comparison
|
|
92
|
+
const results = await pgVector.query({
|
|
93
|
+
indexName: "embeddings",
|
|
94
|
+
queryVector: embedding,
|
|
95
|
+
topK: 10,
|
|
96
|
+
filter: {
|
|
97
|
+
price: { $gt: 100 }
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Multiple conditions
|
|
102
|
+
const results = await pgVector.query({
|
|
103
|
+
indexName: "embeddings",
|
|
104
|
+
queryVector: embedding,
|
|
105
|
+
topK: 10,
|
|
106
|
+
filter: {
|
|
107
|
+
category: "electronics",
|
|
108
|
+
price: { $lt: 1000 },
|
|
109
|
+
inStock: true
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Array operations
|
|
114
|
+
const results = await pgVector.query({
|
|
115
|
+
indexName: "embeddings",
|
|
116
|
+
queryVector: embedding,
|
|
117
|
+
topK: 10,
|
|
118
|
+
filter: {
|
|
119
|
+
tags: { $in: ["sale", "new"] }
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// Logical operators
|
|
124
|
+
const results = await pgVector.query({
|
|
125
|
+
indexName: "embeddings",
|
|
126
|
+
queryVector: embedding,
|
|
127
|
+
topK: 10,
|
|
128
|
+
filter: {
|
|
129
|
+
$or: [
|
|
130
|
+
{ category: "electronics" },
|
|
131
|
+
{ category: "accessories" }
|
|
132
|
+
],
|
|
133
|
+
$and: [
|
|
134
|
+
{ price: { $gt: 50 } },
|
|
135
|
+
{ price: { $lt: 200 } }
|
|
136
|
+
]
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Common use cases for metadata filtering:
|
|
142
|
+
- Filter by document source or type
|
|
143
|
+
- Filter by date ranges
|
|
144
|
+
- Filter by specific categories or tags
|
|
145
|
+
- Filter by numerical ranges (e.g., price, rating)
|
|
146
|
+
- Combine multiple conditions for precise querying
|
|
147
|
+
- Filter by document attributes (e.g., language, author)
|
|
148
|
+
|
|
149
|
+
For an example of how to use metadata filtering, see the [Hybrid Vector Search](../../examples/rag/query/hybrid-vector-search.mdx) example.
|
|
150
|
+
|
|
151
|
+
### Vector Query Tool
|
|
152
|
+
|
|
153
|
+
Sometimes you want to give your agent the ability to query a vector database directly. The Vector Query Tool allows your agent to be in charge of retrieval decisions, combining semantic search with optional filtering and reranking based on the agent's understanding of the user's needs.
|
|
154
|
+
|
|
155
|
+
```ts showLineNumbers copy
|
|
156
|
+
const vectorQueryTool = createVectorQueryTool({
|
|
157
|
+
vectorStoreName: 'pgVector',
|
|
158
|
+
indexName: 'embeddings',
|
|
159
|
+
model: openai.embedding('text-embedding-3-small'),
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
When creating the tool, pay special attention to the tool's name and description - these help the agent understand when and how to use the retrieval capabilities. For example, you might name it "SearchKnowledgeBase" and describe it as "Search through our documentation to find relevant information about X topic."
|
|
164
|
+
|
|
165
|
+
This is particularly useful when:
|
|
166
|
+
- Your agent needs to dynamically decide what information to retrieve
|
|
167
|
+
- The retrieval process requires complex decision-making
|
|
168
|
+
- You want the agent to combine multiple retrieval strategies based on context
|
|
169
|
+
|
|
170
|
+
For detailed configuration options and advanced usage, see the [Vector Query Tool Reference](/reference/tools/vector-query-tool).
|
|
171
|
+
|
|
172
|
+
### Vector Store Prompts
|
|
173
|
+
|
|
174
|
+
Vector store prompts define query patterns and filtering capabilities for each vector database implementation.
|
|
175
|
+
When implementing filtering, these prompts are required in the agent's instructions to specify valid operators and syntax for each vector store implementation.
|
|
176
|
+
|
|
177
|
+
<Tabs items={['Pg Vector', 'Pinecone', 'Qdrant', 'Chroma', 'Astra', 'LibSQL', 'Upstash', 'Cloudflare']}>
|
|
178
|
+
<Tabs.Tab>
|
|
179
|
+
```ts showLineNumbers copy
|
|
180
|
+
import { openai } from '@ai-sdk/openai';
|
|
181
|
+
import { PGVECTOR_PROMPT } from "@mastra/rag";
|
|
182
|
+
|
|
183
|
+
export const ragAgent = new Agent({
|
|
184
|
+
name: 'RAG Agent',
|
|
185
|
+
model: openai('gpt-4o-mini'),
|
|
186
|
+
instructions: `
|
|
187
|
+
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
188
|
+
${PGVECTOR_PROMPT}
|
|
189
|
+
`,
|
|
190
|
+
tools: { vectorQueryTool },
|
|
191
|
+
});
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
</Tabs.Tab>
|
|
195
|
+
<Tabs.Tab>
|
|
196
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
197
|
+
import { openai } from '@ai-sdk/openai';
|
|
198
|
+
import { PINECONE_PROMPT } from "@mastra/rag";
|
|
199
|
+
|
|
200
|
+
export const ragAgent = new Agent({
|
|
201
|
+
name: 'RAG Agent',
|
|
202
|
+
model: openai('gpt-4o-mini'),
|
|
203
|
+
instructions: `
|
|
204
|
+
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
205
|
+
${PINECONE_PROMPT}
|
|
206
|
+
`,
|
|
207
|
+
tools: { vectorQueryTool },
|
|
208
|
+
});
|
|
209
|
+
```
|
|
210
|
+
</Tabs.Tab>
|
|
211
|
+
<Tabs.Tab>
|
|
212
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
213
|
+
import { openai } from '@ai-sdk/openai';
|
|
214
|
+
import { QDRANT_PROMPT } from "@mastra/rag";
|
|
215
|
+
|
|
216
|
+
export const ragAgent = new Agent({
|
|
217
|
+
name: 'RAG Agent',
|
|
218
|
+
model: openai('gpt-4o-mini'),
|
|
219
|
+
instructions: `
|
|
220
|
+
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
221
|
+
${QDRANT_PROMPT}
|
|
222
|
+
`,
|
|
223
|
+
tools: { vectorQueryTool },
|
|
224
|
+
});
|
|
225
|
+
```
|
|
226
|
+
</Tabs.Tab>
|
|
227
|
+
<Tabs.Tab>
|
|
228
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
229
|
+
import { openai } from '@ai-sdk/openai';
|
|
230
|
+
import { CHROMA_PROMPT } from "@mastra/rag";
|
|
231
|
+
|
|
232
|
+
export const ragAgent = new Agent({
|
|
233
|
+
name: 'RAG Agent',
|
|
234
|
+
model: openai('gpt-4o-mini'),
|
|
235
|
+
instructions: `
|
|
236
|
+
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
237
|
+
${CHROMA_PROMPT}
|
|
238
|
+
`,
|
|
239
|
+
tools: { vectorQueryTool },
|
|
240
|
+
});
|
|
241
|
+
```
|
|
242
|
+
</Tabs.Tab>
|
|
243
|
+
<Tabs.Tab>
|
|
244
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
245
|
+
import { openai } from '@ai-sdk/openai';
|
|
246
|
+
import { ASTRA_PROMPT } from "@mastra/rag";
|
|
247
|
+
|
|
248
|
+
export const ragAgent = new Agent({
|
|
249
|
+
name: 'RAG Agent',
|
|
250
|
+
model: openai('gpt-4o-mini'),
|
|
251
|
+
instructions: `
|
|
252
|
+
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
253
|
+
${ASTRA_PROMPT}
|
|
254
|
+
`,
|
|
255
|
+
tools: { vectorQueryTool },
|
|
256
|
+
});
|
|
257
|
+
```
|
|
258
|
+
</Tabs.Tab>
|
|
259
|
+
<Tabs.Tab>
|
|
260
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
261
|
+
import { openai } from '@ai-sdk/openai';
|
|
262
|
+
import { LIBSQL_PROMPT } from "@mastra/rag";
|
|
263
|
+
|
|
264
|
+
export const ragAgent = new Agent({
|
|
265
|
+
name: 'RAG Agent',
|
|
266
|
+
model: openai('gpt-4o-mini'),
|
|
267
|
+
instructions: `
|
|
268
|
+
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
269
|
+
${LIBSQL_PROMPT}
|
|
270
|
+
`,
|
|
271
|
+
tools: { vectorQueryTool },
|
|
272
|
+
});
|
|
273
|
+
```
|
|
274
|
+
</Tabs.Tab>
|
|
275
|
+
<Tabs.Tab>
|
|
276
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
277
|
+
import { openai } from '@ai-sdk/openai';
|
|
278
|
+
import { UPSTASH_PROMPT } from "@mastra/rag";
|
|
279
|
+
|
|
280
|
+
export const ragAgent = new Agent({
|
|
281
|
+
name: 'RAG Agent',
|
|
282
|
+
model: openai('gpt-4o-mini'),
|
|
283
|
+
instructions: `
|
|
284
|
+
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
285
|
+
${UPSTASH_PROMPT}
|
|
286
|
+
`,
|
|
287
|
+
tools: { vectorQueryTool },
|
|
288
|
+
});
|
|
289
|
+
```
|
|
290
|
+
</Tabs.Tab>
|
|
291
|
+
<Tabs.Tab>
|
|
292
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
293
|
+
import { openai } from '@ai-sdk/openai';
|
|
294
|
+
import { VECTORIZE_PROMPT } from "@mastra/rag";
|
|
295
|
+
|
|
296
|
+
export const ragAgent = new Agent({
|
|
297
|
+
name: 'RAG Agent',
|
|
298
|
+
model: openai('gpt-4o-mini'),
|
|
299
|
+
instructions: `
|
|
300
|
+
Process queries using the provided context. Structure responses to be concise and relevant.
|
|
301
|
+
${VECTORIZE_PROMPT}
|
|
302
|
+
`,
|
|
303
|
+
tools: { vectorQueryTool },
|
|
304
|
+
});
|
|
305
|
+
```
|
|
306
|
+
</Tabs.Tab>
|
|
307
|
+
</Tabs>
|
|
308
|
+
|
|
309
|
+
### Re-ranking
|
|
310
|
+
|
|
311
|
+
Initial vector similarity search can sometimes miss nuanced relevance. Re-ranking is a more computationally expensive process, but more accurate algorithm that improves results by:
|
|
312
|
+
|
|
313
|
+
- Considering word order and exact matches
|
|
314
|
+
- Applying more sophisticated relevance scoring
|
|
315
|
+
- Using a method called cross-attention between query and documents
|
|
316
|
+
|
|
317
|
+
Here's how to use re-ranking:
|
|
318
|
+
|
|
319
|
+
```ts showLineNumbers copy
|
|
320
|
+
import { openai } from "@ai-sdk/openai";
|
|
321
|
+
import { rerank } from "@mastra/rag";
|
|
322
|
+
|
|
323
|
+
// Get initial results from vector search
|
|
324
|
+
const initialResults = await pgVector.query({
|
|
325
|
+
indexName: "embeddings",
|
|
326
|
+
queryVector: queryEmbedding,
|
|
327
|
+
topK: 10,
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// Re-rank the results
|
|
331
|
+
const rerankedResults = await rerank(initialResults, query, openai('gpt-4o-mini'));
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
> **Note:** For semantic scoring to work properly during re-ranking, each result must include the text content in its `metadata.text` field.
|
|
335
|
+
|
|
336
|
+
The re-ranked results combine vector similarity with semantic understanding to improve retrieval quality.
|
|
337
|
+
|
|
338
|
+
For more details about re-ranking, see the [rerank()](/reference/rag/rerank) method.
|
|
339
|
+
|
|
340
|
+
For an example of how to use the re-ranking method, see the [Re-ranking Results](../../examples/rag/rerank/rerank.mdx) example.
|
|
341
|
+
|
|
342
|
+
### Graph-based Retrieval
|
|
343
|
+
|
|
344
|
+
For documents with complex relationships, graph-based retrieval can follow connections between chunks. This helps when:
|
|
345
|
+
|
|
346
|
+
- Information is spread across multiple documents
|
|
347
|
+
- Documents reference each other
|
|
348
|
+
- You need to traverse relationships to find complete answers
|
|
349
|
+
|
|
350
|
+
Example setup:
|
|
351
|
+
|
|
352
|
+
```ts showLineNumbers copy
|
|
353
|
+
const graphQueryTool = createGraphQueryTool({
|
|
354
|
+
vectorStoreName: 'pgVector',
|
|
355
|
+
indexName: 'embeddings',
|
|
356
|
+
model: openai.embedding('text-embedding-3-small'),
|
|
357
|
+
graphOptions: {
|
|
358
|
+
threshold: 0.7,
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
For more details about graph-based retrieval, see the [GraphRAG](/reference/rag/graph-rag) class and the [createGraphQueryTool()](/reference/tools/graph-rag-tool) function.
|
|
364
|
+
|
|
365
|
+
For an example of how to use the graph-based retrieval method, see the [Graph-based Retrieval](../../examples/rag/usage/graph-rag.mdx) example.
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Storing Embeddings in A Vector Database | Mastra Docs"
|
|
3
|
+
description: Guide on vector storage options in Mastra, including embedded and dedicated vector databases for similarity search.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { Tabs } from "nextra/components";
|
|
7
|
+
|
|
8
|
+
## Storing Embeddings in A Vector Database
|
|
9
|
+
|
|
10
|
+
After generating embeddings, you need to store them in a database that supports vector similarity search. Mastra provides a consistent interface for storing and querying embeddings across different vector databases.
|
|
11
|
+
|
|
12
|
+
## Supported Databases
|
|
13
|
+
|
|
14
|
+
<Tabs items={['Pg Vector', 'Pinecone', 'Qdrant', 'Chroma', 'Astra', 'LibSQL', 'Upstash', 'Cloudflare']}>
|
|
15
|
+
<Tabs.Tab>
|
|
16
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
17
|
+
import { PgVector } from '@mastra/pg';
|
|
18
|
+
|
|
19
|
+
const store = new PgVector(process.env.POSTGRES_CONNECTION_STRING)
|
|
20
|
+
await store.createIndex({
|
|
21
|
+
indexName: "myCollection",
|
|
22
|
+
dimension: 1536,
|
|
23
|
+
});
|
|
24
|
+
await store.upsert({
|
|
25
|
+
indexName: "myCollection",
|
|
26
|
+
vectors: embeddings,
|
|
27
|
+
metadata: chunks.map(chunk => ({ text: chunk.text })),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Using PostgreSQL with pgvector
|
|
33
|
+
|
|
34
|
+
PostgreSQL with the pgvector extension is a good solution for teams already using PostgreSQL who want to minimize infrastructure complexity.
|
|
35
|
+
For detailed setup instructions and best practices, see the [official pgvector repository](https://github.com/pgvector/pgvector).
|
|
36
|
+
</Tabs.Tab>
|
|
37
|
+
<Tabs.Tab>
|
|
38
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
39
|
+
import { PineconeVector } from '@mastra/pinecone'
|
|
40
|
+
|
|
41
|
+
const store = new PineconeVector(process.env.PINECONE_API_KEY)
|
|
42
|
+
await store.createIndex({
|
|
43
|
+
indexName: "myCollection",
|
|
44
|
+
dimension: 1536,
|
|
45
|
+
});
|
|
46
|
+
await store.upsert({
|
|
47
|
+
indexName: "myCollection",
|
|
48
|
+
vectors: embeddings,
|
|
49
|
+
metadata: chunks.map(chunk => ({ text: chunk.text })),
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
</Tabs.Tab>
|
|
53
|
+
<Tabs.Tab>
|
|
54
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
55
|
+
import { QdrantVector } from '@mastra/qdrant'
|
|
56
|
+
|
|
57
|
+
const store = new QdrantVector({
|
|
58
|
+
url: process.env.QDRANT_URL,
|
|
59
|
+
apiKey: process.env.QDRANT_API_KEY
|
|
60
|
+
})
|
|
61
|
+
await store.createIndex({
|
|
62
|
+
indexName: "myCollection",
|
|
63
|
+
dimension: 1536,
|
|
64
|
+
});
|
|
65
|
+
await store.upsert({
|
|
66
|
+
indexName: "myCollection",
|
|
67
|
+
vectors: embeddings,
|
|
68
|
+
metadata: chunks.map(chunk => ({ text: chunk.text })),
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
</Tabs.Tab>
|
|
72
|
+
<Tabs.Tab>
|
|
73
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
74
|
+
import { ChromaVector } from '@mastra/chroma'
|
|
75
|
+
|
|
76
|
+
const store = new ChromaVector()
|
|
77
|
+
await store.createIndex({
|
|
78
|
+
indexName: "myCollection",
|
|
79
|
+
dimension: 1536,
|
|
80
|
+
});
|
|
81
|
+
await store.upsert({
|
|
82
|
+
indexName: "myCollection",
|
|
83
|
+
vectors: embeddings,
|
|
84
|
+
metadata: chunks.map(chunk => ({ text: chunk.text })),
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
</Tabs.Tab>
|
|
88
|
+
<Tabs.Tab>
|
|
89
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
90
|
+
import { AstraVector } from '@mastra/astra'
|
|
91
|
+
|
|
92
|
+
const store = new AstraVector({
|
|
93
|
+
token: process.env.ASTRA_DB_TOKEN,
|
|
94
|
+
endpoint: process.env.ASTRA_DB_ENDPOINT,
|
|
95
|
+
keyspace: process.env.ASTRA_DB_KEYSPACE
|
|
96
|
+
})
|
|
97
|
+
await store.createIndex({
|
|
98
|
+
indexName: "myCollection",
|
|
99
|
+
dimension: 1536,
|
|
100
|
+
});
|
|
101
|
+
await store.upsert({
|
|
102
|
+
indexName: "myCollection",
|
|
103
|
+
vectors: embeddings,
|
|
104
|
+
metadata: chunks.map(chunk => ({ text: chunk.text })),
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
</Tabs.Tab>
|
|
108
|
+
<Tabs.Tab>
|
|
109
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
110
|
+
import { LibSQLVector } from "@mastra/core/vector/libsql";
|
|
111
|
+
|
|
112
|
+
const store = new LibSQLVector({
|
|
113
|
+
connectionUrl: process.env.DATABASE_URL,
|
|
114
|
+
authToken: process.env.DATABASE_AUTH_TOKEN // Optional: for Turso cloud databases
|
|
115
|
+
})
|
|
116
|
+
await store.createIndex({
|
|
117
|
+
indexName: "myCollection",
|
|
118
|
+
dimension: 1536,
|
|
119
|
+
});
|
|
120
|
+
await store.upsert({
|
|
121
|
+
indexName: "myCollection",
|
|
122
|
+
vectors: embeddings,
|
|
123
|
+
metadata: chunks.map(chunk => ({ text: chunk.text })),
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
</Tabs.Tab>
|
|
127
|
+
<Tabs.Tab>
|
|
128
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
129
|
+
import { UpstashVector } from '@mastra/upstash'
|
|
130
|
+
|
|
131
|
+
const store = new UpstashVector({
|
|
132
|
+
url: process.env.UPSTASH_URL,
|
|
133
|
+
token: process.env.UPSTASH_TOKEN
|
|
134
|
+
})
|
|
135
|
+
await store.createIndex({
|
|
136
|
+
indexName: "myCollection",
|
|
137
|
+
dimension: 1536,
|
|
138
|
+
});
|
|
139
|
+
await store.upsert({
|
|
140
|
+
indexName: "myCollection",
|
|
141
|
+
vectors: embeddings,
|
|
142
|
+
metadata: chunks.map(chunk => ({ text: chunk.text })),
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
</Tabs.Tab>
|
|
146
|
+
<Tabs.Tab>
|
|
147
|
+
```ts filename="vector-store.ts" showLineNumbers copy
|
|
148
|
+
import { CloudflareVector } from '@mastra/vectorize'
|
|
149
|
+
|
|
150
|
+
const store = new CloudflareVector({
|
|
151
|
+
accountId: process.env.CF_ACCOUNT_ID,
|
|
152
|
+
apiToken: process.env.CF_API_TOKEN
|
|
153
|
+
})
|
|
154
|
+
await store.createIndex({
|
|
155
|
+
indexName: "myCollection",
|
|
156
|
+
dimension: 1536,
|
|
157
|
+
});
|
|
158
|
+
await store.upsert({
|
|
159
|
+
indexName: "myCollection",
|
|
160
|
+
vectors: embeddings,
|
|
161
|
+
metadata: chunks.map(chunk => ({ text: chunk.text })),
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
</Tabs.Tab>
|
|
165
|
+
</Tabs>
|
|
166
|
+
|
|
167
|
+
## Using Vector Storage
|
|
168
|
+
|
|
169
|
+
Once initialized, all vector stores share the same interface for creating indexes, upserting embeddings, and querying.
|
|
170
|
+
|
|
171
|
+
### Creating Indexes
|
|
172
|
+
|
|
173
|
+
Before storing embeddings, you need to create an index with the appropriate dimension size for your embedding model:
|
|
174
|
+
|
|
175
|
+
```ts filename="store-embeddings.ts" showLineNumbers copy
|
|
176
|
+
// Create an index with dimension 1536 (for text-embedding-3-small)
|
|
177
|
+
await store.createIndex({
|
|
178
|
+
indexName: 'myCollection',
|
|
179
|
+
dimension: 1536,
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// For other models, use their corresponding dimensions:
|
|
183
|
+
// - text-embedding-3-large: 3072
|
|
184
|
+
// - text-embedding-ada-002: 1536
|
|
185
|
+
// - cohere-embed-multilingual-v3: 1024
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
The dimension size must match the output dimension of your chosen embedding model. Common dimension sizes are:
|
|
189
|
+
- OpenAI text-embedding-3-small: 1536 dimensions
|
|
190
|
+
- OpenAI text-embedding-3-large: 3072 dimensions
|
|
191
|
+
- Cohere embed-multilingual-v3: 1024 dimensions
|
|
192
|
+
|
|
193
|
+
> **Important**: Index dimensions cannot be changed after creation. To use a different model, delete and recreate the index with the new dimension size.
|
|
194
|
+
|
|
195
|
+
### Naming Rules for Databases
|
|
196
|
+
|
|
197
|
+
Each vector database enforces specific naming conventions for indexes and collections to ensure compatibility and prevent conflicts.
|
|
198
|
+
|
|
199
|
+
<Tabs items={['Pg Vector', 'Pinecone', 'Qdrant', 'Chroma', 'Astra', 'LibSQL', 'Upstash', 'Cloudflare']}>
|
|
200
|
+
<Tabs.Tab>
|
|
201
|
+
Index names must:
|
|
202
|
+
- Start with a letter or underscore
|
|
203
|
+
- Contain only letters, numbers, and underscores
|
|
204
|
+
- Example: `my_index_123` is valid
|
|
205
|
+
- Example: `my-index` is not valid (contains hyphen)
|
|
206
|
+
</Tabs.Tab>
|
|
207
|
+
<Tabs.Tab>
|
|
208
|
+
Index names must:
|
|
209
|
+
- Use only lowercase letters, numbers, and dashes
|
|
210
|
+
- Not contain dots (used for DNS routing)
|
|
211
|
+
- Not use non-Latin characters or emojis
|
|
212
|
+
- Have a combined length (with project ID) under 52 characters
|
|
213
|
+
- Example: `my-index-123` is valid
|
|
214
|
+
- Example: `my.index` is not valid (contains dot)
|
|
215
|
+
</Tabs.Tab>
|
|
216
|
+
<Tabs.Tab>
|
|
217
|
+
Collection names must:
|
|
218
|
+
- Be 1-255 characters long
|
|
219
|
+
- Not contain any of these special characters:
|
|
220
|
+
- `< > : " / \ | ? *`
|
|
221
|
+
- Null character (`\0`)
|
|
222
|
+
- Unit separator (`\u{1F}`)
|
|
223
|
+
- Example: `my_collection_123` is valid
|
|
224
|
+
- Example: `my/collection` is not valid (contains slash)
|
|
225
|
+
</Tabs.Tab>
|
|
226
|
+
<Tabs.Tab>
|
|
227
|
+
Collection names must:
|
|
228
|
+
- Be 3-63 characters long
|
|
229
|
+
- Start and end with a letter or number
|
|
230
|
+
- Contain only letters, numbers, underscores, or hyphens
|
|
231
|
+
- Not contain consecutive periods (..)
|
|
232
|
+
- Not be a valid IPv4 address
|
|
233
|
+
- Example: `my-collection-123` is valid
|
|
234
|
+
- Example: `my..collection` is not valid (consecutive periods)
|
|
235
|
+
</Tabs.Tab>
|
|
236
|
+
<Tabs.Tab>
|
|
237
|
+
Collection names must:
|
|
238
|
+
- Not be empty
|
|
239
|
+
- Be 48 characters or less
|
|
240
|
+
- Contain only letters, numbers, and underscores
|
|
241
|
+
- Example: `my_collection_123` is valid
|
|
242
|
+
- Example: `my-collection` is not valid (contains hyphen)
|
|
243
|
+
</Tabs.Tab>
|
|
244
|
+
<Tabs.Tab>
|
|
245
|
+
Index names must:
|
|
246
|
+
- Start with a letter or underscore
|
|
247
|
+
- Contain only letters, numbers, and underscores
|
|
248
|
+
- Example: `my_index_123` is valid
|
|
249
|
+
- Example: `my-index` is not valid (contains hyphen)
|
|
250
|
+
</Tabs.Tab>
|
|
251
|
+
<Tabs.Tab>
|
|
252
|
+
Namespace names must:
|
|
253
|
+
- Be 2-100 characters long
|
|
254
|
+
- Contain only:
|
|
255
|
+
- Alphanumeric characters (a-z, A-Z, 0-9)
|
|
256
|
+
- Underscores, hyphens, dots
|
|
257
|
+
- Not start or end with special characters (_, -, .)
|
|
258
|
+
- Can be case-sensitive
|
|
259
|
+
- Example: `MyNamespace123` is valid
|
|
260
|
+
- Example: `_namespace` is not valid (starts with underscore)
|
|
261
|
+
</Tabs.Tab>
|
|
262
|
+
<Tabs.Tab>
|
|
263
|
+
Index names must:
|
|
264
|
+
- Start with a letter
|
|
265
|
+
- Be shorter than 32 characters
|
|
266
|
+
- Contain only lowercase ASCII letters, numbers, and dashes
|
|
267
|
+
- Use dashes instead of spaces
|
|
268
|
+
- Example: `my-index-123` is valid
|
|
269
|
+
- Example: `My_Index` is not valid (uppercase and underscore)
|
|
270
|
+
</Tabs.Tab>
|
|
271
|
+
</Tabs>
|
|
272
|
+
|
|
273
|
+
### Upserting Embeddings
|
|
274
|
+
|
|
275
|
+
After creating an index, you can store embeddings along with their basic metadata:
|
|
276
|
+
|
|
277
|
+
```ts filename="store-embeddings.ts" showLineNumbers copy
|
|
278
|
+
// Store embeddings with their corresponding metadata
|
|
279
|
+
await store.upsert({
|
|
280
|
+
indexName: 'myCollection', // index name
|
|
281
|
+
vectors: embeddings, // array of embedding vectors
|
|
282
|
+
metadata: chunks.map(chunk => ({
|
|
283
|
+
text: chunk.text, // The original text content
|
|
284
|
+
id: chunk.id // Optional unique identifier
|
|
285
|
+
}))
|
|
286
|
+
});
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
The upsert operation:
|
|
290
|
+
- Takes an array of embedding vectors and their corresponding metadata
|
|
291
|
+
- Updates existing vectors if they share the same ID
|
|
292
|
+
- Creates new vectors if they don't exist
|
|
293
|
+
- Automatically handles batching for large datasets
|
|
294
|
+
|
|
295
|
+
For complete examples of upserting embeddings in different vector stores, see the [Upsert Embeddings](../../examples/rag/upsert/upsert-embeddings.mdx) guide.
|
|
296
|
+
|
|
297
|
+
## Adding Metadata
|
|
298
|
+
|
|
299
|
+
Vector stores support rich metadata (any JSON-serializable fields) for filtering and organization. Since metadata is stored with no fixed schema, use consistent field naming to avoid unexpected query results.
|
|
300
|
+
|
|
301
|
+
**Important**: Metadata is crucial for vector storage - without it, you'd only have numerical embeddings with no way to return the original text or filter results. Always store at least the source text as metadata.
|
|
302
|
+
|
|
303
|
+
```ts showLineNumbers copy
|
|
304
|
+
// Store embeddings with rich metadata for better organization and filtering
|
|
305
|
+
await store.upsert({
|
|
306
|
+
indexName: "myCollection",
|
|
307
|
+
vectors: embeddings,
|
|
308
|
+
metadata: chunks.map((chunk) => ({
|
|
309
|
+
// Basic content
|
|
310
|
+
text: chunk.text,
|
|
311
|
+
id: chunk.id,
|
|
312
|
+
|
|
313
|
+
// Document organization
|
|
314
|
+
source: chunk.source,
|
|
315
|
+
category: chunk.category,
|
|
316
|
+
|
|
317
|
+
// Temporal metadata
|
|
318
|
+
createdAt: new Date().toISOString(),
|
|
319
|
+
version: "1.0",
|
|
320
|
+
|
|
321
|
+
// Custom fields
|
|
322
|
+
language: chunk.language,
|
|
323
|
+
author: chunk.author,
|
|
324
|
+
confidenceScore: chunk.score,
|
|
325
|
+
})),
|
|
326
|
+
});
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
Key metadata considerations:
|
|
330
|
+
- Be strict with field naming - inconsistencies like 'category' vs 'Category' will affect queries
|
|
331
|
+
- Only include fields you plan to filter or sort by - extra fields add overhead
|
|
332
|
+
- Add timestamps (e.g., 'createdAt', 'lastUpdated') to track content freshness
|
|
333
|
+
|
|
334
|
+
## Best Practices
|
|
335
|
+
|
|
336
|
+
- Create indexes before bulk insertions
|
|
337
|
+
- Use batch operations for large insertions (the upsert method handles batching automatically)
|
|
338
|
+
- Only store metadata you'll query against
|
|
339
|
+
- Match embedding dimensions to your model (e.g., 1536 for `text-embedding-3-small`)
|
|
340
|
+
|