files.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. from __future__ import annotations
  3. from typing import TYPE_CHECKING
  4. from typing_extensions import Literal, assert_never
  5. import httpx
  6. from .... import _legacy_response
  7. from ...._types import NOT_GIVEN, Body, Query, Headers, NotGiven, FileTypes
  8. from ...._utils import (
  9. is_given,
  10. maybe_transform,
  11. async_maybe_transform,
  12. )
  13. from ...._compat import cached_property
  14. from ...._resource import SyncAPIResource, AsyncAPIResource
  15. from ...._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
  16. from ....pagination import SyncCursorPage, AsyncCursorPage
  17. from ....types.beta import FileChunkingStrategyParam
  18. from ...._base_client import AsyncPaginator, make_request_options
  19. from ....types.beta.vector_stores import file_list_params, file_create_params
  20. from ....types.beta.file_chunking_strategy_param import FileChunkingStrategyParam
  21. from ....types.beta.vector_stores.vector_store_file import VectorStoreFile
  22. from ....types.beta.vector_stores.vector_store_file_deleted import VectorStoreFileDeleted
  23. __all__ = ["Files", "AsyncFiles"]
  24. class Files(SyncAPIResource):
  25. @cached_property
  26. def with_raw_response(self) -> FilesWithRawResponse:
  27. """
  28. This property can be used as a prefix for any HTTP method call to return
  29. the raw response object instead of the parsed content.
  30. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  31. """
  32. return FilesWithRawResponse(self)
  33. @cached_property
  34. def with_streaming_response(self) -> FilesWithStreamingResponse:
  35. """
  36. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  37. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  38. """
  39. return FilesWithStreamingResponse(self)
  40. def create(
  41. self,
  42. vector_store_id: str,
  43. *,
  44. file_id: str,
  45. chunking_strategy: FileChunkingStrategyParam | NotGiven = NOT_GIVEN,
  46. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  47. # The extra values given here take precedence over values defined on the client or passed to this method.
  48. extra_headers: Headers | None = None,
  49. extra_query: Query | None = None,
  50. extra_body: Body | None = None,
  51. timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
  52. ) -> VectorStoreFile:
  53. """
  54. Create a vector store file by attaching a
  55. [File](https://platform.openai.com/docs/api-reference/files) to a
  56. [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object).
  57. Args:
  58. file_id: A [File](https://platform.openai.com/docs/api-reference/files) ID that the
  59. vector store should use. Useful for tools like `file_search` that can access
  60. files.
  61. chunking_strategy: The chunking strategy used to chunk the file(s). If not set, will use the `auto`
  62. strategy. Only applicable if `file_ids` is non-empty.
  63. extra_headers: Send extra headers
  64. extra_query: Add additional query parameters to the request
  65. extra_body: Add additional JSON properties to the request
  66. timeout: Override the client-level default timeout for this request, in seconds
  67. """
  68. if not vector_store_id:
  69. raise ValueError(f"Expected a non-empty value for `vector_store_id` but received {vector_store_id!r}")
  70. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  71. return self._post(
  72. f"/vector_stores/{vector_store_id}/files",
  73. body=maybe_transform(
  74. {
  75. "file_id": file_id,
  76. "chunking_strategy": chunking_strategy,
  77. },
  78. file_create_params.FileCreateParams,
  79. ),
  80. options=make_request_options(
  81. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  82. ),
  83. cast_to=VectorStoreFile,
  84. )
  85. def retrieve(
  86. self,
  87. file_id: str,
  88. *,
  89. vector_store_id: str,
  90. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  91. # The extra values given here take precedence over values defined on the client or passed to this method.
  92. extra_headers: Headers | None = None,
  93. extra_query: Query | None = None,
  94. extra_body: Body | None = None,
  95. timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
  96. ) -> VectorStoreFile:
  97. """
  98. Retrieves a vector store file.
  99. Args:
  100. extra_headers: Send extra headers
  101. extra_query: Add additional query parameters to the request
  102. extra_body: Add additional JSON properties to the request
  103. timeout: Override the client-level default timeout for this request, in seconds
  104. """
  105. if not vector_store_id:
  106. raise ValueError(f"Expected a non-empty value for `vector_store_id` but received {vector_store_id!r}")
  107. if not file_id:
  108. raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}")
  109. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  110. return self._get(
  111. f"/vector_stores/{vector_store_id}/files/{file_id}",
  112. options=make_request_options(
  113. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  114. ),
  115. cast_to=VectorStoreFile,
  116. )
  117. def list(
  118. self,
  119. vector_store_id: str,
  120. *,
  121. after: str | NotGiven = NOT_GIVEN,
  122. before: str | NotGiven = NOT_GIVEN,
  123. filter: Literal["in_progress", "completed", "failed", "cancelled"] | NotGiven = NOT_GIVEN,
  124. limit: int | NotGiven = NOT_GIVEN,
  125. order: Literal["asc", "desc"] | NotGiven = NOT_GIVEN,
  126. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  127. # The extra values given here take precedence over values defined on the client or passed to this method.
  128. extra_headers: Headers | None = None,
  129. extra_query: Query | None = None,
  130. extra_body: Body | None = None,
  131. timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
  132. ) -> SyncCursorPage[VectorStoreFile]:
  133. """
  134. Returns a list of vector store files.
  135. Args:
  136. after: A cursor for use in pagination. `after` is an object ID that defines your place
  137. in the list. For instance, if you make a list request and receive 100 objects,
  138. ending with obj_foo, your subsequent call can include after=obj_foo in order to
  139. fetch the next page of the list.
  140. before: A cursor for use in pagination. `before` is an object ID that defines your place
  141. in the list. For instance, if you make a list request and receive 100 objects,
  142. starting with obj_foo, your subsequent call can include before=obj_foo in order
  143. to fetch the previous page of the list.
  144. filter: Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.
  145. limit: A limit on the number of objects to be returned. Limit can range between 1 and
  146. 100, and the default is 20.
  147. order: Sort order by the `created_at` timestamp of the objects. `asc` for ascending
  148. order and `desc` for descending order.
  149. extra_headers: Send extra headers
  150. extra_query: Add additional query parameters to the request
  151. extra_body: Add additional JSON properties to the request
  152. timeout: Override the client-level default timeout for this request, in seconds
  153. """
  154. if not vector_store_id:
  155. raise ValueError(f"Expected a non-empty value for `vector_store_id` but received {vector_store_id!r}")
  156. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  157. return self._get_api_list(
  158. f"/vector_stores/{vector_store_id}/files",
  159. page=SyncCursorPage[VectorStoreFile],
  160. options=make_request_options(
  161. extra_headers=extra_headers,
  162. extra_query=extra_query,
  163. extra_body=extra_body,
  164. timeout=timeout,
  165. query=maybe_transform(
  166. {
  167. "after": after,
  168. "before": before,
  169. "filter": filter,
  170. "limit": limit,
  171. "order": order,
  172. },
  173. file_list_params.FileListParams,
  174. ),
  175. ),
  176. model=VectorStoreFile,
  177. )
  178. def delete(
  179. self,
  180. file_id: str,
  181. *,
  182. vector_store_id: str,
  183. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  184. # The extra values given here take precedence over values defined on the client or passed to this method.
  185. extra_headers: Headers | None = None,
  186. extra_query: Query | None = None,
  187. extra_body: Body | None = None,
  188. timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
  189. ) -> VectorStoreFileDeleted:
  190. """Delete a vector store file.
  191. This will remove the file from the vector store but
  192. the file itself will not be deleted. To delete the file, use the
  193. [delete file](https://platform.openai.com/docs/api-reference/files/delete)
  194. endpoint.
  195. Args:
  196. extra_headers: Send extra headers
  197. extra_query: Add additional query parameters to the request
  198. extra_body: Add additional JSON properties to the request
  199. timeout: Override the client-level default timeout for this request, in seconds
  200. """
  201. if not vector_store_id:
  202. raise ValueError(f"Expected a non-empty value for `vector_store_id` but received {vector_store_id!r}")
  203. if not file_id:
  204. raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}")
  205. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  206. return self._delete(
  207. f"/vector_stores/{vector_store_id}/files/{file_id}",
  208. options=make_request_options(
  209. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  210. ),
  211. cast_to=VectorStoreFileDeleted,
  212. )
  213. def create_and_poll(
  214. self,
  215. file_id: str,
  216. *,
  217. vector_store_id: str,
  218. poll_interval_ms: int | NotGiven = NOT_GIVEN,
  219. chunking_strategy: FileChunkingStrategyParam | NotGiven = NOT_GIVEN,
  220. ) -> VectorStoreFile:
  221. """Attach a file to the given vector store and wait for it to be processed."""
  222. self.create(vector_store_id=vector_store_id, file_id=file_id, chunking_strategy=chunking_strategy)
  223. return self.poll(
  224. file_id,
  225. vector_store_id=vector_store_id,
  226. poll_interval_ms=poll_interval_ms,
  227. )
  228. def poll(
  229. self,
  230. file_id: str,
  231. *,
  232. vector_store_id: str,
  233. poll_interval_ms: int | NotGiven = NOT_GIVEN,
  234. ) -> VectorStoreFile:
  235. """Wait for the vector store file to finish processing.
  236. Note: this will return even if the file failed to process, you need to check
  237. file.last_error and file.status to handle these cases
  238. """
  239. headers: dict[str, str] = {"X-Stainless-Poll-Helper": "true"}
  240. if is_given(poll_interval_ms):
  241. headers["X-Stainless-Custom-Poll-Interval"] = str(poll_interval_ms)
  242. while True:
  243. response = self.with_raw_response.retrieve(
  244. file_id,
  245. vector_store_id=vector_store_id,
  246. extra_headers=headers,
  247. )
  248. file = response.parse()
  249. if file.status == "in_progress":
  250. if not is_given(poll_interval_ms):
  251. from_header = response.headers.get("openai-poll-after-ms")
  252. if from_header is not None:
  253. poll_interval_ms = int(from_header)
  254. else:
  255. poll_interval_ms = 1000
  256. self._sleep(poll_interval_ms / 1000)
  257. elif file.status == "cancelled" or file.status == "completed" or file.status == "failed":
  258. return file
  259. else:
  260. if TYPE_CHECKING: # type: ignore[unreachable]
  261. assert_never(file.status)
  262. else:
  263. return file
  264. def upload(
  265. self,
  266. *,
  267. vector_store_id: str,
  268. file: FileTypes,
  269. chunking_strategy: FileChunkingStrategyParam | NotGiven = NOT_GIVEN,
  270. ) -> VectorStoreFile:
  271. """Upload a file to the `files` API and then attach it to the given vector store.
  272. Note the file will be asynchronously processed (you can use the alternative
  273. polling helper method to wait for processing to complete).
  274. """
  275. file_obj = self._client.files.create(file=file, purpose="assistants")
  276. return self.create(vector_store_id=vector_store_id, file_id=file_obj.id, chunking_strategy=chunking_strategy)
  277. def upload_and_poll(
  278. self,
  279. *,
  280. vector_store_id: str,
  281. file: FileTypes,
  282. poll_interval_ms: int | NotGiven = NOT_GIVEN,
  283. chunking_strategy: FileChunkingStrategyParam | NotGiven = NOT_GIVEN,
  284. ) -> VectorStoreFile:
  285. """Add a file to a vector store and poll until processing is complete."""
  286. file_obj = self._client.files.create(file=file, purpose="assistants")
  287. return self.create_and_poll(
  288. vector_store_id=vector_store_id,
  289. file_id=file_obj.id,
  290. chunking_strategy=chunking_strategy,
  291. poll_interval_ms=poll_interval_ms,
  292. )
  293. class AsyncFiles(AsyncAPIResource):
  294. @cached_property
  295. def with_raw_response(self) -> AsyncFilesWithRawResponse:
  296. """
  297. This property can be used as a prefix for any HTTP method call to return
  298. the raw response object instead of the parsed content.
  299. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  300. """
  301. return AsyncFilesWithRawResponse(self)
  302. @cached_property
  303. def with_streaming_response(self) -> AsyncFilesWithStreamingResponse:
  304. """
  305. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  306. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  307. """
  308. return AsyncFilesWithStreamingResponse(self)
  309. async def create(
  310. self,
  311. vector_store_id: str,
  312. *,
  313. file_id: str,
  314. chunking_strategy: FileChunkingStrategyParam | NotGiven = NOT_GIVEN,
  315. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  316. # The extra values given here take precedence over values defined on the client or passed to this method.
  317. extra_headers: Headers | None = None,
  318. extra_query: Query | None = None,
  319. extra_body: Body | None = None,
  320. timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
  321. ) -> VectorStoreFile:
  322. """
  323. Create a vector store file by attaching a
  324. [File](https://platform.openai.com/docs/api-reference/files) to a
  325. [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object).
  326. Args:
  327. file_id: A [File](https://platform.openai.com/docs/api-reference/files) ID that the
  328. vector store should use. Useful for tools like `file_search` that can access
  329. files.
  330. chunking_strategy: The chunking strategy used to chunk the file(s). If not set, will use the `auto`
  331. strategy. Only applicable if `file_ids` is non-empty.
  332. extra_headers: Send extra headers
  333. extra_query: Add additional query parameters to the request
  334. extra_body: Add additional JSON properties to the request
  335. timeout: Override the client-level default timeout for this request, in seconds
  336. """
  337. if not vector_store_id:
  338. raise ValueError(f"Expected a non-empty value for `vector_store_id` but received {vector_store_id!r}")
  339. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  340. return await self._post(
  341. f"/vector_stores/{vector_store_id}/files",
  342. body=await async_maybe_transform(
  343. {
  344. "file_id": file_id,
  345. "chunking_strategy": chunking_strategy,
  346. },
  347. file_create_params.FileCreateParams,
  348. ),
  349. options=make_request_options(
  350. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  351. ),
  352. cast_to=VectorStoreFile,
  353. )
  354. async def retrieve(
  355. self,
  356. file_id: str,
  357. *,
  358. vector_store_id: str,
  359. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  360. # The extra values given here take precedence over values defined on the client or passed to this method.
  361. extra_headers: Headers | None = None,
  362. extra_query: Query | None = None,
  363. extra_body: Body | None = None,
  364. timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
  365. ) -> VectorStoreFile:
  366. """
  367. Retrieves a vector store file.
  368. Args:
  369. extra_headers: Send extra headers
  370. extra_query: Add additional query parameters to the request
  371. extra_body: Add additional JSON properties to the request
  372. timeout: Override the client-level default timeout for this request, in seconds
  373. """
  374. if not vector_store_id:
  375. raise ValueError(f"Expected a non-empty value for `vector_store_id` but received {vector_store_id!r}")
  376. if not file_id:
  377. raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}")
  378. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  379. return await self._get(
  380. f"/vector_stores/{vector_store_id}/files/{file_id}",
  381. options=make_request_options(
  382. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  383. ),
  384. cast_to=VectorStoreFile,
  385. )
  386. def list(
  387. self,
  388. vector_store_id: str,
  389. *,
  390. after: str | NotGiven = NOT_GIVEN,
  391. before: str | NotGiven = NOT_GIVEN,
  392. filter: Literal["in_progress", "completed", "failed", "cancelled"] | NotGiven = NOT_GIVEN,
  393. limit: int | NotGiven = NOT_GIVEN,
  394. order: Literal["asc", "desc"] | NotGiven = NOT_GIVEN,
  395. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  396. # The extra values given here take precedence over values defined on the client or passed to this method.
  397. extra_headers: Headers | None = None,
  398. extra_query: Query | None = None,
  399. extra_body: Body | None = None,
  400. timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
  401. ) -> AsyncPaginator[VectorStoreFile, AsyncCursorPage[VectorStoreFile]]:
  402. """
  403. Returns a list of vector store files.
  404. Args:
  405. after: A cursor for use in pagination. `after` is an object ID that defines your place
  406. in the list. For instance, if you make a list request and receive 100 objects,
  407. ending with obj_foo, your subsequent call can include after=obj_foo in order to
  408. fetch the next page of the list.
  409. before: A cursor for use in pagination. `before` is an object ID that defines your place
  410. in the list. For instance, if you make a list request and receive 100 objects,
  411. starting with obj_foo, your subsequent call can include before=obj_foo in order
  412. to fetch the previous page of the list.
  413. filter: Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.
  414. limit: A limit on the number of objects to be returned. Limit can range between 1 and
  415. 100, and the default is 20.
  416. order: Sort order by the `created_at` timestamp of the objects. `asc` for ascending
  417. order and `desc` for descending order.
  418. extra_headers: Send extra headers
  419. extra_query: Add additional query parameters to the request
  420. extra_body: Add additional JSON properties to the request
  421. timeout: Override the client-level default timeout for this request, in seconds
  422. """
  423. if not vector_store_id:
  424. raise ValueError(f"Expected a non-empty value for `vector_store_id` but received {vector_store_id!r}")
  425. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  426. return self._get_api_list(
  427. f"/vector_stores/{vector_store_id}/files",
  428. page=AsyncCursorPage[VectorStoreFile],
  429. options=make_request_options(
  430. extra_headers=extra_headers,
  431. extra_query=extra_query,
  432. extra_body=extra_body,
  433. timeout=timeout,
  434. query=maybe_transform(
  435. {
  436. "after": after,
  437. "before": before,
  438. "filter": filter,
  439. "limit": limit,
  440. "order": order,
  441. },
  442. file_list_params.FileListParams,
  443. ),
  444. ),
  445. model=VectorStoreFile,
  446. )
  447. async def delete(
  448. self,
  449. file_id: str,
  450. *,
  451. vector_store_id: str,
  452. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  453. # The extra values given here take precedence over values defined on the client or passed to this method.
  454. extra_headers: Headers | None = None,
  455. extra_query: Query | None = None,
  456. extra_body: Body | None = None,
  457. timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
  458. ) -> VectorStoreFileDeleted:
  459. """Delete a vector store file.
  460. This will remove the file from the vector store but
  461. the file itself will not be deleted. To delete the file, use the
  462. [delete file](https://platform.openai.com/docs/api-reference/files/delete)
  463. endpoint.
  464. Args:
  465. extra_headers: Send extra headers
  466. extra_query: Add additional query parameters to the request
  467. extra_body: Add additional JSON properties to the request
  468. timeout: Override the client-level default timeout for this request, in seconds
  469. """
  470. if not vector_store_id:
  471. raise ValueError(f"Expected a non-empty value for `vector_store_id` but received {vector_store_id!r}")
  472. if not file_id:
  473. raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}")
  474. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  475. return await self._delete(
  476. f"/vector_stores/{vector_store_id}/files/{file_id}",
  477. options=make_request_options(
  478. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  479. ),
  480. cast_to=VectorStoreFileDeleted,
  481. )
  482. async def create_and_poll(
  483. self,
  484. file_id: str,
  485. *,
  486. vector_store_id: str,
  487. poll_interval_ms: int | NotGiven = NOT_GIVEN,
  488. chunking_strategy: FileChunkingStrategyParam | NotGiven = NOT_GIVEN,
  489. ) -> VectorStoreFile:
  490. """Attach a file to the given vector store and wait for it to be processed."""
  491. await self.create(vector_store_id=vector_store_id, file_id=file_id, chunking_strategy=chunking_strategy)
  492. return await self.poll(
  493. file_id,
  494. vector_store_id=vector_store_id,
  495. poll_interval_ms=poll_interval_ms,
  496. )
  497. async def poll(
  498. self,
  499. file_id: str,
  500. *,
  501. vector_store_id: str,
  502. poll_interval_ms: int | NotGiven = NOT_GIVEN,
  503. ) -> VectorStoreFile:
  504. """Wait for the vector store file to finish processing.
  505. Note: this will return even if the file failed to process, you need to check
  506. file.last_error and file.status to handle these cases
  507. """
  508. headers: dict[str, str] = {"X-Stainless-Poll-Helper": "true"}
  509. if is_given(poll_interval_ms):
  510. headers["X-Stainless-Custom-Poll-Interval"] = str(poll_interval_ms)
  511. while True:
  512. response = await self.with_raw_response.retrieve(
  513. file_id,
  514. vector_store_id=vector_store_id,
  515. extra_headers=headers,
  516. )
  517. file = response.parse()
  518. if file.status == "in_progress":
  519. if not is_given(poll_interval_ms):
  520. from_header = response.headers.get("openai-poll-after-ms")
  521. if from_header is not None:
  522. poll_interval_ms = int(from_header)
  523. else:
  524. poll_interval_ms = 1000
  525. await self._sleep(poll_interval_ms / 1000)
  526. elif file.status == "cancelled" or file.status == "completed" or file.status == "failed":
  527. return file
  528. else:
  529. if TYPE_CHECKING: # type: ignore[unreachable]
  530. assert_never(file.status)
  531. else:
  532. return file
  533. async def upload(
  534. self,
  535. *,
  536. vector_store_id: str,
  537. file: FileTypes,
  538. chunking_strategy: FileChunkingStrategyParam | NotGiven = NOT_GIVEN,
  539. ) -> VectorStoreFile:
  540. """Upload a file to the `files` API and then attach it to the given vector store.
  541. Note the file will be asynchronously processed (you can use the alternative
  542. polling helper method to wait for processing to complete).
  543. """
  544. file_obj = await self._client.files.create(file=file, purpose="assistants")
  545. return await self.create(
  546. vector_store_id=vector_store_id, file_id=file_obj.id, chunking_strategy=chunking_strategy
  547. )
  548. async def upload_and_poll(
  549. self,
  550. *,
  551. vector_store_id: str,
  552. file: FileTypes,
  553. poll_interval_ms: int | NotGiven = NOT_GIVEN,
  554. chunking_strategy: FileChunkingStrategyParam | NotGiven = NOT_GIVEN,
  555. ) -> VectorStoreFile:
  556. """Add a file to a vector store and poll until processing is complete."""
  557. file_obj = await self._client.files.create(file=file, purpose="assistants")
  558. return await self.create_and_poll(
  559. vector_store_id=vector_store_id,
  560. file_id=file_obj.id,
  561. poll_interval_ms=poll_interval_ms,
  562. chunking_strategy=chunking_strategy,
  563. )
  564. class FilesWithRawResponse:
  565. def __init__(self, files: Files) -> None:
  566. self._files = files
  567. self.create = _legacy_response.to_raw_response_wrapper(
  568. files.create,
  569. )
  570. self.retrieve = _legacy_response.to_raw_response_wrapper(
  571. files.retrieve,
  572. )
  573. self.list = _legacy_response.to_raw_response_wrapper(
  574. files.list,
  575. )
  576. self.delete = _legacy_response.to_raw_response_wrapper(
  577. files.delete,
  578. )
  579. class AsyncFilesWithRawResponse:
  580. def __init__(self, files: AsyncFiles) -> None:
  581. self._files = files
  582. self.create = _legacy_response.async_to_raw_response_wrapper(
  583. files.create,
  584. )
  585. self.retrieve = _legacy_response.async_to_raw_response_wrapper(
  586. files.retrieve,
  587. )
  588. self.list = _legacy_response.async_to_raw_response_wrapper(
  589. files.list,
  590. )
  591. self.delete = _legacy_response.async_to_raw_response_wrapper(
  592. files.delete,
  593. )
  594. class FilesWithStreamingResponse:
  595. def __init__(self, files: Files) -> None:
  596. self._files = files
  597. self.create = to_streamed_response_wrapper(
  598. files.create,
  599. )
  600. self.retrieve = to_streamed_response_wrapper(
  601. files.retrieve,
  602. )
  603. self.list = to_streamed_response_wrapper(
  604. files.list,
  605. )
  606. self.delete = to_streamed_response_wrapper(
  607. files.delete,
  608. )
  609. class AsyncFilesWithStreamingResponse:
  610. def __init__(self, files: AsyncFiles) -> None:
  611. self._files = files
  612. self.create = async_to_streamed_response_wrapper(
  613. files.create,
  614. )
  615. self.retrieve = async_to_streamed_response_wrapper(
  616. files.retrieve,
  617. )
  618. self.list = async_to_streamed_response_wrapper(
  619. files.list,
  620. )
  621. self.delete = async_to_streamed_response_wrapper(
  622. files.delete,
  623. )