|
|
@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
import nltk
|
|
|
|
import nltk
|
|
|
|
import dotenv
|
|
|
|
import dotenv
|
|
|
@ -18,13 +20,16 @@ app = typer.Typer(add_completion=False)
|
|
|
|
nltk.download('punkt', quiet=True)
|
|
|
|
nltk.download('punkt', quiet=True)
|
|
|
|
nltk.download('averaged_perceptron_tagger', quiet=True)
|
|
|
|
nltk.download('averaged_perceptron_tagger', quiet=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Splits all files in specified folder to documents
|
|
|
|
#Splits all files in specified folder to documents
|
|
|
|
@app.command()
|
|
|
|
@app.command()
|
|
|
|
def ingest(directory: Optional[str] = typer.Option("inputs",
|
|
|
|
def ingest(yes: bool = typer.Option(False, "-y", "--yes", prompt=False),
|
|
|
|
help="Path to the directory for index creation."),
|
|
|
|
dir: Optional[List[str]] = typer.Option(["inputs"],
|
|
|
|
files: Optional[List[str]] = typer.Option(None,
|
|
|
|
help="""List of paths to directory for index creation.
|
|
|
|
help="""File paths to use (Optional; overrides directory).
|
|
|
|
E.g. --dir inputs --dir inputs2"""),
|
|
|
|
E.g. --files inputs/1.md --files inputs/2.md"""),
|
|
|
|
file: Optional[List[str]] = typer.Option(None,
|
|
|
|
|
|
|
|
help="""File paths to use (Optional; overrides dir).
|
|
|
|
|
|
|
|
E.g. --file inputs/1.md --file inputs/2.md"""),
|
|
|
|
recursive: Optional[bool] = typer.Option(True,
|
|
|
|
recursive: Optional[bool] = typer.Option(True,
|
|
|
|
help="Whether to recursively search in subdirectories."),
|
|
|
|
help="Whether to recursively search in subdirectories."),
|
|
|
|
limit: Optional[int] = typer.Option(None,
|
|
|
|
limit: Optional[int] = typer.Option(None,
|
|
|
@ -38,27 +43,40 @@ def ingest(directory: Optional[str] = typer.Option("inputs",
|
|
|
|
Creates index from specified location or files.
|
|
|
|
Creates index from specified location or files.
|
|
|
|
By default /inputs folder is used, .rst and .md are parsed.
|
|
|
|
By default /inputs folder is used, .rst and .md are parsed.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
raw_docs = SimpleDirectoryReader(input_dir=directory, input_files=files, recursive=recursive,
|
|
|
|
|
|
|
|
required_exts=formats, num_files_limit=limit,
|
|
|
|
|
|
|
|
exclude_hidden=exclude).load_data()
|
|
|
|
|
|
|
|
raw_docs = [Document.to_langchain_format(raw_doc) for raw_doc in raw_docs]
|
|
|
|
|
|
|
|
print(raw_docs)
|
|
|
|
|
|
|
|
# Here we split the documents, as needed, into smaller chunks.
|
|
|
|
|
|
|
|
# We do this due to the context limits of the LLMs.
|
|
|
|
|
|
|
|
text_splitter = RecursiveCharacterTextSplitter()
|
|
|
|
|
|
|
|
docs = text_splitter.split_documents(raw_docs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Here we check for command line arguments for bot calls.
|
|
|
|
def process_one_docs(directory, folder_name):
|
|
|
|
# If no argument exists or the permission_bypass_flag argument is not '-y',
|
|
|
|
raw_docs = SimpleDirectoryReader(input_dir=directory, input_files=file, recursive=recursive,
|
|
|
|
# user permission is requested to call the API.
|
|
|
|
required_exts=formats, num_files_limit=limit,
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
exclude_hidden=exclude).load_data()
|
|
|
|
permission_bypass_flag = sys.argv[1]
|
|
|
|
raw_docs = [Document.to_langchain_format(raw_doc) for raw_doc in raw_docs]
|
|
|
|
if permission_bypass_flag == '-y':
|
|
|
|
print(raw_docs)
|
|
|
|
call_openai_api(docs)
|
|
|
|
# Here we split the documents, as needed, into smaller chunks.
|
|
|
|
|
|
|
|
# We do this due to the context limits of the LLMs.
|
|
|
|
|
|
|
|
text_splitter = RecursiveCharacterTextSplitter()
|
|
|
|
|
|
|
|
docs = text_splitter.split_documents(raw_docs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Here we check for command line arguments for bot calls.
|
|
|
|
|
|
|
|
# If no argument exists or the yes is not True, then the
|
|
|
|
|
|
|
|
# user permission is requested to call the API.
|
|
|
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
|
|
|
|
if yes:
|
|
|
|
|
|
|
|
call_openai_api(docs, folder_name)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
get_user_permission(docs, folder_name)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
get_user_permission(docs)
|
|
|
|
get_user_permission(docs, folder_name)
|
|
|
|
else:
|
|
|
|
|
|
|
|
get_user_permission(docs)
|
|
|
|
folder_counts = defaultdict(int)
|
|
|
|
|
|
|
|
folder_names = []
|
|
|
|
|
|
|
|
for dir_path in dir:
|
|
|
|
|
|
|
|
folder_name = os.path.basename(os.path.normpath(dir_path))
|
|
|
|
|
|
|
|
folder_counts[folder_name] += 1
|
|
|
|
|
|
|
|
if folder_counts[folder_name] > 1:
|
|
|
|
|
|
|
|
folder_name = f"{folder_name}_{folder_counts[folder_name]}"
|
|
|
|
|
|
|
|
folder_names.append(folder_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for directory, folder_name in zip(dir, folder_names):
|
|
|
|
|
|
|
|
process_one_docs(directory, folder_name)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app()
|
|
|
|
app()
|
|
|
|