@upstash/vector 1.0.0 → 1.0.2-canary
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/LICENSE +21 -0
- package/README.md +67 -209
- package/dist/index.d.mts +25 -24
- package/dist/index.d.ts +25 -24
- package/dist/index.js +1 -5
- package/dist/index.mjs +1 -3
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Upstash
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,273 +1,131 @@
|
|
|
1
|
-
# Upstash Vector Node.js Client
|
|
1
|
+
# Upstash Vector Node.js Client ·  [](https://github.com/upstash/vector-js/actions/workflows/tests.yaml)   
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@upstash/vector` is an HTTP/REST based client for Typescript, built on top of [Upstash REST API](https://upstash.com/docs/vector/api/endpoints/).
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
It is the only connectionless (HTTP based) Vector client and designed for:
|
|
6
6
|
|
|
7
|
-
-
|
|
7
|
+
- Serverless functions (AWS Lambda ...)
|
|
8
|
+
- Cloudflare Workers
|
|
9
|
+
- Next.js, Jamstack ...
|
|
10
|
+
- Client side web/mobile applications
|
|
11
|
+
- WebAssembly
|
|
12
|
+
- and other environments where HTTP is preferred over TCP.
|
|
8
13
|
|
|
9
|
-
|
|
14
|
+
See [the list of APIs](https://upstash.com/docs/vector/api/endpoints) supported.
|
|
10
15
|
|
|
11
|
-
|
|
12
|
-
npm install @upstash/vector
|
|
13
|
-
pnpm add @upstash/vector
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
## Usage
|
|
17
|
-
|
|
18
|
-
### Initializing the client
|
|
16
|
+
## Quick Start
|
|
19
17
|
|
|
20
|
-
|
|
18
|
+
### Install
|
|
21
19
|
|
|
22
|
-
####
|
|
23
|
-
|
|
24
|
-
The environment variables used to configure the client are the following:
|
|
20
|
+
#### Node.js
|
|
25
21
|
|
|
26
22
|
```bash
|
|
27
|
-
|
|
28
|
-
UPSTASH_VECTOR_REST_TOKEN="your_rest_token"
|
|
23
|
+
npm install @upstash/vector
|
|
29
24
|
```
|
|
30
25
|
|
|
31
|
-
|
|
26
|
+
### Create Index
|
|
32
27
|
|
|
33
|
-
|
|
34
|
-
import { Index } from "@upstash/vector";
|
|
35
|
-
|
|
36
|
-
const index = new Index();
|
|
37
|
-
```
|
|
28
|
+
Create a new index on [Upstash](https://console.upstash.com/vector)
|
|
38
29
|
|
|
39
|
-
|
|
30
|
+
## Basic Usage:
|
|
40
31
|
|
|
41
|
-
|
|
42
|
-
could be useful if your application needs to interact with multiple projects, each with a different configuration.
|
|
43
|
-
|
|
44
|
-
```typescript
|
|
32
|
+
```ts
|
|
45
33
|
import { Index } from "@upstash/vector";
|
|
46
34
|
|
|
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
|
-
### Accessing an index
|
|
58
|
-
|
|
59
|
-
To perform data operations on an index, access it using the `index` method.
|
|
60
|
-
|
|
61
|
-
```typescript
|
|
62
|
-
// Now perform index operations
|
|
63
|
-
await index.fetch([1, 2, 3], { includeMetadata: true, includeVectors: true });
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### Accesing an index, with metadata typing
|
|
67
|
-
|
|
68
|
-
If you are storing metadata alongside your vector values, you can pass a type parameter to `index()` in order to get proper TypeScript typechecking.
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
35
|
type Metadata = {
|
|
72
36
|
title: string,
|
|
73
37
|
genre: 'sci-fi' | 'fantasy' | 'horror' | 'action'
|
|
38
|
+
category: "classic" | "modern"
|
|
74
39
|
}
|
|
75
40
|
|
|
41
|
+
const index = new Index<Metadata>({
|
|
42
|
+
url: "<UPSTASH_VECTOR_REST_URL>",
|
|
43
|
+
token: "<UPSTASH_VECTOR_REST_TOKEN>",
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
//Upsert Data
|
|
76
47
|
await index.upsert([{
|
|
77
|
-
id: '
|
|
48
|
+
id: 'upstash-rocks',
|
|
78
49
|
vector: [
|
|
79
50
|
.... // embedding values
|
|
80
51
|
],
|
|
81
52
|
metadata: {
|
|
82
53
|
title: 'Lord of The Rings',
|
|
83
|
-
genre: '
|
|
54
|
+
genre: 'fantasy',
|
|
84
55
|
category: 'classic'
|
|
85
56
|
}
|
|
86
57
|
}])
|
|
87
58
|
|
|
59
|
+
//Query Data
|
|
88
60
|
const results = await index.query<Metadata>({
|
|
89
61
|
vector: [
|
|
90
62
|
... // query embedding
|
|
91
63
|
],
|
|
92
64
|
includeVectors: true,
|
|
65
|
+
includeMetadata: true
|
|
93
66
|
topK: 1,
|
|
94
67
|
})
|
|
95
68
|
|
|
96
|
-
|
|
97
|
-
// Since we passed the Metadata type parameter above,
|
|
98
|
-
// we can interact with metadata fields without having to
|
|
99
|
-
// do any typecasting.
|
|
100
|
-
const { title, genre, category } = results[0].metadata;
|
|
101
|
-
console.log(`The best match in fantasy was ${title}`)
|
|
102
|
-
}
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
### Upsert records
|
|
106
|
-
|
|
107
|
-
Upstash vector expects records inserted into indexes to have the following form:
|
|
108
|
-
|
|
109
|
-
```typescript
|
|
110
|
-
type UpstashRecord = {
|
|
111
|
-
id: number | string;
|
|
112
|
-
vector: number[];
|
|
113
|
-
metadata?: Record<string, unknown>;
|
|
114
|
-
};
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
#### Upsert many
|
|
118
|
-
|
|
119
|
-
To upsert some records, you can use the client like so:
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
// Prepare your data. The length of each array
|
|
123
|
-
// of vector values must match the dimension of
|
|
124
|
-
// the index where you plan to store them.
|
|
125
|
-
const records = [
|
|
126
|
-
{
|
|
127
|
-
id: "1",
|
|
128
|
-
vector: [0.236, 0.971, 0.559],
|
|
129
|
-
},
|
|
130
|
-
{
|
|
131
|
-
id: "2",
|
|
132
|
-
vector: [0.685, 0.111, 0.857],
|
|
133
|
-
},
|
|
134
|
-
];
|
|
135
|
-
|
|
136
|
-
// Upsert the data into your index
|
|
137
|
-
await index.upsert(records);
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
#### Upsert one
|
|
141
|
-
|
|
142
|
-
```typescript
|
|
143
|
-
// Prepare your data. The length of each array
|
|
144
|
-
// of vector values must match the dimension of
|
|
145
|
-
// the index where you plan to store them.
|
|
146
|
-
const record = {
|
|
147
|
-
id: "1",
|
|
148
|
-
vector: [0.236, 0.971, 0.559],
|
|
149
|
-
};
|
|
150
|
-
// Upsert the data into your index
|
|
151
|
-
await index.upsert(record);
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
### Querying
|
|
155
|
-
|
|
156
|
-
#### Querying with vector values
|
|
157
|
-
|
|
158
|
-
The query method accepts a large number of options. The dimension of the query vector must match the dimension of your index.
|
|
159
|
-
|
|
160
|
-
```typescript
|
|
161
|
-
type QueryOptions = {
|
|
162
|
-
vector: number[];
|
|
163
|
-
topK: number;
|
|
164
|
-
includeVectors?: boolean;
|
|
165
|
-
includeMetadata?: boolean;
|
|
166
|
-
};
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
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`.
|
|
170
|
-
|
|
171
|
-
```typescript
|
|
172
|
-
> await index.query({ topK: 3, vector: [ 0.22, 0.66 ]})
|
|
173
|
-
{
|
|
174
|
-
matches: [
|
|
175
|
-
{
|
|
176
|
-
id: '6345',
|
|
177
|
-
score: 1.00000012,
|
|
178
|
-
vector: [],
|
|
179
|
-
metadata: undefined
|
|
180
|
-
},
|
|
181
|
-
{
|
|
182
|
-
id: '1233',
|
|
183
|
-
score: 1.00000012,
|
|
184
|
-
vector: [],
|
|
185
|
-
metadata: undefined
|
|
186
|
-
},
|
|
187
|
-
{
|
|
188
|
-
id: '4142',
|
|
189
|
-
score: 1.00000012,
|
|
190
|
-
vector: [],
|
|
191
|
-
metadata: undefined
|
|
192
|
-
}
|
|
193
|
-
],
|
|
194
|
-
namespace: ''
|
|
195
|
-
}
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
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.
|
|
199
|
-
|
|
200
|
-
### Update a record
|
|
201
|
-
|
|
202
|
-
You may want to update vector `vector` or `metadata`. Specify the id and the attribute value you want to update.
|
|
203
|
-
|
|
204
|
-
```typescript
|
|
69
|
+
//Update Data
|
|
205
70
|
await index.upsert({
|
|
206
|
-
id: "
|
|
207
|
-
metadata: {
|
|
71
|
+
id: "upstash-rocks",
|
|
72
|
+
metadata: {
|
|
73
|
+
title: 'Star Wars',
|
|
74
|
+
genre: 'sci-fi',
|
|
75
|
+
category: 'classic'
|
|
76
|
+
}
|
|
208
77
|
});
|
|
209
|
-
```
|
|
210
78
|
|
|
211
|
-
|
|
79
|
+
//Delete record
|
|
80
|
+
await index.delete("upstash-rocks");
|
|
212
81
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
### Delete records
|
|
82
|
+
//Delete many by id
|
|
83
|
+
await index.delete(["id-1", "id-2", "id-3"]);
|
|
218
84
|
|
|
219
|
-
|
|
85
|
+
//Fetch records by their IDs
|
|
86
|
+
await index.fetch(["id-1", "id-2"]);
|
|
220
87
|
|
|
221
|
-
|
|
88
|
+
//Fetch records with range
|
|
89
|
+
await index.range({
|
|
90
|
+
cursor: 0,
|
|
91
|
+
limit: 5,
|
|
92
|
+
includeVectors: true,
|
|
93
|
+
});
|
|
222
94
|
|
|
223
|
-
|
|
224
|
-
await index.
|
|
225
|
-
```
|
|
95
|
+
//Reset index
|
|
96
|
+
await index.reset();
|
|
226
97
|
|
|
227
|
-
|
|
98
|
+
//Info about index
|
|
99
|
+
await index.info();
|
|
228
100
|
|
|
229
|
-
|
|
230
|
-
await index.
|
|
101
|
+
//Random vector based on stored vectors
|
|
102
|
+
await index.random();
|
|
231
103
|
```
|
|
232
104
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
To get statistics of your index, you can use the client like so:
|
|
236
|
-
|
|
237
|
-
```typescript
|
|
238
|
-
await index.stats(["id-1", "id-2", "id-3"]);
|
|
239
|
-
```
|
|
105
|
+
## Troubleshooting
|
|
240
106
|
|
|
241
|
-
|
|
107
|
+
We have a [Discord](upstash.com/discord) for common problems. If you can't find a solution, please [open an issue](https://github.com/upstash/vector-js/issues/new).
|
|
242
108
|
|
|
243
|
-
|
|
109
|
+
## Docs
|
|
244
110
|
|
|
245
|
-
|
|
246
|
-
await index.reset();
|
|
247
|
-
```
|
|
111
|
+
See [the documentation](https://upstash.com/docs/oss/sdks/ts/vector/overview) for details.
|
|
248
112
|
|
|
249
113
|
## Contributing
|
|
250
114
|
|
|
251
|
-
|
|
115
|
+
### [Install Bun](https://bun.sh/docs/installation)
|
|
252
116
|
|
|
253
|
-
|
|
117
|
+
### Vector Database
|
|
254
118
|
|
|
255
|
-
|
|
119
|
+
Create a new index on [Upstash](https://console.upstash.com/vector) and copy the url and token.
|
|
256
120
|
|
|
257
|
-
|
|
258
|
-
curl -fsSL https://bun.sh/install | bash
|
|
259
|
-
```
|
|
121
|
+
### Running tests
|
|
260
122
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
```bash
|
|
264
|
-
bun run fmt
|
|
123
|
+
```sh
|
|
124
|
+
bun run test
|
|
265
125
|
```
|
|
266
126
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
To run all the tests, make sure you have the relevant environment variables.
|
|
127
|
+
### Building
|
|
270
128
|
|
|
271
|
-
```
|
|
272
|
-
bun run
|
|
129
|
+
```sh
|
|
130
|
+
bun run build
|
|
273
131
|
```
|
package/dist/index.d.mts
CHANGED
|
@@ -42,7 +42,7 @@ type RequesterConfig = {
|
|
|
42
42
|
cache?: CacheSetting;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
declare const ENDPOINTS: readonly ["upsert", "query", "delete", "fetch", "reset", "range", "
|
|
45
|
+
declare const ENDPOINTS: readonly ["upsert", "query", "delete", "fetch", "reset", "range", "info"];
|
|
46
46
|
type EndpointVariants = (typeof ENDPOINTS)[number];
|
|
47
47
|
/**
|
|
48
48
|
* TResult is the raw data returned from upstash, which may need to be transformed or parsed.
|
|
@@ -87,15 +87,6 @@ declare class QueryCommand<TMetadata> extends Command<QueryResult<TMetadata>[]>
|
|
|
87
87
|
constructor(payload: QueryCommandPayload);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
type UpsertCommandPayload = {
|
|
91
|
-
id: number | string;
|
|
92
|
-
vector: number[];
|
|
93
|
-
metadata?: Record<string, unknown>;
|
|
94
|
-
};
|
|
95
|
-
declare class UpsertCommand extends Command<string> {
|
|
96
|
-
constructor(payload: UpsertCommandPayload | UpsertCommandPayload[]);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
90
|
type RangeCommandPayload = {
|
|
100
91
|
cursor: number | string;
|
|
101
92
|
limit: number;
|
|
@@ -110,17 +101,19 @@ declare class RangeCommand<TMetadata> extends Command<RangeResult<TMetadata>> {
|
|
|
110
101
|
constructor(payload: RangeCommandPayload);
|
|
111
102
|
}
|
|
112
103
|
|
|
113
|
-
type
|
|
104
|
+
type InfoResult = {
|
|
114
105
|
vectorCount: number;
|
|
115
106
|
pendingVectorCount: number;
|
|
116
107
|
indexSize: number;
|
|
108
|
+
dimension: number;
|
|
109
|
+
similarityFunction: "COSINE" | "EUCLIDEAN" | "DOT_PRODUCT";
|
|
117
110
|
};
|
|
118
111
|
|
|
119
112
|
type CommandArgs<TCommand extends new (_args: any) => any> = ConstructorParameters<TCommand>[0];
|
|
120
113
|
/**
|
|
121
114
|
* Serverless vector client for upstash vector db.
|
|
122
115
|
*/
|
|
123
|
-
declare class Index$1 {
|
|
116
|
+
declare class Index$1<TIndexMetadata extends Record<string, unknown> = Record<string, unknown>> {
|
|
124
117
|
protected client: Requester;
|
|
125
118
|
/**
|
|
126
119
|
* Create a new vector db client
|
|
@@ -166,7 +159,7 @@ declare class Index$1 {
|
|
|
166
159
|
*
|
|
167
160
|
* @returns A promise that resolves with an array of query result objects when the request to query the index is completed.
|
|
168
161
|
*/
|
|
169
|
-
query: <TMetadata>(args: CommandArgs<typeof QueryCommand>) => Promise<QueryResult<TMetadata>[]>;
|
|
162
|
+
query: <TMetadata extends Record<string, unknown> = TIndexMetadata>(args: CommandArgs<typeof QueryCommand>) => Promise<QueryResult<TMetadata>[]>;
|
|
170
163
|
/**
|
|
171
164
|
* Upserts (Updates and Inserts) specific items into the index.
|
|
172
165
|
* It's used for adding new items to the index or updating existing ones.
|
|
@@ -189,7 +182,15 @@ declare class Index$1 {
|
|
|
189
182
|
*
|
|
190
183
|
* @returns {string} A promise that resolves with the result of the upsert operation after the command is executed.
|
|
191
184
|
*/
|
|
192
|
-
upsert:
|
|
185
|
+
upsert: <TMetadata extends Record<string, unknown> = TIndexMetadata>(args: {
|
|
186
|
+
id: string | number;
|
|
187
|
+
vector: number[];
|
|
188
|
+
metadata?: (TMetadata extends infer U ? U : never) | undefined;
|
|
189
|
+
} | {
|
|
190
|
+
id: string | number;
|
|
191
|
+
vector: number[];
|
|
192
|
+
metadata?: (TMetadata extends infer U ? U : never) | undefined;
|
|
193
|
+
}[]) => Promise<string>;
|
|
193
194
|
/**
|
|
194
195
|
* It's used for retrieving specific items from the index, optionally including
|
|
195
196
|
* their metadata and feature vectors.
|
|
@@ -210,10 +211,10 @@ declare class Index$1 {
|
|
|
210
211
|
*
|
|
211
212
|
* @returns {Promise<FetchReturnResponse<TMetadata>[]>} A promise that resolves with an array of fetched items or null if not found, after the command is executed.
|
|
212
213
|
*/
|
|
213
|
-
fetch: <TMetadata>(ids:
|
|
214
|
+
fetch: <TMetadata extends Record<string, unknown> = TIndexMetadata>(ids: number[] | string[], opts?: {
|
|
214
215
|
includeMetadata?: boolean | undefined;
|
|
215
216
|
includeVectors?: boolean | undefined;
|
|
216
|
-
}) => Promise<FetchResult<TMetadata>[]>;
|
|
217
|
+
} | undefined) => Promise<FetchResult<TMetadata>[]>;
|
|
217
218
|
/**
|
|
218
219
|
* It's used for wiping an entire index.
|
|
219
220
|
*
|
|
@@ -249,19 +250,19 @@ declare class Index$1 {
|
|
|
249
250
|
*
|
|
250
251
|
* @returns {Promise<RangeReturnResponse<TMetadata>>} A promise that resolves with the response containing the next cursor and an array of vectors, after the command is executed.
|
|
251
252
|
*/
|
|
252
|
-
range: <TMetadata>(args: CommandArgs<typeof RangeCommand>) => Promise<RangeResult<TMetadata>>;
|
|
253
|
+
range: <TMetadata extends Record<string, unknown> = TIndexMetadata>(args: CommandArgs<typeof RangeCommand>) => Promise<RangeResult<TMetadata>>;
|
|
253
254
|
/**
|
|
254
|
-
* Retrieves
|
|
255
|
+
* Retrieves info from the index.
|
|
255
256
|
*
|
|
256
257
|
* @example
|
|
257
258
|
* ```js
|
|
258
|
-
* const
|
|
259
|
-
* console.log(
|
|
259
|
+
* const infoResults = await index.info();
|
|
260
|
+
* console.log(infoResults); // Outputs the result of the info operation
|
|
260
261
|
* ```
|
|
261
262
|
*
|
|
262
|
-
* @returns {Promise<
|
|
263
|
+
* @returns {Promise<InfoResult>} A promise that resolves with the response containing the vectorCount, pendingVectorCount, indexSize, dimension count and similarity algorithm after the command is executed.
|
|
263
264
|
*/
|
|
264
|
-
|
|
265
|
+
info: () => Promise<InfoResult>;
|
|
265
266
|
}
|
|
266
267
|
|
|
267
268
|
/**
|
|
@@ -286,7 +287,7 @@ type IndexConfig = {
|
|
|
286
287
|
/**
|
|
287
288
|
* Serverless vector client for upstash.
|
|
288
289
|
*/
|
|
289
|
-
declare class Index extends Index$1 {
|
|
290
|
+
declare class Index<TIndexMetadata extends Record<string, unknown> = Record<string, unknown>> extends Index$1<TIndexMetadata> {
|
|
290
291
|
/**
|
|
291
292
|
* Create a new vector client by providing the url and token
|
|
292
293
|
*
|
|
@@ -334,4 +335,4 @@ declare class Index extends Index$1 {
|
|
|
334
335
|
static fromEnv(config?: Omit<IndexConfig, "url" | "token">): Index;
|
|
335
336
|
}
|
|
336
337
|
|
|
337
|
-
export { type FetchResult, Index, type IndexConfig, type
|
|
338
|
+
export { type FetchResult, Index, type IndexConfig, type InfoResult, type QueryResult, type RangeResult, type Requester, type UpstashRequest, type UpstashResponse, type Vector };
|
package/dist/index.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ type RequesterConfig = {
|
|
|
42
42
|
cache?: CacheSetting;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
declare const ENDPOINTS: readonly ["upsert", "query", "delete", "fetch", "reset", "range", "
|
|
45
|
+
declare const ENDPOINTS: readonly ["upsert", "query", "delete", "fetch", "reset", "range", "info"];
|
|
46
46
|
type EndpointVariants = (typeof ENDPOINTS)[number];
|
|
47
47
|
/**
|
|
48
48
|
* TResult is the raw data returned from upstash, which may need to be transformed or parsed.
|
|
@@ -87,15 +87,6 @@ declare class QueryCommand<TMetadata> extends Command<QueryResult<TMetadata>[]>
|
|
|
87
87
|
constructor(payload: QueryCommandPayload);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
type UpsertCommandPayload = {
|
|
91
|
-
id: number | string;
|
|
92
|
-
vector: number[];
|
|
93
|
-
metadata?: Record<string, unknown>;
|
|
94
|
-
};
|
|
95
|
-
declare class UpsertCommand extends Command<string> {
|
|
96
|
-
constructor(payload: UpsertCommandPayload | UpsertCommandPayload[]);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
90
|
type RangeCommandPayload = {
|
|
100
91
|
cursor: number | string;
|
|
101
92
|
limit: number;
|
|
@@ -110,17 +101,19 @@ declare class RangeCommand<TMetadata> extends Command<RangeResult<TMetadata>> {
|
|
|
110
101
|
constructor(payload: RangeCommandPayload);
|
|
111
102
|
}
|
|
112
103
|
|
|
113
|
-
type
|
|
104
|
+
type InfoResult = {
|
|
114
105
|
vectorCount: number;
|
|
115
106
|
pendingVectorCount: number;
|
|
116
107
|
indexSize: number;
|
|
108
|
+
dimension: number;
|
|
109
|
+
similarityFunction: "COSINE" | "EUCLIDEAN" | "DOT_PRODUCT";
|
|
117
110
|
};
|
|
118
111
|
|
|
119
112
|
type CommandArgs<TCommand extends new (_args: any) => any> = ConstructorParameters<TCommand>[0];
|
|
120
113
|
/**
|
|
121
114
|
* Serverless vector client for upstash vector db.
|
|
122
115
|
*/
|
|
123
|
-
declare class Index$1 {
|
|
116
|
+
declare class Index$1<TIndexMetadata extends Record<string, unknown> = Record<string, unknown>> {
|
|
124
117
|
protected client: Requester;
|
|
125
118
|
/**
|
|
126
119
|
* Create a new vector db client
|
|
@@ -166,7 +159,7 @@ declare class Index$1 {
|
|
|
166
159
|
*
|
|
167
160
|
* @returns A promise that resolves with an array of query result objects when the request to query the index is completed.
|
|
168
161
|
*/
|
|
169
|
-
query: <TMetadata>(args: CommandArgs<typeof QueryCommand>) => Promise<QueryResult<TMetadata>[]>;
|
|
162
|
+
query: <TMetadata extends Record<string, unknown> = TIndexMetadata>(args: CommandArgs<typeof QueryCommand>) => Promise<QueryResult<TMetadata>[]>;
|
|
170
163
|
/**
|
|
171
164
|
* Upserts (Updates and Inserts) specific items into the index.
|
|
172
165
|
* It's used for adding new items to the index or updating existing ones.
|
|
@@ -189,7 +182,15 @@ declare class Index$1 {
|
|
|
189
182
|
*
|
|
190
183
|
* @returns {string} A promise that resolves with the result of the upsert operation after the command is executed.
|
|
191
184
|
*/
|
|
192
|
-
upsert:
|
|
185
|
+
upsert: <TMetadata extends Record<string, unknown> = TIndexMetadata>(args: {
|
|
186
|
+
id: string | number;
|
|
187
|
+
vector: number[];
|
|
188
|
+
metadata?: (TMetadata extends infer U ? U : never) | undefined;
|
|
189
|
+
} | {
|
|
190
|
+
id: string | number;
|
|
191
|
+
vector: number[];
|
|
192
|
+
metadata?: (TMetadata extends infer U ? U : never) | undefined;
|
|
193
|
+
}[]) => Promise<string>;
|
|
193
194
|
/**
|
|
194
195
|
* It's used for retrieving specific items from the index, optionally including
|
|
195
196
|
* their metadata and feature vectors.
|
|
@@ -210,10 +211,10 @@ declare class Index$1 {
|
|
|
210
211
|
*
|
|
211
212
|
* @returns {Promise<FetchReturnResponse<TMetadata>[]>} A promise that resolves with an array of fetched items or null if not found, after the command is executed.
|
|
212
213
|
*/
|
|
213
|
-
fetch: <TMetadata>(ids:
|
|
214
|
+
fetch: <TMetadata extends Record<string, unknown> = TIndexMetadata>(ids: number[] | string[], opts?: {
|
|
214
215
|
includeMetadata?: boolean | undefined;
|
|
215
216
|
includeVectors?: boolean | undefined;
|
|
216
|
-
}) => Promise<FetchResult<TMetadata>[]>;
|
|
217
|
+
} | undefined) => Promise<FetchResult<TMetadata>[]>;
|
|
217
218
|
/**
|
|
218
219
|
* It's used for wiping an entire index.
|
|
219
220
|
*
|
|
@@ -249,19 +250,19 @@ declare class Index$1 {
|
|
|
249
250
|
*
|
|
250
251
|
* @returns {Promise<RangeReturnResponse<TMetadata>>} A promise that resolves with the response containing the next cursor and an array of vectors, after the command is executed.
|
|
251
252
|
*/
|
|
252
|
-
range: <TMetadata>(args: CommandArgs<typeof RangeCommand>) => Promise<RangeResult<TMetadata>>;
|
|
253
|
+
range: <TMetadata extends Record<string, unknown> = TIndexMetadata>(args: CommandArgs<typeof RangeCommand>) => Promise<RangeResult<TMetadata>>;
|
|
253
254
|
/**
|
|
254
|
-
* Retrieves
|
|
255
|
+
* Retrieves info from the index.
|
|
255
256
|
*
|
|
256
257
|
* @example
|
|
257
258
|
* ```js
|
|
258
|
-
* const
|
|
259
|
-
* console.log(
|
|
259
|
+
* const infoResults = await index.info();
|
|
260
|
+
* console.log(infoResults); // Outputs the result of the info operation
|
|
260
261
|
* ```
|
|
261
262
|
*
|
|
262
|
-
* @returns {Promise<
|
|
263
|
+
* @returns {Promise<InfoResult>} A promise that resolves with the response containing the vectorCount, pendingVectorCount, indexSize, dimension count and similarity algorithm after the command is executed.
|
|
263
264
|
*/
|
|
264
|
-
|
|
265
|
+
info: () => Promise<InfoResult>;
|
|
265
266
|
}
|
|
266
267
|
|
|
267
268
|
/**
|
|
@@ -286,7 +287,7 @@ type IndexConfig = {
|
|
|
286
287
|
/**
|
|
287
288
|
* Serverless vector client for upstash.
|
|
288
289
|
*/
|
|
289
|
-
declare class Index extends Index$1 {
|
|
290
|
+
declare class Index<TIndexMetadata extends Record<string, unknown> = Record<string, unknown>> extends Index$1<TIndexMetadata> {
|
|
290
291
|
/**
|
|
291
292
|
* Create a new vector client by providing the url and token
|
|
292
293
|
*
|
|
@@ -334,4 +335,4 @@ declare class Index extends Index$1 {
|
|
|
334
335
|
static fromEnv(config?: Omit<IndexConfig, "url" | "token">): Index;
|
|
335
336
|
}
|
|
336
337
|
|
|
337
|
-
export { type FetchResult, Index, type IndexConfig, type
|
|
338
|
+
export { type FetchResult, Index, type IndexConfig, type InfoResult, type QueryResult, type RangeResult, type Requester, type UpstashRequest, type UpstashResponse, type Vector };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var n=class extends Error{constructor(e){super(e),this.name="UpstashError";}};var i=class{baseUrl;headers;options;retry;constructor(e){this.options={cache:e.cache,signal:e.signal},this.baseUrl=e.baseUrl.replace(/\/$/,""),this.headers={"Content-Type":"application/json",...e.headers},typeof e?.retry=="boolean"&&e?.retry===!1?this.retry={attempts:1,backoff:()=>0}:this.retry={attempts:e?.retry?.retries??5,backoff:e?.retry?.backoff??(t=>Math.exp(t)*50)};}async request(e){let t={cache:this.options.cache,method:"POST",headers:this.headers,body:JSON.stringify(e.body),keepalive:!0,signal:this.options.signal},r=null,a=null;for(let R=0;R<=this.retry.attempts;R++)try{r=await fetch([this.baseUrl,...e.path??[]].join("/"),t);break}catch(x){if(this.options.signal?.aborted){let T=new Blob([JSON.stringify({result:this.options.signal.reason??"Aborted"})]),g={status:200,statusText:this.options.signal.reason??"Aborted"};r=new Response(T,g);break}a=x,await new Promise(T=>setTimeout(T,this.retry.backoff(R)));}if(!r)throw a??new Error("Exhausted all retries");let f=await r.json();if(!r.ok)throw new n(`${f.error}, command was: ${JSON.stringify(e.body)}`);return {result:f.result,error:f.error}}};var o=class{payload;endpoint;constructor(e,t){this.payload=e,this.endpoint=t;}async exec(e){let{result:t,error:r}=await e.request({body:this.payload,path:[this.endpoint]});if(r)throw new n(r);if(typeof t>"u")throw new Error("Request did not return a result");return t}};var p=class extends o{constructor(e){let t=[];Array.isArray(e)?t.push(...e):t.push(e),super(t,"delete");}};var c=class extends o{constructor(e){super(e,"query");}};var u=class extends o{constructor(e){super(e,"upsert");}};var m=class extends o{constructor([e,t]){super({ids:e,...t},"fetch");}};var l=class extends o{constructor(e){super(e,"range");}};var d=class extends o{constructor(){super([],"reset");}};var h=class extends o{constructor(){super([],"stats");}};var y=class{client;constructor(e){this.client=e;}delete=e=>new p(e).exec(this.client);query=e=>new c(e).exec(this.client);upsert=e=>new u(e).exec(this.client);fetch=(...e)=>new m(e).exec(this.client);reset=()=>new d().exec(this.client);range=e=>new l(e).exec(this.client);stats=()=>new h().exec(this.client)};var b=class s extends y{constructor(e){if(typeof e<"u"&&"request"in e){super(e);return}let t=e?.token??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_TOKEN??process.env.UPSTASH_VECTOR_REST_TOKEN,r=e?.url??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_URL??process.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("UPSTASH_VECTOR_REST_TOKEN is missing!");if(!r)throw new Error("UPSTASH_VECTOR_REST_URL is missing!");(r.startsWith(" ")||r.endsWith(" ")||/\r|\n/.test(r))&&console.warn("The vector url contains whitespace or newline, which can cause errors!"),(t.startsWith(" ")||t.endsWith(" ")||/\r|\n/.test(t))&&console.warn("The vector token contains whitespace or newline, which can cause errors!");let a=new i({baseUrl:r,retry:e?.retry,headers:{authorization:`Bearer ${t}`},cache:e?.cache||"no-store",signal:e?.signal});super(a);}static fromEnv(e){let t=process?.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_URL`");let r=process?.env.UPSTASH_VECTOR_REST_TOKEN;if(!r)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_TOKEN`");return new s({...e,url:t,token:r})}};
|
|
4
|
-
|
|
5
|
-
exports.Index = b;
|
|
1
|
+
"use strict";var x=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var U=Object.prototype.hasOwnProperty;var S=(r,e)=>{for(var t in e)x(r,t,{get:e[t],enumerable:!0})},M=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of E(e))!U.call(r,s)&&s!==t&&x(r,s,{get:()=>e[s],enumerable:!(n=w(e,s))||n.enumerable});return r};var _=r=>M(x({},"__esModule",{value:!0}),r);var P={};S(P,{Index:()=>b});module.exports=_(P);var a=class extends Error{constructor(e){super(e),this.name="UpstashError"}};var i=class{baseUrl;headers;options;retry;constructor(e){this.options={cache:e.cache,signal:e.signal},this.baseUrl=e.baseUrl.replace(/\/$/,""),this.headers={"Content-Type":"application/json",...e.headers},typeof e?.retry=="boolean"&&e?.retry===!1?this.retry={attempts:1,backoff:()=>0}:this.retry={attempts:e?.retry?.retries??5,backoff:e?.retry?.backoff??(t=>Math.exp(t)*50)}}async request(e){let t={cache:this.options.cache,method:"POST",headers:this.headers,body:JSON.stringify(e.body),keepalive:!0,signal:this.options.signal},n=null,s=null;for(let y=0;y<=this.retry.attempts;y++)try{n=await fetch([this.baseUrl,...e.path??[]].join("/"),t);break}catch(g){if(this.options.signal?.aborted){let R=new Blob([JSON.stringify({result:this.options.signal.reason??"Aborted"})]),C={status:200,statusText:this.options.signal.reason??"Aborted"};n=new Response(R,C);break}s=g,await new Promise(R=>setTimeout(R,this.retry.backoff(y)))}if(!n)throw s??new Error("Exhausted all retries");let T=await n.json();if(!n.ok)throw new a(`${T.error}, command was: ${JSON.stringify(e.body)}`);return{result:T.result,error:T.error}}};var o=class{payload;endpoint;constructor(e,t){this.payload=e,this.endpoint=t}async exec(e){let{result:t,error:n}=await e.request({body:this.payload,path:[this.endpoint]});if(n)throw new a(n);if(typeof t>"u")throw new Error("Request did not return a result");return t}};var p=class extends o{constructor(e){let t=[];Array.isArray(e)?t.push(...e):t.push(e),super(t,"delete")}};var c=class extends o{constructor(e){super(e,"query")}};var d=class extends o{constructor(e){super(e,"upsert")}};var u=class extends o{constructor([e,t]){super({ids:e,...t},"fetch")}};var m=class extends o{constructor(e){super(e,"range")}};var l=class extends o{constructor(){super([],"reset")}};var h=class extends o{constructor(){super([],"info")}};var f=class{client;constructor(e){this.client=e}delete=e=>new p(e).exec(this.client);query=e=>new c(e).exec(this.client);upsert=e=>new d(e).exec(this.client);fetch=(...e)=>new u(e).exec(this.client);reset=()=>new l().exec(this.client);range=e=>new m(e).exec(this.client);info=()=>new h().exec(this.client)};var b=class r extends f{constructor(e){if(typeof e<"u"&&"request"in e){super(e);return}let t=e?.token??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_TOKEN??process.env.UPSTASH_VECTOR_REST_TOKEN,n=e?.url??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_URL??process.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("UPSTASH_VECTOR_REST_TOKEN is missing!");if(!n)throw new Error("UPSTASH_VECTOR_REST_URL is missing!");(n.startsWith(" ")||n.endsWith(" ")||/\r|\n/.test(n))&&console.warn("The vector url contains whitespace or newline, which can cause errors!"),(t.startsWith(" ")||t.endsWith(" ")||/\r|\n/.test(t))&&console.warn("The vector token contains whitespace or newline, which can cause errors!");let s=new i({baseUrl:n,retry:e?.retry,headers:{authorization:`Bearer ${t}`},cache:e?.cache||"no-store",signal:e?.signal});super(s)}static fromEnv(e){let t=process?.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_URL`");let n=process?.env.UPSTASH_VECTOR_REST_TOKEN;if(!n)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_TOKEN`");return new r({...e,url:t,token:n})}};0&&(module.exports={Index});
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
export { b as Index };
|
|
1
|
+
var s=class extends Error{constructor(e){super(e),this.name="UpstashError"}};var i=class{baseUrl;headers;options;retry;constructor(e){this.options={cache:e.cache,signal:e.signal},this.baseUrl=e.baseUrl.replace(/\/$/,""),this.headers={"Content-Type":"application/json",...e.headers},typeof e?.retry=="boolean"&&e?.retry===!1?this.retry={attempts:1,backoff:()=>0}:this.retry={attempts:e?.retry?.retries??5,backoff:e?.retry?.backoff??(t=>Math.exp(t)*50)}}async request(e){let t={cache:this.options.cache,method:"POST",headers:this.headers,body:JSON.stringify(e.body),keepalive:!0,signal:this.options.signal},r=null,a=null;for(let y=0;y<=this.retry.attempts;y++)try{r=await fetch([this.baseUrl,...e.path??[]].join("/"),t);break}catch(b){if(this.options.signal?.aborted){let R=new Blob([JSON.stringify({result:this.options.signal.reason??"Aborted"})]),g={status:200,statusText:this.options.signal.reason??"Aborted"};r=new Response(R,g);break}a=b,await new Promise(R=>setTimeout(R,this.retry.backoff(y)))}if(!r)throw a??new Error("Exhausted all retries");let T=await r.json();if(!r.ok)throw new s(`${T.error}, command was: ${JSON.stringify(e.body)}`);return{result:T.result,error:T.error}}};var n=class{payload;endpoint;constructor(e,t){this.payload=e,this.endpoint=t}async exec(e){let{result:t,error:r}=await e.request({body:this.payload,path:[this.endpoint]});if(r)throw new s(r);if(typeof t>"u")throw new Error("Request did not return a result");return t}};var p=class extends n{constructor(e){let t=[];Array.isArray(e)?t.push(...e):t.push(e),super(t,"delete")}};var c=class extends n{constructor(e){super(e,"query")}};var d=class extends n{constructor(e){super(e,"upsert")}};var u=class extends n{constructor([e,t]){super({ids:e,...t},"fetch")}};var m=class extends n{constructor(e){super(e,"range")}};var l=class extends n{constructor(){super([],"reset")}};var h=class extends n{constructor(){super([],"info")}};var f=class{client;constructor(e){this.client=e}delete=e=>new p(e).exec(this.client);query=e=>new c(e).exec(this.client);upsert=e=>new d(e).exec(this.client);fetch=(...e)=>new u(e).exec(this.client);reset=()=>new l().exec(this.client);range=e=>new m(e).exec(this.client);info=()=>new h().exec(this.client)};var x=class o extends f{constructor(e){if(typeof e<"u"&&"request"in e){super(e);return}let t=e?.token??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_TOKEN??process.env.UPSTASH_VECTOR_REST_TOKEN,r=e?.url??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_URL??process.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("UPSTASH_VECTOR_REST_TOKEN is missing!");if(!r)throw new Error("UPSTASH_VECTOR_REST_URL is missing!");(r.startsWith(" ")||r.endsWith(" ")||/\r|\n/.test(r))&&console.warn("The vector url contains whitespace or newline, which can cause errors!"),(t.startsWith(" ")||t.endsWith(" ")||/\r|\n/.test(t))&&console.warn("The vector token contains whitespace or newline, which can cause errors!");let a=new i({baseUrl:r,retry:e?.retry,headers:{authorization:`Bearer ${t}`},cache:e?.cache||"no-store",signal:e?.signal});super(a)}static fromEnv(e){let t=process?.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_URL`");let r=process?.env.UPSTASH_VECTOR_REST_TOKEN;if(!r)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_TOKEN`");return new o({...e,url:t,token:r})}};export{x as Index};
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{ "name": "@upstash/vector", "description": "An HTTP/REST based Vector DB client built on top of Upstash REST API.", "module": "./dist/index.mjs", "main": "./dist/index.js", "types": "./dist/index.d.ts", "version": "v1.0.
|
|
1
|
+
{ "name": "@upstash/vector", "description": "An HTTP/REST based Vector DB client built on top of Upstash REST API.", "module": "./dist/index.mjs", "main": "./dist/index.js", "types": "./dist/index.d.ts", "version": "v1.0.2-canary", "keywords": [ "vector", "upstash", "db" ], "author": "Oguzhan Olguncu <oguzhan@upstash.com>", "license": "MIT", "files": [ "dist" ], "bugs": { "url": "https://github.com/upstash/vector/issues" }, "scripts": { "test": "bun test src --coverage --bail --coverageSkipTestFiles=[test-utils.ts]", "fmt": "bunx biome check --apply ./src", "build": "tsup", "prepare": "husky install" }, "devDependencies": { "typescript": "^5.0.0", "@biomejs/biome": "^1.4.1", "@commitlint/cli": "^18.6.0", "@commitlint/config-conventional": "^18.6.0", "bun-types": "latest", "husky": "^8.0.3", "tsup": "latest" }, "repository": { "type": "git", "url": "https://github.com/upstash/vector-js" }, "homepage": "https://upstash.com/vector" }
|