collections.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. from typing import Any, Optional
  2. from uuid import UUID
  3. from shared.api.models import (
  4. WrappedBooleanResponse,
  5. WrappedCollectionResponse,
  6. WrappedCollectionsResponse,
  7. WrappedDocumentsResponse,
  8. WrappedGenericMessageResponse,
  9. WrappedUsersResponse,
  10. )
  11. class CollectionsSDK:
  12. def __init__(self, client):
  13. self.client = client
  14. async def create(
  15. self,
  16. name: str,
  17. description: Optional[str] = None,
  18. ) -> WrappedCollectionResponse:
  19. """Create a new collection.
  20. Args:
  21. name (str): Name of the collection
  22. description (Optional[str]): Description of the collection
  23. Returns:
  24. WrappedCollectionResponse
  25. """
  26. data: dict[str, Any] = {"name": name, "description": description}
  27. response_dict = await self.client._make_request(
  28. "POST",
  29. "collections",
  30. json=data,
  31. version="v3",
  32. )
  33. return WrappedCollectionResponse(**response_dict)
  34. async def list(
  35. self,
  36. ids: Optional[list[str | UUID]] = None,
  37. offset: Optional[int] = 0,
  38. limit: Optional[int] = 100,
  39. owner_only: Optional[bool] = False,
  40. ) -> WrappedCollectionsResponse:
  41. """List collections with pagination and filtering options.
  42. Args:
  43. ids (Optional[list[str | UUID]]): Filter collections by ids
  44. offset (int, optional): Specifies the number of objects to skip. Defaults to 0.
  45. limit (int, optional): Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.
  46. owner_only (Optional[bool]): If true, only returns collections owned by the user, not all accessible collections.
  47. Returns:
  48. WrappedCollectionsResponse
  49. """
  50. params: dict = {
  51. "offset": offset,
  52. "limit": limit,
  53. "owner_only": owner_only,
  54. }
  55. if ids:
  56. params["ids"] = ids
  57. response_dict = await self.client._make_request(
  58. "GET", "collections", params=params, version="v3"
  59. )
  60. return WrappedCollectionsResponse(**response_dict)
  61. async def retrieve(
  62. self,
  63. id: str | UUID,
  64. ) -> WrappedCollectionResponse:
  65. """Get detailed information about a specific collection.
  66. Args:
  67. id (str | UUID): Collection ID to retrieve
  68. Returns:
  69. WrappedCollectionResponse
  70. """
  71. response_dict = await self.client._make_request(
  72. "GET", f"collections/{str(id)}", version="v3"
  73. )
  74. return WrappedCollectionResponse(**response_dict)
  75. async def update(
  76. self,
  77. id: str | UUID,
  78. name: Optional[str] = None,
  79. description: Optional[str] = None,
  80. generate_description: Optional[bool] = False,
  81. ) -> WrappedCollectionResponse:
  82. """Update collection information.
  83. Args:
  84. id (str | UUID): Collection ID to update
  85. name (Optional[str]): Optional new name for the collection
  86. description (Optional[str]): Optional new description for the collection
  87. generate_description (Optional[bool]): Whether to generate a new synthetic description for the collection.
  88. Returns:
  89. WrappedCollectionResponse
  90. """
  91. data: dict[str, Any] = {}
  92. if name is not None:
  93. data["name"] = name
  94. if description is not None:
  95. data["description"] = description
  96. if generate_description:
  97. data["generate_description"] = str(generate_description)
  98. response_dict = await self.client._make_request(
  99. "POST",
  100. f"collections/{str(id)}",
  101. json=data,
  102. version="v3",
  103. )
  104. return WrappedCollectionResponse(**response_dict)
  105. async def delete(
  106. self,
  107. id: str | UUID,
  108. ) -> WrappedBooleanResponse:
  109. """Delete a collection.
  110. Args:
  111. id (str | UUID): Collection ID to delete
  112. Returns:
  113. WrappedBooleanResponse
  114. """
  115. response_dict = await self.client._make_request(
  116. "DELETE", f"collections/{str(id)}", version="v3"
  117. )
  118. return WrappedBooleanResponse(**response_dict)
  119. async def list_documents(
  120. self,
  121. id: str | UUID,
  122. offset: Optional[int] = 0,
  123. limit: Optional[int] = 100,
  124. ) -> WrappedDocumentsResponse:
  125. """List all documents in a collection.
  126. Args:
  127. id (str | UUID): Collection ID
  128. offset (int, optional): Specifies the number of objects to skip. Defaults to 0.
  129. limit (int, optional): Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.
  130. Returns:
  131. WrappedDocumentsResponse
  132. """
  133. params: dict = {
  134. "offset": offset,
  135. "limit": limit,
  136. }
  137. response_dict = await self.client._make_request(
  138. "GET",
  139. f"collections/{str(id)}/documents",
  140. params=params,
  141. version="v3",
  142. )
  143. return WrappedDocumentsResponse(**response_dict)
  144. async def add_document(
  145. self,
  146. id: str | UUID,
  147. document_id: str | UUID,
  148. ) -> WrappedGenericMessageResponse:
  149. """Add a document to a collection.
  150. Args:
  151. id (str | UUID): Collection ID
  152. document_id (str | UUID): Document ID to add
  153. Returns:
  154. WrappedGenericMessageResponse
  155. """
  156. response_dict = await self.client._make_request(
  157. "POST",
  158. f"collections/{str(id)}/documents/{str(document_id)}",
  159. version="v3",
  160. )
  161. return WrappedGenericMessageResponse(**response_dict)
  162. async def remove_document(
  163. self,
  164. id: str | UUID,
  165. document_id: str | UUID,
  166. ) -> WrappedBooleanResponse:
  167. """Remove a document from a collection.
  168. Args:
  169. id (str | UUID): Collection ID
  170. document_id (str | UUID): Document ID to remove
  171. Returns:
  172. WrappedBooleanResponse
  173. """
  174. response_dict = await self.client._make_request(
  175. "DELETE",
  176. f"collections/{str(id)}/documents/{str(document_id)}",
  177. version="v3",
  178. )
  179. return WrappedBooleanResponse(**response_dict)
  180. async def list_users(
  181. self,
  182. id: str | UUID,
  183. offset: Optional[int] = 0,
  184. limit: Optional[int] = 100,
  185. ) -> WrappedUsersResponse:
  186. """List all users in a collection.
  187. Args:
  188. id (str, UUID): Collection ID
  189. offset (int, optional): Specifies the number of objects to skip. Defaults to 0.
  190. limit (int, optional): Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.
  191. Returns:
  192. WrappedUsersResponse
  193. """
  194. params: dict = {
  195. "offset": offset,
  196. "limit": limit,
  197. }
  198. response_dict = await self.client._make_request(
  199. "GET", f"collections/{str(id)}/users", params=params, version="v3"
  200. )
  201. return WrappedUsersResponse(**response_dict)
  202. async def add_user(
  203. self,
  204. id: str | UUID,
  205. user_id: str | UUID,
  206. ) -> WrappedBooleanResponse:
  207. """Add a user to a collection.
  208. Args:
  209. id (str | UUID): Collection ID
  210. user_id (str | UUID): User ID to add
  211. Returns:
  212. WrappedBooleanResponse
  213. """
  214. response_dict = await self.client._make_request(
  215. "POST", f"collections/{str(id)}/users/{str(user_id)}", version="v3"
  216. )
  217. return WrappedBooleanResponse(**response_dict)
  218. async def remove_user(
  219. self,
  220. id: str | UUID,
  221. user_id: str | UUID,
  222. ) -> WrappedBooleanResponse:
  223. """Remove a user from a collection.
  224. Args:
  225. id (str | UUID): Collection ID
  226. user_id (str | UUID): User ID to remove
  227. Returns:
  228. WrappedBooleanResponse
  229. """
  230. response_dict = await self.client._make_request(
  231. "DELETE",
  232. f"collections/{str(id)}/users/{str(user_id)}",
  233. version="v3",
  234. )
  235. return WrappedBooleanResponse(**response_dict)
  236. async def extract(
  237. self,
  238. id: str | UUID,
  239. settings: Optional[dict] = None,
  240. run_with_orchestration: Optional[bool] = True,
  241. ) -> WrappedGenericMessageResponse:
  242. """Extract entities and relationships from documents in a collection.
  243. Args:
  244. id (str | UUID): Collection ID to extract from
  245. settings (Optional[dict]): Settings for the entities and relationships extraction process
  246. run_with_orchestration (Optional[bool]): Whether to run the extraction process with orchestration.
  247. Defaults to True
  248. Returns:
  249. WrappedGenericMessageResponse
  250. """
  251. params = {"run_with_orchestration": run_with_orchestration}
  252. data: dict[str, Any] = {}
  253. if settings is not None:
  254. data["settings"] = settings
  255. response_dict = await self.client._make_request(
  256. "POST",
  257. f"collections/{str(id)}/extract",
  258. params=params,
  259. json=data or None,
  260. version="v3",
  261. )
  262. return WrappedGenericMessageResponse(**response_dict)
  263. async def retrieve_by_name(
  264. self, name: str, owner_id: Optional[str] = None
  265. ) -> WrappedCollectionResponse:
  266. """Retrieve a collection by its name.
  267. For non-superusers, the backend will use the authenticated user's ID.
  268. For superusers, the caller must supply an owner_id to restrict the search.
  269. Args:
  270. name (str): The name of the collection to retrieve.
  271. owner_id (Optional[str]): The owner ID to restrict the search. Required for superusers.
  272. Returns:
  273. WrappedCollectionResponse
  274. """
  275. query_params: dict[str, Any] = {}
  276. if owner_id is not None:
  277. query_params["owner_id"] = owner_id
  278. response_dict = await self.client._make_request(
  279. "GET",
  280. f"collections/name/{name}",
  281. params=query_params,
  282. version="v3",
  283. )
  284. return WrappedCollectionResponse(**response_dict)