Views
No views yet
System tag provided in the prompt structure. To achieve the best inference quality, define the processing mode explicitly prior to user inputs.[CHAT] or [STORY] Mode1System: [CHAT]
2User: Write a story about a girl cleaning up her toys.
3Assistant:
4
5[CODE] Mode1System: [CODE]
2User: Write a python while loop to count to 10.
3Assistant:
4
5[FACT] or [RAG] Mode1System: [FACT]
2Context: The vehicle requires 205/55 R19 tires for optimal performance.
3User: What size tires do I need?
4Assistant:
5
6⚠️ CRITICAL TOKENIZER WARNING: Ensure your prompt structure ends exactly on the colon (Assistant:) with no trailing space. If a physical space is left after the colon, the sub-word tokenizer will misalign, leading to omitted word spaces or combined words.
node-llama-cpp. For optimal streaming results, utilize a sliding-window text decoder to cleanly reconstruct trailing word spaces during active inference.1import {LlamaModel, LlamaContext, LlamaSequence} from "node-llama-cpp";
2import path from "path";
3
4const model = new LlamaModel({
5 modelPath: path.join(__dirname, "model-f16.gguf")
6});
7
8const context = new LlamaContext({model});
9const sequence = new LlamaSequence({context});
10
11// Step 1: Format prompt strictly without a trailing space. Choose your Mode!
12const prompt = `System: [CODE]\nUser: Write a python print statement.\nAssistant:`;
13const tokens = model.tokenize(prompt);
14
15// Step 2: Inject BOS token if missing from sequence start
16const finalTokens = tokens[0] === model.tokens.bos ? tokens : [model.tokens.bos, ...tokens];
17
18let responseTokens: number[] = [];
19let printedLength = 0;
20
21console.log("Assistant stream started:\n");
22
23for await (const token of sequence.evaluate(finalTokens, {
24 temperature: 0.7,
25 topP: 0.95,
26 topK: 50,
27 repeatPenalty: false // Retain natural structural text pacing
28})) {
29 if (token === model.tokens.eos) break;
30 responseTokens.push(token);
31
32 // Dynamic window decoding prevents token boundary space stripping
33 const fullText = model.detokenize(responseTokens);
34 const textChunk = fullText.slice(printedLength);
35 printedLength = fullText.length;
36
37 process.stdout.write(textChunk);
38}
39
40