@relevanceai/sdk 1.149.0 → 2.0.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 +27 -49
- package/dist-cjs/generated/{DiscoveryApi.js → VecDBApi.js} +329 -25
- package/dist-cjs/generated/index.js +1 -1
- package/dist-cjs/index.js +1 -1
- package/dist-cjs/services/index.js +16 -1
- package/dist-cjs/services/vecdb/Dataset.js +137 -0
- package/dist-cjs/services/vecdb/index.js +137 -0
- package/dist-cjs/shared/serviceConfigs.js +4 -4
- package/dist-es/generated/{DiscoveryApi.js → VecDBApi.js} +651 -195
- package/dist-es/generated/index.js +1 -1
- package/dist-es/index.js +1 -1
- package/dist-es/services/index.js +1 -2
- package/dist-es/services/vecdb/Dataset.js +356 -0
- package/dist-es/services/vecdb/index.js +184 -0
- package/dist-es/shared/serviceConfigs.js +4 -4
- package/dist-types/generated/{DiscoveryApi.d.ts → VecDBApi.d.ts} +137 -23
- package/dist-types/generated/{_DiscoveryApiSchemaTypes.d.ts → _VecDBApiSchemaTypes.d.ts} +6841 -4543
- package/dist-types/generated/index.d.ts +1 -1
- package/dist-types/index.d.ts +1 -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/package.json +3 -2
- /package/dist-cjs/generated/{_DiscoveryApiSchemaTypes.js → _VecDBApiSchemaTypes.js} +0 -0
- /package/dist-es/generated/{_DiscoveryApiSchemaTypes.js → _VecDBApiSchemaTypes.js} +0 -0
|
@@ -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;
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.serviceConfigs = void 0;
|
|
4
4
|
exports.serviceConfigs = {
|
|
5
|
-
|
|
6
|
-
schema_url: 'https://api
|
|
7
|
-
endpoint: 'https://api
|
|
8
|
-
name: '
|
|
5
|
+
VecDBApi: {
|
|
6
|
+
schema_url: 'https://api-f1db6c.stack.tryrelevance.com/latest/openapi_schema.json',
|
|
7
|
+
endpoint: 'https://api-f1db6c.stack.tryrelevance.com',
|
|
8
|
+
name: 'VecDBApi',
|
|
9
9
|
},
|
|
10
10
|
};
|