conversations.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 conversations():
  8. """Conversations commands."""
  9. pass
  10. @conversations.command()
  11. @pass_context
  12. async def create(ctx):
  13. """Create a conversation."""
  14. client: R2RAsyncClient = ctx.obj
  15. with timer():
  16. response = await client.conversations.create()
  17. click.echo(json.dumps(response, indent=2))
  18. @conversations.command()
  19. @click.option("--ids", multiple=True, help="Conversation IDs to fetch")
  20. @click.option(
  21. "--offset",
  22. default=0,
  23. help="The offset to start from. Defaults to 0.",
  24. )
  25. @click.option(
  26. "--limit",
  27. default=100,
  28. help="The maximum number of nodes to return. Defaults to 100.",
  29. )
  30. @pass_context
  31. async def list(ctx, ids, offset, limit):
  32. """Get an overview of conversations."""
  33. client: R2RAsyncClient = ctx.obj
  34. ids = list(ids) if ids else None
  35. with timer():
  36. response = await client.conversations.list(
  37. ids=ids,
  38. offset=offset,
  39. limit=limit,
  40. )
  41. for user in response["results"]:
  42. click.echo(json.dumps(user, indent=2))
  43. @conversations.command()
  44. @click.argument("id", required=True, type=str)
  45. @pass_context
  46. async def retrieve(ctx, id):
  47. """Retrieve a collection by ID."""
  48. client: R2RAsyncClient = ctx.obj
  49. with timer():
  50. response = await client.conversations.retrieve(id=id)
  51. click.echo(json.dumps(response, indent=2))
  52. @conversations.command()
  53. @click.argument("id", required=True, type=str)
  54. @pass_context
  55. async def delete(ctx, id):
  56. """Delete a collection by ID."""
  57. client: R2RAsyncClient = ctx.obj
  58. with timer():
  59. response = await client.conversations.delete(id=id)
  60. click.echo(json.dumps(response, indent=2))
  61. @conversations.command()
  62. @click.argument("id", required=True, type=str)
  63. @pass_context
  64. async def list_branches(ctx, id):
  65. """List all branches in a conversation."""
  66. client: R2RAsyncClient = ctx.obj
  67. with timer():
  68. response = await client.conversations.list_branches(
  69. id=id,
  70. )
  71. for user in response["results"]:
  72. click.echo(json.dumps(user, indent=2))
  73. @conversations.command()
  74. @click.argument("id", required=True, type=str)
  75. @click.option(
  76. "--offset",
  77. default=0,
  78. help="The offset to start from. Defaults to 0.",
  79. )
  80. @click.option(
  81. "--limit",
  82. default=100,
  83. help="The maximum number of nodes to return. Defaults to 100.",
  84. )
  85. @pass_context
  86. async def list_users(ctx, id, offset, limit):
  87. """Get an overview of collections."""
  88. client: R2RAsyncClient = ctx.obj
  89. with timer():
  90. response = await client.collections.list_users(
  91. id=id,
  92. offset=offset,
  93. limit=limit,
  94. )
  95. for user in response["results"]:
  96. click.echo(json.dumps(user, indent=2))