openai 4.35.0 → 4.37.0
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/CHANGELOG.md +22 -0
- package/README.md +16 -2
- package/index.d.mts +8 -1
- package/index.d.ts +8 -1
- package/index.d.ts.map +1 -1
- package/index.js +5 -1
- package/index.js.map +1 -1
- package/index.mjs +5 -1
- package/index.mjs.map +1 -1
- package/lib/AssistantStream.d.ts +2 -2
- package/lib/AssistantStream.d.ts.map +1 -1
- package/lib/Util.d.ts +5 -0
- package/lib/Util.d.ts.map +1 -0
- package/lib/Util.js +26 -0
- package/lib/Util.js.map +1 -0
- package/lib/Util.mjs +22 -0
- package/lib/Util.mjs.map +1 -0
- package/package.json +1 -1
- package/resources/beta/{assistants/assistants.d.ts → assistants.d.ts} +217 -41
- package/resources/beta/assistants.d.ts.map +1 -0
- package/resources/beta/{assistants/assistants.js → assistants.js} +6 -13
- package/resources/beta/assistants.js.map +1 -0
- package/resources/beta/{assistants/assistants.mjs → assistants.mjs} +6 -13
- package/resources/beta/assistants.mjs.map +1 -0
- package/resources/beta/beta.d.ts +11 -2
- package/resources/beta/beta.d.ts.map +1 -1
- package/resources/beta/beta.js +5 -1
- package/resources/beta/beta.js.map +1 -1
- package/resources/beta/beta.mjs +5 -1
- package/resources/beta/beta.mjs.map +1 -1
- package/resources/beta/index.d.ts +2 -1
- package/resources/beta/index.d.ts.map +1 -1
- package/resources/beta/index.js +11 -8
- package/resources/beta/index.js.map +1 -1
- package/resources/beta/index.mjs +2 -1
- package/resources/beta/index.mjs.map +1 -1
- package/resources/beta/threads/index.d.ts +1 -1
- package/resources/beta/threads/index.d.ts.map +1 -1
- package/resources/beta/threads/index.js +6 -6
- package/resources/beta/threads/index.js.map +1 -1
- package/resources/beta/threads/index.mjs +1 -1
- package/resources/beta/threads/{messages/messages.d.ts → messages.d.ts} +27 -28
- package/resources/beta/threads/messages.d.ts.map +1 -0
- package/resources/beta/threads/{messages/messages.js → messages.js} +5 -12
- package/resources/beta/threads/messages.js.map +1 -0
- package/resources/beta/threads/{messages/messages.mjs → messages.mjs} +5 -12
- package/resources/beta/threads/messages.mjs.map +1 -0
- package/resources/beta/threads/runs/index.d.ts +1 -1
- package/resources/beta/threads/runs/index.d.ts.map +1 -1
- package/resources/beta/threads/runs/runs.d.ts +75 -29
- package/resources/beta/threads/runs/runs.d.ts.map +1 -1
- package/resources/beta/threads/runs/runs.js +6 -6
- package/resources/beta/threads/runs/runs.js.map +1 -1
- package/resources/beta/threads/runs/runs.mjs +6 -6
- package/resources/beta/threads/runs/runs.mjs.map +1 -1
- package/resources/beta/threads/runs/steps.d.ts +40 -40
- package/resources/beta/threads/runs/steps.d.ts.map +1 -1
- package/resources/beta/threads/runs/steps.js +2 -2
- package/resources/beta/threads/runs/steps.mjs +2 -2
- package/resources/beta/threads/threads.d.ts +498 -32
- package/resources/beta/threads/threads.d.ts.map +1 -1
- package/resources/beta/threads/threads.js +6 -6
- package/resources/beta/threads/threads.js.map +1 -1
- package/resources/beta/threads/threads.mjs +6 -6
- package/resources/beta/threads/threads.mjs.map +1 -1
- package/resources/beta/vector-stores/file-batches.d.ts +142 -0
- package/resources/beta/vector-stores/file-batches.d.ts.map +1 -0
- package/resources/beta/vector-stores/file-batches.js +128 -0
- package/resources/beta/vector-stores/file-batches.js.map +1 -0
- package/resources/beta/vector-stores/file-batches.mjs +124 -0
- package/resources/beta/vector-stores/file-batches.mjs.map +1 -0
- package/resources/beta/vector-stores/files.d.ts +148 -0
- package/resources/beta/vector-stores/files.d.ts.map +1 -0
- package/resources/beta/vector-stores/files.js +148 -0
- package/resources/beta/vector-stores/files.js.map +1 -0
- package/resources/beta/vector-stores/files.mjs +120 -0
- package/resources/beta/vector-stores/files.mjs.map +1 -0
- package/resources/beta/vector-stores/index.d.ts +4 -0
- package/resources/beta/vector-stores/index.d.ts.map +1 -0
- package/resources/beta/vector-stores/index.js +13 -0
- package/resources/beta/vector-stores/index.js.map +1 -0
- package/resources/beta/vector-stores/index.mjs +5 -0
- package/resources/beta/vector-stores/index.mjs.map +1 -0
- package/resources/beta/vector-stores/vector-stores.d.ts +233 -0
- package/resources/beta/vector-stores/vector-stores.d.ts.map +1 -0
- package/resources/beta/vector-stores/vector-stores.js +99 -0
- package/resources/beta/vector-stores/vector-stores.js.map +1 -0
- package/resources/beta/vector-stores/vector-stores.mjs +71 -0
- package/resources/beta/vector-stores/vector-stores.mjs.map +1 -0
- package/resources/chat/chat.d.ts +3 -0
- package/resources/chat/chat.d.ts.map +1 -1
- package/resources/chat/chat.js.map +1 -1
- package/resources/chat/chat.mjs.map +1 -1
- package/resources/chat/completions.d.ts +2 -1
- package/resources/chat/completions.d.ts.map +1 -1
- package/resources/chat/completions.js.map +1 -1
- package/resources/chat/completions.mjs.map +1 -1
- package/resources/chat/index.d.ts +1 -1
- package/resources/chat/index.d.ts.map +1 -1
- package/resources/chat/index.js +3 -3
- package/resources/chat/index.js.map +1 -1
- package/resources/chat/index.mjs +1 -1
- package/resources/chat/index.mjs.map +1 -1
- package/resources/fine-tuning/jobs/jobs.d.ts +1 -1
- package/src/index.ts +12 -0
- package/src/lib/AssistantStream.ts +2 -2
- package/src/lib/Util.ts +23 -0
- package/src/resources/beta/{assistants/assistants.ts → assistants.ts} +254 -58
- package/src/resources/beta/beta.ts +11 -2
- package/src/resources/beta/index.ts +11 -2
- package/src/resources/beta/threads/index.ts +1 -1
- package/src/resources/beta/threads/{messages/messages.ts → messages.ts} +39 -38
- package/src/resources/beta/threads/runs/index.ts +2 -2
- package/src/resources/beta/threads/runs/runs.ts +94 -36
- package/src/resources/beta/threads/runs/steps.ts +49 -49
- package/src/resources/beta/threads/threads.ts +571 -37
- package/src/resources/beta/vector-stores/file-batches.ts +292 -0
- package/src/resources/beta/vector-stores/files.ts +277 -0
- package/src/resources/beta/vector-stores/index.ts +25 -0
- package/src/resources/beta/vector-stores/vector-stores.ts +318 -0
- package/src/resources/chat/chat.ts +23 -0
- package/src/resources/chat/completions.ts +2 -21
- package/src/resources/chat/index.ts +1 -1
- package/src/resources/fine-tuning/jobs/jobs.ts +1 -1
- package/src/version.ts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
- package/resources/beta/assistants/assistants.d.ts.map +0 -1
- package/resources/beta/assistants/assistants.js.map +0 -1
- package/resources/beta/assistants/assistants.mjs.map +0 -1
- package/resources/beta/assistants/files.d.ts +0 -88
- package/resources/beta/assistants/files.d.ts.map +0 -1
- package/resources/beta/assistants/files.js +0 -81
- package/resources/beta/assistants/files.js.map +0 -1
- package/resources/beta/assistants/files.mjs +0 -53
- package/resources/beta/assistants/files.mjs.map +0 -1
- package/resources/beta/assistants/index.d.ts +0 -3
- package/resources/beta/assistants/index.d.ts.map +0 -1
- package/resources/beta/assistants/index.js +0 -11
- package/resources/beta/assistants/index.js.map +0 -1
- package/resources/beta/assistants/index.mjs +0 -4
- package/resources/beta/assistants/index.mjs.map +0 -1
- package/resources/beta/threads/messages/files.d.ts +0 -60
- package/resources/beta/threads/messages/files.d.ts.map +0 -1
- package/resources/beta/threads/messages/files.js +0 -60
- package/resources/beta/threads/messages/files.js.map +0 -1
- package/resources/beta/threads/messages/files.mjs +0 -32
- package/resources/beta/threads/messages/files.mjs.map +0 -1
- package/resources/beta/threads/messages/index.d.ts +0 -3
- package/resources/beta/threads/messages/index.d.ts.map +0 -1
- package/resources/beta/threads/messages/index.js +0 -11
- package/resources/beta/threads/messages/index.js.map +0 -1
- package/resources/beta/threads/messages/index.mjs +0 -4
- package/resources/beta/threads/messages/index.mjs.map +0 -1
- package/resources/beta/threads/messages/messages.d.ts.map +0 -1
- package/resources/beta/threads/messages/messages.js.map +0 -1
- package/resources/beta/threads/messages/messages.mjs.map +0 -1
- package/src/resources/beta/assistants/files.ts +0 -154
- package/src/resources/beta/assistants/index.ts +0 -28
- package/src/resources/beta/threads/messages/files.ts +0 -105
- package/src/resources/beta/threads/messages/index.ts +0 -30
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import * as Core from "../../../core";
|
|
4
|
+
import { APIResource } from "../../../resource";
|
|
5
|
+
import { isRequestOptions } from "../../../core";
|
|
6
|
+
import { sleep } from "../../../core";
|
|
7
|
+
import { Uploadable } from "../../../core";
|
|
8
|
+
import { allSettledWithThrow } from "../../../lib/Util";
|
|
9
|
+
import * as FileBatchesAPI from "./file-batches";
|
|
10
|
+
import * as FilesAPI from "./files";
|
|
11
|
+
import { VectorStoreFilesPage } from "./files";
|
|
12
|
+
import { type CursorPageParams } from "../../../pagination";
|
|
13
|
+
|
|
14
|
+
export class FileBatches extends APIResource {
|
|
15
|
+
/**
|
|
16
|
+
* Create a vector store file batch.
|
|
17
|
+
*/
|
|
18
|
+
create(
|
|
19
|
+
vectorStoreId: string,
|
|
20
|
+
body: FileBatchCreateParams,
|
|
21
|
+
options?: Core.RequestOptions,
|
|
22
|
+
): Core.APIPromise<VectorStoreFileBatch> {
|
|
23
|
+
return this._client.post(`/vector_stores/${vectorStoreId}/file_batches`, {
|
|
24
|
+
body,
|
|
25
|
+
...options,
|
|
26
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Retrieves a vector store file batch.
|
|
32
|
+
*/
|
|
33
|
+
retrieve(
|
|
34
|
+
vectorStoreId: string,
|
|
35
|
+
batchId: string,
|
|
36
|
+
options?: Core.RequestOptions,
|
|
37
|
+
): Core.APIPromise<VectorStoreFileBatch> {
|
|
38
|
+
return this._client.get(`/vector_stores/${vectorStoreId}/file_batches/${batchId}`, {
|
|
39
|
+
...options,
|
|
40
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Cancel a vector store file batch. This attempts to cancel the processing of
|
|
46
|
+
* files in this batch as soon as possible.
|
|
47
|
+
*/
|
|
48
|
+
cancel(
|
|
49
|
+
vectorStoreId: string,
|
|
50
|
+
batchId: string,
|
|
51
|
+
options?: Core.RequestOptions,
|
|
52
|
+
): Core.APIPromise<VectorStoreFileBatch> {
|
|
53
|
+
return this._client.post(`/vector_stores/${vectorStoreId}/file_batches/${batchId}/cancel`, {
|
|
54
|
+
...options,
|
|
55
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Create a vector store batch and poll until all files have been processed.
|
|
61
|
+
*/
|
|
62
|
+
async createAndPoll(
|
|
63
|
+
vectorStoreId: string,
|
|
64
|
+
body: FileBatchCreateParams,
|
|
65
|
+
options?: Core.RequestOptions & { pollIntervalMs?: number },
|
|
66
|
+
): Promise<VectorStoreFileBatch> {
|
|
67
|
+
const batch = await this.create(vectorStoreId, body);
|
|
68
|
+
return await this.poll(vectorStoreId, batch.id, options);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Returns a list of vector store files in a batch.
|
|
73
|
+
*/
|
|
74
|
+
listFiles(
|
|
75
|
+
vectorStoreId: string,
|
|
76
|
+
batchId: string,
|
|
77
|
+
query?: FileBatchListFilesParams,
|
|
78
|
+
options?: Core.RequestOptions,
|
|
79
|
+
): Core.PagePromise<VectorStoreFilesPage, FilesAPI.VectorStoreFile>;
|
|
80
|
+
listFiles(
|
|
81
|
+
vectorStoreId: string,
|
|
82
|
+
batchId: string,
|
|
83
|
+
options?: Core.RequestOptions,
|
|
84
|
+
): Core.PagePromise<VectorStoreFilesPage, FilesAPI.VectorStoreFile>;
|
|
85
|
+
listFiles(
|
|
86
|
+
vectorStoreId: string,
|
|
87
|
+
batchId: string,
|
|
88
|
+
query: FileBatchListFilesParams | Core.RequestOptions = {},
|
|
89
|
+
options?: Core.RequestOptions,
|
|
90
|
+
): Core.PagePromise<VectorStoreFilesPage, FilesAPI.VectorStoreFile> {
|
|
91
|
+
if (isRequestOptions(query)) {
|
|
92
|
+
return this.listFiles(vectorStoreId, batchId, {}, query);
|
|
93
|
+
}
|
|
94
|
+
return this._client.getAPIList(
|
|
95
|
+
`/vector_stores/${vectorStoreId}/file_batches/${batchId}/files`,
|
|
96
|
+
VectorStoreFilesPage,
|
|
97
|
+
{ query, ...options, headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers } },
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Wait for the given file batch to be processed.
|
|
103
|
+
*
|
|
104
|
+
* Note: this will return even if one of the files failed to process, you need to
|
|
105
|
+
* check batch.file_counts.failed_count to handle this case.
|
|
106
|
+
*/
|
|
107
|
+
async poll(
|
|
108
|
+
vectorStoreId: string,
|
|
109
|
+
batchId: string,
|
|
110
|
+
options?: Core.RequestOptions & { pollIntervalMs?: number },
|
|
111
|
+
): Promise<VectorStoreFileBatch> {
|
|
112
|
+
const headers: { [key: string]: string } = { ...options?.headers, 'X-Stainless-Poll-Helper': 'true' };
|
|
113
|
+
if (options?.pollIntervalMs) {
|
|
114
|
+
headers['X-Stainless-Custom-Poll-Interval'] = options.pollIntervalMs.toString();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
while (true) {
|
|
118
|
+
const { data: batch, response } = await this.retrieve(vectorStoreId, batchId, {
|
|
119
|
+
...options,
|
|
120
|
+
headers,
|
|
121
|
+
}).withResponse();
|
|
122
|
+
|
|
123
|
+
switch (batch.status) {
|
|
124
|
+
case 'in_progress':
|
|
125
|
+
let sleepInterval = 5000;
|
|
126
|
+
|
|
127
|
+
if (options?.pollIntervalMs) {
|
|
128
|
+
sleepInterval = options.pollIntervalMs;
|
|
129
|
+
} else {
|
|
130
|
+
const headerInterval = response.headers.get('openai-poll-after-ms');
|
|
131
|
+
if (headerInterval) {
|
|
132
|
+
const headerIntervalMs = parseInt(headerInterval);
|
|
133
|
+
if (!isNaN(headerIntervalMs)) {
|
|
134
|
+
sleepInterval = headerIntervalMs;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
await sleep(sleepInterval);
|
|
139
|
+
break;
|
|
140
|
+
case 'failed':
|
|
141
|
+
case 'completed':
|
|
142
|
+
return batch;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Uploads the given files concurrently and then creates a vector store file batch.
|
|
149
|
+
*
|
|
150
|
+
* The concurrency limit is configurable using the `maxConcurrency` parameter.
|
|
151
|
+
*/
|
|
152
|
+
async uploadAndPoll(
|
|
153
|
+
vectorStoreId: string,
|
|
154
|
+
{ files, fileIds = [] }: { files: Uploadable[]; fileIds?: string[] },
|
|
155
|
+
options?: Core.RequestOptions & { pollIntervalMs?: number; maxConcurrency?: number },
|
|
156
|
+
): Promise<VectorStoreFileBatch> {
|
|
157
|
+
if (files === null || files.length == 0) {
|
|
158
|
+
throw new Error('No files provided to process.');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const configuredConcurrency = options?.maxConcurrency ?? 5;
|
|
162
|
+
//We cap the number of workers at the number of files (so we don't start any unnecessary workers)
|
|
163
|
+
const concurrencyLimit = Math.min(configuredConcurrency, files.length);
|
|
164
|
+
|
|
165
|
+
const client = this._client;
|
|
166
|
+
const fileIterator = files.values();
|
|
167
|
+
const allFileIds: string[] = [...fileIds];
|
|
168
|
+
|
|
169
|
+
//This code is based on this design. The libraries don't accommodate our environment limits.
|
|
170
|
+
// https://stackoverflow.com/questions/40639432/what-is-the-best-way-to-limit-concurrency-when-using-es6s-promise-all
|
|
171
|
+
async function processFiles(iterator: IterableIterator<Uploadable>) {
|
|
172
|
+
for (let item of iterator) {
|
|
173
|
+
const fileObj = await client.files.create({ file: item, purpose: 'assistants' }, options);
|
|
174
|
+
allFileIds.push(fileObj.id);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
//Start workers to process results
|
|
179
|
+
const workers = Array(concurrencyLimit).fill(fileIterator).map(processFiles);
|
|
180
|
+
|
|
181
|
+
//Wait for all processing to complete.
|
|
182
|
+
await allSettledWithThrow(workers);
|
|
183
|
+
|
|
184
|
+
return await this.createAndPoll(vectorStoreId, {
|
|
185
|
+
file_ids: allFileIds,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* A batch of files attached to a vector store.
|
|
192
|
+
*/
|
|
193
|
+
export interface VectorStoreFileBatch {
|
|
194
|
+
/**
|
|
195
|
+
* The identifier, which can be referenced in API endpoints.
|
|
196
|
+
*/
|
|
197
|
+
id: string;
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The Unix timestamp (in seconds) for when the vector store files batch was
|
|
201
|
+
* created.
|
|
202
|
+
*/
|
|
203
|
+
created_at: number;
|
|
204
|
+
|
|
205
|
+
file_counts: VectorStoreFileBatch.FileCounts;
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The object type, which is always `vector_store.file_batch`.
|
|
209
|
+
*/
|
|
210
|
+
object: 'vector_store.files_batch';
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The status of the vector store files batch, which can be either `in_progress`,
|
|
214
|
+
* `completed`, `cancelled` or `failed`.
|
|
215
|
+
*/
|
|
216
|
+
status: 'in_progress' | 'completed' | 'cancelled' | 'failed';
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The ID of the
|
|
220
|
+
* [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object)
|
|
221
|
+
* that the [File](https://platform.openai.com/docs/api-reference/files) is
|
|
222
|
+
* attached to.
|
|
223
|
+
*/
|
|
224
|
+
vector_store_id: string;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export namespace VectorStoreFileBatch {
|
|
228
|
+
export interface FileCounts {
|
|
229
|
+
/**
|
|
230
|
+
* The number of files that where cancelled.
|
|
231
|
+
*/
|
|
232
|
+
cancelled: number;
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The number of files that have been processed.
|
|
236
|
+
*/
|
|
237
|
+
completed: number;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The number of files that have failed to process.
|
|
241
|
+
*/
|
|
242
|
+
failed: number;
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* The number of files that are currently being processed.
|
|
246
|
+
*/
|
|
247
|
+
in_progress: number;
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* The total number of files.
|
|
251
|
+
*/
|
|
252
|
+
total: number;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export interface FileBatchCreateParams {
|
|
257
|
+
/**
|
|
258
|
+
* A list of [File](https://platform.openai.com/docs/api-reference/files) IDs that
|
|
259
|
+
* the vector store should use. Useful for tools like `file_search` that can access
|
|
260
|
+
* files.
|
|
261
|
+
*/
|
|
262
|
+
file_ids: Array<string>;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export interface FileBatchListFilesParams extends CursorPageParams {
|
|
266
|
+
/**
|
|
267
|
+
* A cursor for use in pagination. `before` is an object ID that defines your place
|
|
268
|
+
* in the list. For instance, if you make a list request and receive 100 objects,
|
|
269
|
+
* ending with obj_foo, your subsequent call can include before=obj_foo in order to
|
|
270
|
+
* fetch the previous page of the list.
|
|
271
|
+
*/
|
|
272
|
+
before?: string;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.
|
|
276
|
+
*/
|
|
277
|
+
filter?: 'in_progress' | 'completed' | 'failed' | 'cancelled';
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Sort order by the `created_at` timestamp of the objects. `asc` for ascending
|
|
281
|
+
* order and `desc` for descending order.
|
|
282
|
+
*/
|
|
283
|
+
order?: 'asc' | 'desc';
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export namespace FileBatches {
|
|
287
|
+
export import VectorStoreFileBatch = FileBatchesAPI.VectorStoreFileBatch;
|
|
288
|
+
export import FileBatchCreateParams = FileBatchesAPI.FileBatchCreateParams;
|
|
289
|
+
export import FileBatchListFilesParams = FileBatchesAPI.FileBatchListFilesParams;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export { VectorStoreFilesPage };
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import * as Core from "../../../core";
|
|
4
|
+
import { APIResource } from "../../../resource";
|
|
5
|
+
import { isRequestOptions } from "../../../core";
|
|
6
|
+
import { sleep, Uploadable } from "../../../core";
|
|
7
|
+
import * as FilesAPI from "./files";
|
|
8
|
+
import { CursorPage, type CursorPageParams } from "../../../pagination";
|
|
9
|
+
|
|
10
|
+
export class Files extends APIResource {
|
|
11
|
+
/**
|
|
12
|
+
* Create a vector store file by attaching a
|
|
13
|
+
* [File](https://platform.openai.com/docs/api-reference/files) to a
|
|
14
|
+
* [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object).
|
|
15
|
+
*/
|
|
16
|
+
create(
|
|
17
|
+
vectorStoreId: string,
|
|
18
|
+
body: FileCreateParams,
|
|
19
|
+
options?: Core.RequestOptions,
|
|
20
|
+
): Core.APIPromise<VectorStoreFile> {
|
|
21
|
+
return this._client.post(`/vector_stores/${vectorStoreId}/files`, {
|
|
22
|
+
body,
|
|
23
|
+
...options,
|
|
24
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Retrieves a vector store file.
|
|
30
|
+
*/
|
|
31
|
+
retrieve(
|
|
32
|
+
vectorStoreId: string,
|
|
33
|
+
fileId: string,
|
|
34
|
+
options?: Core.RequestOptions,
|
|
35
|
+
): Core.APIPromise<VectorStoreFile> {
|
|
36
|
+
return this._client.get(`/vector_stores/${vectorStoreId}/files/${fileId}`, {
|
|
37
|
+
...options,
|
|
38
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Returns a list of vector store files.
|
|
44
|
+
*/
|
|
45
|
+
list(
|
|
46
|
+
vectorStoreId: string,
|
|
47
|
+
query?: FileListParams,
|
|
48
|
+
options?: Core.RequestOptions,
|
|
49
|
+
): Core.PagePromise<VectorStoreFilesPage, VectorStoreFile>;
|
|
50
|
+
list(
|
|
51
|
+
vectorStoreId: string,
|
|
52
|
+
options?: Core.RequestOptions,
|
|
53
|
+
): Core.PagePromise<VectorStoreFilesPage, VectorStoreFile>;
|
|
54
|
+
list(
|
|
55
|
+
vectorStoreId: string,
|
|
56
|
+
query: FileListParams | Core.RequestOptions = {},
|
|
57
|
+
options?: Core.RequestOptions,
|
|
58
|
+
): Core.PagePromise<VectorStoreFilesPage, VectorStoreFile> {
|
|
59
|
+
if (isRequestOptions(query)) {
|
|
60
|
+
return this.list(vectorStoreId, {}, query);
|
|
61
|
+
}
|
|
62
|
+
return this._client.getAPIList(`/vector_stores/${vectorStoreId}/files`, VectorStoreFilesPage, {
|
|
63
|
+
query,
|
|
64
|
+
...options,
|
|
65
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Delete a vector store file. This will remove the file from the vector store but
|
|
71
|
+
* the file itself will not be deleted. To delete the file, use the
|
|
72
|
+
* [delete file](https://platform.openai.com/docs/api-reference/files/delete)
|
|
73
|
+
* endpoint.
|
|
74
|
+
*/
|
|
75
|
+
del(
|
|
76
|
+
vectorStoreId: string,
|
|
77
|
+
fileId: string,
|
|
78
|
+
options?: Core.RequestOptions,
|
|
79
|
+
): Core.APIPromise<VectorStoreFileDeleted> {
|
|
80
|
+
return this._client.delete(`/vector_stores/${vectorStoreId}/files/${fileId}`, {
|
|
81
|
+
...options,
|
|
82
|
+
headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Attach a file to the given vector store and wait for it to be processed.
|
|
88
|
+
*/
|
|
89
|
+
async createAndPoll(
|
|
90
|
+
vectorStoreId: string,
|
|
91
|
+
body: FileCreateParams,
|
|
92
|
+
options?: Core.RequestOptions & { pollIntervalMs?: number },
|
|
93
|
+
): Promise<VectorStoreFile> {
|
|
94
|
+
const file = await this.create(vectorStoreId, body, options);
|
|
95
|
+
return await this.poll(vectorStoreId, file.id, options);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Wait for the vector store file to finish processing.
|
|
100
|
+
*
|
|
101
|
+
* Note: this will return even if the file failed to process, you need to check
|
|
102
|
+
* file.last_error and file.status to handle these cases
|
|
103
|
+
*/
|
|
104
|
+
async poll(
|
|
105
|
+
vectorStoreId: string,
|
|
106
|
+
fileId: string,
|
|
107
|
+
options?: Core.RequestOptions & { pollIntervalMs?: number },
|
|
108
|
+
): Promise<VectorStoreFile> {
|
|
109
|
+
const headers: { [key: string]: string } = { ...options?.headers, 'X-Stainless-Poll-Helper': 'true' };
|
|
110
|
+
if (options?.pollIntervalMs) {
|
|
111
|
+
headers['X-Stainless-Custom-Poll-Interval'] = options.pollIntervalMs.toString();
|
|
112
|
+
}
|
|
113
|
+
while (true) {
|
|
114
|
+
const fileResponse = await this.retrieve(vectorStoreId, fileId, {
|
|
115
|
+
...options,
|
|
116
|
+
headers,
|
|
117
|
+
}).withResponse();
|
|
118
|
+
|
|
119
|
+
const file = fileResponse.data;
|
|
120
|
+
|
|
121
|
+
switch (file.status) {
|
|
122
|
+
case 'in_progress':
|
|
123
|
+
let sleepInterval = 5000;
|
|
124
|
+
|
|
125
|
+
if (options?.pollIntervalMs) {
|
|
126
|
+
sleepInterval = options.pollIntervalMs;
|
|
127
|
+
} else {
|
|
128
|
+
const headerInterval = fileResponse.response.headers.get('openai-poll-after-ms');
|
|
129
|
+
if (headerInterval) {
|
|
130
|
+
const headerIntervalMs = parseInt(headerInterval);
|
|
131
|
+
if (!isNaN(headerIntervalMs)) {
|
|
132
|
+
sleepInterval = headerIntervalMs;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
await sleep(sleepInterval);
|
|
137
|
+
break;
|
|
138
|
+
case 'failed':
|
|
139
|
+
case 'completed':
|
|
140
|
+
return file;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Upload a file to the `files` API and then attach it to the given vector store.
|
|
147
|
+
* Note the file will be asynchronously processed (you can use the alternative
|
|
148
|
+
* polling helper method to wait for processing to complete).
|
|
149
|
+
*/
|
|
150
|
+
async upload(
|
|
151
|
+
vectorStoreId: string,
|
|
152
|
+
file: Uploadable,
|
|
153
|
+
options?: Core.RequestOptions,
|
|
154
|
+
): Promise<VectorStoreFile> {
|
|
155
|
+
const fileInfo = await this._client.files.create({ file: file, purpose: 'assistants' }, options);
|
|
156
|
+
return this.create(vectorStoreId, { file_id: fileInfo.id }, options);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Add a file to a vector store and poll until processing is complete.
|
|
161
|
+
*/
|
|
162
|
+
async uploadAndPoll(
|
|
163
|
+
vectorStoreId: string,
|
|
164
|
+
file: Uploadable,
|
|
165
|
+
options?: Core.RequestOptions & { pollIntervalMs?: number },
|
|
166
|
+
): Promise<VectorStoreFile> {
|
|
167
|
+
const fileInfo = await this._client.files.create({ file: file, purpose: 'assistants' }, options);
|
|
168
|
+
return await this.poll(vectorStoreId, fileInfo.id, options);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export class VectorStoreFilesPage extends CursorPage<VectorStoreFile> {}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* A list of files attached to a vector store.
|
|
176
|
+
*/
|
|
177
|
+
export interface VectorStoreFile {
|
|
178
|
+
/**
|
|
179
|
+
* The identifier, which can be referenced in API endpoints.
|
|
180
|
+
*/
|
|
181
|
+
id: string;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The Unix timestamp (in seconds) for when the vector store file was created.
|
|
185
|
+
*/
|
|
186
|
+
created_at: number;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The last error associated with this vector store file. Will be `null` if there
|
|
190
|
+
* are no errors.
|
|
191
|
+
*/
|
|
192
|
+
last_error: VectorStoreFile.LastError | null;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The object type, which is always `vector_store.file`.
|
|
196
|
+
*/
|
|
197
|
+
object: 'vector_store.file';
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The status of the vector store file, which can be either `in_progress`,
|
|
201
|
+
* `completed`, `cancelled`, or `failed`. The status `completed` indicates that the
|
|
202
|
+
* vector store file is ready for use.
|
|
203
|
+
*/
|
|
204
|
+
status: 'in_progress' | 'completed' | 'cancelled' | 'failed';
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The ID of the
|
|
208
|
+
* [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object)
|
|
209
|
+
* that the [File](https://platform.openai.com/docs/api-reference/files) is
|
|
210
|
+
* attached to.
|
|
211
|
+
*/
|
|
212
|
+
vector_store_id: string;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export namespace VectorStoreFile {
|
|
216
|
+
/**
|
|
217
|
+
* The last error associated with this vector store file. Will be `null` if there
|
|
218
|
+
* are no errors.
|
|
219
|
+
*/
|
|
220
|
+
export interface LastError {
|
|
221
|
+
/**
|
|
222
|
+
* One of `server_error` or `rate_limit_exceeded`.
|
|
223
|
+
*/
|
|
224
|
+
code: 'internal_error' | 'file_not_found' | 'parsing_error' | 'unhandled_mime_type';
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* A human-readable description of the error.
|
|
228
|
+
*/
|
|
229
|
+
message: string;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export interface VectorStoreFileDeleted {
|
|
234
|
+
id: string;
|
|
235
|
+
|
|
236
|
+
deleted: boolean;
|
|
237
|
+
|
|
238
|
+
object: 'vector_store.file.deleted';
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export interface FileCreateParams {
|
|
242
|
+
/**
|
|
243
|
+
* A [File](https://platform.openai.com/docs/api-reference/files) ID that the
|
|
244
|
+
* vector store should use. Useful for tools like `file_search` that can access
|
|
245
|
+
* files.
|
|
246
|
+
*/
|
|
247
|
+
file_id: string;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export interface FileListParams extends CursorPageParams {
|
|
251
|
+
/**
|
|
252
|
+
* A cursor for use in pagination. `before` is an object ID that defines your place
|
|
253
|
+
* in the list. For instance, if you make a list request and receive 100 objects,
|
|
254
|
+
* ending with obj_foo, your subsequent call can include before=obj_foo in order to
|
|
255
|
+
* fetch the previous page of the list.
|
|
256
|
+
*/
|
|
257
|
+
before?: string;
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.
|
|
261
|
+
*/
|
|
262
|
+
filter?: 'in_progress' | 'completed' | 'failed' | 'cancelled';
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Sort order by the `created_at` timestamp of the objects. `asc` for ascending
|
|
266
|
+
* order and `desc` for descending order.
|
|
267
|
+
*/
|
|
268
|
+
order?: 'asc' | 'desc';
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export namespace Files {
|
|
272
|
+
export import VectorStoreFile = FilesAPI.VectorStoreFile;
|
|
273
|
+
export import VectorStoreFileDeleted = FilesAPI.VectorStoreFileDeleted;
|
|
274
|
+
export import VectorStoreFilesPage = FilesAPI.VectorStoreFilesPage;
|
|
275
|
+
export import FileCreateParams = FilesAPI.FileCreateParams;
|
|
276
|
+
export import FileListParams = FilesAPI.FileListParams;
|
|
277
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
export {
|
|
4
|
+
VectorStore,
|
|
5
|
+
VectorStoreDeleted,
|
|
6
|
+
VectorStoreCreateParams,
|
|
7
|
+
VectorStoreUpdateParams,
|
|
8
|
+
VectorStoreListParams,
|
|
9
|
+
VectorStoresPage,
|
|
10
|
+
VectorStores,
|
|
11
|
+
} from './vector-stores';
|
|
12
|
+
export {
|
|
13
|
+
VectorStoreFile,
|
|
14
|
+
VectorStoreFileDeleted,
|
|
15
|
+
FileCreateParams,
|
|
16
|
+
FileListParams,
|
|
17
|
+
VectorStoreFilesPage,
|
|
18
|
+
Files,
|
|
19
|
+
} from './files';
|
|
20
|
+
export {
|
|
21
|
+
VectorStoreFileBatch,
|
|
22
|
+
FileBatchCreateParams,
|
|
23
|
+
FileBatchListFilesParams,
|
|
24
|
+
FileBatches,
|
|
25
|
+
} from './file-batches';
|