files.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  3. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  4. if (k2 === undefined) k2 = k;
  5. var desc = Object.getOwnPropertyDescriptor(m, k);
  6. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  7. desc = { enumerable: true, get: function() { return m[k]; } };
  8. }
  9. Object.defineProperty(o, k2, desc);
  10. }) : (function(o, m, k, k2) {
  11. if (k2 === undefined) k2 = k;
  12. o[k2] = m[k];
  13. }));
  14. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  15. Object.defineProperty(o, "default", { enumerable: true, value: v });
  16. }) : function(o, v) {
  17. o["default"] = v;
  18. });
  19. var __importStar = (this && this.__importStar) || function (mod) {
  20. if (mod && mod.__esModule) return mod;
  21. var result = {};
  22. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  23. __setModuleDefault(result, mod);
  24. return result;
  25. };
  26. Object.defineProperty(exports, "__esModule", { value: true });
  27. exports.FileObjectsPage = exports.Files = void 0;
  28. const resource_1 = require("../resource.js");
  29. const core_1 = require("../core.js");
  30. const core_2 = require("../core.js");
  31. const error_1 = require("../error.js");
  32. const Core = __importStar(require("../core.js"));
  33. const pagination_1 = require("../pagination.js");
  34. class Files extends resource_1.APIResource {
  35. /**
  36. * Upload a file that can be used across various endpoints. Individual files can be
  37. * up to 512 MB, and the size of all files uploaded by one organization can be up
  38. * to 100 GB.
  39. *
  40. * The Assistants API supports files up to 2 million tokens and of specific file
  41. * types. See the
  42. * [Assistants Tools guide](https://platform.openai.com/docs/assistants/tools) for
  43. * details.
  44. *
  45. * The Fine-tuning API only supports `.jsonl` files. The input also has certain
  46. * required formats for fine-tuning
  47. * [chat](https://platform.openai.com/docs/api-reference/fine-tuning/chat-input) or
  48. * [completions](https://platform.openai.com/docs/api-reference/fine-tuning/completions-input)
  49. * models.
  50. *
  51. * The Batch API only supports `.jsonl` files up to 200 MB in size. The input also
  52. * has a specific required
  53. * [format](https://platform.openai.com/docs/api-reference/batch/request-input).
  54. *
  55. * Please [contact us](https://help.openai.com/) if you need to increase these
  56. * storage limits.
  57. */
  58. create(body, options) {
  59. return this._client.post('/files', Core.multipartFormRequestOptions({ body, ...options }));
  60. }
  61. /**
  62. * Returns information about a specific file.
  63. */
  64. retrieve(fileId, options) {
  65. return this._client.get(`/files/${fileId}`, options);
  66. }
  67. list(query = {}, options) {
  68. if ((0, core_1.isRequestOptions)(query)) {
  69. return this.list({}, query);
  70. }
  71. return this._client.getAPIList('/files', FileObjectsPage, { query, ...options });
  72. }
  73. /**
  74. * Delete a file.
  75. */
  76. del(fileId, options) {
  77. return this._client.delete(`/files/${fileId}`, options);
  78. }
  79. /**
  80. * Returns the contents of the specified file.
  81. */
  82. content(fileId, options) {
  83. return this._client.get(`/files/${fileId}/content`, {
  84. ...options,
  85. headers: { Accept: 'application/binary', ...options?.headers },
  86. __binaryResponse: true,
  87. });
  88. }
  89. /**
  90. * Returns the contents of the specified file.
  91. *
  92. * @deprecated The `.content()` method should be used instead
  93. */
  94. retrieveContent(fileId, options) {
  95. return this._client.get(`/files/${fileId}/content`, options);
  96. }
  97. /**
  98. * Waits for the given file to be processed, default timeout is 30 mins.
  99. */
  100. async waitForProcessing(id, { pollInterval = 5000, maxWait = 30 * 60 * 1000 } = {}) {
  101. const TERMINAL_STATES = new Set(['processed', 'error', 'deleted']);
  102. const start = Date.now();
  103. let file = await this.retrieve(id);
  104. while (!file.status || !TERMINAL_STATES.has(file.status)) {
  105. await (0, core_2.sleep)(pollInterval);
  106. file = await this.retrieve(id);
  107. if (Date.now() - start > maxWait) {
  108. throw new error_1.APIConnectionTimeoutError({
  109. message: `Giving up on waiting for file ${id} to finish processing after ${maxWait} milliseconds.`,
  110. });
  111. }
  112. }
  113. return file;
  114. }
  115. }
  116. exports.Files = Files;
  117. class FileObjectsPage extends pagination_1.CursorPage {
  118. }
  119. exports.FileObjectsPage = FileObjectsPage;
  120. Files.FileObjectsPage = FileObjectsPage;
  121. //# sourceMappingURL=files.js.map