-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathChatWrapper.ts
307 lines (257 loc) · 12.9 KB
/
ChatWrapper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import {
ChatHistoryItem, ChatModelFunctionCall, ChatModelFunctions, ChatModelResponse, ChatModelSegmentType,
ChatWrapperCheckModelCompatibilityParams, ChatWrapperGenerateContextStateOptions, ChatWrapperGeneratedContextState,
ChatWrapperGenerateInitialHistoryOptions, ChatWrapperSettings, isChatModelResponseSegment
} from "./types.js";
import {LlamaText, SpecialTokensText} from "./utils/LlamaText.js";
import {ChatModelFunctionsDocumentationGenerator} from "./chatWrappers/utils/ChatModelFunctionsDocumentationGenerator.js";
import {jsonDumps} from "./chatWrappers/utils/jsonDumps.js";
import {defaultChatSystemPrompt} from "./config.js";
import {getChatWrapperSegmentDefinition} from "./utils/getChatWrapperSegmentDefinition.js";
import type {JinjaTemplateChatWrapperOptions} from "./chatWrappers/generic/JinjaTemplateChatWrapper.js";
export abstract class ChatWrapper {
public static defaultSettings: ChatWrapperSettings = {
supportsSystemMessages: true,
functions: {
call: {
optionalPrefixSpace: true,
prefix: "||call: ",
paramsPrefix: LlamaText(new SpecialTokensText("(")),
suffix: LlamaText(new SpecialTokensText(")")),
emptyCallParamsPlaceholder: ""
},
result: {
prefix: LlamaText(new SpecialTokensText("\n"), "||result: "),
suffix: LlamaText(new SpecialTokensText("\n"))
}
},
segments: {}
};
public abstract readonly wrapperName: string;
public readonly settings: ChatWrapperSettings = ChatWrapper.defaultSettings;
public generateContextState({
chatHistory, availableFunctions, documentFunctionParams
}: ChatWrapperGenerateContextStateOptions): ChatWrapperGeneratedContextState {
const historyWithFunctions = this.addAvailableFunctionsSystemMessageToHistory(chatHistory, availableFunctions, {
documentParams: documentFunctionParams
});
const texts = historyWithFunctions
.map((item) => {
if (item.type === "system")
return LlamaText(["system: ", LlamaText.fromJSON(item.text)]);
else if (item.type === "user")
return LlamaText(["user: ", item.text]);
else if (item.type === "model")
return LlamaText(["model: ", this.generateModelResponseText(item.response)]);
return item satisfies never;
});
return {
contextText: LlamaText.joinValues("\n", texts),
stopGenerationTriggers: []
};
}
public generateFunctionCallsAndResults(functionCalls: ChatModelFunctionCall[], useRawCall: boolean = true) {
const calls: LlamaText[] = [];
const results: LlamaText[] = [];
const res: LlamaText[] = [];
if (functionCalls.length === 0)
return LlamaText([]);
for (const functionCall of functionCalls) {
if (useRawCall && functionCall.rawCall != null)
calls.push(LlamaText.fromJSON(functionCall.rawCall));
else
calls.push(this.generateFunctionCall(functionCall.name, functionCall.params));
results.push(this.generateFunctionCallResult(functionCall.name, functionCall.params, functionCall.result));
}
if (this.settings.functions.parallelism == null) {
for (let i = 0; i < calls.length; i++) {
res.push(calls[i]!);
res.push(results[i]!);
}
return LlamaText(res);
}
res.push(LlamaText(this.settings.functions.parallelism.call.sectionPrefix ?? ""));
for (let i = 0; i < calls.length; i++) {
if (i > 0)
res.push(LlamaText(this.settings.functions.parallelism.call.betweenCalls ?? ""));
res.push(calls[i]!);
}
res.push(LlamaText(this.settings.functions.parallelism.call.sectionSuffix ?? ""));
res.push(LlamaText(this.settings.functions.parallelism.result?.sectionPrefix ?? ""));
for (let i = 0; i < results.length; i++) {
if (i > 0)
res.push(LlamaText(this.settings.functions.parallelism.result?.betweenResults ?? ""));
res.push(results[i]!);
}
res.push(LlamaText(this.settings.functions.parallelism.result?.sectionSuffix ?? ""));
return LlamaText(res);
}
public generateFunctionCall(name: string, params: any): LlamaText {
const emptyCallParamsPlaceholder = this.settings.functions.call.emptyCallParamsPlaceholder;
return LlamaText([
this.settings.functions.call.prefix,
name,
this.settings.functions.call.paramsPrefix,
(
params === undefined
? (emptyCallParamsPlaceholder === undefined || emptyCallParamsPlaceholder === "")
? ""
: jsonDumps(emptyCallParamsPlaceholder)
: jsonDumps(params)
),
this.settings.functions.call.suffix
]);
}
public generateFunctionCallResult(functionName: string, functionParams: any, result: any): LlamaText {
function resolveParameters(text: string | LlamaText) {
return LlamaText(text)
.mapValues((value) => {
if (typeof value !== "string")
return value;
return value
.replaceAll("{{functionName}}", functionName)
.replaceAll("{{functionParams}}", functionParams === undefined ? "" : jsonDumps(functionParams));
});
}
return LlamaText([
resolveParameters(this.settings.functions.result.prefix),
(
result === undefined
? "void"
: jsonDumps(result)
),
resolveParameters(this.settings.functions.result.suffix)
]);
}
public generateModelResponseText(modelResponse: ChatModelResponse["response"], useRawValues: boolean = true): LlamaText {
const res: LlamaText[] = [];
const pendingFunctionCalls: ChatModelFunctionCall[] = [];
const segmentStack: ChatModelSegmentType[] = [];
let lastSegmentEndedWithoutSuffix: boolean = false;
let needsToAddSegmentReminder = false;
const addFunctionCalls = () => {
if (pendingFunctionCalls.length === 0)
return;
res.push(this.generateFunctionCallsAndResults(pendingFunctionCalls, useRawValues));
pendingFunctionCalls.length = 0;
needsToAddSegmentReminder = true;
};
const addSegmentReminderIfNeeded = () => {
if (lastSegmentEndedWithoutSuffix && segmentStack.length === 0 && this.settings.segments?.closeAllSegments != null) {
lastSegmentEndedWithoutSuffix = false;
res.push(LlamaText(this.settings.segments.closeAllSegments));
} else if (needsToAddSegmentReminder && segmentStack.length > 0 && this.settings.segments?.reiterateStackAfterFunctionCalls) {
for (const segmentType of segmentStack) {
const segmentDefinition = getChatWrapperSegmentDefinition(this.settings, segmentType);
if (segmentDefinition == null)
continue;
res.push(LlamaText(segmentDefinition.prefix));
}
}
};
for (const response of modelResponse) {
if (typeof response === "string") {
addFunctionCalls();
addSegmentReminderIfNeeded();
res.push(LlamaText(response));
continue;
} else if (isChatModelResponseSegment(response)) {
addFunctionCalls();
const segmentDefinition = getChatWrapperSegmentDefinition(this.settings, response.segmentType);
if (response.raw != null && useRawValues)
res.push(LlamaText.fromJSON(response.raw));
else
res.push(
LlamaText([
(segmentStack.length > 0 && segmentStack.at(-1) === response.segmentType)
? ""
: segmentDefinition?.prefix ?? "",
response.text,
response.ended
? (segmentDefinition?.suffix ?? "")
: ""
])
);
lastSegmentEndedWithoutSuffix = response.ended && segmentDefinition?.suffix == null;
if (!response.ended && segmentStack.at(-1) !== response.segmentType)
segmentStack.push(response.segmentType);
else if (response.ended && segmentStack.at(-1) === response.segmentType) {
segmentStack.pop();
if (segmentStack.length === 0 && segmentDefinition?.suffix == null && this.settings.segments?.closeAllSegments != null)
res.push(LlamaText(this.settings.segments.closeAllSegments));
}
continue;
}
if (response.startsNewChunk)
addFunctionCalls();
pendingFunctionCalls.push(response);
}
addFunctionCalls();
addSegmentReminderIfNeeded();
return LlamaText(res);
}
public generateAvailableFunctionsSystemText(availableFunctions: ChatModelFunctions, {documentParams = true}: {
documentParams?: boolean
}): LlamaText {
const functionsDocumentationGenerator = new ChatModelFunctionsDocumentationGenerator(availableFunctions);
if (!functionsDocumentationGenerator.hasAnyFunctions)
return LlamaText([]);
return LlamaText.joinValues("\n", [
"The assistant calls the provided functions as needed to retrieve information instead of relying on existing knowledge.",
"To fulfill a request, the assistant calls relevant functions in advance when needed before responding to the request, and does not tell the user prior to calling a function.",
"Provided functions:",
"```typescript",
functionsDocumentationGenerator.getTypeScriptFunctionSignatures({documentParams}),
"```",
"",
"Calling any of the provided functions can be done like this:",
this.generateFunctionCall("getSomeInfo", {someKey: "someValue"}),
"",
"Note that the || prefix is mandatory.",
"The assistant does not inform the user about using functions and does not explain anything before calling a function.",
"After calling a function, the raw result appears afterwards and is not part of the conversation.",
"To make information be part of the conversation, the assistant paraphrases and repeats the information without the function syntax."
]);
}
public addAvailableFunctionsSystemMessageToHistory(history: readonly ChatHistoryItem[], availableFunctions?: ChatModelFunctions, {
documentParams = true
}: {
documentParams?: boolean
} = {}) {
const availableFunctionNames = Object.keys(availableFunctions ?? {});
if (availableFunctions == null || availableFunctionNames.length === 0)
return history;
const res = history.slice();
const firstNonSystemMessageIndex = res.findIndex((item) => item.type !== "system");
res.splice(Math.max(0, firstNonSystemMessageIndex), 0, {
type: "system",
text: this.generateAvailableFunctionsSystemText(availableFunctions, {documentParams}).toJSON()
});
return res;
}
public generateInitialChatHistory({
systemPrompt = defaultChatSystemPrompt
}: ChatWrapperGenerateInitialHistoryOptions = {}): ChatHistoryItem[] {
return [{
type: "system",
text: LlamaText(systemPrompt ?? defaultChatSystemPrompt).toJSON()
}];
}
/** @internal */
public static _getOptionConfigurationsToTestIfCanSupersedeJinjaTemplate(): ChatWrapperJinjaMatchConfiguration<typeof this> {
return [{}] satisfies ChatWrapperJinjaMatchConfiguration<typeof this>;
}
/** @internal */
public static _checkModelCompatibility(options: ChatWrapperCheckModelCompatibilityParams): boolean {
return true;
}
}
type FirstItemOfTupleOrFallback<T extends any[], Fallback> = T extends [infer U, ...any[]] ? U : Fallback;
export type ChatWrapperJinjaMatchConfiguration<T extends typeof ChatWrapper> = Array<
FirstItemOfTupleOrFallback<ConstructorParameters<T>, object> |
[
testConfig: FirstItemOfTupleOrFallback<ConstructorParameters<T>, object>,
applyConfig: FirstItemOfTupleOrFallback<ConstructorParameters<T>, object>,
testJinjaChatWrapperOptions?: JinjaTemplateChatWrapperOptions
]
>;