files.mjs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. import { APIResource } from "../../resource.mjs";
  3. import { sleep, isRequestOptions } from "../../core.mjs";
  4. import { CursorPage, Page } from "../../pagination.mjs";
  5. export class Files extends APIResource {
  6. /**
  7. * Create a vector store file by attaching a
  8. * [File](https://platform.openai.com/docs/api-reference/files) to a
  9. * [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object).
  10. */
  11. create(vectorStoreId, body, options) {
  12. return this._client.post(`/vector_stores/${vectorStoreId}/files`, {
  13. body,
  14. ...options,
  15. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  16. });
  17. }
  18. /**
  19. * Retrieves a vector store file.
  20. */
  21. retrieve(vectorStoreId, fileId, options) {
  22. return this._client.get(`/vector_stores/${vectorStoreId}/files/${fileId}`, {
  23. ...options,
  24. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  25. });
  26. }
  27. /**
  28. * Update attributes on a vector store file.
  29. */
  30. update(vectorStoreId, fileId, body, options) {
  31. return this._client.post(`/vector_stores/${vectorStoreId}/files/${fileId}`, {
  32. body,
  33. ...options,
  34. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  35. });
  36. }
  37. list(vectorStoreId, query = {}, options) {
  38. if (isRequestOptions(query)) {
  39. return this.list(vectorStoreId, {}, query);
  40. }
  41. return this._client.getAPIList(`/vector_stores/${vectorStoreId}/files`, VectorStoreFilesPage, {
  42. query,
  43. ...options,
  44. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  45. });
  46. }
  47. /**
  48. * Delete a vector store file. This will remove the file from the vector store but
  49. * the file itself will not be deleted. To delete the file, use the
  50. * [delete file](https://platform.openai.com/docs/api-reference/files/delete)
  51. * endpoint.
  52. */
  53. del(vectorStoreId, fileId, options) {
  54. return this._client.delete(`/vector_stores/${vectorStoreId}/files/${fileId}`, {
  55. ...options,
  56. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  57. });
  58. }
  59. /**
  60. * Attach a file to the given vector store and wait for it to be processed.
  61. */
  62. async createAndPoll(vectorStoreId, body, options) {
  63. const file = await this.create(vectorStoreId, body, options);
  64. return await this.poll(vectorStoreId, file.id, options);
  65. }
  66. /**
  67. * Wait for the vector store file to finish processing.
  68. *
  69. * Note: this will return even if the file failed to process, you need to check
  70. * file.last_error and file.status to handle these cases
  71. */
  72. async poll(vectorStoreId, fileId, options) {
  73. const headers = { ...options?.headers, 'X-Stainless-Poll-Helper': 'true' };
  74. if (options?.pollIntervalMs) {
  75. headers['X-Stainless-Custom-Poll-Interval'] = options.pollIntervalMs.toString();
  76. }
  77. while (true) {
  78. const fileResponse = await this.retrieve(vectorStoreId, fileId, {
  79. ...options,
  80. headers,
  81. }).withResponse();
  82. const file = fileResponse.data;
  83. switch (file.status) {
  84. case 'in_progress':
  85. let sleepInterval = 5000;
  86. if (options?.pollIntervalMs) {
  87. sleepInterval = options.pollIntervalMs;
  88. }
  89. else {
  90. const headerInterval = fileResponse.response.headers.get('openai-poll-after-ms');
  91. if (headerInterval) {
  92. const headerIntervalMs = parseInt(headerInterval);
  93. if (!isNaN(headerIntervalMs)) {
  94. sleepInterval = headerIntervalMs;
  95. }
  96. }
  97. }
  98. await sleep(sleepInterval);
  99. break;
  100. case 'failed':
  101. case 'completed':
  102. return file;
  103. }
  104. }
  105. }
  106. /**
  107. * Upload a file to the `files` API and then attach it to the given vector store.
  108. *
  109. * Note the file will be asynchronously processed (you can use the alternative
  110. * polling helper method to wait for processing to complete).
  111. */
  112. async upload(vectorStoreId, file, options) {
  113. const fileInfo = await this._client.files.create({ file: file, purpose: 'assistants' }, options);
  114. return this.create(vectorStoreId, { file_id: fileInfo.id }, options);
  115. }
  116. /**
  117. * Add a file to a vector store and poll until processing is complete.
  118. */
  119. async uploadAndPoll(vectorStoreId, file, options) {
  120. const fileInfo = await this.upload(vectorStoreId, file, options);
  121. return await this.poll(vectorStoreId, fileInfo.id, options);
  122. }
  123. /**
  124. * Retrieve the parsed contents of a vector store file.
  125. */
  126. content(vectorStoreId, fileId, options) {
  127. return this._client.getAPIList(`/vector_stores/${vectorStoreId}/files/${fileId}/content`, FileContentResponsesPage, { ...options, headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers } });
  128. }
  129. }
  130. export class VectorStoreFilesPage extends CursorPage {
  131. }
  132. /**
  133. * Note: no pagination actually occurs yet, this is for forwards-compatibility.
  134. */
  135. export class FileContentResponsesPage extends Page {
  136. }
  137. Files.VectorStoreFilesPage = VectorStoreFilesPage;
  138. Files.FileContentResponsesPage = FileContentResponsesPage;
  139. //# sourceMappingURL=files.mjs.map