New Tool Radar

Local AI

How to Call a GPT4All Model from a Python Script

Install the gpt4all package with pip, load a model by name with the GPT4All class, and call generate inside a chat session to get a helpful reply.

Schematic diagram with three steps: install the gpt4all package with pip, load a model by name with the GPT4All class, then call generate inside a chat session.

GPT4All is not only a desktop chat app: the same models can be called from your own Python script. That is the route to take when a reply belongs inside a program — a small tool that summarises a file, answers a question on a schedule, or feeds a model reply into another step — instead of a chat window you type into by hand. The desktop app, the server mode, and the document search are separate ways to use the same models; this guide covers the script route only.

Install the gpt4all package

Install the package into your Python environment with pip. The official Python SDK docs say to pip-install the gpt4all package, and recommend installing it into its own virtual environment using venv or conda:

pip install gpt4all

No account and no API key is involved: the package runs models on your own computer.

Load a model by name

Models are loaded by name through the GPT4All class. On first use the model is downloaded to your device and saved, so it can be reloaded quickly the next time you create a GPT4All model with the same name. The SDK docs give this example, a 4.66 GB model that needs 8 GB of RAM:

from gpt4all import GPT4All
model = GPT4All("Meta-Llama-3-8B-Instruct.Q4_0.gguf")

If 4.66 GB is more than you want for a first try, the same docs list a smaller option: Phi-3-mini-4k-instruct.Q4_0.gguf at 2.18 GB, needing 4 GB of RAM. Use the smaller name in place of the Llama 3 name and the rest of the script is unchanged.

Ask inside a chat session

Most of the models you can load were trained as assistants, so for a helpful reply rather than a bare continuation, apply the model’s chat template by generating inside a chat session. The SDK docs show it with a laptop question and a 1024-token limit:

with model.chat_session():
    print(model.generate("How can I run LLMs efficiently on my laptop?", max_tokens=1024))

Calling model.generate() directly, without a chat session, prompts the model without applying any templates. The docs warn that this can produce responses that are less like helpful answers and more like mirroring the tone of your prompt — the same prompt that returns a worked explanation of the quadratic formula inside a chat session returns a meandering continuation outside one.

Check What you should see If you do not see it
Installed pip install gpt4all finishes without error Check you are in the right virtual environment
Loaded The model downloads on first use, then loads from disk The model name is misspelled — copy it from the SDK docs table
Chat session The reply reads as a helpful answer to your question You called generate() outside a chat session — wrap it in with model.chat_session():
Reply A printed reply about running LLMs on a laptop The model is still downloading — wait for the first load to finish

Expected result. We checked GPT4All’s own Python SDK docs for the install, the load, the chat-session example, and the direct-generation warning. We did not run the script.

Limits

The model names and sizes above are the SDK docs’ own examples, not recommendations — pick the model that fits your computer, and check its RAM requirement in the same table before you load it. A first load downloads several gigabytes, so it needs internet access once; later loads run offline. The PyPI page shows the same call with a GPU device option (device='gpu'), but this guide covers the CPU default. The reply wording changes from run to run, so the docs’ quadratic-formula output is an example, not a fixed result. We did not run the script, so no reply here is a recorded result.

Sources

See all