libSQL vector store
libSQL storage 実装は、vector 拡張を備えた SQLite のフォーク libSQL と、vector 拡張を備えた Turso による SQLite 互換のベクトル検索を提供し、軽量で効率的なベクトルデータベースソリューションを実現します。
これは @mastra/libsql パッケージに含まれ、メタデータフィルタリングに対応した効率的なベクトル類似度検索を提供します。
インストールインストールへの直接リンク
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/libsql@latest
pnpm add @mastra/libsql@latest
yarn add @mastra/libsql@latest
bun add @mastra/libsql@latest
使用方法使用方法への直接リンク
import { LibSQLVector } from "@mastra/libsql";
// Create a new vector store instance
const store = new LibSQLVector({
id: 'libsql-vector',
url: process.env.DATABASE_URL,
// Optional: for Turso cloud databases
authToken: process.env.DATABASE_AUTH_TOKEN,
});
// Create an index
await store.createIndex({
indexName: "myCollection",
dimension: 1536,
});
// Add vectors with metadata
const vectors = [[0.1, 0.2, ...], [0.3, 0.4, ...]];
const metadata = [
{ text: "first document", category: "A" },
{ text: "second document", category: "B" }
];
await store.upsert({
indexName: "myCollection",
vectors,
metadata,
});
// Query similar vectors
const queryVector = [0.1, 0.2, ...];
const results = await store.query({
indexName: "myCollection",
queryVector,
topK: 10, // top K results
filter: { category: "A" } // optional metadata filter
});
コンストラクターオプションコンストラクターオプションへの直接リンク
url:
authToken?:
syncUrl?:
syncInterval?:
メソッドメソッドへの直接リンク
createIndex()createindexへの直接リンク
新しいベクトルコレクションを作成します。インデックス名の先頭は英字またはアンダースコアにする必要があり、使用できるのは英字、数字、アンダースコアだけです。次元数は正の整数である必要があります。
indexName:
dimension:
metric?:
upsert()upsertへの直接リンク
ベクトルとそのメタデータをインデックスに追加または更新します。トランザクションを使用して、すべてのベクトルをアトミックに挿入します。いずれかの挿入に失敗すると、操作全体がロールバックされます。
indexName:
vectors:
metadata?:
ids?:
query()queryへの直接リンク
省略可能なメタデータフィルタリングを使用して類似ベクトルを検索します。
indexName:
queryVector:
topK?:
filter?:
includeVector?:
minScore?:
describeIndex()describeindexへの直接リンク
インデックスの情報を取得します。
indexName:
戻り値:
interface IndexStats {
dimension: number
count: number
metric: 'cosine' | 'euclidean' | 'dotproduct'
}
deleteIndex()deleteindexへの直接リンク
インデックスとそのすべてのデータを削除します。
indexName:
listIndexes()listindexesへの直接リンク
データベース内のすべてのベクトルインデックスを一覧表示します。
戻り値: Promise<string[]>
truncateIndex()truncateindexへの直接リンク
インデックス構造を維持したまま、インデックスからすべてのベクトルを削除します。
indexName:
updateVector()updatevectorへの直接リンク
ID またはメタデータフィルターで単一のベクトルを更新します。id と filter のいずれか一方だけを指定する必要があります。
indexName:
id?:
filter?:
update:
update.vector?:
update.metadata?:
deleteVector()deletevectorへの直接リンク
ID を指定して、インデックスから特定のベクトルエントリを削除します。
indexName:
id:
deleteVectors()deletevectorsへの直接リンク
ID またはメタデータフィルターで複数のベクトルを削除します。ids と filter のいずれか一方だけを指定する必要があります。
indexName:
ids?:
filter?:
レスポンス型レスポンス型への直接リンク
クエリ結果は次の形式で返されます。
interface QueryResult {
id: string
score: number
metadata: Record<string, any>
vector?: number[] // Only included if includeVector is true
}
エラー処理エラー処理への直接リンク
失敗の種類に応じて、vector store は個別のエラーをスローします。
try {
await store.query({
indexName: 'my-collection',
queryVector: queryVector,
})
} catch (error) {
// Handle specific error cases
if (error.message.includes('Invalid index name format')) {
console.error(
'Index name must start with a letter/underscore and contain only alphanumeric characters',
)
} else if (error.message.includes('Table not found')) {
console.error('The specified index does not exist')
} else {
console.error('Vector store error:', error.message)
}
}
一般的なエラーには次のものがあります。
- インデックス名の形式が無効
- ベクトルの次元数が無効
- テーブルまたはインデックスが見つからない
- データベース接続の問題
- upsert 中のトランザクション失敗
使用例使用例への直接リンク
fastembed によるローカル埋め込みfastembed によるローカル埋め込みへの直接リンク
埋め込みは、Memory の semanticRecall がキーワードではなく意味に基づいて関連メッセージを取得するために使用する数値ベクトルです。この設定では @mastra/fastembed を使用して埋め込みベクトルを生成します。
まず fastembed をインストールします。
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/fastembed@latest
pnpm add @mastra/fastembed@latest
yarn add @mastra/fastembed@latest
bun add @mastra/fastembed@latest
Agent に次の内容を追加します。
import { Memory } from '@mastra/memory'
import { Agent } from '@mastra/core/agent'
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
import { fastembed } from '@mastra/fastembed'
export const libsqlAgent = new Agent({
id: 'libsql-agent',
name: 'libSQL Agent',
instructions:
'You are an AI agent with the ability to automatically recall memories from previous interactions.',
model: 'openai/gpt-5.6-sol',
memory: new Memory({
storage: new LibSQLStore({
id: 'libsql-agent-storage',
url: 'file:libsql-agent.db',
}),
vector: new LibSQLVector({
id: 'libsql-agent-vector',
url: 'file:libsql-agent.db',
}),
embedder: fastembed,
options: {
lastMessages: 10,
semanticRecall: {
topK: 3,
messageRange: 2,
},
generateTitle: true, // Explicitly enable automatic title generation
},
}),
})