diff --git a/docs/web-tests/ts/integrations.ts b/docs/web-tests/ts/integrations.ts index 3ae7967e9..75987cf7d 100644 --- a/docs/web-tests/ts/integrations.ts +++ b/docs/web-tests/ts/integrations.ts @@ -132,3 +132,37 @@ Question: ${input}`, ); // --8<-- [end:frameworks_genkit_custom_retriever] +// --8<-- [start:frameworks_mastra_quickstart] +import { LanceVectorStore } from "@mastra/lance"; + +const store = await LanceVectorStore.create("./lancedb-data"); +const indexName = "documents"; + +await store.createIndex({ indexName, dimension: 3 }); + +await store.upsert({ + indexName, + ids: ["mastra", "lancedb"], + vectors: [ + [0, 1, 0], + [1, 0, 0], + ], +}); + +const results = await store.query({ + indexName, + queryVector: [1, 0, 0], + topK: 1, +}); + +console.log(results[0].id, results[0].score); +store.close(); +// --8<-- [end:frameworks_mastra_quickstart] + +// --8<-- [start:frameworks_mastra_native_index] +await store.createIndex({ + indexName, + dimension: 3, + indexConfig: { type: "ivfflat", numPartitions: 1 }, +}); +// --8<-- [end:frameworks_mastra_native_index] diff --git a/docs/web/docs.json b/docs/web/docs.json index b561346cd..f2cd5ffa6 100644 --- a/docs/web/docs.json +++ b/docs/web/docs.json @@ -676,6 +676,7 @@ "integrations/ai/llamaIndex", "integrations/ai/genkit", "integrations/ai/kiln", + "integrations/ai/mastra", "integrations/ai/prompttools", "integrations/ai/synthetic-data-kit", "integrations/ai/vscode" diff --git a/docs/web/integrations/ai/mastra.mdx b/docs/web/integrations/ai/mastra.mdx new file mode 100644 index 000000000..b5703724a --- /dev/null +++ b/docs/web/integrations/ai/mastra.mdx @@ -0,0 +1,69 @@ +--- +title: "Mastra" +sidebarTitle: "Mastra" +description: "Use LanceDB Embedded as a local vector store in Mastra with @mastra/lance." +availability: + embedded: available + enterprise: unavailable +--- + +import { + TsFrameworksMastraNativeIndex, + TsFrameworksMastraQuickstart, +} from '/snippets/integrations.mdx'; + +[Mastra](https://mastra.ai) is an open-source TypeScript framework for building AI agents, workflows, and retrieval-augmented generation (RAG) pipelines. Its official `@mastra/lance` package connects Mastra to LanceDB Embedded on local disk with no separate database server. + +## Installation {#installation} + +`@mastra/lance` requires Node.js 22.13.0 or later and depends on `@mastra/core`. Install both packages: + + +```bash Bash icon="terminal" +npm install @mastra/lance @mastra/core +``` + + +Installing `@mastra/lance` also installs `@lancedb/lancedb` and the matching native binary for your platform. + +## Vector search quickstart {#vector-search-quickstart} + +Save the following script as `example.mjs`. It opens a local LanceDB directory with `LanceVectorStore.create()`, creates a `documents` table, upserts two 3-dimensional vectors, and runs a nearest-neighbor query: + + + {TsFrameworksMastraQuickstart} + + +Run the script: + + +```bash Bash icon="terminal" +node example.mjs +``` + + +The nearest-neighbor query returns `lancedb` with a cosine similarity score of `1`: + +```text Output +lancedb 1 +``` + +## Native vector index {#native-vector-index} + +The first `createIndex()` call initializes an empty LanceDB table without building a native vector index. When `tableName` is omitted, `indexName` sets the table name and `vector` is the vector column. Queries work on small tables immediately through flat scan. + +To build a native [LanceDB vector index](/indexing/vector-index) when extending the quickstart, call `createIndex()` again after upserting at least 256 rows and before `store.close()`: + + + {TsFrameworksMastraNativeIndex} + + +Supported `indexConfig.type` values include `"ivfflat"`, `"ivfpq"`, and the default HNSW PQ index. + +## Application storage and references {#application-storage-and-references} + +`@mastra/lance` also exports `LanceStorage` for Mastra agent memory and workflow state. The published `LanceStorage.create(id, name, uri)` factory takes three required arguments (`id`, `name`, and `uri`). See the [released `LanceStorage.create` implementation](https://github.com/mastra-ai/mastra/blob/d60ba71be015e2acbdc303208a14563902fb1f39/stores/lance/src/storage/index.ts#L78-L119) for the current signature if you encounter older two-argument examples. + +- [Mastra LanceDB vector store reference](https://mastra.ai/integrations/vectors/lance) +- [Mastra LanceDB storage guide](https://mastra.ai/integrations/databases/lancedb) +- [Released `LanceVectorStore.createIndex` implementation](https://github.com/mastra-ai/mastra/blob/d60ba71be015e2acbdc303208a14563902fb1f39/stores/lance/src/vector/index.ts#L685-L815) diff --git a/docs/web/snippets/integrations.mdx b/docs/web/snippets/integrations.mdx index 00f8bb02e..b8981dcb0 100644 --- a/docs/web/snippets/integrations.mdx +++ b/docs/web/snippets/integrations.mdx @@ -176,3 +176,7 @@ export const TsFrameworksGenkitCustomRetriever = "export const menuRetriever = l export const TsFrameworksGenkitUsage = "import { lancedbIndexerRef, lancedb, lancedbRetrieverRef, WriteMode } from \"genkitx-lancedb\";\nimport { textEmbedding004, vertexAI } from \"@genkit-ai/vertexai\";\nimport { gemini } from \"@genkit-ai/vertexai\";\nimport { z, genkit } from \"genkit\";\nimport { Document } from \"genkit/retriever\";\nimport { chunk } from \"llm-chunk\";\nimport { readFile } from \"fs/promises\";\nimport path from \"path\";\nimport pdf from \"pdf-parse/lib/pdf-parse\";\n\nconst ai = genkit({\n plugins: [\n // vertexAI provides the textEmbedding004 embedder\n vertexAI(),\n\n // the local vector store requires an embedder to translate from text to vector\n lancedb([\n {\n dbUri: \".db\", // optional lancedb uri, default to .db\n tableName: \"table\", // optional table name, default to table\n embedder: textEmbedding004,\n },\n ]),\n ],\n});\n"; +export const TsFrameworksMastraNativeIndex = "await store.createIndex({\n indexName,\n dimension: 3,\n indexConfig: { type: \"ivfflat\", numPartitions: 1 },\n});\n"; + +export const TsFrameworksMastraQuickstart = "import { LanceVectorStore } from \"@mastra/lance\";\n\nconst store = await LanceVectorStore.create(\"./lancedb-data\");\nconst indexName = \"documents\";\n\nawait store.createIndex({ indexName, dimension: 3 });\n\nawait store.upsert({\n indexName,\n ids: [\"mastra\", \"lancedb\"],\n vectors: [\n [0, 1, 0],\n [1, 0, 0],\n ],\n});\n\nconst results = await store.query({\n indexName,\n queryVector: [1, 0, 0],\n topK: 1,\n});\n\nconsole.log(results[0].id, results[0].score);\nstore.close();\n"; +