indices.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 indices():
  8. """Indices commands."""
  9. pass
  10. @indices.command()
  11. @click.option(
  12. "--offset",
  13. default=0,
  14. help="The offset to start from. Defaults to 0.",
  15. )
  16. @click.option(
  17. "--limit",
  18. default=100,
  19. help="The maximum number of nodes to return. Defaults to 100.",
  20. )
  21. @pass_context
  22. async def list(ctx: click.Context, offset, limit):
  23. """Get an overview of indices."""
  24. client: R2RAsyncClient = ctx.obj
  25. try:
  26. with timer():
  27. response = await client.indices.list(
  28. offset=offset,
  29. limit=limit,
  30. )
  31. for user in response["results"]: # type: ignore
  32. click.echo(json.dumps(user, indent=2))
  33. except R2RException as e:
  34. click.echo(str(e), err=True)
  35. except Exception as e:
  36. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  37. @indices.command()
  38. @click.argument("index_name", required=True, type=str)
  39. @click.argument("table_name", required=True, type=str)
  40. @pass_context
  41. async def retrieve(ctx: click.Context, index_name, table_name):
  42. """Retrieve an index by name."""
  43. client: R2RAsyncClient = ctx.obj
  44. try:
  45. with timer():
  46. response = await client.indices.retrieve(
  47. index_name=index_name,
  48. table_name=table_name,
  49. )
  50. click.echo(json.dumps(response, indent=2))
  51. except R2RException as e:
  52. click.echo(str(e), err=True)
  53. except Exception as e:
  54. click.echo(str(f"An unexpected error occurred: {e}"), err=True)
  55. @indices.command()
  56. @click.argument("index_name", required=True, type=str)
  57. @click.argument("table_name", required=True, type=str)
  58. @pass_context
  59. async def delete(ctx: click.Context, index_name, table_name):
  60. """Delete an index by name."""
  61. client: R2RAsyncClient = ctx.obj
  62. try:
  63. with timer():
  64. response = await client.indices.retrieve(
  65. index_name=index_name,
  66. table_name=table_name,
  67. )
  68. click.echo(json.dumps(response, indent=2))
  69. except R2RException as e:
  70. click.echo(str(e), err=True)
  71. except Exception as e:
  72. click.echo(str(f"An unexpected error occurred: {e}"), err=True)