graph_router.py 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  1. import logging
  2. import textwrap
  3. from typing import Optional
  4. from uuid import UUID
  5. from fastapi import Body, Depends, Path, Query
  6. from fastapi.background import BackgroundTasks
  7. from fastapi.responses import FileResponse
  8. from core.base import KGEnrichmentStatus, R2RException, Workflow
  9. from core.base.abstractions import KGRunType, StoreType
  10. from core.base.api.models import (
  11. GenericBooleanResponse,
  12. WrappedBooleanResponse,
  13. WrappedCommunitiesResponse,
  14. WrappedCommunityResponse,
  15. WrappedEntitiesResponse,
  16. WrappedEntityResponse,
  17. WrappedGraphResponse,
  18. WrappedGraphsResponse,
  19. WrappedRelationshipResponse,
  20. WrappedRelationshipsResponse,
  21. )
  22. from core.utils import (
  23. generate_default_user_collection_id,
  24. update_settings_from_dict,
  25. )
  26. from ...abstractions import R2RProviders, R2RServices
  27. from .base_router import BaseRouterV3
  28. logger = logging.getLogger()
  29. class GraphRouter(BaseRouterV3):
  30. def __init__(
  31. self,
  32. providers: R2RProviders,
  33. services: R2RServices,
  34. ):
  35. super().__init__(providers, services)
  36. self._register_workflows()
  37. def _register_workflows(self):
  38. workflow_messages = {}
  39. if self.providers.orchestration.config.provider == "hatchet":
  40. workflow_messages["extract-triples"] = (
  41. "Document extraction task queued successfully."
  42. )
  43. workflow_messages["build-communities"] = (
  44. "Graph enrichment task queued successfully."
  45. )
  46. else:
  47. workflow_messages["extract-triples"] = (
  48. "Document entities and relationships extracted successfully."
  49. )
  50. workflow_messages["build-communities"] = (
  51. "Graph communities created successfully."
  52. )
  53. self.providers.orchestration.register_workflows(
  54. Workflow.KG,
  55. self.services.graph,
  56. workflow_messages,
  57. )
  58. async def _get_collection_id(
  59. self, collection_id: Optional[UUID], auth_user
  60. ) -> UUID:
  61. """Helper method to get collection ID, using default if none provided"""
  62. if collection_id is None:
  63. return generate_default_user_collection_id(auth_user.id)
  64. return collection_id
  65. def _setup_routes(self):
  66. @self.router.get(
  67. "/graphs",
  68. dependencies=[Depends(self.rate_limit_dependency)],
  69. summary="List graphs",
  70. openapi_extra={
  71. "x-codeSamples": [
  72. { # TODO: Verify
  73. "lang": "Python",
  74. "source": textwrap.dedent(
  75. """
  76. from r2r import R2RClient
  77. client = R2RClient()
  78. # when using auth, do client.login(...)
  79. response = client.graphs.list()
  80. """
  81. ),
  82. },
  83. {
  84. "lang": "JavaScript",
  85. "source": textwrap.dedent(
  86. """
  87. const { r2rClient } = require("r2r-js");
  88. const client = new r2rClient();
  89. function main() {
  90. const response = await client.graphs.list({});
  91. }
  92. main();
  93. """
  94. ),
  95. },
  96. ]
  97. },
  98. )
  99. @self.base_endpoint
  100. async def list_graphs(
  101. collection_ids: list[str] = Query(
  102. [],
  103. description="A list of graph IDs to retrieve. If not provided, all graphs will be returned.",
  104. ),
  105. offset: int = Query(
  106. 0,
  107. ge=0,
  108. description="Specifies the number of objects to skip. Defaults to 0.",
  109. ),
  110. limit: int = Query(
  111. 100,
  112. ge=1,
  113. le=1000,
  114. description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
  115. ),
  116. auth_user=Depends(self.providers.auth.auth_wrapper()),
  117. ) -> WrappedGraphsResponse:
  118. """
  119. Returns a paginated list of graphs the authenticated user has access to.
  120. Results can be filtered by providing specific graph IDs. Regular users will only see
  121. graphs they own or have access to. Superusers can see all graphs.
  122. The graphs are returned in order of last modification, with most recent first.
  123. """
  124. requesting_user_id = (
  125. None if auth_user.is_superuser else [auth_user.id]
  126. )
  127. graph_uuids = [UUID(graph_id) for graph_id in collection_ids]
  128. list_graphs_response = await self.services.graph.list_graphs(
  129. # user_ids=requesting_user_id,
  130. graph_ids=graph_uuids,
  131. offset=offset,
  132. limit=limit,
  133. )
  134. return ( # type: ignore
  135. list_graphs_response["results"],
  136. {"total_entries": list_graphs_response["total_entries"]},
  137. )
  138. @self.router.get(
  139. "/graphs/{collection_id}",
  140. dependencies=[Depends(self.rate_limit_dependency)],
  141. summary="Retrieve graph details",
  142. openapi_extra={
  143. "x-codeSamples": [
  144. {
  145. "lang": "Python",
  146. "source": textwrap.dedent(
  147. """
  148. from r2r import R2RClient
  149. client = R2RClient()
  150. # when using auth, do client.login(...)
  151. response = client.graphs.get(
  152. collection_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  153. )"""
  154. ),
  155. },
  156. {
  157. "lang": "JavaScript",
  158. "source": textwrap.dedent(
  159. """
  160. const { r2rClient } = require("r2r-js");
  161. const client = new r2rClient();
  162. function main() {
  163. const response = await client.graphs.retrieve({
  164. collectionId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  165. });
  166. }
  167. main();
  168. """
  169. ),
  170. },
  171. {
  172. "lang": "cURL",
  173. "source": textwrap.dedent(
  174. """
  175. curl -X GET "https://api.example.com/v3/graphs/d09dedb1-b2ab-48a5-b950-6e1f464d83e7" \\
  176. -H "Authorization: Bearer YOUR_API_KEY" """
  177. ),
  178. },
  179. ]
  180. },
  181. )
  182. @self.base_endpoint
  183. async def get_graph(
  184. collection_id: UUID = Path(...),
  185. auth_user=Depends(self.providers.auth.auth_wrapper()),
  186. ) -> WrappedGraphResponse:
  187. """
  188. Retrieves detailed information about a specific graph by ID.
  189. """
  190. if (
  191. # not auth_user.is_superuser
  192. collection_id
  193. not in auth_user.collection_ids
  194. ):
  195. raise R2RException(
  196. "The currently authenticated user does not have access to the specified collection associated with the given graph.",
  197. 403,
  198. )
  199. list_graphs_response = await self.services.graph.list_graphs(
  200. # user_ids=None,
  201. graph_ids=[collection_id],
  202. offset=0,
  203. limit=1,
  204. )
  205. return list_graphs_response["results"][0] # type: ignore
  206. @self.router.post(
  207. "/graphs/{collection_id}/communities/build",
  208. dependencies=[Depends(self.rate_limit_dependency)],
  209. )
  210. @self.base_endpoint
  211. async def build_communities(
  212. collection_id: UUID = Path(
  213. ..., description="The unique identifier of the collection"
  214. ),
  215. run_type: Optional[KGRunType] = Body(
  216. default=KGRunType.ESTIMATE,
  217. description="Run type for the graph enrichment process.",
  218. ),
  219. graph_enrichment_settings: Optional[dict] = Body(
  220. default=None,
  221. description="Settings for the graph enrichment process.",
  222. ),
  223. run_with_orchestration: Optional[bool] = Body(True),
  224. auth_user=Depends(self.providers.auth.auth_wrapper()),
  225. ):
  226. """
  227. Creates communities in the graph by analyzing entity relationships and similarities.
  228. Communities are created through the following process:
  229. 1. Analyzes entity relationships and metadata to build a similarity graph
  230. 2. Applies advanced community detection algorithms (e.g. Leiden) to identify densely connected groups
  231. 3. Creates hierarchical community structure with multiple granularity levels
  232. 4. Generates natural language summaries and statistical insights for each community
  233. The resulting communities can be used to:
  234. - Understand high-level graph structure and organization
  235. - Identify key entity groupings and their relationships
  236. - Navigate and explore the graph at different levels of detail
  237. - Generate insights about entity clusters and their characteristics
  238. The community detection process is configurable through settings like:
  239. - Community detection algorithm parameters
  240. - Summary generation prompt
  241. """
  242. if not auth_user.is_superuser:
  243. raise R2RException(
  244. "Only superusers can build communities", 403
  245. )
  246. if (
  247. # not auth_user.is_superuser
  248. collection_id
  249. not in auth_user.collection_ids
  250. ):
  251. raise R2RException(
  252. "The currently authenticated user does not have access to the collection associated with the given graph.",
  253. 403,
  254. )
  255. # If no collection ID is provided, use the default user collection
  256. # id = generate_default_user_collection_id(auth_user.id)
  257. # If no run type is provided, default to estimate
  258. if not run_type:
  259. run_type = KGRunType.ESTIMATE
  260. # Apply runtime settings overrides
  261. server_graph_enrichment_settings = (
  262. self.providers.database.config.graph_enrichment_settings
  263. )
  264. if graph_enrichment_settings:
  265. server_graph_enrichment_settings = update_settings_from_dict(
  266. server_graph_enrichment_settings, graph_enrichment_settings
  267. )
  268. workflow_input = {
  269. "collection_id": str(collection_id),
  270. "graph_enrichment_settings": server_graph_enrichment_settings.model_dump_json(),
  271. "user": auth_user.json(),
  272. }
  273. if run_with_orchestration:
  274. return await self.providers.orchestration.run_workflow( # type: ignore
  275. "build-communities", {"request": workflow_input}, {}
  276. )
  277. else:
  278. from core.main.orchestration import simple_kg_factory
  279. logger.info("Running build-communities without orchestration.")
  280. simple_kg = simple_kg_factory(self.services.graph)
  281. await simple_kg["build-communities"](workflow_input)
  282. return {
  283. "message": "Graph communities created successfully.",
  284. "task_id": None,
  285. }
  286. @self.router.post(
  287. "/graphs/{collection_id}/reset",
  288. dependencies=[Depends(self.rate_limit_dependency)],
  289. summary="Reset a graph back to the initial state.",
  290. openapi_extra={
  291. "x-codeSamples": [
  292. {
  293. "lang": "Python",
  294. "source": textwrap.dedent(
  295. """
  296. from r2r import R2RClient
  297. client = R2RClient()
  298. # when using auth, do client.login(...)
  299. response = client.graphs.reset(
  300. collection_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  301. )"""
  302. ),
  303. },
  304. {
  305. "lang": "JavaScript",
  306. "source": textwrap.dedent(
  307. """
  308. const { r2rClient } = require("r2r-js");
  309. const client = new r2rClient();
  310. function main() {
  311. const response = await client.graphs.reset({
  312. collectionId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  313. });
  314. }
  315. main();
  316. """
  317. ),
  318. },
  319. {
  320. "lang": "cURL",
  321. "source": textwrap.dedent(
  322. """
  323. curl -X POST "https://api.example.com/v3/graphs/d09dedb1-b2ab-48a5-b950-6e1f464d83e7/reset" \\
  324. -H "Authorization: Bearer YOUR_API_KEY" """
  325. ),
  326. },
  327. ]
  328. },
  329. )
  330. @self.base_endpoint
  331. async def reset(
  332. collection_id: UUID = Path(...),
  333. auth_user=Depends(self.providers.auth.auth_wrapper()),
  334. ) -> WrappedBooleanResponse:
  335. """
  336. Deletes a graph and all its associated data.
  337. This endpoint permanently removes the specified graph along with all
  338. entities and relationships that belong to only this graph.
  339. The original source entities and relationships extracted from underlying documents are not deleted
  340. and are managed through the document lifecycle.
  341. """
  342. if not auth_user.is_superuser:
  343. raise R2RException("Only superusers can reset a graph", 403)
  344. if (
  345. # not auth_user.is_superuser
  346. collection_id
  347. not in auth_user.collection_ids
  348. ):
  349. raise R2RException(
  350. "The currently authenticated user does not have access to the collection associated with the given graph.",
  351. 403,
  352. )
  353. await self.services.graph.reset_graph_v3(id=collection_id)
  354. # await _pull(collection_id, auth_user)
  355. return GenericBooleanResponse(success=True) # type: ignore
  356. # update graph
  357. @self.router.post(
  358. "/graphs/{collection_id}",
  359. dependencies=[Depends(self.rate_limit_dependency)],
  360. summary="Update graph",
  361. openapi_extra={
  362. "x-codeSamples": [
  363. {
  364. "lang": "Python",
  365. "source": textwrap.dedent(
  366. """
  367. from r2r import R2RClient
  368. client = R2RClient()
  369. # when using auth, do client.login(...)
  370. response = client.graphs.update(
  371. collection_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  372. graph={
  373. "name": "New Name",
  374. "description": "New Description"
  375. }
  376. )"""
  377. ),
  378. },
  379. {
  380. "lang": "JavaScript",
  381. "source": textwrap.dedent(
  382. """
  383. const { r2rClient } = require("r2r-js");
  384. const client = new r2rClient();
  385. function main() {
  386. const response = await client.graphs.update({
  387. collection_id: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  388. name: "New Name",
  389. description: "New Description",
  390. });
  391. }
  392. main();
  393. """
  394. ),
  395. },
  396. ]
  397. },
  398. )
  399. @self.base_endpoint
  400. async def update_graph(
  401. collection_id: UUID = Path(
  402. ...,
  403. description="The collection ID corresponding to the graph to update",
  404. ),
  405. name: Optional[str] = Body(
  406. None, description="The name of the graph"
  407. ),
  408. description: Optional[str] = Body(
  409. None, description="An optional description of the graph"
  410. ),
  411. auth_user=Depends(self.providers.auth.auth_wrapper()),
  412. ):
  413. """
  414. Update an existing graphs's configuration.
  415. This endpoint allows updating the name and description of an existing collection.
  416. The user must have appropriate permissions to modify the collection.
  417. """
  418. if not auth_user.is_superuser:
  419. raise R2RException(
  420. "Only superusers can update graph details", 403
  421. )
  422. if (
  423. not auth_user.is_superuser
  424. and id not in auth_user.collection_ids
  425. ):
  426. raise R2RException(
  427. "The currently authenticated user does not have access to the collection associated with the given graph.",
  428. 403,
  429. )
  430. return await self.services.graph.update_graph( # type: ignore
  431. collection_id,
  432. name=name,
  433. description=description,
  434. )
  435. @self.router.get(
  436. "/graphs/{collection_id}/entities",
  437. dependencies=[Depends(self.rate_limit_dependency)],
  438. openapi_extra={
  439. "x-codeSamples": [
  440. {
  441. "lang": "Python",
  442. "source": textwrap.dedent(
  443. """
  444. from r2r import R2RClient
  445. client = R2RClient()
  446. # when using auth, do client.login(...)
  447. response = client.graphs.list_entities(collection_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7")
  448. """
  449. ),
  450. },
  451. {
  452. "lang": "JavaScript",
  453. "source": textwrap.dedent(
  454. """
  455. const { r2rClient } = require("r2r-js");
  456. const client = new r2rClient();
  457. function main() {
  458. const response = await client.graphs.listEntities({
  459. collection_id: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  460. });
  461. }
  462. main();
  463. """
  464. ),
  465. },
  466. ],
  467. },
  468. )
  469. @self.base_endpoint
  470. async def get_entities(
  471. collection_id: UUID = Path(
  472. ...,
  473. description="The collection ID corresponding to the graph to list entities from.",
  474. ),
  475. offset: int = Query(
  476. 0,
  477. ge=0,
  478. description="Specifies the number of objects to skip. Defaults to 0.",
  479. ),
  480. limit: int = Query(
  481. 100,
  482. ge=1,
  483. le=1000,
  484. description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
  485. ),
  486. auth_user=Depends(self.providers.auth.auth_wrapper()),
  487. ) -> WrappedEntitiesResponse:
  488. """Lists all entities in the graph with pagination support."""
  489. if (
  490. # not auth_user.is_superuser
  491. collection_id
  492. not in auth_user.collection_ids
  493. ):
  494. raise R2RException(
  495. "The currently authenticated user does not have access to the collection associated with the given graph.",
  496. 403,
  497. )
  498. entities, count = await self.services.graph.get_entities(
  499. parent_id=collection_id,
  500. offset=offset,
  501. limit=limit,
  502. )
  503. return entities, { # type: ignore
  504. "total_entries": count,
  505. }
  506. @self.router.post(
  507. "/graphs/{collection_id}/entities/export",
  508. summary="Export graph entities to CSV",
  509. dependencies=[Depends(self.rate_limit_dependency)],
  510. openapi_extra={
  511. "x-codeSamples": [
  512. {
  513. "lang": "Python",
  514. "source": textwrap.dedent(
  515. """
  516. from r2r import R2RClient
  517. client = R2RClient("http://localhost:7272")
  518. # when using auth, do client.login(...)
  519. response = client.graphs.export_entities(
  520. collection_id="b4ac4dd6-5f27-596e-a55b-7cf242ca30aa",
  521. output_path="export.csv",
  522. columns=["id", "title", "created_at"],
  523. include_header=True,
  524. )
  525. """
  526. ),
  527. },
  528. {
  529. "lang": "JavaScript",
  530. "source": textwrap.dedent(
  531. """
  532. const { r2rClient } = require("r2r-js");
  533. const client = new r2rClient("http://localhost:7272");
  534. function main() {
  535. await client.graphs.exportEntities({
  536. collectionId: "b4ac4dd6-5f27-596e-a55b-7cf242ca30aa",
  537. outputPath: "export.csv",
  538. columns: ["id", "title", "created_at"],
  539. includeHeader: true,
  540. });
  541. }
  542. main();
  543. """
  544. ),
  545. },
  546. {
  547. "lang": "CLI",
  548. "source": textwrap.dedent(
  549. """
  550. """
  551. ),
  552. },
  553. {
  554. "lang": "cURL",
  555. "source": textwrap.dedent(
  556. """
  557. curl -X POST "http://127.0.0.1:7272/v3/graphs/export_entities" \
  558. -H "Authorization: Bearer YOUR_API_KEY" \
  559. -H "Content-Type: application/json" \
  560. -H "Accept: text/csv" \
  561. -d '{ "columns": ["id", "title", "created_at"], "include_header": true }' \
  562. --output export.csv
  563. """
  564. ),
  565. },
  566. ]
  567. },
  568. )
  569. @self.base_endpoint
  570. async def export_entities(
  571. background_tasks: BackgroundTasks,
  572. collection_id: UUID = Path(
  573. ...,
  574. description="The ID of the collection to export entities from.",
  575. ),
  576. columns: Optional[list[str]] = Body(
  577. None, description="Specific columns to export"
  578. ),
  579. filters: Optional[dict] = Body(
  580. None, description="Filters to apply to the export"
  581. ),
  582. include_header: Optional[bool] = Body(
  583. True, description="Whether to include column headers"
  584. ),
  585. auth_user=Depends(self.providers.auth.auth_wrapper()),
  586. ) -> FileResponse:
  587. """
  588. Export documents as a downloadable CSV file.
  589. """
  590. if not auth_user.is_superuser:
  591. raise R2RException(
  592. "Only a superuser can export data.",
  593. 403,
  594. )
  595. csv_file_path, temp_file = (
  596. await self.services.management.export_graph_entities(
  597. id=collection_id,
  598. columns=columns,
  599. filters=filters,
  600. include_header=include_header,
  601. )
  602. )
  603. background_tasks.add_task(temp_file.close)
  604. return FileResponse(
  605. path=csv_file_path,
  606. media_type="text/csv",
  607. filename="documents_export.csv",
  608. )
  609. @self.router.post(
  610. "/graphs/{collection_id}/entities",
  611. dependencies=[Depends(self.rate_limit_dependency)],
  612. )
  613. @self.base_endpoint
  614. async def create_entity(
  615. collection_id: UUID = Path(
  616. ...,
  617. description="The collection ID corresponding to the graph to add the entity to.",
  618. ),
  619. name: str = Body(
  620. ..., description="The name of the entity to create."
  621. ),
  622. description: str = Body(
  623. ..., description="The description of the entity to create."
  624. ),
  625. category: Optional[str] = Body(
  626. None, description="The category of the entity to create."
  627. ),
  628. metadata: Optional[dict] = Body(
  629. None, description="The metadata of the entity to create."
  630. ),
  631. auth_user=Depends(self.providers.auth.auth_wrapper()),
  632. ) -> WrappedEntityResponse:
  633. """Creates a new entity in the graph."""
  634. if (
  635. # not auth_user.is_superuser
  636. collection_id
  637. not in auth_user.collection_ids
  638. ):
  639. raise R2RException(
  640. "The currently authenticated user does not have access to the collection associated with the given graph.",
  641. 403,
  642. )
  643. return await self.services.graph.create_entity(
  644. name=name,
  645. description=description,
  646. parent_id=collection_id,
  647. category=category,
  648. metadata=metadata,
  649. )
  650. @self.router.post(
  651. "/graphs/{collection_id}/relationships",
  652. dependencies=[Depends(self.rate_limit_dependency)],
  653. )
  654. @self.base_endpoint
  655. async def create_relationship(
  656. collection_id: UUID = Path(
  657. ...,
  658. description="The collection ID corresponding to the graph to add the relationship to.",
  659. ),
  660. subject: str = Body(
  661. ..., description="The subject of the relationship to create."
  662. ),
  663. subject_id: UUID = Body(
  664. ...,
  665. description="The ID of the subject of the relationship to create.",
  666. ),
  667. predicate: str = Body(
  668. ..., description="The predicate of the relationship to create."
  669. ),
  670. object: str = Body(
  671. ..., description="The object of the relationship to create."
  672. ),
  673. object_id: UUID = Body(
  674. ...,
  675. description="The ID of the object of the relationship to create.",
  676. ),
  677. description: str = Body(
  678. ...,
  679. description="The description of the relationship to create.",
  680. ),
  681. weight: float = Body(
  682. 1.0, description="The weight of the relationship to create."
  683. ),
  684. metadata: Optional[dict] = Body(
  685. None, description="The metadata of the relationship to create."
  686. ),
  687. auth_user=Depends(self.providers.auth.auth_wrapper()),
  688. ) -> WrappedRelationshipResponse:
  689. """Creates a new relationship in the graph."""
  690. if not auth_user.is_superuser:
  691. raise R2RException(
  692. "Only superusers can create relationships.", 403
  693. )
  694. if (
  695. # not auth_user.is_superuser
  696. collection_id
  697. not in auth_user.collection_ids
  698. ):
  699. raise R2RException(
  700. "The currently authenticated user does not have access to the collection associated with the given graph.",
  701. 403,
  702. )
  703. return await self.services.graph.create_relationship(
  704. subject=subject,
  705. subject_id=subject_id,
  706. predicate=predicate,
  707. object=object,
  708. object_id=object_id,
  709. description=description,
  710. weight=weight,
  711. metadata=metadata,
  712. parent_id=collection_id,
  713. )
  714. @self.router.post(
  715. "/graphs/{collection_id}/relationships/export",
  716. summary="Export graph relationships to CSV",
  717. dependencies=[Depends(self.rate_limit_dependency)],
  718. openapi_extra={
  719. "x-codeSamples": [
  720. {
  721. "lang": "Python",
  722. "source": textwrap.dedent(
  723. """
  724. from r2r import R2RClient
  725. client = R2RClient("http://localhost:7272")
  726. # when using auth, do client.login(...)
  727. response = client.graphs.export_entities(
  728. collection_id="b4ac4dd6-5f27-596e-a55b-7cf242ca30aa",
  729. output_path="export.csv",
  730. columns=["id", "title", "created_at"],
  731. include_header=True,
  732. )
  733. """
  734. ),
  735. },
  736. {
  737. "lang": "JavaScript",
  738. "source": textwrap.dedent(
  739. """
  740. const { r2rClient } = require("r2r-js");
  741. const client = new r2rClient("http://localhost:7272");
  742. function main() {
  743. await client.graphs.exportEntities({
  744. collectionId: "b4ac4dd6-5f27-596e-a55b-7cf242ca30aa",
  745. outputPath: "export.csv",
  746. columns: ["id", "title", "created_at"],
  747. includeHeader: true,
  748. });
  749. }
  750. main();
  751. """
  752. ),
  753. },
  754. {
  755. "lang": "CLI",
  756. "source": textwrap.dedent(
  757. """
  758. """
  759. ),
  760. },
  761. {
  762. "lang": "cURL",
  763. "source": textwrap.dedent(
  764. """
  765. curl -X POST "http://127.0.0.1:7272/v3/graphs/export_relationships" \
  766. -H "Authorization: Bearer YOUR_API_KEY" \
  767. -H "Content-Type: application/json" \
  768. -H "Accept: text/csv" \
  769. -d '{ "columns": ["id", "title", "created_at"], "include_header": true }' \
  770. --output export.csv
  771. """
  772. ),
  773. },
  774. ]
  775. },
  776. )
  777. @self.base_endpoint
  778. async def export_relationships(
  779. background_tasks: BackgroundTasks,
  780. collection_id: UUID = Path(
  781. ...,
  782. description="The ID of the document to export entities from.",
  783. ),
  784. columns: Optional[list[str]] = Body(
  785. None, description="Specific columns to export"
  786. ),
  787. filters: Optional[dict] = Body(
  788. None, description="Filters to apply to the export"
  789. ),
  790. include_header: Optional[bool] = Body(
  791. True, description="Whether to include column headers"
  792. ),
  793. auth_user=Depends(self.providers.auth.auth_wrapper()),
  794. ) -> FileResponse:
  795. """
  796. Export documents as a downloadable CSV file.
  797. """
  798. if not auth_user.is_superuser:
  799. raise R2RException(
  800. "Only a superuser can export data.",
  801. 403,
  802. )
  803. csv_file_path, temp_file = (
  804. await self.services.management.export_graph_relationships(
  805. id=collection_id,
  806. columns=columns,
  807. filters=filters,
  808. include_header=include_header,
  809. )
  810. )
  811. background_tasks.add_task(temp_file.close)
  812. return FileResponse(
  813. path=csv_file_path,
  814. media_type="text/csv",
  815. filename="documents_export.csv",
  816. )
  817. @self.router.get(
  818. "/graphs/{collection_id}/entities/{entity_id}",
  819. dependencies=[Depends(self.rate_limit_dependency)],
  820. openapi_extra={
  821. "x-codeSamples": [
  822. {
  823. "lang": "Python",
  824. "source": textwrap.dedent(
  825. """
  826. from r2r import R2RClient
  827. client = R2RClient()
  828. # when using auth, do client.login(...)
  829. response = client.graphs.get_entity(
  830. collection_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  831. entity_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  832. )
  833. """
  834. ),
  835. },
  836. {
  837. "lang": "JavaScript",
  838. "source": textwrap.dedent(
  839. """
  840. const { r2rClient } = require("r2r-js");
  841. const client = new r2rClient();
  842. function main() {
  843. const response = await client.graphs.get_entity({
  844. collectionId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  845. entityId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  846. });
  847. }
  848. main();
  849. """
  850. ),
  851. },
  852. ]
  853. },
  854. )
  855. @self.base_endpoint
  856. async def get_entity(
  857. collection_id: UUID = Path(
  858. ...,
  859. description="The collection ID corresponding to the graph containing the entity.",
  860. ),
  861. entity_id: UUID = Path(
  862. ..., description="The ID of the entity to retrieve."
  863. ),
  864. auth_user=Depends(self.providers.auth.auth_wrapper()),
  865. ) -> WrappedEntityResponse:
  866. """Retrieves a specific entity by its ID."""
  867. if (
  868. # not auth_user.is_superuser
  869. collection_id
  870. not in auth_user.collection_ids
  871. ):
  872. raise R2RException(
  873. "The currently authenticated user does not have access to the collection associated with the given graph.",
  874. 403,
  875. )
  876. result = await self.providers.database.graphs_handler.entities.get(
  877. parent_id=collection_id,
  878. store_type=StoreType.GRAPHS,
  879. offset=0,
  880. limit=1,
  881. entity_ids=[entity_id],
  882. )
  883. if len(result) == 0 or len(result[0]) == 0:
  884. raise R2RException("Entity not found", 404)
  885. return result[0][0]
  886. @self.router.post(
  887. "/graphs/{collection_id}/entities/{entity_id}",
  888. dependencies=[Depends(self.rate_limit_dependency)],
  889. )
  890. @self.base_endpoint
  891. async def update_entity(
  892. collection_id: UUID = Path(
  893. ...,
  894. description="The collection ID corresponding to the graph containing the entity.",
  895. ),
  896. entity_id: UUID = Path(
  897. ..., description="The ID of the entity to update."
  898. ),
  899. name: Optional[str] = Body(
  900. ..., description="The updated name of the entity."
  901. ),
  902. description: Optional[str] = Body(
  903. None, description="The updated description of the entity."
  904. ),
  905. category: Optional[str] = Body(
  906. None, description="The updated category of the entity."
  907. ),
  908. metadata: Optional[dict] = Body(
  909. None, description="The updated metadata of the entity."
  910. ),
  911. auth_user=Depends(self.providers.auth.auth_wrapper()),
  912. ) -> WrappedEntityResponse:
  913. """Updates an existing entity in the graph."""
  914. if not auth_user.is_superuser:
  915. raise R2RException(
  916. "Only superusers can update graph entities.", 403
  917. )
  918. if (
  919. # not auth_user.is_superuser
  920. collection_id
  921. not in auth_user.collection_ids
  922. ):
  923. raise R2RException(
  924. "The currently authenticated user does not have access to the collection associated with the given graph.",
  925. 403,
  926. )
  927. return await self.services.graph.update_entity(
  928. entity_id=entity_id,
  929. name=name,
  930. category=category,
  931. description=description,
  932. metadata=metadata,
  933. )
  934. @self.router.delete(
  935. "/graphs/{collection_id}/entities/{entity_id}",
  936. dependencies=[Depends(self.rate_limit_dependency)],
  937. summary="Remove an entity",
  938. openapi_extra={
  939. "x-codeSamples": [
  940. {
  941. "lang": "Python",
  942. "source": textwrap.dedent(
  943. """
  944. from r2r import R2RClient
  945. client = R2RClient()
  946. # when using auth, do client.login(...)
  947. response = client.graphs.remove_entity(
  948. collection_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  949. entity_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  950. )
  951. """
  952. ),
  953. },
  954. {
  955. "lang": "JavaScript",
  956. "source": textwrap.dedent(
  957. """
  958. const { r2rClient } = require("r2r-js");
  959. const client = new r2rClient();
  960. function main() {
  961. const response = await client.graphs.removeEntity({
  962. collectionId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  963. entityId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  964. });
  965. }
  966. main();
  967. """
  968. ),
  969. },
  970. ]
  971. },
  972. )
  973. @self.base_endpoint
  974. async def delete_entity(
  975. collection_id: UUID = Path(
  976. ...,
  977. description="The collection ID corresponding to the graph to remove the entity from.",
  978. ),
  979. entity_id: UUID = Path(
  980. ...,
  981. description="The ID of the entity to remove from the graph.",
  982. ),
  983. auth_user=Depends(self.providers.auth.auth_wrapper()),
  984. ) -> WrappedBooleanResponse:
  985. """Removes an entity from the graph."""
  986. if not auth_user.is_superuser:
  987. raise R2RException(
  988. "Only superusers can delete graph details.", 403
  989. )
  990. if (
  991. # not auth_user.is_superuser
  992. collection_id
  993. not in auth_user.collection_ids
  994. ):
  995. raise R2RException(
  996. "The currently authenticated user does not have access to the collection associated with the given graph.",
  997. 403,
  998. )
  999. await self.services.graph.delete_entity(
  1000. parent_id=collection_id,
  1001. entity_id=entity_id,
  1002. )
  1003. return GenericBooleanResponse(success=True) # type: ignore
  1004. @self.router.get(
  1005. "/graphs/{collection_id}/relationships",
  1006. dependencies=[Depends(self.rate_limit_dependency)],
  1007. description="Lists all relationships in the graph with pagination support.",
  1008. openapi_extra={
  1009. "x-codeSamples": [
  1010. {
  1011. "lang": "Python",
  1012. "source": textwrap.dedent(
  1013. """
  1014. from r2r import R2RClient
  1015. client = R2RClient()
  1016. # when using auth, do client.login(...)
  1017. response = client.graphs.list_relationships(collection_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7")
  1018. """
  1019. ),
  1020. },
  1021. {
  1022. "lang": "JavaScript",
  1023. "source": textwrap.dedent(
  1024. """
  1025. const { r2rClient } = require("r2r-js");
  1026. const client = new r2rClient();
  1027. function main() {
  1028. const response = await client.graphs.listRelationships({
  1029. collectionId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  1030. });
  1031. }
  1032. main();
  1033. """
  1034. ),
  1035. },
  1036. ],
  1037. },
  1038. )
  1039. @self.base_endpoint
  1040. async def get_relationships(
  1041. collection_id: UUID = Path(
  1042. ...,
  1043. description="The collection ID corresponding to the graph to list relationships from.",
  1044. ),
  1045. offset: int = Query(
  1046. 0,
  1047. ge=0,
  1048. description="Specifies the number of objects to skip. Defaults to 0.",
  1049. ),
  1050. limit: int = Query(
  1051. 100,
  1052. ge=1,
  1053. le=1000,
  1054. description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
  1055. ),
  1056. auth_user=Depends(self.providers.auth.auth_wrapper()),
  1057. ) -> WrappedRelationshipsResponse:
  1058. """
  1059. Lists all relationships in the graph with pagination support.
  1060. """
  1061. if (
  1062. # not auth_user.is_superuser
  1063. collection_id
  1064. not in auth_user.collection_ids
  1065. ):
  1066. raise R2RException(
  1067. "The currently authenticated user does not have access to the collection associated with the given graph.",
  1068. 403,
  1069. )
  1070. relationships, count = await self.services.graph.get_relationships(
  1071. parent_id=collection_id,
  1072. offset=offset,
  1073. limit=limit,
  1074. )
  1075. return relationships, { # type: ignore
  1076. "total_entries": count,
  1077. }
  1078. @self.router.get(
  1079. "/graphs/{collection_id}/relationships/{relationship_id}",
  1080. dependencies=[Depends(self.rate_limit_dependency)],
  1081. description="Retrieves a specific relationship by its ID.",
  1082. openapi_extra={
  1083. "x-codeSamples": [
  1084. {
  1085. "lang": "Python",
  1086. "source": textwrap.dedent(
  1087. """
  1088. from r2r import R2RClient
  1089. client = R2RClient()
  1090. # when using auth, do client.login(...)
  1091. response = client.graphs.get_relationship(
  1092. collection_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  1093. relationship_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  1094. )
  1095. """
  1096. ),
  1097. },
  1098. {
  1099. "lang": "JavaScript",
  1100. "source": textwrap.dedent(
  1101. """
  1102. const { r2rClient } = require("r2r-js");
  1103. const client = new r2rClient();
  1104. function main() {
  1105. const response = await client.graphs.getRelationship({
  1106. collectionId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  1107. relationshipId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  1108. });
  1109. }
  1110. main();
  1111. """
  1112. ),
  1113. },
  1114. ],
  1115. },
  1116. )
  1117. @self.base_endpoint
  1118. async def get_relationship(
  1119. collection_id: UUID = Path(
  1120. ...,
  1121. description="The collection ID corresponding to the graph containing the relationship.",
  1122. ),
  1123. relationship_id: UUID = Path(
  1124. ..., description="The ID of the relationship to retrieve."
  1125. ),
  1126. auth_user=Depends(self.providers.auth.auth_wrapper()),
  1127. ) -> WrappedRelationshipResponse:
  1128. """Retrieves a specific relationship by its ID."""
  1129. if (
  1130. # not auth_user.is_superuser
  1131. collection_id
  1132. not in auth_user.collection_ids
  1133. ):
  1134. raise R2RException(
  1135. "The currently authenticated user does not have access to the collection associated with the given graph.",
  1136. 403,
  1137. )
  1138. results = (
  1139. await self.providers.database.graphs_handler.relationships.get(
  1140. parent_id=collection_id,
  1141. store_type=StoreType.GRAPHS,
  1142. offset=0,
  1143. limit=1,
  1144. relationship_ids=[relationship_id],
  1145. )
  1146. )
  1147. if len(results) == 0 or len(results[0]) == 0:
  1148. raise R2RException("Relationship not found", 404)
  1149. return results[0][0]
  1150. @self.router.post(
  1151. "/graphs/{collection_id}/relationships/{relationship_id}",
  1152. dependencies=[Depends(self.rate_limit_dependency)],
  1153. )
  1154. @self.base_endpoint
  1155. async def update_relationship(
  1156. collection_id: UUID = Path(
  1157. ...,
  1158. description="The collection ID corresponding to the graph containing the relationship.",
  1159. ),
  1160. relationship_id: UUID = Path(
  1161. ..., description="The ID of the relationship to update."
  1162. ),
  1163. subject: Optional[str] = Body(
  1164. ..., description="The updated subject of the relationship."
  1165. ),
  1166. subject_id: Optional[UUID] = Body(
  1167. ..., description="The updated subject ID of the relationship."
  1168. ),
  1169. predicate: Optional[str] = Body(
  1170. ..., description="The updated predicate of the relationship."
  1171. ),
  1172. object: Optional[str] = Body(
  1173. ..., description="The updated object of the relationship."
  1174. ),
  1175. object_id: Optional[UUID] = Body(
  1176. ..., description="The updated object ID of the relationship."
  1177. ),
  1178. description: Optional[str] = Body(
  1179. None,
  1180. description="The updated description of the relationship.",
  1181. ),
  1182. weight: Optional[float] = Body(
  1183. None, description="The updated weight of the relationship."
  1184. ),
  1185. metadata: Optional[dict] = Body(
  1186. None, description="The updated metadata of the relationship."
  1187. ),
  1188. auth_user=Depends(self.providers.auth.auth_wrapper()),
  1189. ) -> WrappedRelationshipResponse:
  1190. """Updates an existing relationship in the graph."""
  1191. if not auth_user.is_superuser:
  1192. raise R2RException(
  1193. "Only superusers can update graph details", 403
  1194. )
  1195. if (
  1196. # not auth_user.is_superuser
  1197. collection_id
  1198. not in auth_user.collection_ids
  1199. ):
  1200. raise R2RException(
  1201. "The currently authenticated user does not have access to the collection associated with the given graph.",
  1202. 403,
  1203. )
  1204. return await self.services.graph.update_relationship(
  1205. relationship_id=relationship_id,
  1206. subject=subject,
  1207. subject_id=subject_id,
  1208. predicate=predicate,
  1209. object=object,
  1210. object_id=object_id,
  1211. description=description,
  1212. weight=weight,
  1213. metadata=metadata,
  1214. )
  1215. @self.router.delete(
  1216. "/graphs/{collection_id}/relationships/{relationship_id}",
  1217. dependencies=[Depends(self.rate_limit_dependency)],
  1218. description="Removes a relationship from the graph.",
  1219. openapi_extra={
  1220. "x-codeSamples": [
  1221. {
  1222. "lang": "Python",
  1223. "source": textwrap.dedent(
  1224. """
  1225. from r2r import R2RClient
  1226. client = R2RClient()
  1227. # when using auth, do client.login(...)
  1228. response = client.graphs.delete_relationship(
  1229. collection_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  1230. relationship_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  1231. )
  1232. """
  1233. ),
  1234. },
  1235. {
  1236. "lang": "JavaScript",
  1237. "source": textwrap.dedent(
  1238. """
  1239. const { r2rClient } = require("r2r-js");
  1240. const client = new r2rClient();
  1241. function main() {
  1242. const response = await client.graphs.deleteRelationship({
  1243. collectionId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  1244. relationshipId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  1245. });
  1246. }
  1247. main();
  1248. """
  1249. ),
  1250. },
  1251. ],
  1252. },
  1253. )
  1254. @self.base_endpoint
  1255. async def delete_relationship(
  1256. collection_id: UUID = Path(
  1257. ...,
  1258. description="The collection ID corresponding to the graph to remove the relationship from.",
  1259. ),
  1260. relationship_id: UUID = Path(
  1261. ...,
  1262. description="The ID of the relationship to remove from the graph.",
  1263. ),
  1264. auth_user=Depends(self.providers.auth.auth_wrapper()),
  1265. ) -> WrappedBooleanResponse:
  1266. """Removes a relationship from the graph."""
  1267. if not auth_user.is_superuser:
  1268. raise R2RException(
  1269. "Only superusers can delete a relationship.", 403
  1270. )
  1271. if (
  1272. not auth_user.is_superuser
  1273. and collection_id not in auth_user.collection_ids
  1274. ):
  1275. raise R2RException(
  1276. "The currently authenticated user does not have access to the collection associated with the given graph.",
  1277. 403,
  1278. )
  1279. await self.services.graph.delete_relationship(
  1280. parent_id=collection_id,
  1281. relationship_id=relationship_id,
  1282. )
  1283. return GenericBooleanResponse(success=True) # type: ignore
  1284. @self.router.post(
  1285. "/graphs/{collection_id}/communities",
  1286. dependencies=[Depends(self.rate_limit_dependency)],
  1287. summary="Create a new community",
  1288. openapi_extra={
  1289. "x-codeSamples": [
  1290. {
  1291. "lang": "Python",
  1292. "source": textwrap.dedent(
  1293. """
  1294. from r2r import R2RClient
  1295. client = R2RClient()
  1296. # when using auth, do client.login(...)
  1297. response = client.graphs.create_community(
  1298. collection_id="9fbe403b-c11c-5aae-8ade-ef22980c3ad1",
  1299. name="My Community",
  1300. summary="A summary of the community",
  1301. findings=["Finding 1", "Finding 2"],
  1302. rating=5,
  1303. rating_explanation="This is a rating explanation",
  1304. )
  1305. """
  1306. ),
  1307. },
  1308. {
  1309. "lang": "JavaScript",
  1310. "source": textwrap.dedent(
  1311. """
  1312. const { r2rClient } = require("r2r-js");
  1313. const client = new r2rClient();
  1314. function main() {
  1315. const response = await client.graphs.createCommunity({
  1316. collectionId: "9fbe403b-c11c-5aae-8ade-ef22980c3ad1",
  1317. name: "My Community",
  1318. summary: "A summary of the community",
  1319. findings: ["Finding 1", "Finding 2"],
  1320. rating: 5,
  1321. ratingExplanation: "This is a rating explanation",
  1322. });
  1323. }
  1324. main();
  1325. """
  1326. ),
  1327. },
  1328. ]
  1329. },
  1330. )
  1331. @self.base_endpoint
  1332. async def create_community(
  1333. collection_id: UUID = Path(
  1334. ...,
  1335. description="The collection ID corresponding to the graph to create the community in.",
  1336. ),
  1337. name: str = Body(..., description="The name of the community"),
  1338. summary: str = Body(..., description="A summary of the community"),
  1339. findings: Optional[list[str]] = Body(
  1340. default=[], description="Findings about the community"
  1341. ),
  1342. rating: Optional[float] = Body(
  1343. default=5, ge=1, le=10, description="Rating between 1 and 10"
  1344. ),
  1345. rating_explanation: Optional[str] = Body(
  1346. default="", description="Explanation for the rating"
  1347. ),
  1348. auth_user=Depends(self.providers.auth.auth_wrapper()),
  1349. ) -> WrappedCommunityResponse:
  1350. """
  1351. Creates a new community in the graph.
  1352. While communities are typically built automatically via the /graphs/{id}/communities/build endpoint,
  1353. this endpoint allows you to manually create your own communities.
  1354. This can be useful when you want to:
  1355. - Define custom groupings of entities based on domain knowledge
  1356. - Add communities that weren't detected by the automatic process
  1357. - Create hierarchical organization structures
  1358. - Tag groups of entities with specific metadata
  1359. The created communities will be integrated with any existing automatically detected communities
  1360. in the graph's community structure.
  1361. """
  1362. if not auth_user.is_superuser:
  1363. raise R2RException(
  1364. "Only superusers can create a community.", 403
  1365. )
  1366. if (
  1367. not auth_user.is_superuser
  1368. and collection_id not in auth_user.collection_ids
  1369. ):
  1370. raise R2RException(
  1371. "The currently authenticated user does not have access to the collection associated with the given graph.",
  1372. 403,
  1373. )
  1374. return await self.services.graph.create_community(
  1375. parent_id=collection_id,
  1376. name=name,
  1377. summary=summary,
  1378. findings=findings,
  1379. rating=rating,
  1380. rating_explanation=rating_explanation,
  1381. )
  1382. @self.router.get(
  1383. "/graphs/{collection_id}/communities",
  1384. dependencies=[Depends(self.rate_limit_dependency)],
  1385. summary="List communities",
  1386. openapi_extra={
  1387. "x-codeSamples": [
  1388. {
  1389. "lang": "Python",
  1390. "source": textwrap.dedent(
  1391. """
  1392. from r2r import R2RClient
  1393. client = R2RClient()
  1394. # when using auth, do client.login(...)
  1395. response = client.graphs.list_communities(collection_id="9fbe403b-c11c-5aae-8ade-ef22980c3ad1")
  1396. """
  1397. ),
  1398. },
  1399. {
  1400. "lang": "JavaScript",
  1401. "source": textwrap.dedent(
  1402. """
  1403. const { r2rClient } = require("r2r-js");
  1404. const client = new r2rClient();
  1405. function main() {
  1406. const response = await client.graphs.listCommunities({
  1407. collectionId: "9fbe403b-c11c-5aae-8ade-ef22980c3ad1",
  1408. });
  1409. }
  1410. main();
  1411. """
  1412. ),
  1413. },
  1414. ]
  1415. },
  1416. )
  1417. @self.base_endpoint
  1418. async def get_communities(
  1419. collection_id: UUID = Path(
  1420. ...,
  1421. description="The collection ID corresponding to the graph to get communities for.",
  1422. ),
  1423. offset: int = Query(
  1424. 0,
  1425. ge=0,
  1426. description="Specifies the number of objects to skip. Defaults to 0.",
  1427. ),
  1428. limit: int = Query(
  1429. 100,
  1430. ge=1,
  1431. le=1000,
  1432. description="Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.",
  1433. ),
  1434. auth_user=Depends(self.providers.auth.auth_wrapper()),
  1435. ) -> WrappedCommunitiesResponse:
  1436. """
  1437. Lists all communities in the graph with pagination support.
  1438. """
  1439. if (
  1440. # not auth_user.is_superuser
  1441. collection_id
  1442. not in auth_user.collection_ids
  1443. ):
  1444. raise R2RException(
  1445. "The currently authenticated user does not have access to the collection associated with the given graph.",
  1446. 403,
  1447. )
  1448. communities, count = await self.services.graph.get_communities(
  1449. parent_id=collection_id,
  1450. offset=offset,
  1451. limit=limit,
  1452. )
  1453. return communities, { # type: ignore
  1454. "total_entries": count,
  1455. }
  1456. @self.router.get(
  1457. "/graphs/{collection_id}/communities/{community_id}",
  1458. dependencies=[Depends(self.rate_limit_dependency)],
  1459. summary="Retrieve a community",
  1460. openapi_extra={
  1461. "x-codeSamples": [
  1462. {
  1463. "lang": "Python",
  1464. "source": textwrap.dedent(
  1465. """
  1466. from r2r import R2RClient
  1467. client = R2RClient()
  1468. # when using auth, do client.login(...)
  1469. response = client.graphs.get_community(collection_id="9fbe403b-c11c-5aae-8ade-ef22980c3ad1")
  1470. """
  1471. ),
  1472. },
  1473. {
  1474. "lang": "JavaScript",
  1475. "source": textwrap.dedent(
  1476. """
  1477. const { r2rClient } = require("r2r-js");
  1478. const client = new r2rClient();
  1479. function main() {
  1480. const response = await client.graphs.getCommunity({
  1481. collectionId: "9fbe403b-c11c-5aae-8ade-ef22980c3ad1",
  1482. });
  1483. }
  1484. main();
  1485. """
  1486. ),
  1487. },
  1488. ]
  1489. },
  1490. )
  1491. @self.base_endpoint
  1492. async def get_community(
  1493. collection_id: UUID = Path(
  1494. ...,
  1495. description="The ID of the collection to get communities for.",
  1496. ),
  1497. community_id: UUID = Path(
  1498. ...,
  1499. description="The ID of the community to get.",
  1500. ),
  1501. auth_user=Depends(self.providers.auth.auth_wrapper()),
  1502. ) -> WrappedCommunityResponse:
  1503. """
  1504. Retrieves a specific community by its ID.
  1505. """
  1506. if (
  1507. # not auth_user.is_superuser
  1508. collection_id
  1509. not in auth_user.collection_ids
  1510. ):
  1511. raise R2RException(
  1512. "The currently authenticated user does not have access to the collection associated with the given graph.",
  1513. 403,
  1514. )
  1515. results = (
  1516. await self.providers.database.graphs_handler.communities.get(
  1517. parent_id=collection_id,
  1518. community_ids=[community_id],
  1519. store_type=StoreType.GRAPHS,
  1520. offset=0,
  1521. limit=1,
  1522. )
  1523. )
  1524. if len(results) == 0 or len(results[0]) == 0:
  1525. raise R2RException("Community not found", 404)
  1526. return results[0][0]
  1527. @self.router.delete(
  1528. "/graphs/{collection_id}/communities/{community_id}",
  1529. dependencies=[Depends(self.rate_limit_dependency)],
  1530. summary="Delete a community",
  1531. openapi_extra={
  1532. "x-codeSamples": [
  1533. {
  1534. "lang": "Python",
  1535. "source": textwrap.dedent(
  1536. """
  1537. from r2r import R2RClient
  1538. client = R2RClient()
  1539. # when using auth, do client.login(...)
  1540. response = client.graphs.delete_community(
  1541. collection_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  1542. community_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  1543. )
  1544. """
  1545. ),
  1546. },
  1547. {
  1548. "lang": "JavaScript",
  1549. "source": textwrap.dedent(
  1550. """
  1551. const { r2rClient } = require("r2r-js");
  1552. const client = new r2rClient();
  1553. function main() {
  1554. const response = await client.graphs.deleteCommunity({
  1555. collectionId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  1556. communityId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  1557. });
  1558. }
  1559. main();
  1560. """
  1561. ),
  1562. },
  1563. ]
  1564. },
  1565. )
  1566. @self.base_endpoint
  1567. async def delete_community(
  1568. collection_id: UUID = Path(
  1569. ...,
  1570. description="The collection ID corresponding to the graph to delete the community from.",
  1571. ),
  1572. community_id: UUID = Path(
  1573. ...,
  1574. description="The ID of the community to delete.",
  1575. ),
  1576. auth_user=Depends(self.providers.auth.auth_wrapper()),
  1577. ):
  1578. if (
  1579. not auth_user.is_superuser
  1580. and collection_id not in auth_user.graph_ids
  1581. ):
  1582. raise R2RException(
  1583. "Only superusers can delete communities", 403
  1584. )
  1585. if (
  1586. # not auth_user.is_superuser
  1587. collection_id
  1588. not in auth_user.collection_ids
  1589. ):
  1590. raise R2RException(
  1591. "The currently authenticated user does not have access to the collection associated with the given graph.",
  1592. 403,
  1593. )
  1594. await self.services.graph.delete_community(
  1595. parent_id=collection_id,
  1596. community_id=community_id,
  1597. )
  1598. return GenericBooleanResponse(success=True) # type: ignore
  1599. @self.router.post(
  1600. "/graphs/{collection_id}/communities/export",
  1601. summary="Export document communities to CSV",
  1602. dependencies=[Depends(self.rate_limit_dependency)],
  1603. openapi_extra={
  1604. "x-codeSamples": [
  1605. {
  1606. "lang": "Python",
  1607. "source": textwrap.dedent(
  1608. """
  1609. from r2r import R2RClient
  1610. client = R2RClient("http://localhost:7272")
  1611. # when using auth, do client.login(...)
  1612. response = client.graphs.export_communities(
  1613. collection_id="b4ac4dd6-5f27-596e-a55b-7cf242ca30aa",
  1614. output_path="export.csv",
  1615. columns=["id", "title", "created_at"],
  1616. include_header=True,
  1617. )
  1618. """
  1619. ),
  1620. },
  1621. {
  1622. "lang": "JavaScript",
  1623. "source": textwrap.dedent(
  1624. """
  1625. const { r2rClient } = require("r2r-js");
  1626. const client = new r2rClient("http://localhost:7272");
  1627. function main() {
  1628. await client.graphs.exportCommunities({
  1629. collectionId: "b4ac4dd6-5f27-596e-a55b-7cf242ca30aa",
  1630. outputPath: "export.csv",
  1631. columns: ["id", "title", "created_at"],
  1632. includeHeader: true,
  1633. });
  1634. }
  1635. main();
  1636. """
  1637. ),
  1638. },
  1639. {
  1640. "lang": "CLI",
  1641. "source": textwrap.dedent(
  1642. """
  1643. """
  1644. ),
  1645. },
  1646. {
  1647. "lang": "cURL",
  1648. "source": textwrap.dedent(
  1649. """
  1650. curl -X POST "http://127.0.0.1:7272/v3/graphs/export_communities" \
  1651. -H "Authorization: Bearer YOUR_API_KEY" \
  1652. -H "Content-Type: application/json" \
  1653. -H "Accept: text/csv" \
  1654. -d '{ "columns": ["id", "title", "created_at"], "include_header": true }' \
  1655. --output export.csv
  1656. """
  1657. ),
  1658. },
  1659. ]
  1660. },
  1661. )
  1662. @self.base_endpoint
  1663. async def export_relationships(
  1664. background_tasks: BackgroundTasks,
  1665. collection_id: UUID = Path(
  1666. ...,
  1667. description="The ID of the document to export entities from.",
  1668. ),
  1669. columns: Optional[list[str]] = Body(
  1670. None, description="Specific columns to export"
  1671. ),
  1672. filters: Optional[dict] = Body(
  1673. None, description="Filters to apply to the export"
  1674. ),
  1675. include_header: Optional[bool] = Body(
  1676. True, description="Whether to include column headers"
  1677. ),
  1678. auth_user=Depends(self.providers.auth.auth_wrapper()),
  1679. ) -> FileResponse:
  1680. """
  1681. Export documents as a downloadable CSV file.
  1682. """
  1683. if not auth_user.is_superuser:
  1684. raise R2RException(
  1685. "Only a superuser can export data.",
  1686. 403,
  1687. )
  1688. csv_file_path, temp_file = (
  1689. await self.services.management.export_graph_communities(
  1690. id=collection_id,
  1691. columns=columns,
  1692. filters=filters,
  1693. include_header=include_header,
  1694. )
  1695. )
  1696. background_tasks.add_task(temp_file.close)
  1697. return FileResponse(
  1698. path=csv_file_path,
  1699. media_type="text/csv",
  1700. filename="documents_export.csv",
  1701. )
  1702. @self.router.post(
  1703. "/graphs/{collection_id}/communities/{community_id}",
  1704. dependencies=[Depends(self.rate_limit_dependency)],
  1705. summary="Update community",
  1706. openapi_extra={
  1707. "x-codeSamples": [
  1708. {
  1709. "lang": "Python",
  1710. "source": textwrap.dedent(
  1711. """
  1712. from r2r import R2RClient
  1713. client = R2RClient()
  1714. # when using auth, do client.login(...)
  1715. response = client.graphs.update_community(
  1716. collection_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  1717. community_update={
  1718. "metadata": {
  1719. "topic": "Technology",
  1720. "description": "Tech companies and products"
  1721. }
  1722. }
  1723. )"""
  1724. ),
  1725. },
  1726. {
  1727. "lang": "JavaScript",
  1728. "source": textwrap.dedent(
  1729. """
  1730. const { r2rClient } = require("r2r-js");
  1731. const client = new r2rClient();
  1732. async function main() {
  1733. const response = await client.graphs.updateCommunity({
  1734. collectionId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  1735. communityId: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7",
  1736. communityUpdate: {
  1737. metadata: {
  1738. topic: "Technology",
  1739. description: "Tech companies and products"
  1740. }
  1741. }
  1742. });
  1743. }
  1744. main();
  1745. """
  1746. ),
  1747. },
  1748. ]
  1749. },
  1750. )
  1751. @self.base_endpoint
  1752. async def update_community(
  1753. collection_id: UUID = Path(...),
  1754. community_id: UUID = Path(...),
  1755. name: Optional[str] = Body(None),
  1756. summary: Optional[str] = Body(None),
  1757. findings: Optional[list[str]] = Body(None),
  1758. rating: Optional[float] = Body(default=None, ge=1, le=10),
  1759. rating_explanation: Optional[str] = Body(None),
  1760. auth_user=Depends(self.providers.auth.auth_wrapper()),
  1761. ) -> WrappedCommunityResponse:
  1762. """
  1763. Updates an existing community in the graph.
  1764. """
  1765. if (
  1766. not auth_user.is_superuser
  1767. and collection_id not in auth_user.graph_ids
  1768. ):
  1769. raise R2RException(
  1770. "Only superusers can update communities.", 403
  1771. )
  1772. if (
  1773. # not auth_user.is_superuser
  1774. collection_id
  1775. not in auth_user.collection_ids
  1776. ):
  1777. raise R2RException(
  1778. "The currently authenticated user does not have access to the collection associated with the given graph.",
  1779. 403,
  1780. )
  1781. return await self.services.graph.update_community(
  1782. community_id=community_id,
  1783. name=name,
  1784. summary=summary,
  1785. findings=findings,
  1786. rating=rating,
  1787. rating_explanation=rating_explanation,
  1788. )
  1789. @self.router.post(
  1790. "/graphs/{collection_id}/pull",
  1791. dependencies=[Depends(self.rate_limit_dependency)],
  1792. summary="Pull latest entities to the graph",
  1793. openapi_extra={
  1794. "x-codeSamples": [
  1795. {
  1796. "lang": "Python",
  1797. "source": textwrap.dedent(
  1798. """
  1799. from r2r import R2RClient
  1800. client = R2RClient()
  1801. # when using auth, do client.login(...)
  1802. response = client.graphs.pull(
  1803. collection_id="d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  1804. )"""
  1805. ),
  1806. },
  1807. {
  1808. "lang": "JavaScript",
  1809. "source": textwrap.dedent(
  1810. """
  1811. const { r2rClient } = require("r2r-js");
  1812. const client = new r2rClient();
  1813. async function main() {
  1814. const response = await client.graphs.pull({
  1815. collection_id: "d09dedb1-b2ab-48a5-b950-6e1f464d83e7"
  1816. });
  1817. }
  1818. main();
  1819. """
  1820. ),
  1821. },
  1822. ]
  1823. },
  1824. )
  1825. @self.base_endpoint
  1826. async def pull(
  1827. collection_id: UUID = Path(
  1828. ..., description="The ID of the graph to initialize."
  1829. ),
  1830. force: Optional[bool] = Body(
  1831. False,
  1832. description="If true, forces a re-pull of all entities and relationships.",
  1833. ),
  1834. # document_ids: list[UUID] = Body(
  1835. # ..., description="List of document IDs to add to the graph."
  1836. # ),
  1837. auth_user=Depends(self.providers.auth.auth_wrapper()),
  1838. ) -> WrappedBooleanResponse:
  1839. """
  1840. Adds documents to a graph by copying their entities and relationships.
  1841. This endpoint:
  1842. 1. Copies document entities to the graphs_entities table
  1843. 2. Copies document relationships to the graphs_relationships table
  1844. 3. Associates the documents with the graph
  1845. When a document is added:
  1846. - Its entities and relationships are copied to graph-specific tables
  1847. - Existing entities/relationships are updated by merging their properties
  1848. - The document ID is recorded in the graph's document_ids array
  1849. Documents added to a graph will contribute their knowledge to:
  1850. - Graph analysis and querying
  1851. - Community detection
  1852. - Knowledge graph enrichment
  1853. The user must have access to both the graph and the documents being added.
  1854. """
  1855. # Check user permissions for graph
  1856. if not auth_user.is_superuser:
  1857. raise R2RException("Only superusers can `pull` a graph.", 403)
  1858. if (
  1859. # not auth_user.is_superuser
  1860. collection_id
  1861. not in auth_user.collection_ids
  1862. ):
  1863. raise R2RException(
  1864. "The currently authenticated user does not have access to the collection associated with the given graph.",
  1865. 403,
  1866. )
  1867. list_graphs_response = await self.services.graph.list_graphs(
  1868. # user_ids=None,
  1869. graph_ids=[collection_id],
  1870. offset=0,
  1871. limit=1,
  1872. )
  1873. if len(list_graphs_response["results"]) == 0:
  1874. raise R2RException("Graph not found", 404)
  1875. collection_id = list_graphs_response["results"][0].collection_id
  1876. documents = []
  1877. document_req = (
  1878. await self.providers.database.collections_handler.documents_in_collection(
  1879. collection_id, offset=0, limit=100
  1880. )
  1881. )["results"]
  1882. documents.extend(document_req)
  1883. while len(document_req) == 100:
  1884. document_req = (
  1885. await self.providers.database.collections_handler.documents_in_collection(
  1886. collection_id, offset=len(documents), limit=100
  1887. )
  1888. )["results"]
  1889. documents.extend(document_req)
  1890. success = False
  1891. for document in documents:
  1892. # TODO - Add better checks for user permissions
  1893. if (
  1894. not auth_user.is_superuser
  1895. and document.id
  1896. not in auth_user.document_ids # TODO - extend to include checks on collections
  1897. ):
  1898. raise R2RException(
  1899. f"The currently authenticated user does not have access to document {document.id}",
  1900. 403,
  1901. )
  1902. entities = (
  1903. await self.providers.database.graphs_handler.entities.get(
  1904. parent_id=document.id,
  1905. store_type=StoreType.DOCUMENTS,
  1906. offset=0,
  1907. limit=100,
  1908. )
  1909. )
  1910. has_document = (
  1911. await self.providers.database.graphs_handler.has_document(
  1912. collection_id, document.id
  1913. )
  1914. )
  1915. if has_document:
  1916. logger.info(
  1917. f"Document {document.id} is already in graph {collection_id}, skipping."
  1918. )
  1919. continue
  1920. if len(entities[0]) == 0:
  1921. if not force:
  1922. logger.warning(
  1923. f"Document {document.id} has no entities, extraction may not have been called, skipping."
  1924. )
  1925. continue
  1926. else:
  1927. logger.warning(
  1928. f"Document {document.id} has no entities, but force=True, continuing."
  1929. )
  1930. success = (
  1931. await self.providers.database.graphs_handler.add_documents(
  1932. id=collection_id,
  1933. document_ids=[document.id],
  1934. )
  1935. )
  1936. if not success:
  1937. logger.warning(
  1938. f"No documents were added to graph {collection_id}, marking as failed."
  1939. )
  1940. if success:
  1941. await self.providers.database.documents_handler.set_workflow_status(
  1942. id=collection_id,
  1943. status_type="graph_sync_status",
  1944. status=KGEnrichmentStatus.SUCCESS,
  1945. )
  1946. return GenericBooleanResponse(success=success) # type: ignore