collections.py 9.1 KB

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