Update as per the feedback.

- moved args parser inside main
- assigned empty list to docs.
- Updated README.md.
This commit is contained in:
abhiruka 2023-05-20 07:40:05 +08:00
parent 9fb7f07e3c
commit f8805c80f8
2 changed files with 15 additions and 33 deletions

View File

@ -83,25 +83,7 @@ Type `exit` to finish the script.
### Script Arguments ### Script Arguments
The script also supports optional command-line arguments to modify its behavior: 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
- `--hide-source` or `-S`: Use this flag to disable printing of the source documents used for answers. By default, the source documents are printed.
```shell
python privateGPT.py --hide-source
```
- `--mute-stream` or `-M`: Use this flag to disable LLM standard output streaming response, which by default prints progress to the console.
```shell
python privateGPT.py --mute-stream
```
You can combine these options if needed:
```shell
python privateGPT.py --hide-source --mute-callback
```
# How does it work? # How does it work?

View File

@ -18,12 +18,14 @@ model_n_ctx = os.environ.get('MODEL_N_CTX')
from constants import CHROMA_SETTINGS from constants import CHROMA_SETTINGS
def main(hide_source=False, mute_stream=False): def main():
# Parse the command line arguments
args = parse_arguments()
embeddings = HuggingFaceEmbeddings(model_name=embeddings_model_name) embeddings = HuggingFaceEmbeddings(model_name=embeddings_model_name)
db = Chroma(persist_directory=persist_directory, embedding_function=embeddings, client_settings=CHROMA_SETTINGS) db = Chroma(persist_directory=persist_directory, embedding_function=embeddings, client_settings=CHROMA_SETTINGS)
retriever = db.as_retriever() retriever = db.as_retriever()
# activate/deactivate the streaming StdOut callback for LLMs # activate/deactivate the streaming StdOut callback for LLMs
callbacks = [] if mute_stream else [StreamingStdOutCallbackHandler()] callbacks = [] if args.mute_stream else [StreamingStdOutCallbackHandler()]
# Prepare the LLM # Prepare the LLM
match model_type: match model_type:
case "LlamaCpp": case "LlamaCpp":
@ -33,7 +35,7 @@ def main(hide_source=False, mute_stream=False):
case _default: case _default:
print(f"Model {model_type} not supported!") print(f"Model {model_type} not supported!")
exit; exit;
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents= not hide_source) qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents= not args.hide_source)
# Interactive questions and answers # Interactive questions and answers
while True: while True:
query = input("\nEnter a query: ") query = input("\nEnter a query: ")
@ -42,7 +44,7 @@ def main(hide_source=False, mute_stream=False):
# Get the answer from the chain # Get the answer from the chain
res = qa(query) res = qa(query)
answer, docs = res['result'], None if hide_source else res['source_documents'] answer, docs = res['result'], [] if args.hide_source else res['source_documents']
# Print the result # Print the result
print("\n\n> Question:") print("\n\n> Question:")
@ -50,14 +52,14 @@ def main(hide_source=False, mute_stream=False):
print("\n> Answer:") print("\n> Answer:")
print(answer) print(answer)
# Print the relevant sources used for the answer, if source is True # Print the relevant sources used for the answer
if not hide_source and docs:
for document in docs: for document in docs:
print("\n> " + document.metadata["source"] + ":") print("\n> " + document.metadata["source"] + ":")
print(document.page_content) print(document.page_content)
def parse_arguments(): def parse_arguments():
parser = argparse.ArgumentParser() 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', parser.add_argument("--hide-source", "-S", action='store_true',
help='Use this flag to disable printing of source documents used for answers.') help='Use this flag to disable printing of source documents used for answers.')
@ -69,6 +71,4 @@ def parse_arguments():
if __name__ == "__main__": if __name__ == "__main__":
# Parse the command line arguments main()
args = parse_arguments()
main(hide_source=args.hide_source, mute_stream=args.mute_stream)