Running GPT4All LLM Locally, No Internet Needed — Just a Few Lines of Code!

Autor(a):

In the era of advanced AI technologies, cloud-based solutions have been at the forefront of innovation, enabling users to access powerful language models like GPT-4All seamlessly. However, as technology evolves, so do our needs for increased efficiency, privacy, and control.

GPT4All

Free, local, and privacy-aware chatbots

gpt4all.io

It is an open-sourced ecosystem of powerful and customizable LLM models developed by Nomic.ai. The goal of this project is that anybody can run these models locally on our devices without an internet connection.

Intriguing Advantages!

  1. Running LLm locally with Enhanced Privacy and Security.
  2. Tailored Precision with eco-system of models for different use cases.
  3. Offline support and simple to integrate by any person or enterprise.

Incorporating into Your Code

Note: This article focuses on the utilization of GPT4All LLM in a local, offline environment, specifically for Python projects. The outlined instructions can be adapted for use in other environments as well.

Downloading required model

GPT4All provides many free LLM models to choose to download. The size of models usually ranges from 3–10 GB.

Some of the models are:

  • Falcon 7B: Fine-tuned for assistant-style interactions, excelling in generating detailed responses.
  • Mistral 7B base model: Large-scale language model trained on diverse texts for broad language understanding.
  • Rift Coder v1.5: Trained on an extensive code snippet corpus for proficient coding-related responses.
  • Wizard LM 13b: Versatile model trained on diverse texts, including books, articles, and web content.
  • Hermes: Capable of producing detailed responses, trained on a diverse range of texts.

To download GPT4All models from the official website, follow these steps:

  1. Visit the official GPT4All website 1.
  2. Scroll down to the Model Explorer section.
  3. Select the model of your interest.
  4. Click on the model to download.
  5. Move the downloaded file to the local project folder.

Before this step, it’s essential to create a Python project and relocate the model to the project directory. Once the project is set up, open the terminal and install GPT4All using the following command.

pip install gpt4all

For more details check gpt4all-PyPI

Installing gpt4all in terminal

Coding and execution

After successfully downloading and moving the model to the project directory, and having installed the GPT4All package, we aim to demonstrate local utilization following the sample example given in the official documentation. In this simplified scenario, we are configuring the model for offline use only.

The following is the code and the prompt for testing the model.

from gpt4all import GPT4All
import sys


def draw_cat():
model = GPT4All(model_name='orca-mini-3b-gguf2-q4_0.gguf', allow_download=False, model_path='models/')
tokens = []
for token in model.generate("How AI can help to improve human society?", streaming=True):
tokens.append(token)
sys.stdout.write(token)

sys.stdout.flush()


if __name__ == '__main__':
draw_cat()

In the above code, we are importing the model orca-mini-3b-gguf3-q4_0.gguf from a local directory models .

How AI can help to improve human society? is the prompt will be passed to the model.

Utilize the ‘streaming=true’ flag to dynamically stream the answer text to the terminal, bypassing the conventional approach of waiting for the model to complete the output.

Here’s an overview of the project directory structure and code layout.

Upon execution of the code, the output appears as follows:

The model produced the aforementioned response while executing the code in offline mode.

Refer to the official documentation for GPT4All in Python to explore further details on utilizing these models.

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *