files.mjs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. import { APIResource } from "../core/resource.mjs";
  3. import { CursorPage } from "../core/pagination.mjs";
  4. import { buildHeaders } from "../internal/headers.mjs";
  5. import { sleep } from "../internal/utils/sleep.mjs";
  6. import { APIConnectionTimeoutError } from "../error.mjs";
  7. import { multipartFormRequestOptions } from "../internal/uploads.mjs";
  8. import { path } from "../internal/utils/path.mjs";
  9. export class Files extends APIResource {
  10. /**
  11. * Upload a file that can be used across various endpoints. Individual files can be
  12. * up to 512 MB, and the size of all files uploaded by one organization can be up
  13. * to 1 TB.
  14. *
  15. * - The Assistants API supports files up to 2 million tokens and of specific file
  16. * types. See the
  17. * [Assistants Tools guide](https://platform.openai.com/docs/assistants/tools)
  18. * for details.
  19. * - The Fine-tuning API only supports `.jsonl` files. The input also has certain
  20. * required formats for fine-tuning
  21. * [chat](https://platform.openai.com/docs/api-reference/fine-tuning/chat-input)
  22. * or
  23. * [completions](https://platform.openai.com/docs/api-reference/fine-tuning/completions-input)
  24. * models.
  25. * - The Batch API only supports `.jsonl` files up to 200 MB in size. The input
  26. * also has a specific required
  27. * [format](https://platform.openai.com/docs/api-reference/batch/request-input).
  28. *
  29. * Please [contact us](https://help.openai.com/) if you need to increase these
  30. * storage limits.
  31. */
  32. create(body, options) {
  33. return this._client.post('/files', multipartFormRequestOptions({ body, ...options }, this._client));
  34. }
  35. /**
  36. * Returns information about a specific file.
  37. */
  38. retrieve(fileID, options) {
  39. return this._client.get(path `/files/${fileID}`, options);
  40. }
  41. /**
  42. * Returns a list of files.
  43. */
  44. list(query = {}, options) {
  45. return this._client.getAPIList('/files', (CursorPage), { query, ...options });
  46. }
  47. /**
  48. * Delete a file and remove it from all vector stores.
  49. */
  50. delete(fileID, options) {
  51. return this._client.delete(path `/files/${fileID}`, options);
  52. }
  53. /**
  54. * Returns the contents of the specified file.
  55. */
  56. content(fileID, options) {
  57. return this._client.get(path `/files/${fileID}/content`, {
  58. ...options,
  59. headers: buildHeaders([{ Accept: 'application/binary' }, options?.headers]),
  60. __binaryResponse: true,
  61. });
  62. }
  63. /**
  64. * Waits for the given file to be processed, default timeout is 30 mins.
  65. */
  66. async waitForProcessing(id, { pollInterval = 5000, maxWait = 30 * 60 * 1000 } = {}) {
  67. const TERMINAL_STATES = new Set(['processed', 'error', 'deleted']);
  68. const start = Date.now();
  69. let file = await this.retrieve(id);
  70. while (!file.status || !TERMINAL_STATES.has(file.status)) {
  71. await sleep(pollInterval);
  72. file = await this.retrieve(id);
  73. if (Date.now() - start > maxWait) {
  74. throw new APIConnectionTimeoutError({
  75. message: `Giving up on waiting for file ${id} to finish processing after ${maxWait} milliseconds.`,
  76. });
  77. }
  78. }
  79. return file;
  80. }
  81. }
  82. //# sourceMappingURL=files.mjs.map