Skip to content

Class: TemplateChatWrapper ​

Defined in: chatWrappers/generic/TemplateChatWrapper.ts:76

A chat wrapper based on a simple template.

Example ​

ts
import {TemplateChatWrapper} from "node-llama-cpp";

const chatWrapper = new TemplateChatWrapper({
    template: "{{systemPrompt}}\n{{history}}model: {{completion}}\nuser: ",
    historyTemplate: {
        system: "system: {{message}}\n",
        user: "user: {{message}}\n",
        model: "model: {{message}}\n"
    },
    // functionCallMessageTemplate: { // optional
    //     call: "[[call: {{functionName}}({{functionParams}})]]",
    //     result: " [[result: {{functionCallResult}}]]"
    // },
    // segments: {
    //     thoughtTemplate: "<think>{{content}}</think>",
    //     reopenThoughtAfterFunctionCalls: true
    // }
});

{{systemPrompt}} is optional and is replaced with the first system message (when is does, that system message is not included in the history).

{{history}} is replaced with the chat history. Each message in the chat history is converted using the template passed to historyTemplate for the message role, and all messages are joined together.

{{completion}} is where the model's response is generated. The text that comes after {{completion}} is used to determine when the model has finished generating the response, and thus is mandatory.

functionCallMessageTemplate is used to specify the format in which functions can be called by the model and how their results are fed to the model after the function call.

segments is used to specify the format of the segments generated by the model (like thought segments).

Extends ​

Constructors ​

Constructor ​

Defined in: chatWrappers/generic/TemplateChatWrapper.ts:91

Parameters ​

ParameterType
__namedParametersTemplateChatWrapperOptions

Returns ​

TemplateChatWrapper

Overrides ​

ChatWrapper.constructor

Properties ​

defaultSettings ​

ts
static defaultSettings: ChatWrapperSettings;

Defined in: ChatWrapper.ts:15

Inherited from ​

ChatWrapper.defaultSettings


wrapperName ​

ts
readonly wrapperName: "Template" = "Template";

Defined in: chatWrappers/generic/TemplateChatWrapper.ts:77

Overrides ​

ChatWrapper.wrapperName


settings ​

ts
readonly settings: ChatWrapperSettings;

Defined in: chatWrappers/generic/TemplateChatWrapper.ts:78

Overrides ​

ChatWrapper.settings


template ​

ts
readonly template: 
  | `${string}${string}${string}`
  | `${string}${string}${string}${string}`;

Defined in: chatWrappers/generic/TemplateChatWrapper.ts:80


historyTemplate ​

ts
readonly historyTemplate: Readonly<TemplateChatWrapperOptions["historyTemplate"]>;

Defined in: chatWrappers/generic/TemplateChatWrapper.ts:81


joinAdjacentMessagesOfTheSameType ​

ts
readonly joinAdjacentMessagesOfTheSameType: boolean;

Defined in: chatWrappers/generic/TemplateChatWrapper.ts:82

Methods ​

generateFunctionCallsAndResults() ​

ts
generateFunctionCallsAndResults(functionCalls: ChatModelFunctionCall[], useRawCall?: boolean): LlamaText;

Defined in: ChatWrapper.ts:63

Parameters ​

ParameterTypeDefault value
functionCallsChatModelFunctionCall[]undefined
useRawCallbooleantrue

Returns ​

LlamaText

Inherited from ​

ChatWrapper.generateFunctionCallsAndResults


generateFunctionCall() ​

ts
generateFunctionCall(name: string, params: any): LlamaText;

Defined in: ChatWrapper.ts:110

Parameters ​

ParameterType
namestring
paramsany

Returns ​

LlamaText

Inherited from ​

ChatWrapper.generateFunctionCall


generateFunctionCallResult() ​

ts
generateFunctionCallResult(
   functionName: string, 
   functionParams: any, 
   result: any): LlamaText;

Defined in: ChatWrapper.ts:127

Parameters ​

ParameterType
functionNamestring
functionParamsany
resultany

Returns ​

LlamaText

Inherited from ​

ChatWrapper.generateFunctionCallResult


generateModelResponseText() ​

ts
generateModelResponseText(modelResponse: (
  | string
  | ChatModelFunctionCall
  | ChatModelSegment)[], useRawValues?: boolean): LlamaText;

Defined in: ChatWrapper.ts:158

Parameters ​

ParameterTypeDefault value
modelResponse( | string | ChatModelFunctionCall | ChatModelSegment)[]undefined
useRawValuesbooleantrue

Returns ​

LlamaText

Inherited from ​

ChatWrapper.generateModelResponseText


generateAvailableFunctionsSystemText() ​

ts
generateAvailableFunctionsSystemText(availableFunctions: ChatModelFunctions, __namedParameters: {
  documentParams?: boolean;
}): LlamaText;

Defined in: ChatWrapper.ts:243

Parameters ​

ParameterType
availableFunctionsChatModelFunctions
__namedParameters{ documentParams?: boolean; }
__namedParameters.documentParams?boolean

Returns ​

LlamaText

Inherited from ​

ChatWrapper.generateAvailableFunctionsSystemText


addAvailableFunctionsSystemMessageToHistory() ​

ts
addAvailableFunctionsSystemMessageToHistory(
   history: readonly ChatHistoryItem[], 
   availableFunctions?: ChatModelFunctions, 
   __namedParameters?: {
  documentParams?: boolean;
}): readonly ChatHistoryItem[];

Defined in: ChatWrapper.ts:269

Parameters ​

ParameterType
historyreadonly ChatHistoryItem[]
availableFunctions?ChatModelFunctions
__namedParameters?{ documentParams?: boolean; }
__namedParameters.documentParams?boolean

Returns ​

readonly ChatHistoryItem[]

Inherited from ​

ChatWrapper.addAvailableFunctionsSystemMessageToHistory


generateInitialChatHistory() ​

ts
generateInitialChatHistory(__namedParameters?: ChatWrapperGenerateInitialHistoryOptions): ChatHistoryItem[];

Defined in: ChatWrapper.ts:290

Parameters ​

ParameterType
__namedParametersChatWrapperGenerateInitialHistoryOptions

Returns ​

ChatHistoryItem[]

Inherited from ​

ChatWrapper.generateInitialChatHistory


generateContextState() ​

Defined in: chatWrappers/generic/TemplateChatWrapper.ts:124

Parameters ​

ParameterType
__namedParametersChatWrapperGenerateContextStateOptions

Returns ​

ChatWrapperGeneratedContextState

Overrides ​

ChatWrapper.generateContextState