跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Vectors API

The Vectors API provides methods to work with vector embeddings for semantic search and similarity matching in Mastra.

Working with vectors
Working with vectors的直接链接

Get an instance of a vector store:

const vector = mastraClient.getVector('vector-name')

Vector methods
Vector methods的直接链接

Get Vector Index Details
Get Vector Index Details的直接链接

Retrieve information about a specific vector index:

const details = await vector.details('index-name')

Create Vector Index
Create Vector Index的直接链接

Create a new vector index:

const result = await vector.createIndex({
indexName: 'new-index',
dimension: 128,
metric: 'cosine', // 'cosine', 'euclidean', or 'dotproduct'
})

Upsert Vectors
Upsert Vectors的直接链接

Add or update vectors in an index:

const ids = await vector.upsert({
indexName: 'my-index',
vectors: [
[0.1, 0.2, 0.3], // First vector
[0.4, 0.5, 0.6], // Second vector
],
metadata: [{ label: 'first' }, { label: 'second' }],
ids: ['id1', 'id2'], // Optional: Custom IDs
})

Query Vectors
Query Vectors的直接链接

Search for similar vectors:

const results = await vector.query({
indexName: 'my-index',
queryVector: [0.1, 0.2, 0.3],
topK: 10,
filter: { label: 'first' }, // Optional: Metadata filter
includeVector: true, // Optional: Include vectors in results
})

Get All Indexes
Get All Indexes的直接链接

List all available indexes:

const indexes = await vector.getIndexes()

Delete Index
Delete Index的直接链接

Delete a vector index:

const result = await vector.delete('index-name')