files.js 3.5 KB

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