-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.ts
More file actions
317 lines (280 loc) · 9.37 KB
/
parse.ts
File metadata and controls
317 lines (280 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { APIResource } from '../resource';
import { isRequestOptions } from '../core';
import * as Core from '../core';
import * as Shared from './shared';
export class Parse extends APIResource {
/**
* Parse a file into a structured Markdown and/or JSON. Files must be less than
* 100MB and 400 pages. We use LibreOffice to convert DOC(X) and PPT(X) files to
* PDF, which may affect page count.
*
* See our [blog post](https://contextual.ai/blog/document-parser-for-rag) and
* [code examples](https://github.com/ContextualAI/examples/blob/main/03-standalone-api/04-parse/parse.ipynb).
* Email [parse-feedback@contextual.ai](mailto:parse-feedback@contextual.ai) with
* any feedback or questions.
*/
create(body: ParseCreateParams, options?: Core.RequestOptions): Core.APIPromise<ParseCreateResponse> {
return this._client.post('/parse', Core.multipartFormRequestOptions({ body, ...options }));
}
/**
* Get the results of a parse job.
*
* Parse job results are retained for up to 30 days after job creation. Fetching
* results for a parse job that is older than 30 days will return a 404 error.
*/
jobResults(
jobId: string,
query?: ParseJobResultsParams,
options?: Core.RequestOptions,
): Core.APIPromise<ParseJobResultsResponse>;
jobResults(jobId: string, options?: Core.RequestOptions): Core.APIPromise<ParseJobResultsResponse>;
jobResults(
jobId: string,
query: ParseJobResultsParams | Core.RequestOptions = {},
options?: Core.RequestOptions,
): Core.APIPromise<ParseJobResultsResponse> {
if (isRequestOptions(query)) {
return this.jobResults(jobId, {}, query);
}
return this._client.get(`/parse/jobs/${jobId}/results`, { query, ...options });
}
/**
* Get the status of a parse job.
*
* Parse job results are retained for up to 30 days after job creation. Fetching a
* status for a parse job that is older than 30 days will return a 404 error.
*/
jobStatus(jobId: string, options?: Core.RequestOptions): Core.APIPromise<ParseJobStatusResponse> {
return this._client.get(`/parse/jobs/${jobId}/status`, options);
}
/**
* Get list of parse jobs, sorted from most recent to oldest.
*
* Returns all jobs from the last 30 days, or since the optional `uploaded_after`
* timestamp.
*/
jobs(query?: ParseJobsParams, options?: Core.RequestOptions): Core.APIPromise<ParseJobsResponse>;
jobs(options?: Core.RequestOptions): Core.APIPromise<ParseJobsResponse>;
jobs(
query: ParseJobsParams | Core.RequestOptions = {},
options?: Core.RequestOptions,
): Core.APIPromise<ParseJobsResponse> {
if (isRequestOptions(query)) {
return this.jobs({}, query);
}
return this._client.get('/parse/jobs', { query, ...options });
}
}
/**
* /parse response object.
*/
export interface ParseCreateResponse {
/**
* Unique ID of the parse job
*/
job_id: string;
}
/**
* /parse results reponse object.
*/
export interface ParseJobResultsResponse {
/**
* The name of the file that was uploaded for parsing
*/
file_name: string;
/**
* The current status of the parse job
*/
status: 'pending' | 'processing' | 'retrying' | 'completed' | 'failed' | 'cancelled';
/**
* Document-level metadata parsed from the document
*/
document_metadata?: ParseJobResultsResponse.DocumentMetadata;
/**
* The parsed, structured Markdown of the input file. Only present if
* `markdown-document` was among the requested output types.
*/
markdown_document?: string;
/**
* Per-page parse results, containing per-page Markdown (if `markdown-per-page` was
* requested) and/or per-page `ParsedBlock`s (if `blocks-per-page` was requested).
*/
pages?: Array<ParseJobResultsResponse.Page>;
}
export namespace ParseJobResultsResponse {
/**
* Document-level metadata parsed from the document
*/
export interface DocumentMetadata {
/**
* Hierarchy of the document, as both heading blocks and a markdown table of
* contents
*/
hierarchy?: DocumentMetadata.Hierarchy;
}
export namespace DocumentMetadata {
/**
* Hierarchy of the document, as both heading blocks and a markdown table of
* contents
*/
export interface Hierarchy {
/**
* Heading blocks which define the hierarchy of the document
*/
blocks?: Array<Shared.ParsedBlock>;
/**
* Markdown representation of the table of contents for this document
*/
table_of_contents?: string;
}
}
/**
* Per-page parse results.
*/
export interface Page {
/**
* The index of the parsed page (zero-indexed)
*/
index: number;
/**
* The parsed, structured blocks of this page. Present if `blocks-per-page` was
* among the requested output types.
*/
blocks?: Array<Shared.ParsedBlock>;
/**
* The parsed, structured Markdown of this page. Present if `markdown-per-page` was
* among the requested output types.
*/
markdown?: string;
}
}
/**
* /parse status reponse object.
*/
export interface ParseJobStatusResponse {
/**
* The name of the file that was uploaded for parsing
*/
file_name: string;
/**
* The current status of the parse job
*/
status: 'pending' | 'processing' | 'retrying' | 'completed' | 'failed' | 'cancelled';
}
/**
* /parse list jobs object.
*/
export interface ParseJobsResponse {
/**
* List of parse jobs
*/
jobs: Array<ParseJobsResponse.Job>;
/**
* Total number of parse jobs
*/
total_jobs: number;
/**
* Next cursor to continue pagination. Omitted if there are no more parse jobs
* after these ones.
*/
next_cursor?: string;
}
export namespace ParseJobsResponse {
export interface Job {
/**
* Unique ID of the parse job
*/
id: string;
/**
* The name of the file that was uploaded for parsing
*/
file_name: string;
/**
* The current status of the parse job
*/
status: 'pending' | 'processing' | 'retrying' | 'completed' | 'failed' | 'cancelled';
}
}
export interface ParseCreateParams {
/**
* The file to be parsed. The file type must be PDF, DOC / DOCX, PPT / PPTX.
*/
raw_file: Core.Uploadable;
/**
* Adds a table of contents to the output with the structure of the entire parsed
* document. This feature is in beta. Controls parsing heading levels (e.g. H1, H2,
* H3) at higher quality. Not permitted in `basic` parsing_mode, or if page_range
* is not continuous and/or does not start from page zero.
*/
enable_document_hierarchy?: boolean;
/**
* Controls whether tables are split into multiple tables by row with the headers
* propagated. Use for improving LLM comprehension of very large tables. Not
* permitted in `basic` parsing_mode.
*/
enable_split_tables?: boolean;
/**
* Controls how thorough figure captions are. `concise` is short and minimizes
* chances of hallucinations. `detailed` is more thorough and can include
* commentary; this mode is in beta. Not permitted in `basic` parsing_mode.
*/
figure_caption_mode?: 'concise' | 'detailed';
/**
* Threshold number of table cells beyond which large tables are split if
* `enable_split_tables` is True. Must be null if `enable_split_tables` is False.
*/
max_split_table_cells?: number;
/**
* Optional string representing page range to be parsed. Format: comma-separated
* indexes (0-based, e.g. `0,1,2,5,6`), or ranges inclusive of both ends (e.g.
* `0-2,5,6`)
*/
page_range?: string;
/**
* The settings to use for parsing. `basic` is for simple, text-only documents.
* `standard` is for complex documents with images, complex hierarchy, and/or no
* natively encoded textual data (e.g. for scanned documents).
*/
parse_mode?: 'basic' | 'standard';
}
export interface ParseJobResultsParams {
/**
* The desired output format(s) of the parsed file. Must be `markdown-document`,
* `markdown-per-page`, and/or `blocks-per-page`. Specify multiple values to get
* multiple formats in the response. `markdown-document` parses the whole document
* into a single concatenated markdown output. `markdown-per-page` provides
* markdown output per page. `blocks-per-page` provides a structured JSON
* representation of the content blocks on each page, sorted by reading order.
*/
output_types?: Array<'markdown-document' | 'markdown-per-page' | 'blocks-per-page'>;
}
export interface ParseJobsParams {
/**
* Cursor from the previous call to list parse jobs, used to retrieve the next set
* of results
*/
cursor?: string;
/**
* Maximum number of parse jobs to return
*/
limit?: number;
/**
* Filters to only documents uploaded to `/parse` at or after specified UTC
* timestamp. If not provided, or if the provided timestamp is before the maximum
* parse job retention period (30 days), the maximum retention period will be used
* instead.
*/
uploaded_after?: string;
}
export declare namespace Parse {
export {
type ParseCreateResponse as ParseCreateResponse,
type ParseJobResultsResponse as ParseJobResultsResponse,
type ParseJobStatusResponse as ParseJobStatusResponse,
type ParseJobsResponse as ParseJobsResponse,
type ParseCreateParams as ParseCreateParams,
type ParseJobResultsParams as ParseJobResultsParams,
type ParseJobsParams as ParseJobsParams,
};
}