Talk to language models with AIMODEL(), NEWAIMODEL() and AIPARSER()
total5 does not ship a connection to any provider. It gives you three pieces that work together:
NEWAIMODEL('name', config, driver)registers the driver of one or more models ('a,b'registers the same driver for two names). The driver isfunction(ai, done):ai.payloadhasmodel,messages,tools,promptandthink,ai.configis the configuration andai.options.streamis the function the caller passed tostream()(orundefined). Answer withdone(error, response).AIMODEL('name')starts a request for a registered model and returns a builder.system(text),user(text)andassistant(text)append a message,prompt(text),tool(definition)andthink()fill the rest of the payload, andconfigure({ ... })adds toai.configfor this call only. Finish it withcallback(fn),await promise($)orstream(fn); the driver runs after the current turn, so you can chain everything first.AIPARSER(provider)returns a parser for the streamed answer of a provider (the source oftotal5knowsopenai,claude/anthropic,gemini/google,ollamaandopenai_responses, and reads any other name as a generic format; onlyopenaiis run below).write(chunk)takes pieces of the stream (it keeps incomplete lines until the next piece),end()finishes it, and both return{ content, thinking, tools, reasoning }.parser.on('content', fn)is called with every part of the text as it arrives.
A model that was not registered answers with the error AI model not found.. The example registers a local driver that returns a response from the supplied messages.
NEWAIMODEL('echo', { prefix: 'Echo' }, function(ai, done) {
const last = ai.payload.messages.filter(n => n.role === 'user').pop();
const text = ai.config.prefix + ': ' + (last ? last.content : '');
// A real driver would send ai.payload to the provider and send the pieces to ai.options.stream
if (ai.options.stream) {
for (const word of text.split(' '))
ai.options.stream(word + ' ');
}
done(null, { content: text, roles: ai.payload.messages.map(n => n.role), tools: ai.payload.tools.length, think: !!ai.payload.think });
});exports.install = function() {
ROUTE('POST /ask/', ask);
ROUTE('GET /ask-config/', askconfig);
ROUTE('GET /ask-stream/', askstream);
ROUTE('GET /ask-missing/', missing);
ROUTE('GET /parse/', parse);
};
async function ask($) {
const response = await AIMODEL('echo').system('You are brief.').user($.body.question).tool({ name: 'weather' }).think().promise($);
$.json(response);
}
function askconfig($) {
AIMODEL('echo').user('hello').configure({ prefix: 'Custom' }).callback($.callback());
}
function askstream($) {
const parts = [];
AIMODEL('echo').user('one two three').stream(part => parts.push(part)).callback(function(err, response) {
$.json({ parts: parts, content: response.content });
});
}
function missing($) {
AIMODEL('nobody').user('hi').callback($.callback());
}
function parse($) {
const parser = AIPARSER('openai');
const events = [];
parser.on('content', e => events.push(e.delta));
// the second piece starts in the middle of a line
parser.write('data: {"choices":[{"delta":{"content":"Hel"}}]}\n\ndata: {"choices":[{"delta":{"con');
parser.write('tent":"lo"}}]}\n\ndata: [DONE]\n');
const output = parser.end();
$.json({ output: output, events: events });
}