Upstash Vector Node.js Client ·
[!NOTE]
This project is in GA Stage.
The Upstash Professional Support fully covers this project. It receives regular updates, and bug fixes.
The Upstash team is committed to maintaining and improving its functionality.
@upstash/vector
is an HTTP/REST based client for Typescript, built on top of Upstash REST API.
It is the only connectionless (HTTP based) Vector client and designed for:
- Serverless functions (AWS Lambda ...)
- Cloudflare Workers
- Next.js, Jamstack ...
- Client side web/mobile applications
- WebAssembly
- and other environments where HTTP is preferred over TCP.
See the list of APIs supported.
Quick Start
Install
Node.js
npm install @upstash/vector
Create Index
Create a new index on Upstash
Basic Usage:
import { Index } from "@upstash/vector";
type Metadata = {
title: string,
genre: 'sci-fi' | 'fantasy' | 'horror' | 'action'
category: "classic" | "modern"
}
const index = new Index<Metadata>({
url: "<UPSTASH_VECTOR_REST_URL>",
token: "<UPSTASH_VECTOR_REST_TOKEN>",
});
await index.upsert([{
id: 'upstash-rocks',
vector: [
....
],
metadata: {
title: 'Lord of The Rings',
genre: 'fantasy',
category: 'classic'
}
}])
await index.upsert([{
id: 'tokyo',
data: "Tokyo is the capital of Japan.",
}])
await index.upsert([{
id: 'tokyo',
data: "Tokyo is the capital of Japan.",
vector: [......]
}])
const results = await index.query<Metadata>(
{
vector: [
...
],
includeVectors: true,
includeMetadata: true,
topK: 1,
filter: "genre = 'fantasy' and title = 'Lord of the Rings'"
},
{
namespace: "example-namespace"
}
)
const results = await index.query(
{
data: "Where is the capital of Japan",
topK: 1,
},
)
const results = await index.query(
{
vector: [
...
],
includeData: true,
topK: 1,
},
)
await index.upsert(
{
id: "upstash-rocks",
metadata: {
title: 'Star Wars',
genre: 'sci-fi',
category: 'classic'
}
},
{
namespace: "namespace"
}
);
await index.delete("upstash-rocks", {namespace: "example-namespace"});
await index.delete(["id-1", "id-2", "id-3"]);
await index.fetch(["id-1", "id-2"], {namespace: "example-namespace"});
await index.fetch(["id-1", "id-2"], {namespace: "example-namespace", includeData:true});
await index.range(
{
cursor: 0,
limit: 5,
includeVectors: true,
},
{
namespace: "example-namespace"
}
);
await index.reset();
await index.info();
await index.random({namespace: "example-namespace"});
await index.listNamespaces();
await index.deleteNamespace("namespace-to-be-deleted");
Namespaces
Upstash Vector allows you to partition a single index into multiple isolated namespaces. Each namespace functions as a self-contained subset of the index, in which read and write requests are only limited to one namespace. To learn more about it, see Namespaces
Example
import { Index } from "@upstash/vector";
type Metadata = {
title: string;
genre: "sci-fi" | "fantasy" | "horror" | "action";
category: "classic" | "modern";
};
const index = new Index<Metadata>({
url: "<UPSTASH_VECTOR_REST_URL>",
token: "<UPSTASH_VECTOR_REST_TOKEN>",
});
const namespace = index.namespace("example-namespace");
//Upsert Data
await namespace.upsert([{
id: 'upstash-rocks',
vector: [
....
],
metadata: {
title: 'Lord of The Rings',
genre: 'fantasy',
category: 'classic'
}
}])
const results = await namespace.query<Metadata>(
{
vector: [
...
],
includeVectors: true,
includeMetadata: true,
topK: 1,
filter: "genre = 'fantasy' and title = 'Lord of the Rings'"
},
)
await namespace.delete("upstash-rocks");
await namespace.fetch(["id-1", "id-2"]);
Metadata Filtering
If you wanna learn more about filtering check: Metadata Filtering
Troubleshooting
We have a Discord for common problems. If you can't find a solution, please open an issue.
Docs
See the documentation for details.
Contributing
Vector Database
Create a new index on Upstash and copy the url and token.
Running tests
bun run test
Building
bun run build
Contributing
Make sure you have Bun.js installed and have those relevant keys with specific vector dimensions:
UPSTASH_VECTOR_REST_URL="XXXXX"
UPSTASH_VECTOR_REST_TOKEN="XXXXX"