privateGPT.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. from langchain.chains import RetrievalQA
  3. from langchain.embeddings import HuggingFaceEmbeddings
  4. from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
  5. from langchain.vectorstores import Chroma
  6. from langchain.llms import Ollama
  7. import chromadb
  8. import os
  9. import argparse
  10. import time
  11. model = os.environ.get("MODEL", "llama2-uncensored")
  12. # For embeddings model, the example uses a sentence-transformers model
  13. # https://www.sbert.net/docs/pretrained_models.html
  14. # "The all-mpnet-base-v2 model provides the best quality, while all-MiniLM-L6-v2 is 5 times faster and still offers good quality."
  15. embeddings_model_name = os.environ.get("EMBEDDINGS_MODEL_NAME", "all-MiniLM-L6-v2")
  16. persist_directory = os.environ.get("PERSIST_DIRECTORY", "db")
  17. target_source_chunks = int(os.environ.get('TARGET_SOURCE_CHUNKS',4))
  18. from constants import CHROMA_SETTINGS
  19. def main():
  20. # Parse the command line arguments
  21. args = parse_arguments()
  22. embeddings = HuggingFaceEmbeddings(model_name=embeddings_model_name)
  23. # db = Chroma(persist_directory=persist_directory, embedding_function=embeddings, client_settings=CHROMA_SETTINGS)
  24. db = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
  25. retriever = db.as_retriever(search_kwargs={"k": target_source_chunks})
  26. # activate/deactivate the streaming StdOut callback for LLMs
  27. callbacks = [] if args.mute_stream else [StreamingStdOutCallbackHandler()]
  28. llm = Ollama(model=model, callbacks=callbacks)
  29. qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents= not args.hide_source)
  30. # Interactive questions and answers
  31. while True:
  32. query = input("\nEnter a query: ")
  33. if query == "exit":
  34. break
  35. if query.strip() == "":
  36. continue
  37. # Get the answer from the chain
  38. start = time.time()
  39. res = qa(query)
  40. answer, docs = res['result'], [] if args.hide_source else res['source_documents']
  41. end = time.time()
  42. # Print the result
  43. print("\n\n> Question:")
  44. print(query)
  45. print(answer)
  46. # Print the relevant sources used for the answer
  47. for document in docs:
  48. print("\n> " + document.metadata["source"] + ":")
  49. print(document.page_content)
  50. def parse_arguments():
  51. parser = argparse.ArgumentParser(description='privateGPT: Ask questions to your documents without an internet connection, '
  52. 'using the power of LLMs.')
  53. parser.add_argument("--hide-source", "-S", action='store_true',
  54. help='Use this flag to disable printing of source documents used for answers.')
  55. parser.add_argument("--mute-stream", "-M",
  56. action='store_true',
  57. help='Use this flag to disable the streaming StdOut callback for LLMs.')
  58. return parser.parse_args()
  59. if __name__ == "__main__":
  60. main()