Skip to content

Commit 39eec91

Browse files
committed
feat: pass a model to resolveChatWrapper
1 parent bdca928 commit 39eec91

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

docs/guide/chat-wrapper.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,38 @@ console.log("User: " + q2);
254254
const a2 = await session.prompt(q2);
255255
console.log("AI: " + a2);
256256
```
257+
258+
## Default Chat Wrapper Options
259+
You can use the [`resolveChatWrapper(...)`](../api/functions/resolveChatWrapper.md) function to resolve the best chat wrapper for a given model,
260+
and configure the default options for each of the builtin chat wrappers it may resolve to.
261+
262+
```typescript
263+
import {fileURLToPath} from "url";
264+
import path from "path";
265+
import {getLlama, LlamaChatSession, resolveChatWrapper} from "node-llama-cpp";
266+
267+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
268+
269+
const llama = await getLlama();
270+
const model = await llama.loadModel({
271+
modelPath: path.join(__dirname, "models", "Meta-Llama-3-8B-Instruct.Q4_K_M.gguf")
272+
});
273+
const context = await model.createContext();
274+
const session = new LlamaChatSession({
275+
contextSequence: context.getSequence(),
276+
chatWrapper: resolveChatWrapper(model, {// [!code highlight]
277+
customWrapperSettings: {// [!code highlight]
278+
"llama3.1": {// [!code highlight]
279+
cuttingKnowledgeDate: new Date("2025-01-01T00:00:00Z")// [!code highlight]
280+
}// [!code highlight]
281+
}// [!code highlight]
282+
})// [!code highlight]
283+
});
284+
285+
286+
const q1 = "Hi there, how are you?";
287+
console.log("User: " + q1);
288+
289+
const a1 = await session.prompt(q1);
290+
console.log("AI: " + a1);
291+
```

src/chatWrappers/utils/resolveChatWrapper.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,37 @@ export type ResolveChatWrapperOptions = {
100100
noJinja?: boolean
101101
};
102102

103+
export type ResolveChatWrapperWithModelOptions = {
104+
/**
105+
* Resolve to a specific chat wrapper type.
106+
* You better not set this option unless you need to force a specific chat wrapper type.
107+
*
108+
* Defaults to `"auto"`.
109+
*/
110+
type?: "auto" | SpecializedChatWrapperTypeName | TemplateChatWrapperTypeName,
111+
112+
customWrapperSettings?: {
113+
[wrapper in keyof typeof chatWrappers]?: ConstructorParameters<(typeof chatWrappers)[wrapper]>[0]
114+
},
115+
116+
/**
117+
* Defaults to `true`.
118+
*/
119+
warningLogs?: boolean,
120+
121+
/**
122+
* Defaults to `true`.
123+
*/
124+
fallbackToOtherWrappersOnJinjaError?: boolean,
125+
126+
/**
127+
* Don't resolve to a Jinja chat wrapper unless `type` is set to a Jinja chat wrapper type.
128+
*
129+
* Defaults to `false`.
130+
*/
131+
noJinja?: boolean
132+
};
133+
103134
/**
104135
* Resolve to a chat wrapper instance based on the provided information.
105136
* The more information provided, the better the resolution will be (except for `type`).
@@ -113,6 +144,21 @@ export type ResolveChatWrapperOptions = {
113144
* if the chat template format is invalid, it fallbacks to resolve other chat wrappers,
114145
* unless `fallbackToOtherWrappersOnJinjaError` is set to `false` (in which case, it will throw an error).
115146
* @example
147+
* ```typescript
148+
* import {getLlama, resolveChatWrapper, GeneralChatWrapper} from "node-llama-cpp";
149+
*
150+
* const llama = await getLlama();
151+
* const model = await llama.loadModel({modelPath: "path/to/model.gguf"});
152+
*
153+
* const chatWrapper = resolveChatWrapper(model, {
154+
* customWrapperSettings: {
155+
* "llama3.1": {
156+
* cuttingKnowledgeDate: new Date("2025-01-01T00:00:00Z")
157+
* }
158+
* }
159+
* }) ?? new GeneralChatWrapper()
160+
* ```
161+
* @example
116162
*```typescript
117163
* import {getLlama, resolveChatWrapper, GeneralChatWrapper} from "node-llama-cpp";
118164
*
@@ -127,11 +173,15 @@ export type ResolveChatWrapperOptions = {
127173
* }) ?? new GeneralChatWrapper()
128174
* ```
129175
*/
176+
export function resolveChatWrapper(model: LlamaModel, options?: ResolveChatWrapperWithModelOptions): BuiltInChatWrapperType;
130177
export function resolveChatWrapper(options: ResolveChatWrapperOptions): BuiltInChatWrapperType | null;
131-
export function resolveChatWrapper(options: LlamaModel): BuiltInChatWrapperType;
132-
export function resolveChatWrapper(options: ResolveChatWrapperOptions | LlamaModel): BuiltInChatWrapperType | null {
178+
export function resolveChatWrapper(
179+
options: ResolveChatWrapperOptions | LlamaModel,
180+
modelOptions?: ResolveChatWrapperWithModelOptions
181+
): BuiltInChatWrapperType | null {
133182
if (options instanceof LlamaModel)
134183
return resolveChatWrapper({
184+
...(modelOptions ?? {}),
135185
bosString: options.tokens.bosString,
136186
filename: options.filename,
137187
fileInfo: options.fileInfo,

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ import {ChatHistoryFunctionCallMessageTemplate} from "./chatWrappers/generic/uti
6666
import {
6767
resolvableChatWrapperTypeNames, type ResolvableChatWrapperTypeName, specializedChatWrapperTypeNames,
6868
type SpecializedChatWrapperTypeName, templateChatWrapperTypeNames, type TemplateChatWrapperTypeName, resolveChatWrapper,
69-
type ResolveChatWrapperOptions, type BuiltInChatWrapperType, chatWrappers
69+
type ResolveChatWrapperOptions, type ResolveChatWrapperWithModelOptions, type BuiltInChatWrapperType, chatWrappers
7070
} from "./chatWrappers/utils/resolveChatWrapper.js";
7171
import {ChatModelFunctionsDocumentationGenerator} from "./chatWrappers/utils/ChatModelFunctionsDocumentationGenerator.js";
7272
import {
@@ -90,8 +90,8 @@ import {jsonDumps} from "./chatWrappers/utils/jsonDumps.js";
9090
import {experimentalChunkDocument} from "./evaluator/utils/chunkDocument.js";
9191

9292
import {
93-
type ChatHistoryItem, type ChatModelFunctionCall, type ChatModelSegmentType, type ChatModelFunctions, type ChatModelResponse,
94-
type ChatSessionModelFunction, type ChatSessionModelFunctions, type ChatSystemMessage, type ChatUserMessage,
93+
type ChatHistoryItem, type ChatModelFunctionCall, type ChatModelSegmentType, type ChatModelSegment, type ChatModelFunctions,
94+
type ChatModelResponse, type ChatSessionModelFunction, type ChatSessionModelFunctions, type ChatSystemMessage, type ChatUserMessage,
9595
type Token, type Tokenizer, type Detokenizer, isChatModelResponseFunctionCall, isChatModelResponseSegment,
9696
type LLamaContextualRepeatPenalty, type ChatWrapperSettings, type ChatWrapperSettingsSegment,
9797
type ChatWrapperGenerateContextStateOptions, type ChatWrapperGeneratedContextState, type ChatWrapperGenerateInitialHistoryOptions
@@ -222,6 +222,7 @@ export {
222222
resolveChatWrapper,
223223
type BuiltInChatWrapperType,
224224
type ResolveChatWrapperOptions,
225+
type ResolveChatWrapperWithModelOptions,
225226
resolvableChatWrapperTypeNames,
226227
type ResolvableChatWrapperTypeName,
227228
specializedChatWrapperTypeNames,
@@ -250,6 +251,7 @@ export {
250251
type ChatHistoryItem,
251252
type ChatModelFunctionCall,
252253
type ChatModelSegmentType,
254+
type ChatModelSegment,
253255
type ChatModelFunctions,
254256
type ChatModelResponse,
255257
type ChatSessionModelFunction,

0 commit comments

Comments
 (0)