chromadb 1.7.2 → 1.7.3-beta2
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/dist/chromadb.d.ts +230 -488
- package/dist/chromadb.legacy-esm.js +154 -879
- package/dist/chromadb.mjs +154 -879
- package/dist/chromadb.mjs.map +1 -1
- package/dist/cjs/chromadb.cjs +157 -885
- package/dist/cjs/chromadb.cjs.map +1 -1
- package/dist/cjs/chromadb.d.cts +230 -488
- package/package.json +2 -6
- package/src/ChromaClient.ts +41 -67
- package/src/Collection.ts +58 -17
- package/src/embeddings/DefaultEmbeddingFunction.ts +99 -0
- package/src/embeddings/TransformersEmbeddingFunction.ts +99 -0
- package/src/generated/api.ts +45 -563
- package/src/generated/configuration.ts +5 -5
- package/src/generated/index.ts +2 -2
- package/src/generated/models.ts +35 -79
- package/src/generated/runtime.ts +5 -5
- package/src/index.ts +3 -36
- package/src/types.ts +0 -75
- package/src/utils.ts +3 -17
- package/src/AdminClient.ts +0 -272
- package/src/CloudClient.ts +0 -46
- package/src/embeddings/GoogleGeminiEmbeddingFunction.ts +0 -69
- package/src/embeddings/HuggingFaceEmbeddingServerFunction.ts +0 -31
- package/src/embeddings/JinaEmbeddingFunction.ts +0 -46
package/src/AdminClient.ts
DELETED
|
@@ -1,272 +0,0 @@
|
|
|
1
|
-
import { Configuration, ApiApi as DefaultApi } from "./generated";
|
|
2
|
-
import { handleSuccess, handleError, validateTenantDatabase } from "./utils";
|
|
3
|
-
import { ConfigOptions } from './types';
|
|
4
|
-
import {
|
|
5
|
-
AuthOptions,
|
|
6
|
-
ClientAuthProtocolAdapter,
|
|
7
|
-
IsomorphicFetchClientAuthProtocolAdapter
|
|
8
|
-
} from "./auth";
|
|
9
|
-
|
|
10
|
-
const DEFAULT_TENANT = "default_tenant"
|
|
11
|
-
const DEFAULT_DATABASE = "default_database"
|
|
12
|
-
|
|
13
|
-
// interface for tenant
|
|
14
|
-
interface Tenant {
|
|
15
|
-
name: string,
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// interface for tenant
|
|
19
|
-
interface Database {
|
|
20
|
-
name: string,
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class AdminClient {
|
|
24
|
-
/**
|
|
25
|
-
* @ignore
|
|
26
|
-
*/
|
|
27
|
-
private api: DefaultApi & ConfigOptions;
|
|
28
|
-
private apiAdapter: ClientAuthProtocolAdapter<any>|undefined;
|
|
29
|
-
public tenant: string = DEFAULT_TENANT;
|
|
30
|
-
public database: string = DEFAULT_DATABASE;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Creates a new AdminClient instance.
|
|
34
|
-
* @param {Object} params - The parameters for creating a new client
|
|
35
|
-
* @param {string} [params.path] - The base path for the Chroma API.
|
|
36
|
-
* @returns {AdminClient} A new AdminClient instance.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* ```typescript
|
|
40
|
-
* const client = new AdminClient({
|
|
41
|
-
* path: "http://localhost:8000"
|
|
42
|
-
* });
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
constructor({
|
|
46
|
-
path,
|
|
47
|
-
fetchOptions,
|
|
48
|
-
auth,
|
|
49
|
-
tenant = DEFAULT_TENANT,
|
|
50
|
-
database = DEFAULT_DATABASE
|
|
51
|
-
}: {
|
|
52
|
-
path?: string,
|
|
53
|
-
fetchOptions?: RequestInit,
|
|
54
|
-
auth?: AuthOptions,
|
|
55
|
-
tenant?: string,
|
|
56
|
-
database?: string,
|
|
57
|
-
} = {}) {
|
|
58
|
-
if (path === undefined) path = "http://localhost:8000";
|
|
59
|
-
this.tenant = tenant;
|
|
60
|
-
this.database = database;
|
|
61
|
-
|
|
62
|
-
const apiConfig: Configuration = new Configuration({
|
|
63
|
-
basePath: path,
|
|
64
|
-
});
|
|
65
|
-
if (auth !== undefined) {
|
|
66
|
-
this.apiAdapter = new IsomorphicFetchClientAuthProtocolAdapter(new DefaultApi(apiConfig), auth);
|
|
67
|
-
this.api = this.apiAdapter.getApi();
|
|
68
|
-
} else {
|
|
69
|
-
this.api = new DefaultApi(apiConfig);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
this.api.options = fetchOptions ?? {};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Sets the tenant and database for the client.
|
|
77
|
-
*
|
|
78
|
-
* @param {Object} params - The parameters for setting tenant and database.
|
|
79
|
-
* @param {string} params.tenant - The name of the tenant.
|
|
80
|
-
* @param {string} params.database - The name of the database.
|
|
81
|
-
*
|
|
82
|
-
* @returns {Promise<void>} A promise that returns nothing
|
|
83
|
-
* @throws {Error} Any issues
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* ```typescript
|
|
87
|
-
* await adminClient.setTenant({
|
|
88
|
-
* tenant: "my_tenant",
|
|
89
|
-
* database: "my_database",
|
|
90
|
-
* });
|
|
91
|
-
* ```
|
|
92
|
-
*/
|
|
93
|
-
public async setTenant({
|
|
94
|
-
tenant = DEFAULT_TENANT,
|
|
95
|
-
database = DEFAULT_DATABASE
|
|
96
|
-
}: {
|
|
97
|
-
tenant: string,
|
|
98
|
-
database?: string,
|
|
99
|
-
}): Promise<void> {
|
|
100
|
-
await validateTenantDatabase(this, tenant, database);
|
|
101
|
-
this.tenant = tenant;
|
|
102
|
-
this.database = database;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Sets the database for the client.
|
|
107
|
-
*
|
|
108
|
-
* @param {Object} params - The parameters for setting the database.
|
|
109
|
-
* @param {string} params.database - The name of the database.
|
|
110
|
-
*
|
|
111
|
-
* @returns {Promise<void>} A promise that returns nothing
|
|
112
|
-
* @throws {Error} Any issues
|
|
113
|
-
*
|
|
114
|
-
* @example
|
|
115
|
-
* ```typescript
|
|
116
|
-
* await adminClient.setDatabase({
|
|
117
|
-
* database: "my_database",
|
|
118
|
-
* });
|
|
119
|
-
* ```
|
|
120
|
-
*/
|
|
121
|
-
public async setDatabase({
|
|
122
|
-
database = DEFAULT_DATABASE
|
|
123
|
-
}: {
|
|
124
|
-
database?: string,
|
|
125
|
-
}): Promise<void> {
|
|
126
|
-
await validateTenantDatabase(this, this.tenant, database);
|
|
127
|
-
this.database = database;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Creates a new tenant with the specified properties.
|
|
132
|
-
*
|
|
133
|
-
* @param {Object} params - The parameters for creating a new tenant.
|
|
134
|
-
* @param {string} params.name - The name of the tenant.
|
|
135
|
-
*
|
|
136
|
-
* @returns {Promise<Tenant>} A promise that resolves to the created tenant.
|
|
137
|
-
* @throws {Error} If there is an issue creating the tenant.
|
|
138
|
-
*
|
|
139
|
-
* @example
|
|
140
|
-
* ```typescript
|
|
141
|
-
* await adminClient.createTenant({
|
|
142
|
-
* name: "my_tenant",
|
|
143
|
-
* });
|
|
144
|
-
* ```
|
|
145
|
-
*/
|
|
146
|
-
public async createTenant({
|
|
147
|
-
name,
|
|
148
|
-
}: {
|
|
149
|
-
name: string,
|
|
150
|
-
}): Promise<Tenant> {
|
|
151
|
-
const newTenant = await this.api
|
|
152
|
-
.createTenant({name}, this.api.options)
|
|
153
|
-
.then(handleSuccess)
|
|
154
|
-
.catch(handleError);
|
|
155
|
-
|
|
156
|
-
// newTenant is null if successful
|
|
157
|
-
if (newTenant && newTenant.error) {
|
|
158
|
-
throw new Error(newTenant.error);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return {name: name} as Tenant
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Gets a tenant with the specified properties.
|
|
166
|
-
*
|
|
167
|
-
* @param {Object} params - The parameters for getting a tenant.
|
|
168
|
-
* @param {string} params.name - The name of the tenant.
|
|
169
|
-
*
|
|
170
|
-
* @returns {Promise<Tenant>} A promise that resolves to the tenant.
|
|
171
|
-
* @throws {Error} If there is an issue getting the tenant.
|
|
172
|
-
*
|
|
173
|
-
* @example
|
|
174
|
-
* ```typescript
|
|
175
|
-
* await adminClient.getTenant({
|
|
176
|
-
* name: "my_tenant",
|
|
177
|
-
* });
|
|
178
|
-
* ```
|
|
179
|
-
*/
|
|
180
|
-
public async getTenant({
|
|
181
|
-
name,
|
|
182
|
-
}: {
|
|
183
|
-
name: string,
|
|
184
|
-
}): Promise<Tenant> {
|
|
185
|
-
const getTenant = await this.api
|
|
186
|
-
.getTenant(name, this.api.options)
|
|
187
|
-
.then(handleSuccess)
|
|
188
|
-
.catch(handleError);
|
|
189
|
-
|
|
190
|
-
if (getTenant.error) {
|
|
191
|
-
throw new Error(getTenant.error);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return {name: getTenant.name} as Tenant
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Creates a new database with the specified properties.
|
|
199
|
-
*
|
|
200
|
-
* @param {Object} params - The parameters for creating a new database.
|
|
201
|
-
* @param {string} params.name - The name of the database.
|
|
202
|
-
* @param {string} params.tenantName - The name of the tenant.
|
|
203
|
-
*
|
|
204
|
-
* @returns {Promise<Database>} A promise that resolves to the created database.
|
|
205
|
-
* @throws {Error} If there is an issue creating the database.
|
|
206
|
-
*
|
|
207
|
-
* @example
|
|
208
|
-
* ```typescript
|
|
209
|
-
* await adminClient.createDatabase({
|
|
210
|
-
* name: "my_database",
|
|
211
|
-
* tenantName: "my_tenant",
|
|
212
|
-
* });
|
|
213
|
-
* ```
|
|
214
|
-
*/
|
|
215
|
-
public async createDatabase({
|
|
216
|
-
name,
|
|
217
|
-
tenantName
|
|
218
|
-
}: {
|
|
219
|
-
name: string,
|
|
220
|
-
tenantName: string,
|
|
221
|
-
}): Promise<Database> {
|
|
222
|
-
const newDatabase = await this.api
|
|
223
|
-
.createDatabase(tenantName, {name}, this.api.options)
|
|
224
|
-
.then(handleSuccess)
|
|
225
|
-
.catch(handleError);
|
|
226
|
-
|
|
227
|
-
// newDatabase is null if successful
|
|
228
|
-
if (newDatabase && newDatabase.error) {
|
|
229
|
-
throw new Error(newDatabase.error);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
return {name: name} as Database
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Gets a database with the specified properties.
|
|
237
|
-
*
|
|
238
|
-
* @param {Object} params - The parameters for getting a database.
|
|
239
|
-
* @param {string} params.name - The name of the database.
|
|
240
|
-
* @param {string} params.tenantName - The name of the tenant.
|
|
241
|
-
*
|
|
242
|
-
* @returns {Promise<Database>} A promise that resolves to the database.
|
|
243
|
-
* @throws {Error} If there is an issue getting the database.
|
|
244
|
-
*
|
|
245
|
-
* @example
|
|
246
|
-
* ```typescript
|
|
247
|
-
* await adminClient.getDatabase({
|
|
248
|
-
* name: "my_database",
|
|
249
|
-
* tenantName: "my_tenant",
|
|
250
|
-
* });
|
|
251
|
-
* ```
|
|
252
|
-
*/
|
|
253
|
-
public async getDatabase({
|
|
254
|
-
name,
|
|
255
|
-
tenantName
|
|
256
|
-
}: {
|
|
257
|
-
name: string,
|
|
258
|
-
tenantName: string,
|
|
259
|
-
}): Promise<Database> {
|
|
260
|
-
const getDatabase = await this.api
|
|
261
|
-
.getDatabase(name, tenantName, this.api.options)
|
|
262
|
-
.then(handleSuccess)
|
|
263
|
-
.catch(handleError);
|
|
264
|
-
|
|
265
|
-
if (getDatabase.error) {
|
|
266
|
-
throw new Error(getDatabase.error);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
return {name: getDatabase.name} as Database
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
}
|
package/src/CloudClient.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
// create a cloudclient class that takes in an api key and an optional database
|
|
3
|
-
// this should wrap ChromaClient and specify the auth scheme correctly
|
|
4
|
-
|
|
5
|
-
import { ChromaClient } from "./ChromaClient";
|
|
6
|
-
|
|
7
|
-
interface CloudClientParams {
|
|
8
|
-
apiKey?: string;
|
|
9
|
-
database?: string;
|
|
10
|
-
cloudHost?: string;
|
|
11
|
-
cloudPort?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
class CloudClient extends ChromaClient{
|
|
15
|
-
|
|
16
|
-
constructor({apiKey, database, cloudHost, cloudPort}: CloudClientParams) {
|
|
17
|
-
// If no API key is provided, try to load it from the environment variable
|
|
18
|
-
if (!apiKey) {
|
|
19
|
-
apiKey = process.env.CHROMA_API_KEY;
|
|
20
|
-
}
|
|
21
|
-
if (!apiKey) {
|
|
22
|
-
throw new Error("No API key provided");
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
cloudHost = cloudHost || "https://api.trychroma.com";
|
|
26
|
-
cloudPort = cloudPort || "8000";
|
|
27
|
-
|
|
28
|
-
const path = `${cloudHost}:${cloudPort}`;
|
|
29
|
-
|
|
30
|
-
const auth = {
|
|
31
|
-
provider: "token",
|
|
32
|
-
credentials: apiKey,
|
|
33
|
-
providerOptions: { headerType: "X_CHROMA_TOKEN" },
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return new ChromaClient({
|
|
37
|
-
path: path,
|
|
38
|
-
auth: auth,
|
|
39
|
-
database: database,
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
super()
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export { CloudClient };
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { IEmbeddingFunction } from "./IEmbeddingFunction";
|
|
2
|
-
|
|
3
|
-
let googleGenAiApi: any;
|
|
4
|
-
|
|
5
|
-
export class GoogleGenerativeAiEmbeddingFunction implements IEmbeddingFunction {
|
|
6
|
-
private api_key: string;
|
|
7
|
-
private model: string;
|
|
8
|
-
private googleGenAiApi?: any;
|
|
9
|
-
private taskType: string;
|
|
10
|
-
|
|
11
|
-
constructor({ googleApiKey, model, taskType }: { googleApiKey: string, model?: string, taskType?: string }) {
|
|
12
|
-
// we used to construct the client here, but we need to async import the types
|
|
13
|
-
// for the openai npm package, and the constructor can not be async
|
|
14
|
-
this.api_key = googleApiKey;
|
|
15
|
-
this.model = model || "embedding-001";
|
|
16
|
-
this.taskType = taskType || "RETRIEVAL_DOCUMENT";
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
private async loadClient() {
|
|
20
|
-
if(this.googleGenAiApi) return;
|
|
21
|
-
try {
|
|
22
|
-
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
|
|
23
|
-
const { googleGenAi } = await GoogleGenerativeAiEmbeddingFunction.import();
|
|
24
|
-
googleGenAiApi = googleGenAi;
|
|
25
|
-
// googleGenAiApi.init(this.api_key);
|
|
26
|
-
googleGenAiApi = new googleGenAiApi(this.api_key);
|
|
27
|
-
} catch (_a) {
|
|
28
|
-
// @ts-ignore
|
|
29
|
-
if (_a.code === 'MODULE_NOT_FOUND') {
|
|
30
|
-
throw new Error("Please install the @google/generative-ai package to use the GoogleGenerativeAiEmbeddingFunction, `npm install -S @google/generative-ai`");
|
|
31
|
-
}
|
|
32
|
-
throw _a; // Re-throw other errors
|
|
33
|
-
}
|
|
34
|
-
this.googleGenAiApi = googleGenAiApi;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public async generate(texts: string[]) {
|
|
38
|
-
|
|
39
|
-
await this.loadClient();
|
|
40
|
-
const model = this.googleGenAiApi.getGenerativeModel({ model: this.model});
|
|
41
|
-
const response = await model.batchEmbedContents({
|
|
42
|
-
requests: texts.map((t) => ({
|
|
43
|
-
content: { parts: [{ text: t }] },
|
|
44
|
-
taskType: this.taskType,
|
|
45
|
-
})),
|
|
46
|
-
});
|
|
47
|
-
const embeddings = response.embeddings.map((e: any) => e.values);
|
|
48
|
-
|
|
49
|
-
return embeddings;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** @ignore */
|
|
53
|
-
static async import(): Promise<{
|
|
54
|
-
// @ts-ignore
|
|
55
|
-
googleGenAi: typeof import("@google/generative-ai");
|
|
56
|
-
}> {
|
|
57
|
-
try {
|
|
58
|
-
// @ts-ignore
|
|
59
|
-
const { GoogleGenerativeAI } = await import("@google/generative-ai");
|
|
60
|
-
const googleGenAi = GoogleGenerativeAI;
|
|
61
|
-
return { googleGenAi };
|
|
62
|
-
} catch (e) {
|
|
63
|
-
throw new Error(
|
|
64
|
-
"Please install @google/generative-ai as a dependency with, e.g. `yarn add @google/generative-ai`"
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { IEmbeddingFunction } from "./IEmbeddingFunction";
|
|
2
|
-
|
|
3
|
-
let CohereAiApi: any;
|
|
4
|
-
|
|
5
|
-
export class HuggingFaceEmbeddingServerFunction implements IEmbeddingFunction {
|
|
6
|
-
private url: string;
|
|
7
|
-
|
|
8
|
-
constructor({ url }: { url: string }) {
|
|
9
|
-
// we used to construct the client here, but we need to async import the types
|
|
10
|
-
// for the openai npm package, and the constructor can not be async
|
|
11
|
-
this.url = url;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public async generate(texts: string[]) {
|
|
15
|
-
const response = await fetch(this.url, {
|
|
16
|
-
method: 'POST',
|
|
17
|
-
headers: {
|
|
18
|
-
'Content-Type': 'application/json'
|
|
19
|
-
},
|
|
20
|
-
body: JSON.stringify({ 'inputs': texts })
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
if (!response.ok) {
|
|
24
|
-
throw new Error(`Failed to generate embeddings: ${response.statusText}`);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const data = await response.json();
|
|
28
|
-
return data;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { IEmbeddingFunction } from "./IEmbeddingFunction";
|
|
2
|
-
|
|
3
|
-
export class JinaEmbeddingFunction implements IEmbeddingFunction {
|
|
4
|
-
private model_name: string;
|
|
5
|
-
private api_url: string;
|
|
6
|
-
private headers: { [key: string]: string };
|
|
7
|
-
|
|
8
|
-
constructor({ jinaai_api_key, model_name }: { jinaai_api_key: string; model_name?: string }) {
|
|
9
|
-
this.model_name = model_name || 'jina-embeddings-v2-base-en';
|
|
10
|
-
this.api_url = 'https://api.jina.ai/v1/embeddings';
|
|
11
|
-
this.headers = {
|
|
12
|
-
Authorization: `Bearer ${jinaai_api_key}`,
|
|
13
|
-
'Accept-Encoding': 'identity',
|
|
14
|
-
'Content-Type': 'application/json',
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public async generate(texts: string[]) {
|
|
19
|
-
try {
|
|
20
|
-
const response = await fetch(this.api_url, {
|
|
21
|
-
method: 'POST',
|
|
22
|
-
headers: this.headers,
|
|
23
|
-
body: JSON.stringify({
|
|
24
|
-
input: texts,
|
|
25
|
-
model: this.model_name,
|
|
26
|
-
}),
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
const data = (await response.json()) as { data: any[]; detail: string };
|
|
30
|
-
if (!data || !data.data) {
|
|
31
|
-
throw new Error(data.detail);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const embeddings: any[] = data.data;
|
|
35
|
-
const sortedEmbeddings = embeddings.sort((a, b) => a.index - b.index);
|
|
36
|
-
|
|
37
|
-
return sortedEmbeddings.map((result) => result.embedding);
|
|
38
|
-
} catch (error) {
|
|
39
|
-
if (error instanceof Error) {
|
|
40
|
-
throw new Error(`Error calling Jina AI API: ${error.message}`);
|
|
41
|
-
} else {
|
|
42
|
-
throw new Error(`Error calling Jina AI API: ${error}`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|