Skip to content

Class: TemplateChatWrapper

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}}]]"
    // }
});

{{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.

Extends

Constructors

new TemplateChatWrapper()

ts
new TemplateChatWrapper(__namedParameters: TemplateChatWrapperOptions): TemplateChatWrapper

Parameters

ParameterType
__namedParametersTemplateChatWrapperOptions

Returns

TemplateChatWrapper

Overrides

ChatWrapper.constructor

Defined in

chatWrappers/generic/TemplateChatWrapper.ts:71

Properties

defaultSettings

ts
static defaultSettings: ChatWrapperSettings;

Inherited from

ChatWrapper.defaultSettings

Defined in

ChatWrapper.ts:11


wrapperName

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

Overrides

ChatWrapper.wrapperName

Defined in

chatWrappers/generic/TemplateChatWrapper.ts:57


settings

ts
readonly settings: ChatWrapperSettings;

Overrides

ChatWrapper.settings

Defined in

chatWrappers/generic/TemplateChatWrapper.ts:58


template

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

Defined in

chatWrappers/generic/TemplateChatWrapper.ts:60


historyTemplate

ts
readonly historyTemplate: Readonly<{
  system: `${string}${string}`;
  user: `${string}${string}`;
  model: `${string}${string}`;
 }>;

Type declaration

system
ts
system: `${string}${string}`;
user
ts
user: `${string}${string}`;
model
ts
model: `${string}${string}`;

Defined in

chatWrappers/generic/TemplateChatWrapper.ts:61


joinAdjacentMessagesOfTheSameType

ts
readonly joinAdjacentMessagesOfTheSameType: boolean;

Defined in

chatWrappers/generic/TemplateChatWrapper.ts:62

Methods

generateFunctionCallsAndResults()

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

Parameters

ParameterTypeDefault value
functionCallsChatModelFunctionCall[]undefined
useRawCallbooleantrue

Returns

LlamaText

Inherited from

ChatWrapper.generateFunctionCallsAndResults

Defined in

ChatWrapper.ts:55


generateFunctionCall()

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

Parameters

ParameterType
namestring
paramsany

Returns

LlamaText

Inherited from

ChatWrapper.generateFunctionCall

Defined in

ChatWrapper.ts:102


generateFunctionCallResult()

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

Parameters

ParameterType
functionNamestring
functionParamsany
resultany

Returns

LlamaText

Inherited from

ChatWrapper.generateFunctionCallResult

Defined in

ChatWrapper.ts:116


generateModelResponseText()

ts
generateModelResponseText(modelResponse: (string | ChatModelFunctionCall)[], useRawCall: boolean): LlamaText

Parameters

ParameterTypeDefault value
modelResponse(string | ChatModelFunctionCall)[]undefined
useRawCallbooleantrue

Returns

LlamaText

Inherited from

ChatWrapper.generateModelResponseText

Defined in

ChatWrapper.ts:140


generateAvailableFunctionsSystemText()

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

Parameters

ParameterType
availableFunctionsChatModelFunctions
__namedParametersobject
__namedParameters.documentParams?boolean

Returns

LlamaText

Inherited from

ChatWrapper.generateAvailableFunctionsSystemText

Defined in

ChatWrapper.ts:170


addAvailableFunctionsSystemMessageToHistory()

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

Parameters

ParameterType
historyreadonly ChatHistoryItem[]
availableFunctions?ChatModelFunctions
__namedParameters?object
__namedParameters.documentParams?boolean

Returns

readonly ChatHistoryItem[]

Inherited from

ChatWrapper.addAvailableFunctionsSystemMessageToHistory

Defined in

ChatWrapper.ts:196


generateInitialChatHistory()

ts
generateInitialChatHistory(__namedParameters: ChatWrapperGenerateInitialHistoryOptions): ChatHistoryItem[]

Parameters

ParameterType
__namedParametersChatWrapperGenerateInitialHistoryOptions

Returns

ChatHistoryItem[]

Inherited from

ChatWrapper.generateInitialChatHistory

Defined in

ChatWrapper.ts:217


generateContextState()

ts
generateContextState(__namedParameters: ChatWrapperGenerateContextStateOptions): ChatWrapperGeneratedContextState

Parameters

ParameterType
__namedParametersChatWrapperGenerateContextStateOptions

Returns

ChatWrapperGeneratedContextState

Overrides

ChatWrapper.generateContextState

Defined in

chatWrappers/generic/TemplateChatWrapper.ts:102