@upstash/vector 0.1.0-alpha → 0.1.0-alpha-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +213 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,15 +1,221 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Upstash Vector Node.js Client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This is the official Node.js client for [Upstash](https://upstash.com/), written in TypeScript.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
## Documentation
|
|
6
|
+
|
|
7
|
+
- [**Reference Documentation**](https://upstash.com/docs/vector/overall/getstarted)
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install @upstash/vector
|
|
13
|
+
pnpm add @upstash/vector
|
|
7
14
|
```
|
|
8
15
|
|
|
9
|
-
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
### Initializing the client
|
|
19
|
+
|
|
20
|
+
There are two pieces of configuration required to use the Upstash vector client: an REST token and REST URL. These values can be passed using environment variables or in code through a configuration object. Find your configuration values in the console dashboard at [https://console.upstash.com/](https://console.upstash.com/).
|
|
21
|
+
|
|
22
|
+
#### Using environment variables
|
|
23
|
+
|
|
24
|
+
The environment variables used to configure the client are the following:
|
|
10
25
|
|
|
11
26
|
```bash
|
|
12
|
-
|
|
27
|
+
UPSTASH_VECTOR_REST_URL="your_rest_url"
|
|
28
|
+
UPSTASH_VECTOR_REST_TOKEN="your_rest_token"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
When these environment variables are set, the client constructor does not require any additional arguments.
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { fromEnv } from "@upstash/vector";
|
|
35
|
+
|
|
36
|
+
const index = fromEnv();
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### Using a configuration object
|
|
40
|
+
|
|
41
|
+
If you prefer to pass configuration in code, the constructor accepts a config object containing the `url` and `token` values. This
|
|
42
|
+
could be useful if your application needs to interact with multiple projects, each with a different configuration.
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { Index } from "@upstash/vector";
|
|
46
|
+
|
|
47
|
+
const index = new Index({
|
|
48
|
+
url: "<UPSTASH_VECTOR_REST_URL>",
|
|
49
|
+
token: "<UPSTASH_VECTOR_REST_TOKEN>",
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Index operations
|
|
54
|
+
|
|
55
|
+
Upstash vector indexes support operations for working with vector data using operations such as upsert, query, fetch, and delete.
|
|
56
|
+
|
|
57
|
+
### Targeting an index
|
|
58
|
+
|
|
59
|
+
To perform data operations on an index, you target it using the `index` method.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
const index = new Index();
|
|
63
|
+
|
|
64
|
+
// Now perform index operations
|
|
65
|
+
await index.fetch([1, 2, 3], { includeMetadata: true, includeVectors: true });
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Targeting an index, with metadata typing
|
|
69
|
+
|
|
70
|
+
If you are storing metadata alongside your vector values, you can pass a type parameter to `index()` in order to get proper TypeScript typechecking.
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
const index = new Index();
|
|
74
|
+
|
|
75
|
+
type Metadata = {
|
|
76
|
+
title: string,
|
|
77
|
+
genre: 'sci-fi' | 'fantasy' | 'horror' | 'action'
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
await index.upsert([{
|
|
81
|
+
id: '1234',
|
|
82
|
+
vector: [
|
|
83
|
+
.... // embedding values
|
|
84
|
+
],
|
|
85
|
+
metadata: {
|
|
86
|
+
title: 'Lord of The Rings',
|
|
87
|
+
genre: 'drama',
|
|
88
|
+
category: 'classic'
|
|
89
|
+
}
|
|
90
|
+
}])
|
|
91
|
+
|
|
92
|
+
const results = await index.query<Metadata>({
|
|
93
|
+
vector: [
|
|
94
|
+
... // query embedding
|
|
95
|
+
],
|
|
96
|
+
includeVectors: true,
|
|
97
|
+
topK: 1,
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
if (results[0].metadata) {
|
|
101
|
+
// Since we passed the Metadata type parameter above,
|
|
102
|
+
// we can interact with metadata fields without having to
|
|
103
|
+
// do any typecasting.
|
|
104
|
+
const { title, genre, category } = movie.metadata;
|
|
105
|
+
console.log(`The best match in fantasy was ${title}`)
|
|
106
|
+
}
|
|
13
107
|
```
|
|
14
108
|
|
|
15
|
-
|
|
109
|
+
### Upsert records
|
|
110
|
+
|
|
111
|
+
Upstash vector expects records inserted into indexes to have the following form:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
type UpstashRecord = {
|
|
115
|
+
id: number | string;
|
|
116
|
+
vector: number[];
|
|
117
|
+
metadata?: Record<string, unknown>;
|
|
118
|
+
};
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
To upsert some records, you can use the client like so:
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
const index = new Index();
|
|
125
|
+
|
|
126
|
+
// Prepare your data. The length of each array
|
|
127
|
+
// of vector values must match the dimension of
|
|
128
|
+
// the index where you plan to store them.
|
|
129
|
+
const records = [
|
|
130
|
+
{
|
|
131
|
+
id: "1",
|
|
132
|
+
vector: [0.236, 0.971, 0.559],
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
id: "2",
|
|
136
|
+
vector: [0.685, 0.111, 0.857],
|
|
137
|
+
},
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
// Upsert the data into your index
|
|
141
|
+
await index.upsert(records);
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Querying
|
|
145
|
+
|
|
146
|
+
#### Querying with vector values
|
|
147
|
+
|
|
148
|
+
The query method accepts a large number of options. The dimension of the query vector must match the dimension of your index.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
type QueryOptions = {
|
|
152
|
+
vector: number[];
|
|
153
|
+
topK: number;
|
|
154
|
+
includeVectors?: boolean;
|
|
155
|
+
includeMetadata?: boolean;
|
|
156
|
+
};
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
For example, to query by vector values you would pass the `vector` param in the options configuration. For brevity sake this example query vector is tiny (dimension 2), but in a more realistic use case this query vector would be an embedding outputted by a model. Look at the [Example code](#example-code) to see more realistic examples of how to use `query`.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
> await index.query({ topK: 3, vector: [ 0.22, 0.66 ]})
|
|
163
|
+
{
|
|
164
|
+
matches: [
|
|
165
|
+
{
|
|
166
|
+
id: '6345',
|
|
167
|
+
score: 1.00000012,
|
|
168
|
+
vector: [],
|
|
169
|
+
metadata: undefined
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
id: '1233',
|
|
173
|
+
score: 1.00000012,
|
|
174
|
+
vector: [],
|
|
175
|
+
metadata: undefined
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
id: '4142',
|
|
179
|
+
score: 1.00000012,
|
|
180
|
+
vector: [],
|
|
181
|
+
metadata: undefined
|
|
182
|
+
}
|
|
183
|
+
],
|
|
184
|
+
namespace: ''
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
You include options to `includeMetadata: true` or `includeVectors: true` if you need this information. By default these are not returned to keep the response payload small.
|
|
189
|
+
|
|
190
|
+
### Update a record
|
|
191
|
+
|
|
192
|
+
You may want to update vector `vector` or `metadata`. Specify the id and the attribute value you want to update.
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
await index.upsert({
|
|
196
|
+
id: "18593",
|
|
197
|
+
metadata: { genre: "romance" },
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Fetch records by their IDs
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
const fetchResult = await index.fetch(["id-1", "id-2"]);
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Delete records
|
|
208
|
+
|
|
209
|
+
For convenience there are several delete-related options. You can verify the results of a delete operation by trying to `fetch()` a record.
|
|
210
|
+
|
|
211
|
+
#### Delete one
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
await index.delete("id-to-delete");
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
#### Delete many by id
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
await index.delete(["id-1", "id-2", "id-3"]);
|
|
221
|
+
```
|