Skip to main content

Upstash Vector

Upstash Vector is a REST based serverless vector database, designed for working with vector embeddings.

Setup

  1. Create Upstash Vector Index

You can create an index from Upstash Console. For further reference, see docs.

  1. Install Upstash Vector SDK.
npm install -S @upstash/vector

We use OpenAI for the embeddings of the below examples. However, you can also create the embeddings using the model of your choice, that is available in the LangChain.

npm install @langchain/openai @langchain/community

Create Upstash Vector Client

There are two ways to create the client. You can either pass the credentials as string manually from the .env file (or as string variables), or you can retrieve the credentials from the environment automatically.

import { Index } from "@upstash/vector";
import { OpenAIEmbeddings } from "@langchain/openai";
import { UpstashVectorStore } from "@langchain/community/vectorstores/upstash";

const embeddings = new OpenAIEmbeddings({});

// Creating the index with the provided credentials.
const indexWithCredentials = new Index({
url: process.env.UPSTASH_VECTOR_REST_URL as string,
token: process.env.UPSTASH_VECTOR_REST_TOKEN as string,
});

const storeWithCredentials = new UpstashVectorStore(embeddings, {
index: indexWithCredentials,
});

// Creating the index from the environment variables automatically.
const indexFromEnv = new Index();

const storeFromEnv = new UpstashVectorStore(embeddings, {
index: indexFromEnv,
});

API Reference:

Index and Query Documents

You can index the LangChain documents with any model of your choice, and perform a search over these documents. It's possible to apply metadata filtering to the search results. See the related docs here.

import { Index } from "@upstash/vector";
import { OpenAIEmbeddings } from "@langchain/openai";
import { Document } from "@langchain/core/documents";
import { UpstashVectorStore } from "@langchain/community/vectorstores/upstash";

const index = new Index({
url: process.env.UPSTASH_VECTOR_REST_URL as string,
token: process.env.UPSTASH_VECTOR_REST_TOKEN as string,
});

const embeddings = new OpenAIEmbeddings({});

const UpstashVector = new UpstashVectorStore(embeddings, { index });

// Creating the docs to be indexed.
const id = new Date().getTime();
const documents = [
new Document({
metadata: { name: id },
pageContent: "Hello there!",
}),
new Document({
metadata: { name: id },
pageContent: "What are you building?",
}),
new Document({
metadata: { time: id },
pageContent: "Upstash Vector is great for building AI applications.",
}),
new Document({
metadata: { time: id },
pageContent: "To be, or not to be, that is the question.",
}),
];

// Creating embeddings from the provided documents, and adding them to Upstash database.
await UpstashVector.addDocuments(documents);

// Waiting vectors to be indexed in the vector store.
// eslint-disable-next-line no-promise-executor-return
await new Promise((resolve) => setTimeout(resolve, 1000));

const queryResult = await UpstashVector.similaritySearchWithScore(
"Vector database",
2
);

console.log(queryResult);
/**
[
[
Document {
pageContent: 'Upstash Vector is great for building AI applications.',
metadata: [Object]
},
0.9016147
],
[
Document {
pageContent: 'What are you building?',
metadata: [Object]
},
0.8613077
]
]
*/

API Reference:

Namespaces

You can use namespaces to partition your data in the index. Namespaces are useful when you want to query over huge amount of data, and you want to partition the data to make the queries faster. When you use namespaces, there won't be post-filtering on the results which will make the query results more precise.

import { Index } from "@upstash/vector";
import { OpenAIEmbeddings } from "@langchain/openai";
import { Document } from "@langchain/core/documents";
import { UpstashVectorStore } from "@langchain/community/vectorstores/upstash";

const index = new Index({
url: process.env.UPSTASH_VECTOR_REST_URL as string,
token: process.env.UPSTASH_VECTOR_REST_TOKEN as string,
});

const embeddings = new OpenAIEmbeddings({});

const UpstashVector = new UpstashVectorStore(embeddings, {
index,
namespace: "test-namespace",
});

// Creating the docs to be indexed.
const id = new Date().getTime();
const documents = [
new Document({
metadata: { name: id },
pageContent: "Vector databases are great!",
}),
];

// Creating embeddings from the provided documents, and adding them to target namespace in Upstash Vector database.
await UpstashVector.addDocuments(documents);

// Waiting vectors to be indexed in the vector store.
// eslint-disable-next-line no-promise-executor-return
await new Promise((resolve) => setTimeout(resolve, 1000));

const queryResult = await UpstashVector.similaritySearchWithScore(
"Vector database",
1
);

console.log(queryResult);
/**
[
[
Document {
pageContent: 'Vector databases are great!',
metadata: [Object]
},
0.9016147
],
]
*/

API Reference:

Upstash embeddings

It's possible to use the embeddings service of Upstash, which is based on the embedding model of choice when creating the vector database. You don't need to create the embeddings manually, as the Upstash Vector service will handle this for you.

import { Index } from "@upstash/vector";
import { Document } from "@langchain/core/documents";
import { UpstashVectorStore } from "@langchain/community/vectorstores/upstash";
import { FakeEmbeddings } from "@langchain/core/utils/testing";

const index = new Index({
url: process.env.UPSTASH_VECTOR_REST_URL as string,
token: process.env.UPSTASH_VECTOR_REST_TOKEN as string,
});

// Initializing the UpstashVectorStore with the Upstash Embeddings configuration.
// Passing FakeEmbeddings here will enable the store to generate embeddings using Upstash Embeddings.
const UpstashVector = new UpstashVectorStore(new FakeEmbeddings(), { index });

// Creating the docs to be indexed.
const id = new Date().getTime();
const documents = [
new Document({
metadata: { name: id },
pageContent: "Hello there!",
}),
new Document({
metadata: { name: id },
pageContent: "What are you building?",
}),
new Document({
metadata: { time: id },
pageContent: "Upstash Vector is great for building AI applications.",
}),
new Document({
metadata: { time: id },
pageContent: "To be, or not to be, that is the question.",
}),
];

// Creating embeddings from the provided documents, and adding them to Upstash database.
await UpstashVector.addDocuments(documents);

// Waiting vectors to be indexed in the vector store.
// eslint-disable-next-line no-promise-executor-return
await new Promise((resolve) => setTimeout(resolve, 1000));

const queryResult = await UpstashVector.similaritySearchWithScore(
"Vector database",
2
);

console.log(queryResult);
/**
[
[
Document {
pageContent: 'Upstash Vector is great for building AI applications.',
metadata: [Object]
},
0.9016147
],
[
Document {
pageContent: 'What are you building?',
metadata: [Object]
},
0.8613077
]
]
*/

API Reference:

Delete Documents

You can also delete the documents you've indexed previously.

import { Index } from "@upstash/vector";
import { OpenAIEmbeddings } from "@langchain/openai";
import { UpstashVectorStore } from "@langchain/community/vectorstores/upstash";

const index = new Index({
url: process.env.UPSTASH_VECTOR_REST_URL as string,
token: process.env.UPSTASH_VECTOR_REST_TOKEN as string,
});

const embeddings = new OpenAIEmbeddings({});

const UpstashVector = new UpstashVectorStore(embeddings, { index });

// Creating the docs to be indexed.
const createdAt = new Date().getTime();

const IDs = await UpstashVector.addDocuments([
{ pageContent: "hello", metadata: { a: createdAt + 1 } },
{ pageContent: "car", metadata: { a: createdAt } },
{ pageContent: "adjective", metadata: { a: createdAt } },
{ pageContent: "hi", metadata: { a: createdAt } },
]);

// Waiting vectors to be indexed in the vector store.
// eslint-disable-next-line no-promise-executor-return
await new Promise((resolve) => setTimeout(resolve, 1000));

await UpstashVector.delete({ ids: [IDs[0], IDs[2], IDs[3]] });

API Reference:


Was this page helpful?


You can also leave detailed feedback on GitHub.