createVectorQueryTool()
The createVectorQueryTool()
function creates a tool for semantic search over vector stores. It supports filtering, reranking, and integrates with various vector store backends.
Basic Usage
import { createVectorQueryTool } from "@mastra/rag";
const queryTool = createVectorQueryTool({
vectorStoreName: "pinecone",
indexName: "docs",
options: {
provider: "OPEN_AI",
model: "text-embedding-3-small",
maxRetries: 3
}
});
Parameters
vectorStoreName:
string
Name of the vector store to query (must be configured in Mastra)
indexName:
string
Name of the index within the vector store
options:
EmbeddingOptions
Configuration for embedding generation
reranker?:
RerankConfig
Options for reranking results
id?:
string
Custom ID for the tool (defaults to 'VectorQuery {vectorStoreName} {indexName} Tool')
description?:
string
Custom description for the tool (by default, provides instructions for querying the vector store with examples of filtering and reranking if enabled)
RerankConfig
model:
ModelConfig
Configuration for the reranking method
options?:
RerankerOptions
Options for the reranking process
object
weights?:
WeightConfig
Weights for scoring components (semantic: 0.4, vector: 0.4, position: 0.2)
topK?:
number
Number of top results to return
Returns
The tool returns an object with:
relevantContext:
string
Combined text from the most relevant document chunks
Default Tool Description
The default tool description provides:
- Instructions for querying the specified vector store
- Examples of filter syntax when filtering is enabled
Example with Filters
const queryTool = createVectorQueryTool({
vectorStoreName: "pinecone",
indexName: "docs",
options: {
provider: "OPEN_AI",
model: "text-embedding-3-small",
maxRetries: 3
},
enableFilters: true,
});
The tool processes queries with metadata filtering when enabled:
- Filter conditions are specified in the user’s query
- Number of results can be customized with in the user query (default: 10)
- Filters are automatically translated for each vector store’s requirements
For detailed filter syntax and store-specific capabilities, see the Metadata Filters documentation.
Example with Reranking
const queryTool = createVectorQueryTool({
vectorStoreName: "milvus",
indexName: "documentation",
options: {
provider: "OPEN_AI",
model: "text-embedding-3-small",
maxRetries: 3
},
reranker: {
model: {
provider: "OPEN_AI",
name: "gpt-4o-mini",
},
options: {
weights: {
semantic: 0.5, // Semantic relevance weight
vector: 0.3, // Vector similarity weight
position: 0.2 // Original position weight
},
topK: 5
}
}
});
Reranking improves result quality by combining:
- Semantic relevance: Using LLM-based scoring of text similarity
- Vector similarity: Original vector distance scores
- Position bias: Consideration of original result ordering
- Query analysis: Adjustments based on query characteristics
The reranker processes the initial vector search results and returns a reordered list optimized for relevance.
Tool Details
The tool is created with:
- ID:
VectorQuery {vectorStoreName} {indexName} Tool
- Input Schema: Requires queryText and filter objects
- Output Schema: Returns relevantContext string