collections.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import json
  2. import asyncclick as click
  3. from asyncclick import pass_context
  4. from cli.utils.timer import timer
  5. from r2r import R2RAsyncClient, R2RException
  6. @click.group()
  7. def collections():
  8. """Collections commands."""
  9. pass
  10. @collections.command()
  11. @click.argument("name", required=True, type=str)
  12. @click.option("--description", type=str)
  13. @pass_context
  14. async def create(ctx: click.Context, name, description):
  15. """Create a collection."""
  16. client: R2RAsyncClient = ctx.obj
  17. try:
  18. with timer():
  19. response = await client.collections.create(
  20. name=name,
  21. description=description,
  22. )
  23. click.echo(json.dumps(response, indent=2))
  24. except R2RException as e:
  25. click.echo(str(e), err=True)
  26. except Exception as e:
  27. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  28. @collections.command()
  29. @click.option("--ids", multiple=True, help="Collection IDs to fetch")
  30. @click.option(
  31. "--offset",
  32. default=0,
  33. help="The offset to start from. Defaults to 0.",
  34. )
  35. @click.option(
  36. "--limit",
  37. default=100,
  38. help="The maximum number of nodes to return. Defaults to 100.",
  39. )
  40. @pass_context
  41. async def list(ctx: click.Context, ids, offset, limit):
  42. """Get an overview of collections."""
  43. client: R2RAsyncClient = ctx.obj
  44. ids = list(ids) if ids else None
  45. try:
  46. with timer():
  47. response = await client.collections.list(
  48. ids=ids,
  49. offset=offset,
  50. limit=limit,
  51. )
  52. click.echo(json.dumps(response, indent=2))
  53. except R2RException as e:
  54. click.echo(str(e), err=True)
  55. except Exception as e:
  56. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  57. @collections.command()
  58. @click.argument("id", required=True, type=str)
  59. @pass_context
  60. async def retrieve(ctx: click.Context, id):
  61. """Retrieve a collection by ID."""
  62. client: R2RAsyncClient = ctx.obj
  63. try:
  64. with timer():
  65. response = await client.collections.retrieve(id=id)
  66. click.echo(json.dumps(response, indent=2))
  67. except R2RException as e:
  68. click.echo(str(e), err=True)
  69. except Exception as e:
  70. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  71. @collections.command()
  72. @click.argument("id", required=True, type=str)
  73. @pass_context
  74. async def delete(ctx: click.Context, id):
  75. """Delete a collection by ID."""
  76. client: R2RAsyncClient = ctx.obj
  77. try:
  78. with timer():
  79. response = await client.collections.delete(id=id)
  80. click.echo(json.dumps(response, indent=2))
  81. except R2RException as e:
  82. click.echo(str(e), err=True)
  83. except Exception as e:
  84. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  85. @collections.command()
  86. @click.argument("id", required=True, type=str)
  87. @click.option(
  88. "--offset",
  89. default=0,
  90. help="The offset to start from. Defaults to 0.",
  91. )
  92. @click.option(
  93. "--limit",
  94. default=100,
  95. help="The maximum number of nodes to return. Defaults to 100.",
  96. )
  97. @pass_context
  98. async def list_documents(ctx: click.Context, id, offset, limit):
  99. """Get an overview of collections."""
  100. client: R2RAsyncClient = ctx.obj
  101. try:
  102. with timer():
  103. response = await client.collections.list_documents(
  104. id=id,
  105. offset=offset,
  106. limit=limit,
  107. )
  108. for user in response["results"]: # type: ignore
  109. click.echo(json.dumps(user, indent=2))
  110. except R2RException as e:
  111. click.echo(str(e), err=True)
  112. except Exception as e:
  113. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  114. @collections.command()
  115. @click.argument("id", required=True, type=str)
  116. @click.option(
  117. "--offset",
  118. default=0,
  119. help="The offset to start from. Defaults to 0.",
  120. )
  121. @click.option(
  122. "--limit",
  123. default=100,
  124. help="The maximum number of nodes to return. Defaults to 100.",
  125. )
  126. @pass_context
  127. async def list_users(ctx: click.Context, id, offset, limit):
  128. """Get an overview of collections."""
  129. client: R2RAsyncClient = ctx.obj
  130. try:
  131. with timer():
  132. response = await client.collections.list_users(
  133. id=id,
  134. offset=offset,
  135. limit=limit,
  136. )
  137. for user in response["results"]: # type: ignore
  138. click.echo(json.dumps(user, indent=2))
  139. except R2RException as e:
  140. click.echo(str(e), err=True)
  141. except Exception as e:
  142. click.echo(str(f"An unexpected error occurred: {e}"), err=True)