files.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. import { APIResource } from "../resource.mjs";
  3. import { isRequestOptions } from "../core.mjs";
  4. import { sleep } from "../core.mjs";
  5. import { APIConnectionTimeoutError } from "../error.mjs";
  6. import * as Core from "../core.mjs";
  7. import { CursorPage } from "../pagination.mjs";
  8. export class Files extends APIResource {
  9. /**
  10. * Upload a file that can be used across various endpoints. Individual files can be
  11. * up to 512 MB, and the size of all files uploaded by one organization can be up
  12. * to 100 GB.
  13. *
  14. * The Assistants API supports files up to 2 million tokens and of specific file
  15. * types. See the
  16. * [Assistants Tools guide](https://platform.openai.com/docs/assistants/tools) for
  17. * details.
  18. *
  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) or
  22. * [completions](https://platform.openai.com/docs/api-reference/fine-tuning/completions-input)
  23. * models.
  24. *
  25. * The Batch API only supports `.jsonl` files up to 200 MB in size. The input also
  26. * 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', Core.multipartFormRequestOptions({ body, ...options }));
  34. }
  35. /**
  36. * Returns information about a specific file.
  37. */
  38. retrieve(fileId, options) {
  39. return this._client.get(`/files/${fileId}`, options);
  40. }
  41. list(query = {}, options) {
  42. if (isRequestOptions(query)) {
  43. return this.list({}, query);
  44. }
  45. return this._client.getAPIList('/files', FileObjectsPage, { query, ...options });
  46. }
  47. /**
  48. * Delete a file.
  49. */
  50. del(fileId, options) {
  51. return this._client.delete(`/files/${fileId}`, options);
  52. }
  53. /**
  54. * Returns the contents of the specified file.
  55. */
  56. content(fileId, options) {
  57. return this._client.get(`/files/${fileId}/content`, {
  58. ...options,
  59. headers: { Accept: 'application/binary', ...options?.headers },
  60. __binaryResponse: true,
  61. });
  62. }
  63. /**
  64. * Returns the contents of the specified file.
  65. *
  66. * @deprecated The `.content()` method should be used instead
  67. */
  68. retrieveContent(fileId, options) {
  69. return this._client.get(`/files/${fileId}/content`, options);
  70. }
  71. /**
  72. * Waits for the given file to be processed, default timeout is 30 mins.
  73. */
  74. async waitForProcessing(id, { pollInterval = 5000, maxWait = 30 * 60 * 1000 } = {}) {
  75. const TERMINAL_STATES = new Set(['processed', 'error', 'deleted']);
  76. const start = Date.now();
  77. let file = await this.retrieve(id);
  78. while (!file.status || !TERMINAL_STATES.has(file.status)) {
  79. await sleep(pollInterval);
  80. file = await this.retrieve(id);
  81. if (Date.now() - start > maxWait) {
  82. throw new APIConnectionTimeoutError({
  83. message: `Giving up on waiting for file ${id} to finish processing after ${maxWait} milliseconds.`,
  84. });
  85. }
  86. }
  87. return file;
  88. }
  89. }
  90. export class FileObjectsPage extends CursorPage {
  91. }
  92. Files.FileObjectsPage = FileObjectsPage;
  93. //# sourceMappingURL=files.mjs.map