Upstash Vector
Upstash Vector is a REST based serverless vector database, designed for working with vector embeddings.
Setup
- Create Upstash Vector Index
You can create an index from Upstash Console. For further reference, see docs.
- Install Upstash Vector SDK.
- npm
- Yarn
- pnpm
npm install -S @upstash/vector
yarn add @upstash/vector
pnpm add @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
- Yarn
- pnpm
npm install @langchain/openai @langchain/community
yarn add @langchain/openai @langchain/community
pnpm add @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:
- OpenAIEmbeddings from
@langchain/openai
- UpstashVectorStore from
@langchain/community/vectorstores/upstash
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:
- OpenAIEmbeddings from
@langchain/openai
- Document from
@langchain/core/documents
- UpstashVectorStore from
@langchain/community/vectorstores/upstash
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:
- OpenAIEmbeddings from
@langchain/openai
- Document from
@langchain/core/documents
- UpstashVectorStore from
@langchain/community/vectorstores/upstash
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:
- Document from
@langchain/core/documents
- UpstashVectorStore from
@langchain/community/vectorstores/upstash
- FakeEmbeddings from
@langchain/core/utils/testing
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:
- OpenAIEmbeddings from
@langchain/openai
- UpstashVectorStore from
@langchain/community/vectorstores/upstash
Related
- Vector store conceptual guide
- Vector store how-to guides