collections_router.py 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. import logging
  2. import textwrap
  3. import time
  4. from typing import Optional
  5. from uuid import UUID
  6. from fastapi import Body, Depends, Path, Query
  7. from core.base import KGCreationSettings, KGRunType, R2RException
  8. from core.base.api.models import (
  9. GenericBooleanResponse,
  10. WrappedBooleanResponse,
  11. WrappedCollectionResponse,
  12. WrappedCollectionsResponse,
  13. WrappedDocumentsResponse,
  14. WrappedGenericMessageResponse,
  15. WrappedUsersResponse,
  16. )
  17. from core.utils import update_settings_from_dict
  18. from ...abstractions import R2RProviders, R2RServices
  19. from .base_router import BaseRouterV3
  20. logger = logging.getLogger()
  21. from enum import Enum
  22. from uuid import UUID
  23. from core.base import R2RException
  24. class CollectionAction(str, Enum):
  25. VIEW = "view"
  26. EDIT = "edit"
  27. DELETE = "delete"
  28. MANAGE_USERS = "manage_users"
  29. ADD_DOCUMENT = "add_document"
  30. REMOVE_DOCUMENT = "remove_document"
  31. async def authorize_collection_action(
  32. auth_user, collection_id: UUID, action: CollectionAction, services
  33. ) -> bool:
  34. """
  35. Authorize a user's action on a given collection based on:
  36. - If user is superuser (admin): Full access.
  37. - If user is owner of the collection: Full access.
  38. - If user is a member of the collection (in `collection_ids`): VIEW only.
  39. - Otherwise: No access.
  40. """
  41. # Superusers have complete access
  42. if auth_user.is_superuser:
  43. return True
  44. # Fetch collection details: owner_id and members
  45. results = (
  46. await services.management.collections_overview(
  47. 0, 1, collection_ids=[collection_id]
  48. )
  49. )["results"]
  50. if len(results) == 0:
  51. raise R2RException("The specified collection does not exist.", 404)
  52. details = results[0]
  53. owner_id = details.owner_id
  54. # Check if user is owner
  55. if auth_user.id == owner_id:
  56. # Owner can do all actions
  57. return True
  58. # Check if user is a member (non-owner)
  59. if collection_id in auth_user.collection_ids:
  60. # Members can only view
  61. if action == CollectionAction.VIEW:
  62. return True
  63. else:
  64. raise R2RException(
  65. "Insufficient permissions for this action.", 403
  66. )
  67. # User is neither owner nor member
  68. raise R2RException("You do not have access to this collection.", 403)
  69. class CollectionsRouter(BaseRouterV3):
  70. def __init__(self, providers: R2RProviders, services: R2RServices):
  71. super().__init__(providers, services)
  72. def _setup_routes(self):
  73. @self.router.post(
  74. "/collections",
  75. summary="Create a new collection",
  76. dependencies=[Depends(self.rate_limit_dependency)],
  77. openapi_extra={
  78. "x-codeSamples": [
  79. {
  80. "lang": "Python",
  81. "source": textwrap.dedent(
  82. """
  83. from r2r import R2RClient
  84. client = R2RClient()
  85. # when using auth, do client.login(...)
  86. result = client.collections.create(
  87. name="My New Collection",
  88. description="This is a sample collection"
  89. )
  90. """
  91. ),
  92. },
  93. {
  94. "lang": "JavaScript",
  95. "source": textwrap.dedent(
  96. """
  97. const { r2rClient } = require("r2r-js");
  98. const client = new r2rClient();
  99. function main() {
  100. const response = await client.collections.create({
  101. name: "My New Collection",
  102. description: "This is a sample collection"
  103. });
  104. }
  105. main();
  106. """
  107. ),
  108. },
  109. {
  110. "lang": "CLI",
  111. "source": textwrap.dedent(
  112. """
  113. r2r collections create "My New Collection" --description="This is a sample collection"
  114. """
  115. ),
  116. },
  117. {
  118. "lang": "cURL",
  119. "source": textwrap.dedent(
  120. """
  121. curl -X POST "https://api.example.com/v3/collections" \\
  122. -H "Content-Type: application/json" \\
  123. -H "Authorization: Bearer YOUR_API_KEY" \\
  124. -d '{"name": "My New Collection", "description": "This is a sample collection"}'
  125. """
  126. ),
  127. },
  128. ]
  129. },
  130. )
  131. @self.base_endpoint
  132. async def create_collection(
  133. name: str = Body(..., description="The name of the collection"),
  134. description: Optional[str] = Body(
  135. None, description="An optional description of the collection"
  136. ),
  137. auth_user=Depends(self.providers.auth.auth_wrapper()),
  138. ) -> WrappedCollectionResponse:
  139. """
  140. Create a new collection and automatically add the creating user to it.
  141. This endpoint allows authenticated users to create a new collection with a specified name
  142. and optional description. The user creating the collection is automatically added as a member.
  143. """
  144. collection = await self.services.management.create_collection(
  145. owner_id=auth_user.id,
  146. name=name,
  147. description=description,
  148. )
  149. # Add the creating user to the collection
  150. await self.services.management.add_user_to_collection(
  151. auth_user.id, collection.id
  152. )
  153. return collection
  154. @self.router.get(
  155. "/collections",
  156. summary="List collections",
  157. dependencies=[Depends(self.rate_limit_dependency)],
  158. openapi_extra={
  159. "x-codeSamples": [
  160. {
  161. "lang": "Python",
  162. "source": textwrap.dedent(
  163. """
  164. from r2r import R2RClient
  165. client = R2RClient()
  166. # when using auth, do client.login(...)
  167. result = client.collections.list(
  168. offset=0,
  169. limit=10,
  170. )
  171. """
  172. ),
  173. },
  174. {
  175. "lang": "JavaScript",
  176. "source": textwrap.dedent(
  177. """
  178. const { r2rClient } = require("r2r-js");
  179. const client = new r2rClient();
  180. function main() {
  181. const response = await client.collections.list();
  182. }
  183. main();
  184. """
  185. ),
  186. },
  187. {
  188. "lang": "CLI",
  189. "source": textwrap.dedent(
  190. """
  191. r2r collections list
  192. """
  193. ),
  194. },
  195. {
  196. "lang": "cURL",
  197. "source": textwrap.dedent(
  198. """
  199. curl -X GET "https://api.example.com/v3/collections?offset=0&limit=10&name=Sample" \\
  200. -H "Authorization: Bearer YOUR_API_KEY"
  201. """
  202. ),
  203. },
  204. ]
  205. },
  206. )
  207. @self.base_endpoint
  208. async def list_collections(
  209. ids: list[str] = Query(
  210. [],
  211. description="A list of collection IDs to retrieve. If not provided, all collections will be returned.",
  212. ),
  213. offset: int = Query(
  214. 0,
  215. ge=0,
  216. description="Specifies the number of objects to skip. Defaults to 0.",
  217. ),
  218. limit: int = Query(
  219. 100,
  220. ge=1,
  221. le=1000,
  222. description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
  223. ),
  224. auth_user=Depends(self.providers.auth.auth_wrapper()),
  225. ) -> WrappedCollectionsResponse:
  226. """
  227. Returns a paginated list of collections the authenticated user has access to.
  228. Results can be filtered by providing specific collection IDs. Regular users will only see
  229. collections they own or have access to. Superusers can see all collections.
  230. The collections are returned in order of last modification, with most recent first.
  231. """
  232. requesting_user_id = (
  233. None if auth_user.is_superuser else [auth_user.id]
  234. )
  235. collection_uuids = [UUID(collection_id) for collection_id in ids]
  236. collections_overview_response = (
  237. await self.services.management.collections_overview(
  238. user_ids=requesting_user_id,
  239. collection_ids=collection_uuids,
  240. offset=offset,
  241. limit=limit,
  242. )
  243. )
  244. return ( # type: ignore
  245. collections_overview_response["results"],
  246. {
  247. "total_entries": collections_overview_response[
  248. "total_entries"
  249. ]
  250. },
  251. )
  252. @self.router.get(
  253. "/collections/{id}",
  254. summary="Get collection details",
  255. dependencies=[Depends(self.rate_limit_dependency)],
  256. openapi_extra={
  257. "x-codeSamples": [
  258. {
  259. "lang": "Python",
  260. "source": textwrap.dedent(
  261. """
  262. from r2r import R2RClient
  263. client = R2RClient()
  264. # when using auth, do client.login(...)
  265. result = client.collections.retrieve("123e4567-e89b-12d3-a456-426614174000")
  266. """
  267. ),
  268. },
  269. {
  270. "lang": "JavaScript",
  271. "source": textwrap.dedent(
  272. """
  273. const { r2rClient } = require("r2r-js");
  274. const client = new r2rClient();
  275. function main() {
  276. const response = await client.collections.retrieve({id: "123e4567-e89b-12d3-a456-426614174000"});
  277. }
  278. main();
  279. """
  280. ),
  281. },
  282. {
  283. "lang": "CLI",
  284. "source": textwrap.dedent(
  285. """
  286. r2r collections retrieve 123e4567-e89b-12d3-a456-426614174000
  287. """
  288. ),
  289. },
  290. {
  291. "lang": "cURL",
  292. "source": textwrap.dedent(
  293. """
  294. curl -X GET "https://api.example.com/v3/collections/123e4567-e89b-12d3-a456-426614174000" \\
  295. -H "Authorization: Bearer YOUR_API_KEY"
  296. """
  297. ),
  298. },
  299. ]
  300. },
  301. )
  302. @self.base_endpoint
  303. async def get_collection(
  304. id: UUID = Path(
  305. ..., description="The unique identifier of the collection"
  306. ),
  307. auth_user=Depends(self.providers.auth.auth_wrapper()),
  308. ) -> WrappedCollectionResponse:
  309. """
  310. Get details of a specific collection.
  311. This endpoint retrieves detailed information about a single collection identified by its UUID.
  312. The user must have access to the collection to view its details.
  313. """
  314. await authorize_collection_action(
  315. auth_user, id, CollectionAction.VIEW, self.services
  316. )
  317. collections_overview_response = (
  318. await self.services.management.collections_overview(
  319. user_ids=None,
  320. collection_ids=[id],
  321. offset=0,
  322. limit=1,
  323. )
  324. )
  325. overview = collections_overview_response["results"]
  326. if len(overview) == 0:
  327. raise R2RException(
  328. "The specified collection does not exist.",
  329. 404,
  330. )
  331. return overview[0]
  332. @self.router.post(
  333. "/collections/{id}",
  334. summary="Update collection",
  335. dependencies=[Depends(self.rate_limit_dependency)],
  336. openapi_extra={
  337. "x-codeSamples": [
  338. {
  339. "lang": "Python",
  340. "source": textwrap.dedent(
  341. """
  342. from r2r import R2RClient
  343. client = R2RClient()
  344. # when using auth, do client.login(...)
  345. result = client.collections.update(
  346. "123e4567-e89b-12d3-a456-426614174000",
  347. name="Updated Collection Name",
  348. description="Updated description"
  349. )
  350. """
  351. ),
  352. },
  353. {
  354. "lang": "JavaScript",
  355. "source": textwrap.dedent(
  356. """
  357. const { r2rClient } = require("r2r-js");
  358. const client = new r2rClient();
  359. function main() {
  360. const response = await client.collections.update({
  361. id: "123e4567-e89b-12d3-a456-426614174000",
  362. name: "Updated Collection Name",
  363. description: "Updated description"
  364. });
  365. }
  366. main();
  367. """
  368. ),
  369. },
  370. {
  371. "lang": "cURL",
  372. "source": textwrap.dedent(
  373. """
  374. curl -X POST "https://api.example.com/v3/collections/123e4567-e89b-12d3-a456-426614174000" \\
  375. -H "Content-Type: application/json" \\
  376. -H "Authorization: Bearer YOUR_API_KEY" \\
  377. -d '{"name": "Updated Collection Name", "description": "Updated description"}'
  378. """
  379. ),
  380. },
  381. ]
  382. },
  383. )
  384. @self.base_endpoint
  385. async def update_collection(
  386. id: UUID = Path(
  387. ...,
  388. description="The unique identifier of the collection to update",
  389. ),
  390. name: Optional[str] = Body(
  391. None, description="The name of the collection"
  392. ),
  393. description: Optional[str] = Body(
  394. None, description="An optional description of the collection"
  395. ),
  396. generate_description: Optional[bool] = Body(
  397. False,
  398. description="Whether to generate a new synthetic description for the collection",
  399. ),
  400. auth_user=Depends(self.providers.auth.auth_wrapper()),
  401. ) -> WrappedCollectionResponse:
  402. """
  403. Update an existing collection's configuration.
  404. This endpoint allows updating the name and description of an existing collection.
  405. The user must have appropriate permissions to modify the collection.
  406. """
  407. await authorize_collection_action(
  408. auth_user, id, CollectionAction.EDIT, self.services
  409. )
  410. if generate_description and description is not None:
  411. raise R2RException(
  412. "Cannot provide both a description and request to synthetically generate a new one.",
  413. 400,
  414. )
  415. return await self.services.management.update_collection( # type: ignore
  416. id,
  417. name=name,
  418. description=description,
  419. generate_description=generate_description,
  420. )
  421. @self.router.delete(
  422. "/collections/{id}",
  423. summary="Delete collection",
  424. dependencies=[Depends(self.rate_limit_dependency)],
  425. openapi_extra={
  426. "x-codeSamples": [
  427. {
  428. "lang": "Python",
  429. "source": textwrap.dedent(
  430. """
  431. from r2r import R2RClient
  432. client = R2RClient()
  433. # when using auth, do client.login(...)
  434. result = client.collections.delete("123e4567-e89b-12d3-a456-426614174000")
  435. """
  436. ),
  437. },
  438. {
  439. "lang": "JavaScript",
  440. "source": textwrap.dedent(
  441. """
  442. const { r2rClient } = require("r2r-js");
  443. const client = new r2rClient();
  444. function main() {
  445. const response = await client.collections.delete({id: "123e4567-e89b-12d3-a456-426614174000"});
  446. }
  447. main();
  448. """
  449. ),
  450. },
  451. {
  452. "lang": "CLI",
  453. "source": textwrap.dedent(
  454. """
  455. r2r collections delete 123e4567-e89b-12d3-a456-426614174000
  456. """
  457. ),
  458. },
  459. {
  460. "lang": "cURL",
  461. "source": textwrap.dedent(
  462. """
  463. curl -X DELETE "https://api.example.com/v3/collections/123e4567-e89b-12d3-a456-426614174000" \\
  464. -H "Authorization: Bearer YOUR_API_KEY"
  465. """
  466. ),
  467. },
  468. ]
  469. },
  470. )
  471. @self.base_endpoint
  472. async def delete_collection(
  473. id: UUID = Path(
  474. ...,
  475. description="The unique identifier of the collection to delete",
  476. ),
  477. auth_user=Depends(self.providers.auth.auth_wrapper()),
  478. ) -> WrappedBooleanResponse:
  479. """
  480. Delete an existing collection.
  481. This endpoint allows deletion of a collection identified by its UUID.
  482. The user must have appropriate permissions to delete the collection.
  483. Deleting a collection removes all associations but does not delete the documents within it.
  484. """
  485. await authorize_collection_action(
  486. auth_user, id, CollectionAction.DELETE, self.services
  487. )
  488. await self.services.management.delete_collection(id)
  489. return GenericBooleanResponse(success=True) # type: ignore
  490. @self.router.post(
  491. "/collections/{id}/documents/{document_id}",
  492. summary="Add document to collection",
  493. dependencies=[Depends(self.rate_limit_dependency)],
  494. openapi_extra={
  495. "x-codeSamples": [
  496. {
  497. "lang": "Python",
  498. "source": textwrap.dedent(
  499. """
  500. from r2r import R2RClient
  501. client = R2RClient()
  502. # when using auth, do client.login(...)
  503. result = client.collections.add_document(
  504. "123e4567-e89b-12d3-a456-426614174000",
  505. "456e789a-b12c-34d5-e678-901234567890"
  506. )
  507. """
  508. ),
  509. },
  510. {
  511. "lang": "JavaScript",
  512. "source": textwrap.dedent(
  513. """
  514. const { r2rClient } = require("r2r-js");
  515. const client = new r2rClient();
  516. function main() {
  517. const response = await client.collections.addDocument({
  518. id: "123e4567-e89b-12d3-a456-426614174000"
  519. documentId: "456e789a-b12c-34d5-e678-901234567890"
  520. });
  521. }
  522. main();
  523. """
  524. ),
  525. },
  526. {
  527. "lang": "cURL",
  528. "source": textwrap.dedent(
  529. """
  530. curl -X POST "https://api.example.com/v3/collections/123e4567-e89b-12d3-a456-426614174000/documents/456e789a-b12c-34d5-e678-901234567890" \\
  531. -H "Authorization: Bearer YOUR_API_KEY"
  532. """
  533. ),
  534. },
  535. ]
  536. },
  537. )
  538. @self.base_endpoint
  539. async def add_document_to_collection(
  540. id: UUID = Path(...),
  541. document_id: UUID = Path(...),
  542. auth_user=Depends(self.providers.auth.auth_wrapper()),
  543. ) -> WrappedGenericMessageResponse:
  544. """
  545. Add a document to a collection.
  546. """
  547. await authorize_collection_action(
  548. auth_user, id, CollectionAction.ADD_DOCUMENT, self.services
  549. )
  550. return (
  551. await self.services.management.assign_document_to_collection(
  552. document_id, id
  553. )
  554. )
  555. @self.router.get(
  556. "/collections/{id}/documents",
  557. summary="List documents in collection",
  558. dependencies=[Depends(self.rate_limit_dependency)],
  559. openapi_extra={
  560. "x-codeSamples": [
  561. {
  562. "lang": "Python",
  563. "source": textwrap.dedent(
  564. """
  565. from r2r import R2RClient
  566. client = R2RClient()
  567. # when using auth, do client.login(...)
  568. result = client.collections.list_documents(
  569. "123e4567-e89b-12d3-a456-426614174000",
  570. offset=0,
  571. limit=10,
  572. )
  573. """
  574. ),
  575. },
  576. {
  577. "lang": "JavaScript",
  578. "source": textwrap.dedent(
  579. """
  580. const { r2rClient } = require("r2r-js");
  581. const client = new r2rClient();
  582. function main() {
  583. const response = await client.collections.listDocuments({id: "123e4567-e89b-12d3-a456-426614174000"});
  584. }
  585. main();
  586. """
  587. ),
  588. },
  589. {
  590. "lang": "CLI",
  591. "source": textwrap.dedent(
  592. """
  593. r2r collections list-documents 123e4567-e89b-12d3-a456-426614174000
  594. """
  595. ),
  596. },
  597. {
  598. "lang": "cURL",
  599. "source": textwrap.dedent(
  600. """
  601. curl -X GET "https://api.example.com/v3/collections/123e4567-e89b-12d3-a456-426614174000/documents?offset=0&limit=10" \\
  602. -H "Authorization: Bearer YOUR_API_KEY"
  603. """
  604. ),
  605. },
  606. ]
  607. },
  608. )
  609. @self.base_endpoint
  610. async def get_collection_documents(
  611. id: UUID = Path(
  612. ..., description="The unique identifier of the collection"
  613. ),
  614. offset: int = Query(
  615. 0,
  616. ge=0,
  617. description="Specifies the number of objects to skip. Defaults to 0.",
  618. ),
  619. limit: int = Query(
  620. 100,
  621. ge=1,
  622. le=1000,
  623. description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
  624. ),
  625. auth_user=Depends(self.providers.auth.auth_wrapper()),
  626. ) -> WrappedDocumentsResponse:
  627. """
  628. Get all documents in a collection with pagination and sorting options.
  629. This endpoint retrieves a paginated list of documents associated with a specific collection.
  630. It supports sorting options to customize the order of returned documents.
  631. """
  632. await authorize_collection_action(
  633. auth_user, id, CollectionAction.VIEW, self.services
  634. )
  635. documents_in_collection_response = (
  636. await self.services.management.documents_in_collection(
  637. id, offset, limit
  638. )
  639. )
  640. return documents_in_collection_response["results"], { # type: ignore
  641. "total_entries": documents_in_collection_response[
  642. "total_entries"
  643. ]
  644. }
  645. @self.router.delete(
  646. "/collections/{id}/documents/{document_id}",
  647. summary="Remove document from collection",
  648. dependencies=[Depends(self.rate_limit_dependency)],
  649. openapi_extra={
  650. "x-codeSamples": [
  651. {
  652. "lang": "Python",
  653. "source": textwrap.dedent(
  654. """
  655. from r2r import R2RClient
  656. client = R2RClient()
  657. # when using auth, do client.login(...)
  658. result = client.collections.remove_document(
  659. "123e4567-e89b-12d3-a456-426614174000",
  660. "456e789a-b12c-34d5-e678-901234567890"
  661. )
  662. """
  663. ),
  664. },
  665. {
  666. "lang": "JavaScript",
  667. "source": textwrap.dedent(
  668. """
  669. const { r2rClient } = require("r2r-js");
  670. const client = new r2rClient();
  671. function main() {
  672. const response = await client.collections.removeDocument({
  673. id: "123e4567-e89b-12d3-a456-426614174000"
  674. documentId: "456e789a-b12c-34d5-e678-901234567890"
  675. });
  676. }
  677. main();
  678. """
  679. ),
  680. },
  681. {
  682. "lang": "cURL",
  683. "source": textwrap.dedent(
  684. """
  685. curl -X DELETE "https://api.example.com/v3/collections/123e4567-e89b-12d3-a456-426614174000/documents/456e789a-b12c-34d5-e678-901234567890" \\
  686. -H "Authorization: Bearer YOUR_API_KEY"
  687. """
  688. ),
  689. },
  690. ]
  691. },
  692. )
  693. @self.base_endpoint
  694. async def remove_document_from_collection(
  695. id: UUID = Path(
  696. ..., description="The unique identifier of the collection"
  697. ),
  698. document_id: UUID = Path(
  699. ...,
  700. description="The unique identifier of the document to remove",
  701. ),
  702. auth_user=Depends(self.providers.auth.auth_wrapper()),
  703. ) -> WrappedBooleanResponse:
  704. """
  705. Remove a document from a collection.
  706. This endpoint removes the association between a document and a collection.
  707. It does not delete the document itself. The user must have permissions to modify the collection.
  708. """
  709. await authorize_collection_action(
  710. auth_user, id, CollectionAction.REMOVE_DOCUMENT, self.services
  711. )
  712. await self.services.management.remove_document_from_collection(
  713. document_id, id
  714. )
  715. return GenericBooleanResponse(success=True) # type: ignore
  716. @self.router.get(
  717. "/collections/{id}/users",
  718. summary="List users in collection",
  719. dependencies=[Depends(self.rate_limit_dependency)],
  720. openapi_extra={
  721. "x-codeSamples": [
  722. {
  723. "lang": "Python",
  724. "source": textwrap.dedent(
  725. """
  726. from r2r import R2RClient
  727. client = R2RClient()
  728. # when using auth, do client.login(...)
  729. result = client.collections.list_users(
  730. "123e4567-e89b-12d3-a456-426614174000",
  731. offset=0,
  732. limit=10,
  733. )
  734. """
  735. ),
  736. },
  737. {
  738. "lang": "JavaScript",
  739. "source": textwrap.dedent(
  740. """
  741. const { r2rClient } = require("r2r-js");
  742. const client = new r2rClient();
  743. function main() {
  744. const response = await client.collections.listUsers({
  745. id: "123e4567-e89b-12d3-a456-426614174000"
  746. });
  747. }
  748. main();
  749. """
  750. ),
  751. },
  752. {
  753. "lang": "CLI",
  754. "source": textwrap.dedent(
  755. """
  756. r2r collections list-users 123e4567-e89b-12d3-a456-426614174000
  757. """
  758. ),
  759. },
  760. {
  761. "lang": "cURL",
  762. "source": textwrap.dedent(
  763. """
  764. curl -X GET "https://api.example.com/v3/collections/123e4567-e89b-12d3-a456-426614174000/users?offset=0&limit=10" \\
  765. -H "Authorization: Bearer YOUR_API_KEY"
  766. """
  767. ),
  768. },
  769. ]
  770. },
  771. )
  772. @self.base_endpoint
  773. async def get_collection_users(
  774. id: UUID = Path(
  775. ..., description="The unique identifier of the collection"
  776. ),
  777. offset: int = Query(
  778. 0,
  779. ge=0,
  780. description="Specifies the number of objects to skip. Defaults to 0.",
  781. ),
  782. limit: int = Query(
  783. 100,
  784. ge=1,
  785. le=1000,
  786. description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
  787. ),
  788. auth_user=Depends(self.providers.auth.auth_wrapper()),
  789. ) -> WrappedUsersResponse:
  790. """
  791. Get all users in a collection with pagination and sorting options.
  792. This endpoint retrieves a paginated list of users who have access to a specific collection.
  793. It supports sorting options to customize the order of returned users.
  794. """
  795. await authorize_collection_action(
  796. auth_user, id, CollectionAction.VIEW, self.services
  797. )
  798. users_in_collection_response = (
  799. await self.services.management.get_users_in_collection(
  800. collection_id=id,
  801. offset=offset,
  802. limit=min(max(limit, 1), 1000),
  803. )
  804. )
  805. return users_in_collection_response["results"], { # type: ignore
  806. "total_entries": users_in_collection_response["total_entries"]
  807. }
  808. @self.router.post(
  809. "/collections/{id}/users/{user_id}",
  810. summary="Add user to collection",
  811. dependencies=[Depends(self.rate_limit_dependency)],
  812. openapi_extra={
  813. "x-codeSamples": [
  814. {
  815. "lang": "Python",
  816. "source": textwrap.dedent(
  817. """
  818. from r2r import R2RClient
  819. client = R2RClient()
  820. # when using auth, do client.login(...)
  821. result = client.collections.add_user(
  822. "123e4567-e89b-12d3-a456-426614174000",
  823. "789a012b-c34d-5e6f-g789-012345678901"
  824. )
  825. """
  826. ),
  827. },
  828. {
  829. "lang": "JavaScript",
  830. "source": textwrap.dedent(
  831. """
  832. const { r2rClient } = require("r2r-js");
  833. const client = new r2rClient();
  834. function main() {
  835. const response = await client.collections.addUser({
  836. id: "123e4567-e89b-12d3-a456-426614174000"
  837. userId: "789a012b-c34d-5e6f-g789-012345678901"
  838. });
  839. }
  840. main();
  841. """
  842. ),
  843. },
  844. {
  845. "lang": "cURL",
  846. "source": textwrap.dedent(
  847. """
  848. curl -X POST "https://api.example.com/v3/collections/123e4567-e89b-12d3-a456-426614174000/users/789a012b-c34d-5e6f-g789-012345678901" \\
  849. -H "Authorization: Bearer YOUR_API_KEY"
  850. """
  851. ),
  852. },
  853. ]
  854. },
  855. )
  856. @self.base_endpoint
  857. async def add_user_to_collection(
  858. id: UUID = Path(
  859. ..., description="The unique identifier of the collection"
  860. ),
  861. user_id: UUID = Path(
  862. ..., description="The unique identifier of the user to add"
  863. ),
  864. auth_user=Depends(self.providers.auth.auth_wrapper()),
  865. ) -> WrappedBooleanResponse:
  866. """
  867. Add a user to a collection.
  868. This endpoint grants a user access to a specific collection.
  869. The authenticated user must have admin permissions for the collection to add new users.
  870. """
  871. await authorize_collection_action(
  872. auth_user, id, CollectionAction.MANAGE_USERS, self.services
  873. )
  874. result = await self.services.management.add_user_to_collection(
  875. user_id, id
  876. )
  877. return GenericBooleanResponse(success=result) # type: ignore
  878. @self.router.delete(
  879. "/collections/{id}/users/{user_id}",
  880. summary="Remove user from collection",
  881. dependencies=[Depends(self.rate_limit_dependency)],
  882. openapi_extra={
  883. "x-codeSamples": [
  884. {
  885. "lang": "Python",
  886. "source": textwrap.dedent(
  887. """
  888. from r2r import R2RClient
  889. client = R2RClient()
  890. # when using auth, do client.login(...)
  891. result = client.collections.remove_user(
  892. "123e4567-e89b-12d3-a456-426614174000",
  893. "789a012b-c34d-5e6f-g789-012345678901"
  894. )
  895. """
  896. ),
  897. },
  898. {
  899. "lang": "JavaScript",
  900. "source": textwrap.dedent(
  901. """
  902. const { r2rClient } = require("r2r-js");
  903. const client = new r2rClient();
  904. function main() {
  905. const response = await client.collections.removeUser({
  906. id: "123e4567-e89b-12d3-a456-426614174000"
  907. userId: "789a012b-c34d-5e6f-g789-012345678901"
  908. });
  909. }
  910. main();
  911. """
  912. ),
  913. },
  914. {
  915. "lang": "cURL",
  916. "source": textwrap.dedent(
  917. """
  918. curl -X DELETE "https://api.example.com/v3/collections/123e4567-e89b-12d3-a456-426614174000/users/789a012b-c34d-5e6f-g789-012345678901" \\
  919. -H "Authorization: Bearer YOUR_API_KEY"
  920. """
  921. ),
  922. },
  923. ]
  924. },
  925. )
  926. @self.base_endpoint
  927. async def remove_user_from_collection(
  928. id: UUID = Path(
  929. ..., description="The unique identifier of the collection"
  930. ),
  931. user_id: UUID = Path(
  932. ..., description="The unique identifier of the user to remove"
  933. ),
  934. auth_user=Depends(self.providers.auth.auth_wrapper()),
  935. ) -> WrappedBooleanResponse:
  936. """
  937. Remove a user from a collection.
  938. This endpoint revokes a user's access to a specific collection.
  939. The authenticated user must have admin permissions for the collection to remove users.
  940. """
  941. await authorize_collection_action(
  942. auth_user, id, CollectionAction.MANAGE_USERS, self.services
  943. )
  944. result = (
  945. await self.services.management.remove_user_from_collection(
  946. user_id, id
  947. )
  948. )
  949. return GenericBooleanResponse(success=True) # type: ignore
  950. @self.router.post(
  951. "/collections/{id}/extract",
  952. summary="Extract entities and relationships",
  953. dependencies=[Depends(self.rate_limit_dependency)],
  954. openapi_extra={
  955. "x-codeSamples": [
  956. {
  957. "lang": "Python",
  958. "source": textwrap.dedent(
  959. """
  960. from r2r import R2RClient
  961. client = R2RClient()
  962. # when using auth, do client.login(...)
  963. result = client.documents.extract(
  964. id="9fbe403b-c11c-5aae-8ade-ef22980c3ad1"
  965. )
  966. """
  967. ),
  968. },
  969. ],
  970. },
  971. )
  972. @self.base_endpoint
  973. async def extract(
  974. id: UUID = Path(
  975. ...,
  976. description="The ID of the document to extract entities and relationships from.",
  977. ),
  978. run_type: KGRunType = Query(
  979. default=KGRunType.RUN,
  980. description="Whether to return an estimate of the creation cost or to actually extract the document.",
  981. ),
  982. settings: Optional[KGCreationSettings] = Body(
  983. default=None,
  984. description="Settings for the entities and relationships extraction process.",
  985. ),
  986. run_with_orchestration: Optional[bool] = Query(
  987. default=True,
  988. description="Whether to run the entities and relationships extraction process with orchestration.",
  989. ),
  990. auth_user=Depends(self.providers.auth.auth_wrapper()),
  991. ):
  992. """
  993. Extracts entities and relationships from a document.
  994. The entities and relationships extraction process involves:
  995. 1. Parsing documents into semantic chunks
  996. 2. Extracting entities and relationships using LLMs
  997. """
  998. await authorize_collection_action(
  999. auth_user, id, CollectionAction.EDIT, self.services
  1000. )
  1001. settings = settings.dict() if settings else None # type: ignore
  1002. if not auth_user.is_superuser:
  1003. logger.warning("Implement permission checks here.")
  1004. # If no run type is provided, default to estimate
  1005. if not run_type:
  1006. run_type = KGRunType.ESTIMATE
  1007. # Apply runtime settings overrides
  1008. server_graph_creation_settings = (
  1009. self.providers.database.config.graph_creation_settings
  1010. )
  1011. if settings:
  1012. server_graph_creation_settings = update_settings_from_dict(
  1013. server_settings=server_graph_creation_settings,
  1014. settings_dict=settings, # type: ignore
  1015. )
  1016. if run_with_orchestration:
  1017. workflow_input = {
  1018. "collection_id": str(id),
  1019. "graph_creation_settings": server_graph_creation_settings.model_dump_json(),
  1020. "user": auth_user.json(),
  1021. }
  1022. return await self.providers.orchestration.run_workflow( # type: ignore
  1023. "extract-triples", {"request": workflow_input}, {}
  1024. )
  1025. else:
  1026. from core.main.orchestration import simple_kg_factory
  1027. logger.info("Running extract-triples without orchestration.")
  1028. simple_kg = simple_kg_factory(self.services.graph)
  1029. await simple_kg["extract-triples"](workflow_input) # type: ignore
  1030. return { # type: ignore
  1031. "message": "Graph created successfully.",
  1032. "task_id": None,
  1033. }