Skip to content

Commit

Permalink
llamaindex-cli to handle glob patterns correctly
Browse files Browse the repository at this point in the history
This change modifies the llamaindex-cli such that it can
handle the --files argument to properly handle glob patterns.

In order to handle globs like the example given in the issue,
the number of arguments (nargs) value must be set to + in order
to return a list of files or patterns in this case.

Because argparse now returns a list, so restructuring on how
the files are processed needed to be changed along with the
signature of the function.

Fixes run-llama#11798

Signed-off-by: Eric Brown <[email protected]>
  • Loading branch information
ericwb committed Feb 24, 2025
1 parent 81d4b87 commit dff4c6f
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions llama-index-cli/llama_index/cli/rag/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from argparse import ArgumentParser
from glob import iglob
from pathlib import Path
from typing import Any, Callable, Dict, Optional, Union, cast
from typing import Any, Callable, Dict, List, Optional, Union, cast

from llama_index.core import (
Settings,
Expand Down Expand Up @@ -176,7 +176,7 @@ def chat_engine_from_query_pipeline(

async def handle_cli(
self,
files: Optional[str] = None,
files: Optional[List[str]] = None,
question: Optional[str] = None,
chat: bool = False,
verbose: bool = False,
Expand Down Expand Up @@ -205,8 +205,11 @@ async def handle_cli(
if self.verbose:
print("Saving/Loading from persist_dir: ", self.persist_dir)
if files is not None:
expanded_files = []
for pattern in files:
expanded_files.extend(iglob(pattern, recursive=True))
documents = []
for _file in iglob(files, recursive=True):
for _file in expanded_files:
_file = os.path.abspath(_file)
if os.path.isdir(_file):
reader = SimpleDirectoryReader(
Expand All @@ -228,7 +231,7 @@ async def handle_cli(

# Append the `--files` argument to the history file
with open(f"{self.persist_dir}/{RAG_HISTORY_FILE_NAME}", "a") as f:
f.write(files + "\n")
f.write(str(files) + "\n")

if create_llama:
if shutil.which("npx") is None:
Expand Down Expand Up @@ -337,9 +340,10 @@ def add_parser_args(
"-f",
"--files",
type=str,
nargs="+",
help=(
"The name of the file or directory you want to ask a question about,"
'such as "file.pdf".'
"The name of the file(s) or directory you want to ask a question about,"
'such as "file.pdf". Supports globs like "*.py".'
),
)
parser.add_argument(
Expand Down

0 comments on commit dff4c6f

Please sign in to comment.