What's the simplest way to run the CLI commands as python functions? #513
Unanswered
nikhilmaddirala
asked this question in
Q&A
Replies: 1 comment
-
You can refer to the code below.
import argparse
from graphrag.index.cli import index_cli
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--config",
help="The configuration yaml file to use when running the pipeline",
required=False,
type=str,
)
parser.add_argument(
"-v",
"--verbose",
help="Runs the pipeline with verbose logging",
action="store_true",
)
parser.add_argument(
"--memprofile",
help="Runs the pipeline with memory profiling",
action="store_true",
)
parser.add_argument(
"--root",
help="If no configuration is defined, the root directory to use for input data and output data",
# Only required if config is not defined
required=False,
type=str,
)
parser.add_argument(
"--resume",
help="Resume a given data run leveraging Parquet output files.",
# Only required if config is not defined
required=False,
default=None,
type=str,
)
parser.add_argument(
"--reporter",
help="The progress reporter to use. Valid values are 'rich', 'print', or 'none'",
type=str,
)
parser.add_argument(
"--emit",
help="The data formats to emit, comma-separated. Valid values are 'parquet' and 'csv'. default='parquet,csv'",
type=str,
)
parser.add_argument(
"--dryrun",
help="Run the pipeline without actually executing any steps and inspect the configuration.",
action="store_true",
)
parser.add_argument("--nocache", help="Disable LLM cache.", action="store_true")
parser.add_argument(
"--init",
help="Create an initial configuration in the given path.",
action="store_true",
)
args = parser.parse_args()
index_cli(
root=args.root,
verbose=args.verbose or False,
resume=args.resume,
memprofile=args.memprofile or False,
nocache=args.nocache or False,
reporter=args.reporter,
config=args.config,
emit=args.emit,
dryrun=args.dryrun or False,
init=args.init or False,
cli=True,
)
import argparse
from enum import Enum
from graphrag.query.cli import run_global_search
INVALID_METHOD_ERROR = "Invalid method"
class SearchType(Enum):
"""The type of search to run."""
LOCAL = "local"
GLOBAL = "global"
def __str__(self):
"""Return the string representation of the enum value."""
return self.value
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--data",
help="The path with the output data from the pipeline",
required=False,
type=str,
)
parser.add_argument(
"--root",
help="The data project root.",
required=False,
type=str,
)
parser.add_argument(
"--method",
help="The method to run, one of: local or global",
required=True,
type=SearchType,
choices=list(SearchType),
)
parser.add_argument(
"--community_level",
help="Community level in the Leiden community hierarchy from which we will load the community reports higher value means we use reports on smaller communities",
type=int,
default=2,
)
parser.add_argument(
"--response_type",
help="Free form text describing the response type and format, can be anything, e.g. Multiple Paragraphs, Single Paragraph, Single Sentence, List of 3-7 Points, Single Page, Multi-Page Report",
type=str,
default="Multiple Paragraphs",
)
parser.add_argument(
"query",
nargs=1,
help="The query to run",
type=str,
)
args = parser.parse_args()
match args.method:
case SearchType.LOCAL:
run_local_search(
args.data,
args.root,
args.community_level,
args.response_type,
args.query[0],
)
case SearchType.GLOBAL:
run_global_search(
args.data,
args.root,
args.community_level,
args.response_type,
args.query[0],
)
case _:
raise ValueError(INVALID_METHOD_ERROR) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The docs provide these simple CLI commands to get started:
Can anyone help me define simple python functions that would accomplish the same thing by importing graphrag? Thanks in advance! 😊
Beta Was this translation helpful? Give feedback.
All reactions