new-ingest

Ingest with a CLI
This commit is contained in:
Pavel 2023-02-14 19:37:07 +04:00
parent c3037b0f53
commit af20c7298a
3 changed files with 46 additions and 19 deletions

View File

@ -1,6 +1,9 @@
import sys import sys
import nltk import nltk
import dotenv import dotenv
import typer
from typing import List, Optional
from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.text_splitter import RecursiveCharacterTextSplitter
@ -10,28 +13,52 @@ from parser.open_ai_func import call_openai_api, get_user_permission
dotenv.load_dotenv() dotenv.load_dotenv()
#Specify your folder HERE app = typer.Typer(add_completion=False)
directory_to_ingest = 'inputs'
nltk.download('punkt') nltk.download('punkt', quiet=True)
nltk.download('averaged_perceptron_tagger') nltk.download('averaged_perceptron_tagger', quiet=True)
#Splits all files in specified folder to documents #Splits all files in specified folder to documents
raw_docs = SimpleDirectoryReader(input_dir=directory_to_ingest).load_data() @app.command()
raw_docs = [Document.to_langchain_format(raw_doc) for raw_doc in raw_docs] def ingest(directory: Optional[str] = typer.Option("inputs",
# Here we split the documents, as needed, into smaller chunks. help="Path to the directory for index creation."),
# We do this due to the context limits of the LLMs. files: Optional[List[str]] = typer.Option(None,
text_splitter = RecursiveCharacterTextSplitter() help="""File paths to use (Optional; overrides directory).
docs = text_splitter.split_documents(raw_docs) E.g. --files inputs/1.md --files inputs/2.md"""),
recursive: Optional[bool] = typer.Option(True,
help="Whether to recursively search in subdirectories."),
limit: Optional[int] = typer.Option(None,
help="Maximum number of files to read."),
formats: Optional[List[str]] = typer.Option([".rst", ".md"],
help="""List of required extensions (list with .)
Currently supported: .rst, .md, .pdf, .docx, .csv, .epub"""),
exclude: Optional[bool] = typer.Option(True, help="Whether to exclude hidden files (dotfiles).")):
# Here we check for command line arguments for bot calls. """
# If no argument exists or the permission_bypass_flag argument is not '-y', Creates index from specified location or files.
# user permission is requested to call the API. By default /inputs folder is used, .rst and .md are parsed.
if len(sys.argv) > 1: """
permission_bypass_flag = sys.argv[1] raw_docs = SimpleDirectoryReader(input_dir=directory, input_files=files, recursive=recursive,
if permission_bypass_flag == '-y': required_exts=formats, num_files_limit=limit,
call_openai_api(docs) 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.
# If no argument exists or the permission_bypass_flag argument is not '-y',
# user permission is requested to call the API.
if len(sys.argv) > 1:
permission_bypass_flag = sys.argv[1]
if permission_bypass_flag == '-y':
call_openai_api(docs)
else:
get_user_permission(docs)
else: else:
get_user_permission(docs) get_user_permission(docs)
else:
get_user_permission(docs) if __name__ == "__main__":
app()