GPT4All provides two ways to interact with models: Chat Sessions and Stateless Usage.
Chat Sessions
Use model.createChatSession() to maintain context between completions. This is ideal for back-and-forth conversations. A model instance can only have one active chat session at a time. You can set default options (like temperature) and a systemPrompt for the entire session.
Stateless Usage
Use createCompletion(model, ...) directly on the model instance for one-off completions. Context is not maintained between calls. If providing an array of messages for a stateless call, the last message must have the role user, otherwise an error is thrown.
import { createCompletion, loadModel } from "../src/gpt4all.js";
const model = await loadModel("orca-mini-3b-gguf2-q4_0.gguf", {
verbose: true,
device: "gpu",
nCtx: 2048,
});
// Chat Session (Stateful)
const chat = await model.createChatSession({
temperature: 0.8,
systemPrompt: "### System:\nYou are an advanced mathematician.\n\n",
});
const res1 = await createCompletion(chat, "What is 1 + 1?");
// Stateless (One-off)
const res2 = await createCompletion(model, "What is 1 + 1?");
model.dispose();