Chat with your documents on your local device using GPT models. No data leaves your device and 100% private.
Go to file
2023-09-14 00:55:57 -07:00
.github Add github-actions.yml 2023-06-13 16:59:26 +01:00
localGPTUI Changed the UI of LocalGPT 2023-07-27 22:14:24 +05:30
SOURCE_DOCUMENTS adding example constitution.pdf file 2023-08-29 00:47:14 -07:00
.dockerignore add CUDA Dockerfile and .dockerignore 2023-07-31 13:19:18 +02:00
.editorconfig Add config files needed for pre-commit 2023-06-13 16:59:26 +01:00
.flake8 Update .flake8 to ignore E203 2023-06-13 16:59:26 +01:00
.gitignore Major code update to run_localGPT 2023-09-13 01:04:40 -07:00
.pre-commit-config.yaml Add .pre-commit-config.yaml file 2023-06-13 16:59:26 +01:00
.pyup.yml Add config files needed for pre-commit 2023-06-13 16:59:26 +01:00
ACKNOWLEDGEMENT.md Add ACKNOWLEDGMENT.md 2023-06-13 16:59:26 +01:00
constants.py Default back to GGML model constants.py 2023-09-13 09:35:49 -07:00
CONTRIBUTING.md Add CONTRIBUTING.md 2023-06-13 16:59:26 +01:00
Dockerfile use correct llama-cpp-python version in Dockerfile 2023-09-05 12:22:10 +02:00
ingest.py Merge pull request #388 from karthikcs/kcs-upgrade-chroma 2023-08-28 22:46:55 -07:00
LICENSE Adding project files via upload. 2023-05-23 22:56:13 -07:00
load_models.py Major code update to run_localGPT 2023-09-13 01:04:40 -07:00
localGPT_UI.py updated API code and added a new UI 2023-08-10 19:50:38 -07:00
prompt_template_utils.py Major code update to run_localGPT 2023-09-13 01:04:40 -07:00
pyproject.toml Add config files needed for pre-commit 2023-06-13 16:59:26 +01:00
README.md Update README.md 2023-09-14 00:55:57 -07:00
requirements.txt Upgrade to latest ChromaDB version 2023-08-18 12:32:07 +00:00
run_localGPT_API.py centralized definitions in constants.py 2023-08-04 21:56:23 -07:00
run_localGPT_v2.py Major code update to run_localGPT 2023-09-13 01:04:40 -07:00
run_localGPT.py when download large model from huggingface, there is often a ReadTimeoutError, add resume_download to avoid to rerun from start 2023-09-03 22:07:28 +08:00

LocalGPT: Secure, Local Conversations with Your Documents 🌐

LocalGPT is an open-source initiative that allows you to converse with your documents without compromising your privacy. With everything running locally, you can be assured that no data ever leaves your computer. Dive into the world of secure, local document interactions with LocalGPT.

Features 🌟

  • Utmost Privacy: Your data remains on your computer, ensuring 100% security.
  • Versatile Model Support: Seamlessly integrate a variety of open-source models, including HF, GPTQ, GGML, and GGUF.
  • Diverse Embeddings: Choose from a range of open-source embeddings.
  • Reuse Your LLM: Once downloaded, reuse your LLM without the need for repeated downloads.

Dive Deeper with Our Videos 🎥

Technical Details 🛠️

LocalGPT replaces the GPT4ALL model with the Vicuna-7B model, utilizing InstructorEmbeddings over LlamaEmbeddings. Both embeddings and LLMs are GPU-optimized, but CPU support is also available.

Built Using 🧩

Environment Setup 🌍

  1. 📥 Clone the repo using git:
git clone https://github.com/PromtEngineer/localGPT.git
  1. 🐍 Instal conda for virtual environment management. Create and activate a new virtual environment.
conda create -n localGPT python=3.10.0
conda activate localGPT
  1. 🛠️ Install the dependencies using pip

To set up your environment to run the code, first install all requirements:

pip install -r requirements.txt

Important Note:

LocalGPT uses LlamaCpp-Python for GGML (you will need llama-cpp-python <=0.1.76) and GGUF (llama-cpp-python >=0.1.83) models.

If you want to use BLAS or Metal with llama-cpp you can set appropriate flags:

For NVIDIA GPUs support, use cuBLAS

# Example: cuBLAS
CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install -r requirements.txt

For Apple Metal (M1/M2) support, use

# Example: METAL
CMAKE_ARGS="-DLLAMA_METAL=on"  FORCE_CMAKE=1 pip install -r requirements.txt

For more details, please refer to llama-cpp

Docker 🐳

Installing the required packages for GPU inference on Nvidia GPUs, like gcc 11 and CUDA 11, may cause conflicts with other packages in your system. As an alternative to Conda, you can use Docker with the provided Dockerfile. It includes CUDA, your system just needs Docker, BuildKit, your Nvidia GPU driver and the Nvidia container toolkit. Build as docker build . -t localgpt, requires BuildKit. Docker BuildKit does not support GPU during docker build time right now, only during docker run. Run as docker run -it --mount src="$HOME/.cache",target=/root/.cache,type=bind --gpus=all localgpt.

Test dataset

For testing, this repository comes with Constitution of USA as an example file to use.

Ingesting your OWN data.

Put you files in the SOURCE_DOCUMENTS folder. You can put multiple folders within the SOURCE_DOCUMENTS folder and the code will recursively read your files.

Support file formats:

LocalGPT currently supports the following file formats. LocalGPT uses LangChain for loading these file formats. The code in constants.py uses a DOCUMENT_MAP dictionary to map a file format to the corresponding loader. In order to add support for another file format, simply add this dictionary with the file format and the corresponding loader from LangChain.

DOCUMENT_MAP = {
    ".txt": TextLoader,
    ".md": TextLoader,
    ".py": TextLoader,
    ".pdf": PDFMinerLoader,
    ".csv": CSVLoader,
    ".xls": UnstructuredExcelLoader,
    ".xlsx": UnstructuredExcelLoader,
    ".docx": Docx2txtLoader,
    ".doc": Docx2txtLoader,
}

DOCUMENT_MAP = { ".txt": TextLoader, ".md": TextLoader, ".py": TextLoader, ".pdf": PDFMinerLoader, ".csv": CSVLoader, ".xls": UnstructuredExcelLoader, ".xlsx": UnstructuredExcelLoader, ".docx": Docx2txtLoader, ".doc": Docx2txtLoader, }

Put any and all of your .txt, .pdf, or .csv files into the SOURCE_DOCUMENTS directory in the load_documents() function, replace the docs_path with the absolute path of your source_documents directory.

The current default file types are .txt, .pdf, .csv, and .xlsx, if you want to use any other file type, you will need to convert it to one of the default file types.

Run the following command to ingest all the data.

defaults to cuda

python ingest.py

Use the device type argument to specify a given device.

python ingest.py --device_type cpu

Use help for a full list of supported devices.

python ingest.py --help

It will create an index containing the local vectorstore. Will take time, depending on the size of your documents. You can ingest as many documents as you want, and all will be accumulated in the local embeddings database. If you want to start from an empty database, delete the index.

Note: When you run this for the first time, it will download take time as it has to download the embedding model. In the subseqeunt runs, no data will leave your local enviroment and can be run without internet connection.

Ask questions to your documents, locally!

In order to ask a question, run a command like:

python run_localGPT.py

And wait for the script to require your input.

> Enter a query:

Hit enter. Wait while the LLM model consumes the prompt and prepares the answer. Once done, it will print the answer and the 4 sources it used as context from your documents; you can then ask another question without re-running the script, just wait for the prompt again.

Note: When you run this for the first time, it will need internet connection to download the vicuna-7B model. After that you can turn off your internet connection, and the script inference would still work. No data gets out of your local environment.

Type exit to finish the script.

Run it on CPU

By default, localGPT will use your GPU to run both the ingest.py and run_localGPT.py scripts. But if you do not have a GPU and want to run this on CPU, now you can do that (Warning: Its going to be slow!). You will need to use --device_type cpuflag with both scripts.

For Ingestion run the following:

python ingest.py --device_type cpu

In order to ask a question, run a command like:

python run_localGPT.py --device_type cpu

Run quantized for M1/M2:

GGML quantized models for Apple Silicon (M1/M2) are supported through the llama-cpp library, example. GPTQ quantized models that leverage auto-gptq will not work, see here. GGML models will work for CPU or MPS.

Troubleshooting

Install MPS: 1- Follow this page to build up PyTorch with Metal Performance Shaders (MPS) support. PyTorch uses the new MPS backend for GPU training acceleration. It is good practice to verify mps support using a simple Python script as mentioned in the provided link.

2- By following the page, here is an example of what you may initiate in your terminal

xcode-select --install
conda install pytorch torchvision torchaudio -c pytorch-nightly
pip install chardet
pip install cchardet
pip uninstall charset_normalizer
pip install charset_normalizer
pip install pdfminer.six
pip install xformers

Upgrade packages: Your langchain or llama-cpp version could be outdated. Upgrade your packages by running install again.

pip install -r requirements.txt

If you are still getting errors, try installing the latest llama-cpp-python with these flags, and see thread.

CMAKE_ARGS="-DLLAMA_METAL=on" FORCE_CMAKE=1 pip install -U llama-cpp-python --no-cache-dir

Run the UI

  1. Open constants.py in an editor of your choice and depending on choice add the LLM you want to use. By default, the following model will be used:

    MODEL_ID = "TheBloke/Llama-2-7B-Chat-GGML"
    MODEL_BASENAME = "llama-2-7b-chat.ggmlv3.q4_0.bin"
    
  2. Open up a terminal and activate your python environment that contains the dependencies installed from requirements.txt.

  3. Navigate to the /LOCALGPT directory.

  4. Run the following command python run_localGPT_API.py. The API should being to run.

  5. Wait until everything has loaded in. You should see something like INFO:werkzeug:Press CTRL+C to quit.

  6. Open up a second terminal and activate the same python environment.

  7. Navigate to the /LOCALGPT/localGPTUI directory.

  8. Run the command python localGPTUI.py.

  9. Open up a web browser and go the address http://localhost:5111/.

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.

  • ingest.py uses LangChain tools to parse the document and create embeddings locally using InstructorEmbeddings. It then stores the result in a local vector database using Chroma vector store.
  • run_localGPT.py uses a local LLM to understand questions and create answers. The context for the answers is extracted from the local vector store using a similarity search to locate the right piece of context from the docs.
  • You can replace this local LLM with any other LLM from the HuggingFace. Make sure whatever LLM you select is in the HF format.

How to select different LLM models?

The following will provide instructions on how you can select a different LLM model to create your response:

  1. Open up constants.py in the editor of your choice.

  2. Change the MODEL_ID and MODEL_BASENAME. If you are using a quantized model (GGML, GPTQ), you will need to provide MODEL_BASENAME. For unquatized models, set MODEL_BASENAME to NONE

  3. There are a number of example models from HuggingFace that have already been tested to be run with the original trained model (ending with HF or have a .bin in its "Files and versions"), and quantized models (ending with GPTQ or have a .no-act-order or .safetensors in its "Files and versions").

  4. For models that end with HF or have a .bin inside its "Files and versions" on its HuggingFace page.

    • Make sure you have a model_id selected. For example -> MODEL_ID = "TheBloke/guanaco-7B-HF"
    • If you go to its HuggingFace repo and go to "Files and versions" you will notice model files that end with a .bin extension.
    • Any model files that contain .bin extensions will be run with the following code where the # load the LLM for generating Natural Language responses comment is found.
    • MODEL_ID = "TheBloke/guanaco-7B-HF"
  5. For models that contain GPTQ in its name and or have a .no-act-order or .safetensors extension inside its "Files and versions on its HuggingFace page.

    • Make sure you have a model_id selected. For example -> model_id = "TheBloke/wizardLM-7B-GPTQ"

    • You will also need its model basename file selected. For example -> model_basename = "wizardLM-7B-GPTQ-4bit.compat.no-act-order.safetensors"

    • If you go to its HuggingFace repo and go to "Files and versions" you will notice a model file that ends with a .safetensors extension.

    • Any model files that contain no-act-order or .safetensors extensions will be run with the following code where the # load the LLM for generating Natural Language responses comment is found.

    • MODEL_ID = "TheBloke/WizardLM-7B-uncensored-GPTQ"

      MODEL_BASENAME = "WizardLM-7B-uncensored-GPTQ-4bit-128g.compat.no-act-order.safetensors"

  6. Comment out all other instances of MODEL_ID="other model names", MODEL_BASENAME=other base model names, and llm = load_model(args*)

System Requirements

Python Version

To use this software, you must have Python 3.10 or later installed. Earlier versions of Python will not compile.

C++ Compiler

If you encounter an error while building a wheel during the pip install process, you may need to install a C++ compiler on your computer.

For Windows 10/11

To install a C++ compiler on Windows 10/11, follow these steps:

  1. Install Visual Studio 2022.
  2. Make sure the following components are selected:
    • Universal Windows Platform development
    • C++ CMake tools for Windows
  3. Download the MinGW installer from the MinGW website.
  4. Run the installer and select the "gcc" component.

NVIDIA Driver's Issues:

Follow this page to install NVIDIA Drivers.

Star History

Star History Chart

Disclaimer

This is a test project to validate the feasibility of a fully local solution for question answering using LLMs and Vector embeddings. It is not production ready, and it is not meant to be used in production. Vicuna-7B is based on the Llama model so that has the original Llama license.

Common Errors