New Tool Radar

Local Model Setup

How to Chat With GPT4All’s Server From Python

Point the OpenAI Python client at GPT4All's local server on port 4891 and chat with your local model from a script.

Schematic diagram with four steps: enable the server, install the OpenAI client, set the base URL to localhost:4891/v1, then chat and read the reply.

GPT4All’s local server speaks the OpenAI API, so the standard OpenAI Python client works against it with one change: the base URL. Point it at http://localhost:4891/v1 and your script chats with the model on your own machine.

Point the OpenAI client at localhost:4891

Enable the local server in GPT4All’s Settings, then install the old-style OpenAI client with pip install openai~=0.28 and set its base URL: openai.api_base = "http://localhost:4891/v1" and openai.api_key = "not needed". Then call openai.ChatCompletion.create(model="<model-name>", messages=[{"role": "user", "content": "Hello"}]) with the model name exactly as GET /v1/models reports it.

pip install openai~=0.28
import openai
openai.api_base = "http://localhost:4891/v1"
openai.api_key = "not needed"
resp = openai.ChatCompletion.create(
    model="<model-name>",
    messages=[{"role": "user", "content": "Hello"}])
print(resp.choices[0].message.content)

The version pin matters: the wiki’s example uses the 0.28 client, whose api_base style differs from the 1.x client. If you use the 1.x client, pass base_url="http://localhost:4891/v1" to the OpenAI() constructor instead. Either way, the model name must match the server’s list exactly.

Try it: four checks

Check What you should see If you do not see it
Server on The local server is enabled in Settings Port 4891, localhost
Client installed pip install openai~=0.28 succeeds Or use the 1.x client with base_url
Base URL set api_base points at localhost:4891/v1 Check the URL spelling
Reply returns The chat call returns a reply Check the model name from /v1/models

Expected result. We checked the GPT4All Local API Server wiki, which gives the Python example with openai~=0.28, api_base http://localhost:4891/v1, and the ChatCompletion call. We did not run the code on this machine.

Limits

This guide covers the Python client setup only. The 0.28 and 1.x clients set the base URL differently. We did not test any code ourselves.

Sources

See all