123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- import json
- import asyncclick as click
- from asyncclick import pass_context
- from cli.utils.param_types import JSON
- from cli.utils.timer import timer
- from r2r import R2RAsyncClient
- @click.group()
- def graphs():
- """Graphs commands."""
- pass
- @graphs.command()
- @click.option(
- "--collection-ids", multiple=True, help="Collection IDs to fetch"
- )
- @click.option(
- "--offset",
- default=0,
- help="The offset to start from. Defaults to 0.",
- )
- @click.option(
- "--limit",
- default=100,
- help="The maximum number of graphs to return. Defaults to 100.",
- )
- @pass_context
- async def list(ctx, collection_ids, offset, limit):
- """List available graphs."""
- client: R2RAsyncClient = ctx.obj
- collection_ids = list(collection_ids) if collection_ids else None
- with timer():
- response = await client.graphs.list(
- collection_ids=collection_ids,
- offset=offset,
- limit=limit,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @pass_context
- async def retrieve(ctx, collection_id):
- """Retrieve a specific graph by collection ID."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.retrieve(collection_id=collection_id)
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @pass_context
- async def reset(ctx, collection_id):
- """Reset a graph, removing all its data."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.reset(collection_id=collection_id)
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.option("--name", help="New name for the graph")
- @click.option("--description", help="New description for the graph")
- @pass_context
- async def update(ctx, collection_id, name, description):
- """Update graph information."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.update(
- collection_id=collection_id,
- name=name,
- description=description,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.option(
- "--offset",
- default=0,
- help="The offset to start from. Defaults to 0.",
- )
- @click.option(
- "--limit",
- default=100,
- help="The maximum number of entities to return. Defaults to 100.",
- )
- @pass_context
- async def list_entities(ctx, collection_id, offset, limit):
- """List entities in a graph."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.list_entities(
- collection_id=collection_id,
- offset=offset,
- limit=limit,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.argument("entity_id", required=True, type=str)
- @pass_context
- async def get_entity(ctx, collection_id, entity_id):
- """Get entity information from a graph."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.get_entity(
- collection_id=collection_id,
- entity_id=entity_id,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.argument("entity_id", required=True, type=str)
- @pass_context
- async def remove_entity(ctx, collection_id, entity_id):
- """Remove an entity from a graph."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.remove_entity(
- collection_id=collection_id,
- entity_id=entity_id,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.option(
- "--offset",
- default=0,
- help="The offset to start from. Defaults to 0.",
- )
- @click.option(
- "--limit",
- default=100,
- help="The maximum number of relationships to return. Defaults to 100.",
- )
- @pass_context
- async def list_relationships(ctx, collection_id, offset, limit):
- """List relationships in a graph."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.list_relationships(
- collection_id=collection_id,
- offset=offset,
- limit=limit,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.argument("relationship_id", required=True, type=str)
- @pass_context
- async def get_relationship(ctx, collection_id, relationship_id):
- """Get relationship information from a graph."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.get_relationship(
- collection_id=collection_id,
- relationship_id=relationship_id,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.argument("relationship_id", required=True, type=str)
- @pass_context
- async def remove_relationship(ctx, collection_id, relationship_id):
- """Remove a relationship from a graph."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.remove_relationship(
- collection_id=collection_id,
- relationship_id=relationship_id,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.option(
- "--settings", required=True, type=JSON, help="Build settings as JSON"
- )
- @click.option("--run-type", default="estimate", help="Type of build to run")
- @click.option(
- "--run-without-orchestration",
- is_flag=True,
- help="Run without orchestration",
- )
- @pass_context
- async def build(
- ctx, collection_id, settings, run_type, run_without_orchestration
- ):
- """Build a graph with specified settings."""
- client: R2RAsyncClient = ctx.obj
- run_with_orchestration = not run_without_orchestration
- with timer():
- response = await client.graphs.build(
- collection_id=collection_id,
- settings=settings,
- run_type=run_type,
- run_with_orchestration=run_with_orchestration,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.option(
- "--offset",
- default=0,
- help="The offset to start from. Defaults to 0.",
- )
- @click.option(
- "--limit",
- default=100,
- help="The maximum number of communities to return. Defaults to 100.",
- )
- @pass_context
- async def list_communities(ctx, collection_id, offset, limit):
- """List communities in a graph."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.list_communities(
- collection_id=collection_id,
- offset=offset,
- limit=limit,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.argument("community_id", required=True, type=str)
- @pass_context
- async def get_community(ctx, collection_id, community_id):
- """Get community information from a graph."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.get_community(
- collection_id=collection_id,
- community_id=community_id,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.argument("community_id", required=True, type=str)
- @click.option("--name", help="New name for the community")
- @click.option("--summary", help="New summary for the community")
- @click.option(
- "--findings",
- type=JSON,
- help="New findings for the community as JSON array",
- )
- @click.option("--rating", type=int, help="New rating for the community")
- @click.option(
- "--rating-explanation", help="New rating explanation for the community"
- )
- @click.option("--level", type=int, help="New level for the community")
- @click.option(
- "--attributes", type=JSON, help="New attributes for the community as JSON"
- )
- @pass_context
- async def update_community(
- ctx,
- collection_id,
- community_id,
- name,
- summary,
- findings,
- rating,
- rating_explanation,
- level,
- attributes,
- ):
- """Update community information."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.update_community(
- collection_id=collection_id,
- community_id=community_id,
- name=name,
- summary=summary,
- findings=findings,
- rating=rating,
- rating_explanation=rating_explanation,
- level=level,
- attributes=attributes,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.argument("community_id", required=True, type=str)
- @pass_context
- async def delete_community(ctx, collection_id, community_id):
- """Delete a community from a graph."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.delete_community(
- collection_id=collection_id,
- community_id=community_id,
- )
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @pass_context
- async def pull(ctx, collection_id):
- """Pull documents into a graph."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.pull(collection_id=collection_id)
- click.echo(json.dumps(response, indent=2))
- @graphs.command()
- @click.argument("collection_id", required=True, type=str)
- @click.argument("document_id", required=True, type=str)
- @pass_context
- async def remove_document(ctx, collection_id, document_id):
- """Remove a document from a graph."""
- client: R2RAsyncClient = ctx.obj
- with timer():
- response = await client.graphs.remove_document(
- collection_id=collection_id,
- document_id=document_id,
- )
- click.echo(json.dumps(response, indent=2))
|