files.mjs 5.7 KB

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