indices.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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
  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, offset, limit):
  23. """Get an overview of indices."""
  24. client: R2RAsyncClient = ctx.obj
  25. with timer():
  26. response = await client.indices.list(
  27. offset=offset,
  28. limit=limit,
  29. )
  30. for user in response["results"]:
  31. click.echo(json.dumps(user, indent=2))
  32. @indices.command()
  33. @click.argument("index_name", required=True, type=str)
  34. @click.argument("table_name", required=True, type=str)
  35. @pass_context
  36. async def retrieve(ctx, index_name, table_name):
  37. """Retrieve an index by name."""
  38. client: R2RAsyncClient = ctx.obj
  39. with timer():
  40. response = await client.indices.retrieve(
  41. index_name=index_name,
  42. table_name=table_name,
  43. )
  44. click.echo(json.dumps(response, indent=2))
  45. @indices.command()
  46. @click.argument("index_name", required=True, type=str)
  47. @click.argument("table_name", required=True, type=str)
  48. @pass_context
  49. async def delete(ctx, index_name, table_name):
  50. """Delete an index by name."""
  51. client: R2RAsyncClient = ctx.obj
  52. with timer():
  53. response = await client.indices.retrieve(
  54. index_name=index_name,
  55. table_name=table_name,
  56. )
  57. click.echo(json.dumps(response, indent=2))
  58. @indices.command()
  59. @click.argument("id", required=True, type=str)
  60. @pass_context
  61. async def list_branches(ctx, id):
  62. """List all branches in a conversation."""
  63. client: R2RAsyncClient = ctx.obj
  64. with timer():
  65. response = await client.indices.list_branches(
  66. id=id,
  67. )
  68. for user in response["results"]:
  69. click.echo(json.dumps(user, indent=2))