diff --git a/README.md b/README.md index 46ee119d..11cb2632 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,11 @@ Note: you could turn off your internet connection, and the script inference woul Type `exit` to finish the script. + +### Script Arguments +The script also supports optional command-line arguments to modify its behavior. You can see a full list of these arguments by running the command ```python privateGPT.py --help``` in your terminal + + # How does it work? Selecting the right local models and the power of `LangChain` you can run the entire pipeline locally, without any data leaving your environment, and with reasonable performance. diff --git a/privateGPT.py b/privateGPT.py index d4545ce8..7adab52d 100755 --- a/privateGPT.py +++ b/privateGPT.py @@ -6,6 +6,7 @@ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.vectorstores import Chroma from langchain.llms import GPT4All, LlamaCpp import os +import argparse load_dotenv() @@ -19,11 +20,14 @@ model_n_ctx = os.environ.get('MODEL_N_CTX') from constants import CHROMA_SETTINGS def main(): + # Parse the command line arguments + args = parse_arguments() embeddings = HuggingFaceEmbeddings(model_name=embeddings_model_name) db = Chroma(persist_directory=persist_directory, embedding_function=embeddings, client_settings=CHROMA_SETTINGS) retriever = db.as_retriever() + # activate/deactivate the streaming StdOut callback for LLMs + callbacks = [] if args.mute_stream else [StreamingStdOutCallbackHandler()] # Prepare the LLM - callbacks = [StreamingStdOutCallbackHandler()] match model_type: case "LlamaCpp": llm = LlamaCpp(model_path=model_path, n_ctx=model_n_ctx, callbacks=callbacks, verbose=False) @@ -32,7 +36,7 @@ def main(): case _default: print(f"Model {model_type} not supported!") exit; - qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True) + qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents= not args.hide_source) # Interactive questions and answers while True: query = input("\nEnter a query: ") @@ -41,7 +45,7 @@ def main(): # Get the answer from the chain res = qa(query) - answer, docs = res['result'], res['source_documents'] + answer, docs = res['result'], [] if args.hide_source else res['source_documents'] # Print the result print("\n\n> Question:") @@ -54,5 +58,18 @@ def main(): print("\n> " + document.metadata["source"] + ":") print(document.page_content) +def parse_arguments(): + parser = argparse.ArgumentParser(description='privateGPT: Ask questions to your documents without an internet connection, ' + 'using the power of LLMs.') + parser.add_argument("--hide-source", "-S", action='store_true', + help='Use this flag to disable printing of source documents used for answers.') + + parser.add_argument("--mute-stream", "-M", + action='store_true', + help='Use this flag to disable the streaming StdOut callback for LLMs.') + + return parser.parse_args() + + if __name__ == "__main__": main()