conversations.py 3.5 KB

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