DocsReferenceRAGUpstashVector

Upstash Vector Store

The UpstashVector class provides vector search using Upstash Vector, a serverless vector database service that provides vector similarity search with metadata filtering capabilities.

Constructor Options

url:

string
Upstash Vector database URL

token:

string
Upstash Vector API token

Methods

createIndex()

Note: This method is a no-op for Upstash as indexes are created automatically.

indexName:

string
Name of the index to create

dimension:

number
Vector dimension (must match your embedding model)

metric?:

'cosine' | 'euclidean' | 'dotproduct'
= cosine
Distance metric for similarity search

upsert()

indexName:

string
Name of the index to upsert into

vectors:

number[][]
Array of embedding vectors

metadata?:

Record<string, any>[]
Metadata for each vector

ids?:

string[]
Optional vector IDs (auto-generated if not provided)

query()

indexName:

string
Name of the index to query

queryVector:

number[]
Query vector to find similar vectors

topK?:

number
= 10
Number of results to return

filter?:

Record<string, any>
Metadata filters for the query

includeVector?:

boolean
= false
Whether to include vectors in the results

listIndexes()

Returns an array of index names (namespaces) as strings.

describeIndex()

indexName:

string
Name of the index to describe

Returns:

interface IndexStats {
  dimension: number;
  count: number;
  metric: "cosine" | "euclidean" | "dotproduct";
}

deleteIndex()

indexName:

string
Name of the index (namespace) to delete

updateIndexById()

indexName:

string
Name of the index to update

id:

string
ID of the item to update

update:

object
Update object containing vector and/or metadata

The update object can have the following properties:

  • vector (optional): An array of numbers representing the new vector.
  • metadata (optional): A record of key-value pairs for metadata.

Throws an error if neither vector nor metadata is provided, or if only metadata is provided.

deleteIndexById()

indexName:

string
Name of the index from which to delete the item

id:

string
ID of the item to delete

Attempts to delete an item by its ID from the specified index. Logs an error message if the deletion fails.

Response Types

Query results are returned in this format:

interface QueryResult {
  id: string;
  score: number;
  metadata: Record<string, any>;
  vector?: number[]; // Only included if includeVector is true
}

Error Handling

The store throws typed errors that can be caught:

try {
  await store.query({
    indexName: "index_name",
    queryVector: queryVector,
  });
} catch (error) {
  if (error instanceof VectorStoreError) {
    console.log(error.code); // 'connection_failed' | 'invalid_dimension' | etc
    console.log(error.details); // Additional error context
  }
}

Environment Variables

Required environment variables:

  • UPSTASH_VECTOR_URL: Your Upstash Vector database URL
  • UPSTASH_VECTOR_TOKEN: Your Upstash Vector API token