Rozhovor s jazykovými modely s AIMODEL(), NEWAIMODel() a AIPARSER()
total5 neobsahuje připojení k žádnému poskytovateli. Dává vám tři části, které spolupracují:
NEWAIMODEL('name', config, driver)zaregistruje ovladač jednoho nebo více modelů ('a,b'zaregistruje tentýž ovladač pro dva názvy). Ovladač jefunction(ai, done):ai.payloadmámodel,messages,tools,promptathink,ai.configje konfigurace aai.options.streamje funkce, kterou volající předal dostream()(neboundefined). Odpovězte přesdone(error, response).AIMODEL('name')zahájí požadavek pro zaregistrovaný model a vrátí builder.system(text),user(text)aassistant(text)připojí zprávu,prompt(text),tool(definition)athink()vyplní zbytek payloadu aconfigure({ ... })přidá doai.configjen pro toto volání. Ukončete jej přescallback(fn),await promise($)nebostream(fn); ovladač se spustí po aktuálním cyklu, takže vše můžete nejprve zřetězit.AIPARSER(provider)vrátí parser pro streamovanou odpověď poskytovatele (zdrojtotal5znáopenai,claude/anthropic,gemini/google,ollamaaopenai_responsesa každý jiný název čte jako obecný formát; níže se spouští pouzeopenai).write(chunk)přijímá kousky streamu (neúplné řádky drží do dalšího kousku),end()jej ukončí a obě vracejí{ content, thinking, tools, reasoning }.parser.on('content', fn)se volá s každou částí textu, jak přichází.
Model, který nebyl zaznamenán odpovědi s chybou AI model not found.. Příklad zaznamenává místní řidič, který vrací odpověď z dodaných zpráv.
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 });
}