@relevanceai/sdk 2.0.0 → 2.0.2
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-cjs/generated/VecDBApi.js +1682 -0
- package/dist-cjs/generated/_VecDBApiSchemaTypes.js +3 -0
- package/dist-cjs/generated/index.js +17 -0
- package/dist-cjs/index.js +18 -0
- package/dist-cjs/services/discovery/Dataset.js +126 -0
- package/dist-cjs/services/discovery/index.js +159 -0
- package/dist-cjs/services/index.js +18 -0
- package/dist-cjs/services/vecdb/Dataset.js +137 -0
- package/dist-cjs/services/vecdb/index.js +137 -0
- package/dist-cjs/shared/BaseClient.js +35 -0
- package/dist-cjs/shared/generate.js +90 -0
- package/dist-cjs/shared/serviceConfigs.js +10 -0
- package/dist-es/generated/VecDBApi.js +2579 -0
- package/dist-es/generated/_VecDBApiSchemaTypes.js +2 -0
- package/dist-es/generated/index.js +1 -0
- package/dist-es/index.js +2 -0
- package/dist-es/services/discovery/Dataset.js +126 -0
- package/dist-es/services/discovery/index.js +159 -0
- package/dist-es/services/index.js +2 -0
- package/dist-es/services/vecdb/Dataset.js +356 -0
- package/dist-es/services/vecdb/index.js +184 -0
- package/dist-es/shared/BaseClient.js +82 -0
- package/dist-es/shared/generate.js +224 -0
- package/dist-es/shared/serviceConfigs.js +7 -0
- package/dist-types/generated/VecDBApi.d.ts +632 -0
- package/dist-types/generated/_VecDBApiSchemaTypes.d.ts +22299 -0
- package/dist-types/generated/index.d.ts +1 -0
- package/dist-types/index.d.ts +2 -0
- package/dist-types/services/discovery/Dataset.d.ts +0 -0
- package/dist-types/services/discovery/index.d.ts +0 -0
- package/dist-types/services/index.d.ts +1 -0
- package/dist-types/services/vecdb/Dataset.d.ts +52 -0
- package/dist-types/services/vecdb/index.d.ts +44 -0
- package/dist-types/shared/BaseClient.d.ts +28 -0
- package/dist-types/shared/generate.d.ts +1 -0
- package/dist-types/shared/serviceConfigs.d.ts +8 -0
- package/package.json +1 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./VecDBApi"), exports);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./generated"), exports);
|
|
18
|
+
__exportStar(require("./services"), exports);
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// import { QueryBuilder, DiscoveryClient, _QueryBuilder } from ".";
|
|
3
|
+
// import { DeleteDocumentOutput, DeleteWhereOutput, GetDocumentOutput, SimpleSearchPostOutput, BulkInsertOutput,UpdateWhereOutput,BulkUpdateOutput } from "../..";
|
|
4
|
+
// import { _GenericMethodOptions } from "../../shared/BaseClient";
|
|
5
|
+
// interface searchOptions {
|
|
6
|
+
// debounce?:number;
|
|
7
|
+
// rawPayload?:any;
|
|
8
|
+
// }
|
|
9
|
+
// export class Dataset {
|
|
10
|
+
// client: DiscoveryClient;
|
|
11
|
+
// name: string;
|
|
12
|
+
// config: any;
|
|
13
|
+
// debounceTimer?:NodeJS.Timeout;
|
|
14
|
+
// constructor(client: DiscoveryClient, name: string, options: any) {
|
|
15
|
+
// // TODO validate name
|
|
16
|
+
// this.client = client;
|
|
17
|
+
// this.name = name;
|
|
18
|
+
// this.config = options;
|
|
19
|
+
// }
|
|
20
|
+
// get datasetName(): string {
|
|
21
|
+
// return this.name;
|
|
22
|
+
// };
|
|
23
|
+
// async insertDocument(document: any, options?: _GenericMethodOptions) {
|
|
24
|
+
// const response = await this.client.apiClient.Insert({
|
|
25
|
+
// document,
|
|
26
|
+
// ...options
|
|
27
|
+
// }, { dataset_id: this.name });
|
|
28
|
+
// return response.body;
|
|
29
|
+
// }
|
|
30
|
+
// // without options
|
|
31
|
+
// async search(): Promise<SimpleSearchPostOutput>;
|
|
32
|
+
// async search(query?: _QueryBuilder): Promise<SimpleSearchPostOutput>;
|
|
33
|
+
// async search(options?:searchOptions): Promise<SimpleSearchPostOutput>;
|
|
34
|
+
// async search(query?: _QueryBuilder,options?:searchOptions): Promise<SimpleSearchPostOutput>;
|
|
35
|
+
// async search(...args:any[]) {
|
|
36
|
+
// let payload: any = {};
|
|
37
|
+
// let options:searchOptions = {};
|
|
38
|
+
// for (const arg of args) {
|
|
39
|
+
// if (arg instanceof _QueryBuilder) {
|
|
40
|
+
// payload = {...payload,...arg.build()};
|
|
41
|
+
// }
|
|
42
|
+
// else {
|
|
43
|
+
// options = arg;
|
|
44
|
+
// if (options.rawPayload) payload = {...payload,...options.rawPayload};
|
|
45
|
+
// }
|
|
46
|
+
// }
|
|
47
|
+
// const reqCallback = async () => await this.client.apiClient.SimpleSearchPost(payload, { dataset_id: this.name });
|
|
48
|
+
// if (options.debounce && this.debounceTimer) {
|
|
49
|
+
// clearTimeout(this.debounceTimer);
|
|
50
|
+
// return new Promise((resolve) => {
|
|
51
|
+
// this.debounceTimer = setTimeout(async () => {const res = await reqCallback();resolve(res)},options.debounce);
|
|
52
|
+
// });
|
|
53
|
+
// } else {
|
|
54
|
+
// const response = await reqCallback();
|
|
55
|
+
// return response.body;
|
|
56
|
+
// }
|
|
57
|
+
// }
|
|
58
|
+
// async insertDocuments(documents: any, options?: _GenericMethodOptions & { batchSize?: number,retryCount?:number, progressCallback?: (progress:BulkInsertOutput[]) => any }):Promise<BulkInsertOutput> {
|
|
59
|
+
// const results = await this._GenericBulkOperation<any,BulkInsertOutput>({
|
|
60
|
+
// data:documents??[],
|
|
61
|
+
// ...options,
|
|
62
|
+
// fn:async (documentsSlice) => (await this.client.apiClient.BulkInsert({ documents:documentsSlice }, { dataset_id: this.name })).body
|
|
63
|
+
// });
|
|
64
|
+
// const finalResults = results.reduce((prev,cur) => {
|
|
65
|
+
// prev.failed_documents = prev.failed_documents.concat(cur.failed_documents);
|
|
66
|
+
// prev.inserted += cur.inserted
|
|
67
|
+
// return prev;
|
|
68
|
+
// },{inserted:0,failed_documents:[]});
|
|
69
|
+
// return finalResults;
|
|
70
|
+
// }
|
|
71
|
+
// // TODO - ChunkSearch, insert, insertAndVectorize?, vectorize,
|
|
72
|
+
// async _GenericBulkOperation<InputItem,OutputItem>({data,batchSize,fn,retryCount}:{
|
|
73
|
+
// data:InputItem[],
|
|
74
|
+
// fn:(data:InputItem[]) => Promise<OutputItem>,
|
|
75
|
+
// batchSize?:number,
|
|
76
|
+
// retryCount?:number,
|
|
77
|
+
// progressCallback?:(progress:OutputItem[]) => any
|
|
78
|
+
// }):Promise<OutputItem[]> {
|
|
79
|
+
// batchSize = batchSize ?? 10000;
|
|
80
|
+
// retryCount = retryCount ?? 1;
|
|
81
|
+
// const results:OutputItem[] = [];
|
|
82
|
+
// for (let i = 0; i < data?.length; i += batchSize) {
|
|
83
|
+
// for (let retrysSoFar = 0; retrysSoFar < retryCount; retrysSoFar++) {
|
|
84
|
+
// try {
|
|
85
|
+
// const res = await fn(data.slice(i, i + batchSize));
|
|
86
|
+
// results.push(res);
|
|
87
|
+
// break;
|
|
88
|
+
// } catch (e) { console.error(`Bulk operation failed with error, retrying - ${e}`) }
|
|
89
|
+
// }
|
|
90
|
+
// }
|
|
91
|
+
// return results;
|
|
92
|
+
// }
|
|
93
|
+
// async updateDocument(documentId: string, partialUpdates: any) {
|
|
94
|
+
// const response = await this.client.apiClient.Update({ id: documentId, updates: partialUpdates });
|
|
95
|
+
// return response.body;
|
|
96
|
+
// }
|
|
97
|
+
// async updateDocuments(updates: any, options?: _GenericMethodOptions & { batchSize?: number,retryCount?:number, progressCallback?: (progress:BulkUpdateOutput[]) => any }):Promise<BulkUpdateOutput> {
|
|
98
|
+
// const results = await this._GenericBulkOperation<any,BulkUpdateOutput>({
|
|
99
|
+
// data:updates??[],
|
|
100
|
+
// ...options,
|
|
101
|
+
// fn:async (updatesSlice) => (await this.client.apiClient.BulkUpdate({ updates:updatesSlice }, { dataset_id: this.name })).body
|
|
102
|
+
// });
|
|
103
|
+
// const finalResults = results.reduce((prev,cur) => {
|
|
104
|
+
// prev.failed_documents = prev.failed_documents.concat(cur.failed_documents);
|
|
105
|
+
// prev.inserted += cur.inserted
|
|
106
|
+
// return prev;
|
|
107
|
+
// },{inserted:0,failed_documents:[]});
|
|
108
|
+
// return finalResults;
|
|
109
|
+
// }
|
|
110
|
+
// async updateDocumentsWhere(filters: _QueryBuilder, partialUpdates: {[id:string]:any}):Promise<UpdateWhereOutput> {
|
|
111
|
+
// return (await this.client.apiClient.UpdateWhere({ filters: filters.build().filters, updates: partialUpdates })).body;
|
|
112
|
+
// }
|
|
113
|
+
// async getDocument(documentId: string):Promise<GetDocumentOutput> {
|
|
114
|
+
// return (await this.client.apiClient.GetDocument({document_id:documentId})).body;
|
|
115
|
+
// }
|
|
116
|
+
// async deleteDocument(documentId: string):Promise<DeleteDocumentOutput> {
|
|
117
|
+
// return (await this.client.apiClient.DeleteDocument({id:documentId})).body;
|
|
118
|
+
// }
|
|
119
|
+
// async deleteDocuments(documentIds: [string]):Promise<DeleteWhereOutput> {
|
|
120
|
+
// const filters = QueryBuilder().match('_id',documentIds);
|
|
121
|
+
// return (await this.client.apiClient.DeleteWhere({ filters: filters.build().filters??[] })).body;
|
|
122
|
+
// }
|
|
123
|
+
// async deleteDocumentsWhere(filters: _QueryBuilder):Promise<DeleteWhereOutput> {
|
|
124
|
+
// return (await this.client.apiClient.DeleteWhere({ filters: filters.build().filters??[]})).body;
|
|
125
|
+
// }
|
|
126
|
+
// }
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// import { DiscoveryApiClient, BulkInsertInput, BulkInsertOutput } from '../../';
|
|
3
|
+
// import { _ClientInput, CommandInput, CommandOutput, _GenericMethodOptions } from '../../shared/BaseClient';
|
|
4
|
+
// import { operations, components } from '../../generated/_DiscoveryApiSchemaTypes';
|
|
5
|
+
// import { Dataset } from './Dataset';
|
|
6
|
+
// type bodyType = operations['SimpleSearchPost']['requestBody']['content']['application/json'];
|
|
7
|
+
// export function QueryBuilder():_QueryBuilder{
|
|
8
|
+
// return new _QueryBuilder();
|
|
9
|
+
// }
|
|
10
|
+
// export function FilterBuilder():_FilterBuilder{
|
|
11
|
+
// return new _FilterBuilder();
|
|
12
|
+
// }
|
|
13
|
+
// export class _FilterBuilder {
|
|
14
|
+
// body: bodyType;
|
|
15
|
+
// constructor() {
|
|
16
|
+
// this.body = {filters:[],fieldsToAggregate:[],fieldsToAggregateStats:[]};
|
|
17
|
+
// }
|
|
18
|
+
// buildFilters() {
|
|
19
|
+
// return this.body.filters;
|
|
20
|
+
// }
|
|
21
|
+
// rawFilter(filter: components['schemas']['filterListItem']) {
|
|
22
|
+
// this.body.filters?.push(filter);
|
|
23
|
+
// return this;
|
|
24
|
+
// }
|
|
25
|
+
// filter(type: string, key: string, value: string, ...options: any) {
|
|
26
|
+
// this.body.filters?.push({
|
|
27
|
+
// [type]: {
|
|
28
|
+
// key,
|
|
29
|
+
// value,
|
|
30
|
+
// ...options
|
|
31
|
+
// }
|
|
32
|
+
// });
|
|
33
|
+
// return this;
|
|
34
|
+
// }
|
|
35
|
+
// match(field: string, value: any) {
|
|
36
|
+
// this.body.filters?.push({ match: { key: field, value } });
|
|
37
|
+
// return this;
|
|
38
|
+
// }
|
|
39
|
+
// wildcard(field: string, value: any) {
|
|
40
|
+
// this.body.filters?.push({ wildcard: { key: field, value } });
|
|
41
|
+
// return this;
|
|
42
|
+
// }
|
|
43
|
+
// selfreference(fielda: string, fieldb: string, operation: "<=" | ">=" | "<" | ">" | "==" | "!=") {
|
|
44
|
+
// this.body.filters?.push({ selfreference: { a: fielda, b: fieldb, operation } });
|
|
45
|
+
// return this;
|
|
46
|
+
// }
|
|
47
|
+
// range(field: string, options: Omit<components['schemas']['filterListItem']['range'],'key'>) {
|
|
48
|
+
// this.body.filters?.push({ range: { key: field, ...options } });
|
|
49
|
+
// return this;
|
|
50
|
+
// }
|
|
51
|
+
// or(filters: _FilterBuilder[]) {
|
|
52
|
+
// this.body.filters?.push({ or: filters.map(f => f.body.filters ?? []) });
|
|
53
|
+
// return this;
|
|
54
|
+
// }
|
|
55
|
+
// not(filter: _FilterBuilder) {
|
|
56
|
+
// this.body.filters?.push({ not: filter.body?.filters ?? [] });
|
|
57
|
+
// return this;
|
|
58
|
+
// }
|
|
59
|
+
// }
|
|
60
|
+
// export class _QueryBuilder extends _FilterBuilder {
|
|
61
|
+
// defaultQueryValue?: string;
|
|
62
|
+
// shouldPerformTextQuery:boolean;
|
|
63
|
+
// constructor() {
|
|
64
|
+
// super();
|
|
65
|
+
// this.shouldPerformTextQuery = false;
|
|
66
|
+
// }
|
|
67
|
+
// build() {
|
|
68
|
+
// if (!this.shouldPerformTextQuery) return this.body;
|
|
69
|
+
// if (!this.defaultQueryValue) throw new Error("Please set the search query by calling .query('my search query') before performing a text search.");
|
|
70
|
+
// this.body.query = this.defaultQueryValue;
|
|
71
|
+
// return this.body;
|
|
72
|
+
// }
|
|
73
|
+
// query(query:string,fieldsToSearch?: bodyType['fieldsToSearch']) {
|
|
74
|
+
// this.defaultQueryValue = query;
|
|
75
|
+
// if (fieldsToSearch) this.body.fieldsToSearch = fieldsToSearch;
|
|
76
|
+
// return this;
|
|
77
|
+
// }
|
|
78
|
+
// queryConfig(weight:number,options?:bodyType['queryConfig']) {
|
|
79
|
+
// this.body.queryConfig = {weight,...(options??{})};
|
|
80
|
+
// return this;
|
|
81
|
+
// }
|
|
82
|
+
// text(field?:string): _QueryBuilder;
|
|
83
|
+
// text(field?:string,weight?:number):_QueryBuilder{
|
|
84
|
+
// this.shouldPerformTextQuery = true;
|
|
85
|
+
// if (!field) return this; // support searching all fields
|
|
86
|
+
// if (!this.body.fieldsToSearch) this.body.fieldsToSearch = [];
|
|
87
|
+
// if (!weight) this.body.fieldsToSearch.push(field);
|
|
88
|
+
// else this.body.fieldsToSearch.push({field,weight});
|
|
89
|
+
// return this;
|
|
90
|
+
// }
|
|
91
|
+
// vector(field: string, weight?: number): _QueryBuilder;
|
|
92
|
+
// vector(field: string, options?: components['schemas']['vectorSearchQuery']): _QueryBuilder;
|
|
93
|
+
// vector(field: string, weight?: number, options?: components['schemas']['vectorSearchQuery']): _QueryBuilder;
|
|
94
|
+
// vector(field: string, ...args:any[]) {
|
|
95
|
+
// if (!Array.isArray(this.body.vectorSearchQuery)) this.body.vectorSearchQuery = [];
|
|
96
|
+
// let payload:components['schemas']['vectorSearchQuery'] = {field};
|
|
97
|
+
// const inferredModelMatch = field.match(/_(.*)_.*vector_/) // title_text@1-0_vector_ -> text@1-0
|
|
98
|
+
// if (inferredModelMatch && inferredModelMatch[1]) payload.model = inferredModelMatch[1]; // this can be overridden
|
|
99
|
+
// for (const arg of args) {
|
|
100
|
+
// if (typeof arg ==='number') payload.weight = arg; // weight
|
|
101
|
+
// else payload = {...payload,...arg}; // options
|
|
102
|
+
// }
|
|
103
|
+
// this.body.vectorSearchQuery.push(payload);
|
|
104
|
+
// return this;
|
|
105
|
+
// }
|
|
106
|
+
// sort(field: string, direction: 'asc' | 'desc') {
|
|
107
|
+
// if (!this?.body?.sort?.length) this.body.sort = {};
|
|
108
|
+
// this.body.sort[field] = direction;
|
|
109
|
+
// return this;
|
|
110
|
+
// }
|
|
111
|
+
// textSort(field: string, direction: 'asc' | 'desc') {
|
|
112
|
+
// if (!this?.body?.textSort?.length) this.body.textSort = {};
|
|
113
|
+
// this.body.textSort[field] = direction;
|
|
114
|
+
// return this;
|
|
115
|
+
// }
|
|
116
|
+
// rawOption(key: string, value: any) {
|
|
117
|
+
// (this.body as any)[key] = value;
|
|
118
|
+
// return this;
|
|
119
|
+
// }
|
|
120
|
+
// minimumRelevance(value: bodyType['minimumRelevance']) {
|
|
121
|
+
// this.body.minimumRelevance = value;
|
|
122
|
+
// return this;
|
|
123
|
+
// }
|
|
124
|
+
// page(value: bodyType['page']) {
|
|
125
|
+
// this.body.page = value;
|
|
126
|
+
// return this;
|
|
127
|
+
// }
|
|
128
|
+
// pageSize(value: bodyType['pageSize']) {
|
|
129
|
+
// this.body.pageSize = value;
|
|
130
|
+
// return this;
|
|
131
|
+
// }
|
|
132
|
+
// includeFields(fields:bodyType['includeFields']) {
|
|
133
|
+
// this.body.includeFields = fields;
|
|
134
|
+
// }
|
|
135
|
+
// excludeFields(fields:bodyType['excludeFields']) {
|
|
136
|
+
// this.body.excludeFields = fields;
|
|
137
|
+
// }
|
|
138
|
+
// includeVectors(whetherToInclude:bodyType['includeVectors']){
|
|
139
|
+
// this.body.includeVectors = whetherToInclude;
|
|
140
|
+
// }
|
|
141
|
+
// aggregate(field: string, options?: { options?: any, aggregates?: _QueryBuilder }) {
|
|
142
|
+
// this.body.fieldsToAggregate?.push({ field, ...options, fieldsToAggregate: options?.aggregates?.body.fieldsToAggregate ?? [] });
|
|
143
|
+
// return this;
|
|
144
|
+
// }
|
|
145
|
+
// aggregateStats(field: string, interval?: number) {
|
|
146
|
+
// this.body.fieldsToAggregateStats?.push({ field, interval });
|
|
147
|
+
// return this;
|
|
148
|
+
// }
|
|
149
|
+
// }
|
|
150
|
+
// export class DiscoveryClient {
|
|
151
|
+
// apiClient:DiscoveryApiClient;
|
|
152
|
+
// constructor(config?: _ClientInput) {
|
|
153
|
+
// this.apiClient = new DiscoveryApiClient(config ?? {});
|
|
154
|
+
// }
|
|
155
|
+
// dataset(name: string, options?: any) {
|
|
156
|
+
// let dataset = new Dataset(this, name, options);
|
|
157
|
+
// return dataset;
|
|
158
|
+
// }
|
|
159
|
+
// }
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// export * from './discovery';
|
|
18
|
+
__exportStar(require("./vecdb"), exports);
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Dataset = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
5
|
+
class Dataset {
|
|
6
|
+
constructor(client, name, options) {
|
|
7
|
+
// TODO validate name
|
|
8
|
+
this.client = client;
|
|
9
|
+
this.name = name;
|
|
10
|
+
this.config = options || {};
|
|
11
|
+
}
|
|
12
|
+
get datasetName() {
|
|
13
|
+
return this.name;
|
|
14
|
+
}
|
|
15
|
+
;
|
|
16
|
+
async createIfNotExist() {
|
|
17
|
+
try {
|
|
18
|
+
await this.client.apiClient.GetDatasetDetails({}, { dataset_id: this.name });
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
await this.client.apiClient.CreateDataset({ id: this.name, ...(this.config.schema ? { schema: this.config.schema } : {}) });
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async recreateIfExists() {
|
|
27
|
+
try {
|
|
28
|
+
await this.client.apiClient.GetDatasetDetails({}, { dataset_id: this.name });
|
|
29
|
+
await this.client.apiClient.DeleteDataset({}, { dataset_id: this.name });
|
|
30
|
+
await this.client.apiClient.CreateDataset({ id: this.name, ...(this.config.schema ? { schema: this.config.schema } : {}) });
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async insertDocument(document, options) {
|
|
38
|
+
const response = await this.client.apiClient.Insert({
|
|
39
|
+
document,
|
|
40
|
+
...options
|
|
41
|
+
}, { dataset_id: this.name });
|
|
42
|
+
return response.body;
|
|
43
|
+
}
|
|
44
|
+
async search(...args) {
|
|
45
|
+
let payload = {};
|
|
46
|
+
let options = {};
|
|
47
|
+
for (const arg of args) {
|
|
48
|
+
if (arg instanceof _1._QueryBuilder) {
|
|
49
|
+
payload = { ...payload, ...arg.build() };
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
options = arg;
|
|
53
|
+
if (options.rawPayload)
|
|
54
|
+
payload = { ...payload, ...options.rawPayload };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const reqCallback = async () => await this.client.apiClient.SimpleSearchPost(payload, { dataset_id: this.name });
|
|
58
|
+
if (options.debounce && this.debounceTimer) {
|
|
59
|
+
clearTimeout(this.debounceTimer);
|
|
60
|
+
return new Promise((resolve) => {
|
|
61
|
+
this.debounceTimer = setTimeout(async () => { const res = await reqCallback(); resolve(res); }, options.debounce);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const response = await reqCallback();
|
|
66
|
+
return response.body;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async insertDocuments(documents, encoders, options) {
|
|
70
|
+
const results = await this._GenericBulkOperation({
|
|
71
|
+
data: documents !== null && documents !== void 0 ? documents : [],
|
|
72
|
+
...options,
|
|
73
|
+
fn: async (documentsSlice) => (await this.client.apiClient.BulkInsert({ documents: documentsSlice, ...(encoders ? { encoders } : {}) }, { dataset_id: this.name })).body
|
|
74
|
+
});
|
|
75
|
+
const finalResults = results.reduce((prev, cur) => {
|
|
76
|
+
prev.failed_documents = prev.failed_documents.concat(cur.failed_documents);
|
|
77
|
+
prev.inserted += cur.inserted;
|
|
78
|
+
return prev;
|
|
79
|
+
}, { inserted: 0, failed_documents: [] });
|
|
80
|
+
return finalResults;
|
|
81
|
+
}
|
|
82
|
+
// TODO - ChunkSearch, insert, insertAndVectorize?, vectorize,
|
|
83
|
+
async _GenericBulkOperation({ data, batchSize, fn, retryCount }) {
|
|
84
|
+
batchSize = batchSize !== null && batchSize !== void 0 ? batchSize : 10000;
|
|
85
|
+
retryCount = retryCount !== null && retryCount !== void 0 ? retryCount : 1;
|
|
86
|
+
const results = [];
|
|
87
|
+
for (let i = 0; i < (data === null || data === void 0 ? void 0 : data.length); i += batchSize) {
|
|
88
|
+
for (let retrysSoFar = 0; retrysSoFar < retryCount; retrysSoFar++) {
|
|
89
|
+
try {
|
|
90
|
+
const res = await fn(data.slice(i, i + batchSize));
|
|
91
|
+
results.push(res);
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
console.error(`Bulk operation failed with error, retrying - ${e}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return results;
|
|
100
|
+
}
|
|
101
|
+
async updateDocument(documentId, partialUpdates) {
|
|
102
|
+
const response = await this.client.apiClient.Update({ id: documentId, updates: partialUpdates });
|
|
103
|
+
return response.body;
|
|
104
|
+
}
|
|
105
|
+
async updateDocuments(updates, options) {
|
|
106
|
+
const results = await this._GenericBulkOperation({
|
|
107
|
+
data: updates !== null && updates !== void 0 ? updates : [],
|
|
108
|
+
...options,
|
|
109
|
+
fn: async (updatesSlice) => (await this.client.apiClient.BulkUpdate({ updates: updatesSlice }, { dataset_id: this.name })).body
|
|
110
|
+
});
|
|
111
|
+
const finalResults = results.reduce((prev, cur) => {
|
|
112
|
+
prev.failed_documents = prev.failed_documents.concat(cur.failed_documents);
|
|
113
|
+
prev.inserted += cur.inserted;
|
|
114
|
+
return prev;
|
|
115
|
+
}, { inserted: 0, failed_documents: [] });
|
|
116
|
+
return finalResults;
|
|
117
|
+
}
|
|
118
|
+
async updateDocumentsWhere(filters, partialUpdates) {
|
|
119
|
+
return (await this.client.apiClient.UpdateWhere({ filters: filters.build().filters, updates: partialUpdates })).body;
|
|
120
|
+
}
|
|
121
|
+
async getDocument(documentId) {
|
|
122
|
+
return (await this.client.apiClient.GetDocument({ document_id: documentId })).body;
|
|
123
|
+
}
|
|
124
|
+
async deleteDocument(documentId) {
|
|
125
|
+
return (await this.client.apiClient.DeleteDocument({ id: documentId })).body;
|
|
126
|
+
}
|
|
127
|
+
async deleteDocuments(documentIds) {
|
|
128
|
+
var _a;
|
|
129
|
+
const filters = (0, _1.QueryBuilder)().match('_id', documentIds);
|
|
130
|
+
return (await this.client.apiClient.DeleteWhere({ filters: (_a = filters.build().filters) !== null && _a !== void 0 ? _a : [] })).body;
|
|
131
|
+
}
|
|
132
|
+
async deleteDocumentsWhere(filters) {
|
|
133
|
+
var _a;
|
|
134
|
+
return (await this.client.apiClient.DeleteWhere({ filters: (_a = filters.build().filters) !== null && _a !== void 0 ? _a : [] })).body;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.Dataset = Dataset;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VecDBClient = exports._QueryBuilder = exports._FilterBuilder = exports.FilterBuilder = exports.QueryBuilder = void 0;
|
|
4
|
+
const __1 = require("../../");
|
|
5
|
+
const Dataset_1 = require("./Dataset");
|
|
6
|
+
function QueryBuilder() {
|
|
7
|
+
return new _QueryBuilder();
|
|
8
|
+
}
|
|
9
|
+
exports.QueryBuilder = QueryBuilder;
|
|
10
|
+
function FilterBuilder() {
|
|
11
|
+
return new _FilterBuilder();
|
|
12
|
+
}
|
|
13
|
+
exports.FilterBuilder = FilterBuilder;
|
|
14
|
+
class _FilterBuilder {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.body = { filters: [], fieldsToAggregate: [], fieldsToAggregateStats: [] };
|
|
17
|
+
}
|
|
18
|
+
buildFilters() {
|
|
19
|
+
return this.body.filters;
|
|
20
|
+
}
|
|
21
|
+
rawFilter(filter) {
|
|
22
|
+
var _a;
|
|
23
|
+
(_a = this.body.filters) === null || _a === void 0 ? void 0 : _a.push(filter);
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
filter(type, key, value, ...options) {
|
|
27
|
+
var _a;
|
|
28
|
+
(_a = this.body.filters) === null || _a === void 0 ? void 0 : _a.push({
|
|
29
|
+
[type]: {
|
|
30
|
+
key,
|
|
31
|
+
value,
|
|
32
|
+
...options
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
match(field, value) {
|
|
38
|
+
var _a;
|
|
39
|
+
(_a = this.body.filters) === null || _a === void 0 ? void 0 : _a.push({ match: { field: field, value } });
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
wildcard(field, value) {
|
|
43
|
+
var _a;
|
|
44
|
+
(_a = this.body.filters) === null || _a === void 0 ? void 0 : _a.push({ wildcard: { field: field, value } });
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
range(field, options) {
|
|
48
|
+
var _a;
|
|
49
|
+
(_a = this.body.filters) === null || _a === void 0 ? void 0 : _a.push({ range: { field: field, ...options } });
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
or(filters) {
|
|
53
|
+
var _a;
|
|
54
|
+
(_a = this.body.filters) === null || _a === void 0 ? void 0 : _a.push({ or: filters.map(f => { var _a; return (_a = f.body.filters) !== null && _a !== void 0 ? _a : []; }) });
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports._FilterBuilder = _FilterBuilder;
|
|
59
|
+
class _QueryBuilder extends _FilterBuilder {
|
|
60
|
+
constructor() {
|
|
61
|
+
super();
|
|
62
|
+
this.shouldPerformTextQuery = false;
|
|
63
|
+
}
|
|
64
|
+
build() {
|
|
65
|
+
if (!this.shouldPerformTextQuery)
|
|
66
|
+
return this.body;
|
|
67
|
+
if (!this.defaultQueryValue)
|
|
68
|
+
throw new Error("Please set the search query by calling .query('my search query') before performing a text search.");
|
|
69
|
+
this.body.query = this.defaultQueryValue;
|
|
70
|
+
return this.body;
|
|
71
|
+
}
|
|
72
|
+
vector(field, ...args) {
|
|
73
|
+
if (!Array.isArray(this.body.vectorSearchQuery))
|
|
74
|
+
this.body.vectorSearchQuery = [];
|
|
75
|
+
let payload = { field }; // TODO components['schemas']['simpleSearchAndFlatFilterItem']['search']['vectorSearchQuery']
|
|
76
|
+
const inferredModelMatch = field.match(/_(.*)_.*vector_/); // title_text@1-0_vector_ -> text@1-0
|
|
77
|
+
if (inferredModelMatch && inferredModelMatch[1])
|
|
78
|
+
payload.model = inferredModelMatch[1]; // this can be overridden
|
|
79
|
+
for (const arg of args) {
|
|
80
|
+
if (typeof arg === 'number')
|
|
81
|
+
payload.weight = arg; // weight
|
|
82
|
+
else
|
|
83
|
+
payload = { ...payload, ...arg }; // options
|
|
84
|
+
}
|
|
85
|
+
this.body.vectorSearchQuery.push(payload);
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
sort(field, direction) {
|
|
89
|
+
var _a, _b;
|
|
90
|
+
if (!((_b = (_a = this === null || this === void 0 ? void 0 : this.body) === null || _a === void 0 ? void 0 : _a.sort) === null || _b === void 0 ? void 0 : _b.length))
|
|
91
|
+
this.body.sort = {};
|
|
92
|
+
this.body.sort[field] = direction;
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
rawOption(key, value) {
|
|
96
|
+
this.body[key] = value;
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
minimumRelevance(value) {
|
|
100
|
+
this.body.minimumRelevance = value;
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
page(value) {
|
|
104
|
+
this.body.page = value;
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
pageSize(value) {
|
|
108
|
+
this.body.pageSize = value;
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
includeFields(fields) {
|
|
112
|
+
this.body.includeFields = fields;
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
ask(query, field, options) {
|
|
116
|
+
var _a;
|
|
117
|
+
this.body.instantAnswerQuery = {
|
|
118
|
+
query,
|
|
119
|
+
field: field,
|
|
120
|
+
preset: (_a = options.preset) !== null && _a !== void 0 ? _a : 'support3',
|
|
121
|
+
...(options.titleReferenceField ? { titleReferenceField: options.titleReferenceField } : {}),
|
|
122
|
+
...(options.urlReferenceField ? { urlReferenceField: options.urlReferenceField } : {}),
|
|
123
|
+
};
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports._QueryBuilder = _QueryBuilder;
|
|
128
|
+
class VecDBClient {
|
|
129
|
+
constructor(config) {
|
|
130
|
+
this.apiClient = new __1.VecDBApiClient(config !== null && config !== void 0 ? config : {});
|
|
131
|
+
}
|
|
132
|
+
dataset(name, options) {
|
|
133
|
+
let dataset = new Dataset_1.Dataset(this, name, options);
|
|
134
|
+
return dataset;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.VecDBClient = VecDBClient;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
var _a, _b;
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports._GenericClient = void 0;
|
|
8
|
+
const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
9
|
+
const serviceConfigs_1 = require("./serviceConfigs");
|
|
10
|
+
const envVars = {
|
|
11
|
+
project: (_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.RELEVANCE_PROJECT,
|
|
12
|
+
api_key: (_b = process === null || process === void 0 ? void 0 : process.env) === null || _b === void 0 ? void 0 : _b.RELEVANCE_API_KEY,
|
|
13
|
+
};
|
|
14
|
+
class _GenericClient {
|
|
15
|
+
constructor(config) {
|
|
16
|
+
this.config = config;
|
|
17
|
+
this.serviceConfig = serviceConfigs_1.serviceConfigs[config.service_name];
|
|
18
|
+
}
|
|
19
|
+
async SendRequest({ input, path, method, options }) {
|
|
20
|
+
var _a, _b, _c, _d, _e;
|
|
21
|
+
const settings = {
|
|
22
|
+
method,
|
|
23
|
+
headers: { authorization: `${(_a = this.config.project) !== null && _a !== void 0 ? _a : envVars.project}:${(_b = this.config.api_key) !== null && _b !== void 0 ? _b : envVars.api_key}` },
|
|
24
|
+
};
|
|
25
|
+
if (method.toLowerCase() !== 'get')
|
|
26
|
+
settings.body = JSON.stringify(input);
|
|
27
|
+
const final_dataset_id = (_d = (_c = options === null || options === void 0 ? void 0 : options.dataset_id) !== null && _c !== void 0 ? _c : this.config.dataset_id) !== null && _d !== void 0 ? _d : "";
|
|
28
|
+
const res = await (0, cross_fetch_1.default)(`${(_e = this.config.endpoint) !== null && _e !== void 0 ? _e : this.serviceConfig.endpoint}/latest${path.replace('{dataset_id}', final_dataset_id)}`, settings);
|
|
29
|
+
if (!res.ok)
|
|
30
|
+
throw new Error(`${path} ${method} failed with status ${res.status}: ${(await res.text())}`);
|
|
31
|
+
const body = await res.json();
|
|
32
|
+
return { body };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports._GenericClient = _GenericClient;
|