graphs.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import json
  2. import asyncclick as click
  3. from asyncclick import pass_context
  4. from cli.utils.param_types import JSON
  5. from cli.utils.timer import timer
  6. from r2r import R2RAsyncClient
  7. @click.group()
  8. def graphs():
  9. """Graphs commands."""
  10. pass
  11. @graphs.command()
  12. @click.option(
  13. "--collection-ids", multiple=True, help="Collection IDs to fetch"
  14. )
  15. @click.option(
  16. "--offset",
  17. default=0,
  18. help="The offset to start from. Defaults to 0.",
  19. )
  20. @click.option(
  21. "--limit",
  22. default=100,
  23. help="The maximum number of graphs to return. Defaults to 100.",
  24. )
  25. @pass_context
  26. async def list(ctx, collection_ids, offset, limit):
  27. """List available graphs."""
  28. client: R2RAsyncClient = ctx.obj
  29. collection_ids = list(collection_ids) if collection_ids else None
  30. with timer():
  31. response = await client.graphs.list(
  32. collection_ids=collection_ids,
  33. offset=offset,
  34. limit=limit,
  35. )
  36. click.echo(json.dumps(response, indent=2))
  37. @graphs.command()
  38. @click.argument("collection_id", required=True, type=str)
  39. @pass_context
  40. async def retrieve(ctx, collection_id):
  41. """Retrieve a specific graph by collection ID."""
  42. client: R2RAsyncClient = ctx.obj
  43. with timer():
  44. response = await client.graphs.retrieve(collection_id=collection_id)
  45. click.echo(json.dumps(response, indent=2))
  46. @graphs.command()
  47. @click.argument("collection_id", required=True, type=str)
  48. @pass_context
  49. async def reset(ctx, collection_id):
  50. """Reset a graph, removing all its data."""
  51. client: R2RAsyncClient = ctx.obj
  52. with timer():
  53. response = await client.graphs.reset(collection_id=collection_id)
  54. click.echo(json.dumps(response, indent=2))
  55. @graphs.command()
  56. @click.argument("collection_id", required=True, type=str)
  57. @click.option("--name", help="New name for the graph")
  58. @click.option("--description", help="New description for the graph")
  59. @pass_context
  60. async def update(ctx, collection_id, name, description):
  61. """Update graph information."""
  62. client: R2RAsyncClient = ctx.obj
  63. with timer():
  64. response = await client.graphs.update(
  65. collection_id=collection_id,
  66. name=name,
  67. description=description,
  68. )
  69. click.echo(json.dumps(response, indent=2))
  70. @graphs.command()
  71. @click.argument("collection_id", required=True, type=str)
  72. @click.option(
  73. "--offset",
  74. default=0,
  75. help="The offset to start from. Defaults to 0.",
  76. )
  77. @click.option(
  78. "--limit",
  79. default=100,
  80. help="The maximum number of entities to return. Defaults to 100.",
  81. )
  82. @pass_context
  83. async def list_entities(ctx, collection_id, offset, limit):
  84. """List entities in a graph."""
  85. client: R2RAsyncClient = ctx.obj
  86. with timer():
  87. response = await client.graphs.list_entities(
  88. collection_id=collection_id,
  89. offset=offset,
  90. limit=limit,
  91. )
  92. click.echo(json.dumps(response, indent=2))
  93. @graphs.command()
  94. @click.argument("collection_id", required=True, type=str)
  95. @click.argument("entity_id", required=True, type=str)
  96. @pass_context
  97. async def get_entity(ctx, collection_id, entity_id):
  98. """Get entity information from a graph."""
  99. client: R2RAsyncClient = ctx.obj
  100. with timer():
  101. response = await client.graphs.get_entity(
  102. collection_id=collection_id,
  103. entity_id=entity_id,
  104. )
  105. click.echo(json.dumps(response, indent=2))
  106. @graphs.command()
  107. @click.argument("collection_id", required=True, type=str)
  108. @click.argument("entity_id", required=True, type=str)
  109. @pass_context
  110. async def remove_entity(ctx, collection_id, entity_id):
  111. """Remove an entity from a graph."""
  112. client: R2RAsyncClient = ctx.obj
  113. with timer():
  114. response = await client.graphs.remove_entity(
  115. collection_id=collection_id,
  116. entity_id=entity_id,
  117. )
  118. click.echo(json.dumps(response, indent=2))
  119. @graphs.command()
  120. @click.argument("collection_id", required=True, type=str)
  121. @click.option(
  122. "--offset",
  123. default=0,
  124. help="The offset to start from. Defaults to 0.",
  125. )
  126. @click.option(
  127. "--limit",
  128. default=100,
  129. help="The maximum number of relationships to return. Defaults to 100.",
  130. )
  131. @pass_context
  132. async def list_relationships(ctx, collection_id, offset, limit):
  133. """List relationships in a graph."""
  134. client: R2RAsyncClient = ctx.obj
  135. with timer():
  136. response = await client.graphs.list_relationships(
  137. collection_id=collection_id,
  138. offset=offset,
  139. limit=limit,
  140. )
  141. click.echo(json.dumps(response, indent=2))
  142. @graphs.command()
  143. @click.argument("collection_id", required=True, type=str)
  144. @click.argument("relationship_id", required=True, type=str)
  145. @pass_context
  146. async def get_relationship(ctx, collection_id, relationship_id):
  147. """Get relationship information from a graph."""
  148. client: R2RAsyncClient = ctx.obj
  149. with timer():
  150. response = await client.graphs.get_relationship(
  151. collection_id=collection_id,
  152. relationship_id=relationship_id,
  153. )
  154. click.echo(json.dumps(response, indent=2))
  155. @graphs.command()
  156. @click.argument("collection_id", required=True, type=str)
  157. @click.argument("relationship_id", required=True, type=str)
  158. @pass_context
  159. async def remove_relationship(ctx, collection_id, relationship_id):
  160. """Remove a relationship from a graph."""
  161. client: R2RAsyncClient = ctx.obj
  162. with timer():
  163. response = await client.graphs.remove_relationship(
  164. collection_id=collection_id,
  165. relationship_id=relationship_id,
  166. )
  167. click.echo(json.dumps(response, indent=2))
  168. @graphs.command()
  169. @click.argument("collection_id", required=True, type=str)
  170. @click.option(
  171. "--settings", required=True, type=JSON, help="Build settings as JSON"
  172. )
  173. @click.option("--run-type", default="estimate", help="Type of build to run")
  174. @click.option(
  175. "--run-without-orchestration",
  176. is_flag=True,
  177. help="Run without orchestration",
  178. )
  179. @pass_context
  180. async def build(
  181. ctx, collection_id, settings, run_type, run_without_orchestration
  182. ):
  183. """Build a graph with specified settings."""
  184. client: R2RAsyncClient = ctx.obj
  185. run_with_orchestration = not run_without_orchestration
  186. with timer():
  187. response = await client.graphs.build(
  188. collection_id=collection_id,
  189. settings=settings,
  190. run_type=run_type,
  191. run_with_orchestration=run_with_orchestration,
  192. )
  193. click.echo(json.dumps(response, indent=2))
  194. @graphs.command()
  195. @click.argument("collection_id", required=True, type=str)
  196. @click.option(
  197. "--offset",
  198. default=0,
  199. help="The offset to start from. Defaults to 0.",
  200. )
  201. @click.option(
  202. "--limit",
  203. default=100,
  204. help="The maximum number of communities to return. Defaults to 100.",
  205. )
  206. @pass_context
  207. async def list_communities(ctx, collection_id, offset, limit):
  208. """List communities in a graph."""
  209. client: R2RAsyncClient = ctx.obj
  210. with timer():
  211. response = await client.graphs.list_communities(
  212. collection_id=collection_id,
  213. offset=offset,
  214. limit=limit,
  215. )
  216. click.echo(json.dumps(response, indent=2))
  217. @graphs.command()
  218. @click.argument("collection_id", required=True, type=str)
  219. @click.argument("community_id", required=True, type=str)
  220. @pass_context
  221. async def get_community(ctx, collection_id, community_id):
  222. """Get community information from a graph."""
  223. client: R2RAsyncClient = ctx.obj
  224. with timer():
  225. response = await client.graphs.get_community(
  226. collection_id=collection_id,
  227. community_id=community_id,
  228. )
  229. click.echo(json.dumps(response, indent=2))
  230. @graphs.command()
  231. @click.argument("collection_id", required=True, type=str)
  232. @click.argument("community_id", required=True, type=str)
  233. @click.option("--name", help="New name for the community")
  234. @click.option("--summary", help="New summary for the community")
  235. @click.option(
  236. "--findings",
  237. type=JSON,
  238. help="New findings for the community as JSON array",
  239. )
  240. @click.option("--rating", type=int, help="New rating for the community")
  241. @click.option(
  242. "--rating-explanation", help="New rating explanation for the community"
  243. )
  244. @click.option("--level", type=int, help="New level for the community")
  245. @click.option(
  246. "--attributes", type=JSON, help="New attributes for the community as JSON"
  247. )
  248. @pass_context
  249. async def update_community(
  250. ctx,
  251. collection_id,
  252. community_id,
  253. name,
  254. summary,
  255. findings,
  256. rating,
  257. rating_explanation,
  258. level,
  259. attributes,
  260. ):
  261. """Update community information."""
  262. client: R2RAsyncClient = ctx.obj
  263. with timer():
  264. response = await client.graphs.update_community(
  265. collection_id=collection_id,
  266. community_id=community_id,
  267. name=name,
  268. summary=summary,
  269. findings=findings,
  270. rating=rating,
  271. rating_explanation=rating_explanation,
  272. level=level,
  273. attributes=attributes,
  274. )
  275. click.echo(json.dumps(response, indent=2))
  276. @graphs.command()
  277. @click.argument("collection_id", required=True, type=str)
  278. @click.argument("community_id", required=True, type=str)
  279. @pass_context
  280. async def delete_community(ctx, collection_id, community_id):
  281. """Delete a community from a graph."""
  282. client: R2RAsyncClient = ctx.obj
  283. with timer():
  284. response = await client.graphs.delete_community(
  285. collection_id=collection_id,
  286. community_id=community_id,
  287. )
  288. click.echo(json.dumps(response, indent=2))
  289. @graphs.command()
  290. @click.argument("collection_id", required=True, type=str)
  291. @pass_context
  292. async def pull(ctx, collection_id):
  293. """Pull documents into a graph."""
  294. client: R2RAsyncClient = ctx.obj
  295. with timer():
  296. response = await client.graphs.pull(collection_id=collection_id)
  297. click.echo(json.dumps(response, indent=2))
  298. @graphs.command()
  299. @click.argument("collection_id", required=True, type=str)
  300. @click.argument("document_id", required=True, type=str)
  301. @pass_context
  302. async def remove_document(ctx, collection_id, document_id):
  303. """Remove a document from a graph."""
  304. client: R2RAsyncClient = ctx.obj
  305. with timer():
  306. response = await client.graphs.remove_document(
  307. collection_id=collection_id,
  308. document_id=document_id,
  309. )
  310. click.echo(json.dumps(response, indent=2))