First upload version 0.0.1
This commit is contained in:
7
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/UniqueIdGenerator.d.ts
generated
vendored
Normal file
7
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/UniqueIdGenerator.d.ts
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
export declare class UniqueIdGenerator {
|
||||
readonly antiText: string;
|
||||
private readonly _ids;
|
||||
constructor(antiText: string);
|
||||
generateId(numbersOnly?: boolean): string;
|
||||
removeId(id: string): void;
|
||||
}
|
||||
30
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/UniqueIdGenerator.js
generated
vendored
Normal file
30
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/UniqueIdGenerator.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
export class UniqueIdGenerator {
|
||||
antiText;
|
||||
_ids = new Set();
|
||||
constructor(antiText) {
|
||||
this.antiText = antiText;
|
||||
}
|
||||
generateId(numbersOnly = false) {
|
||||
let id;
|
||||
do {
|
||||
if (numbersOnly) {
|
||||
do {
|
||||
id = (Math.random()
|
||||
.toString(10)
|
||||
.slice(2)
|
||||
.slice(0, String(Number.MAX_SAFE_INTEGER).length - 1));
|
||||
} while (id.startsWith("0"));
|
||||
}
|
||||
else
|
||||
id = "W" + (Math.random()
|
||||
.toString(36)
|
||||
.slice(2)) + "W";
|
||||
} while (this._ids.has(id) || this.antiText.includes(id));
|
||||
this._ids.add(id);
|
||||
return id;
|
||||
}
|
||||
removeId(id) {
|
||||
this._ids.delete(id);
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=UniqueIdGenerator.js.map
|
||||
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/UniqueIdGenerator.js.map
generated
vendored
Normal file
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/UniqueIdGenerator.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"UniqueIdGenerator.js","sourceRoot":"","sources":["../../../../src/chatWrappers/generic/utils/UniqueIdGenerator.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,iBAAiB;IACV,QAAQ,CAAS;IAChB,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAE1C,YAAmB,QAAgB;QAC/B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC7B,CAAC;IAEM,UAAU,CAAC,cAAuB,KAAK;QAC1C,IAAI,EAAU,CAAC;QAEf,GAAG,CAAC;YACA,IAAI,WAAW,EAAE,CAAC;gBACd,GAAG,CAAC;oBACA,EAAE,GAAG,CACD,IAAI,CAAC,MAAM,EAAE;yBACR,QAAQ,CAAC,EAAE,CAAC;yBACZ,KAAK,CAAC,CAAC,CAAC;yBACR,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAC5D,CAAC;gBACN,CAAC,QAAQ,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;YACjC,CAAC;;gBACG,EAAE,GAAG,GAAG,GAAG,CACP,IAAI,CAAC,MAAM,EAAE;qBACR,QAAQ,CAAC,EAAE,CAAC;qBACZ,KAAK,CAAC,CAAC,CAAC,CAChB,GAAG,GAAG,CAAC;QAChB,CAAC,QAAQ,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE;QAE1D,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAElB,OAAO,EAAE,CAAC;IACd,CAAC;IAEM,QAAQ,CAAC,EAAU;QACtB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACzB,CAAC;CACJ"}
|
||||
24
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/chatHistoryFunctionCallMessageTemplate.d.ts
generated
vendored
Normal file
24
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/chatHistoryFunctionCallMessageTemplate.d.ts
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
import { ChatWrapperSettings } from "../../../types.js";
|
||||
export declare function parseFunctionCallMessageTemplate(template?: ChatHistoryFunctionCallMessageTemplate): ChatWrapperSettings["functions"] | null;
|
||||
/**
|
||||
* Template format for how functions can be called by the model and how their results are fed to the model after function calls.
|
||||
*
|
||||
* Consists of an object with two properties:
|
||||
* 1. **`call`**: The function call template.
|
||||
* 2. **`result`**: The function call result template.
|
||||
*
|
||||
* For example:
|
||||
* ```ts
|
||||
* const template: ChatHistoryFunctionCallMessageTemplate = {
|
||||
* call: "[[call: {{functionName}}({{functionParams}})]]",
|
||||
* result: " [[result: {{functionCallResult}}]]"
|
||||
* };
|
||||
* ```
|
||||
*
|
||||
* It's mandatory for the call template to have text before <span v-pre>`{{functionName}}`</span> in order for the chat wrapper know when
|
||||
* to activate the function calling grammar.
|
||||
*/
|
||||
export type ChatHistoryFunctionCallMessageTemplate = {
|
||||
call: `${string}{{functionName}}${string}{{functionParams}}${string}`;
|
||||
result: `${string}{{functionCallResult}}${string}`;
|
||||
};
|
||||
45
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/chatHistoryFunctionCallMessageTemplate.js
generated
vendored
Normal file
45
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/chatHistoryFunctionCallMessageTemplate.js
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
import { parseTextTemplate } from "../../../utils/parseTextTemplate.js";
|
||||
export function parseFunctionCallMessageTemplate(template) {
|
||||
if (template == null)
|
||||
return null;
|
||||
const { call: functionCallTemplate, result: functionCallResultTemplate } = template;
|
||||
if (functionCallTemplate == null || functionCallResultTemplate == null)
|
||||
throw new Error("Both function call and function call result templates are required");
|
||||
const parsedFunctionCallTemplate = parseTextTemplate(functionCallTemplate, [{
|
||||
text: "{{functionName}}",
|
||||
key: "functionName"
|
||||
}, {
|
||||
text: "{{functionParams}}",
|
||||
key: "functionParams"
|
||||
}]);
|
||||
const parsedFunctionCallResultTemplate = parseTextTemplate(functionCallResultTemplate, [{
|
||||
text: "{{functionCallResult}}",
|
||||
key: "functionCallResult"
|
||||
}]);
|
||||
const callPrefix = parsedFunctionCallTemplate.functionName.prefix;
|
||||
const callParamsPrefix = parsedFunctionCallTemplate.functionParams.prefix;
|
||||
const callSuffix = parsedFunctionCallTemplate.functionParams.suffix;
|
||||
const resultPrefix = parsedFunctionCallResultTemplate.functionCallResult.prefix;
|
||||
const resultSuffix = parsedFunctionCallResultTemplate.functionCallResult.suffix;
|
||||
if (callPrefix.length === 0)
|
||||
throw new Error("Function call template must have text before \"{{functionName}}\"");
|
||||
if (callSuffix.length === 0)
|
||||
throw new Error("Function call template must have text after \"{{functionParams}}\"");
|
||||
if (resultPrefix.length === 0)
|
||||
throw new Error("Function call result template must have text before \"{{functionCallResult}}\"");
|
||||
if (resultSuffix.length === 0)
|
||||
throw new Error("Function call result template must have text after \"{{functionCallResult}}\"");
|
||||
return {
|
||||
call: {
|
||||
optionalPrefixSpace: true,
|
||||
prefix: callPrefix,
|
||||
paramsPrefix: callParamsPrefix,
|
||||
suffix: callSuffix
|
||||
},
|
||||
result: {
|
||||
prefix: resultPrefix,
|
||||
suffix: resultSuffix
|
||||
}
|
||||
};
|
||||
}
|
||||
//# sourceMappingURL=chatHistoryFunctionCallMessageTemplate.js.map
|
||||
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/chatHistoryFunctionCallMessageTemplate.js.map
generated
vendored
Normal file
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/chatHistoryFunctionCallMessageTemplate.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"chatHistoryFunctionCallMessageTemplate.js","sourceRoot":"","sources":["../../../../src/chatWrappers/generic/utils/chatHistoryFunctionCallMessageTemplate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,qCAAqC,CAAC;AAGtE,MAAM,UAAU,gC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,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,+EAA+E,CAAC,CAAC;IAErG,OAAO;QACH,IAAI,EAAE;YACF,mBAAmB,EAAE,IAAI;YACzB,MAAM,EAAE,UAAU;YAClB,YAAY,EAAE,gBAAgB;YAC9B,MAAM,EAAE,UAAU;SACrB;QACD,MAAM,EAAE;YACJ,MAAM,EAAE,YAAY;YACpB,MAAM,EAAE,YAAY;SACvB;KACJ,CAAC;AACN,CAAC"}
|
||||
19
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/extractFunctionCallSettingsFromJinjaTemplate.d.ts
generated
vendored
Normal file
19
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/extractFunctionCallSettingsFromJinjaTemplate.d.ts
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
import { ChatHistoryItem, ChatModelFunctions, ChatWrapperSettings } from "../../../types.js";
|
||||
import { UniqueIdGenerator } from "./UniqueIdGenerator.js";
|
||||
export declare function extractFunctionCallSettingsFromJinjaTemplate({ idsGenerator, renderTemplate }: {
|
||||
idsGenerator: UniqueIdGenerator;
|
||||
renderTemplate({}: {
|
||||
chatHistory: ChatHistoryItem[];
|
||||
functions: ChatModelFunctions;
|
||||
additionalParams: Record<string, unknown>;
|
||||
stringifyFunctionParams: boolean;
|
||||
stringifyFunctionResults: boolean;
|
||||
combineModelMessageAndToolCalls: boolean;
|
||||
squashModelTextResponses?: boolean;
|
||||
}): string;
|
||||
}): {
|
||||
settings: ChatWrapperSettings["functions"] | null;
|
||||
stringifyParams: boolean;
|
||||
stringifyResult: boolean;
|
||||
combineModelMessageAndToolCalls: boolean;
|
||||
};
|
||||
521
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/extractFunctionCallSettingsFromJinjaTemplate.js
generated
vendored
Normal file
521
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/extractFunctionCallSettingsFromJinjaTemplate.js
generated
vendored
Normal file
@@ -0,0 +1,521 @@
|
||||
import { splitText } from "lifecycle-utils";
|
||||
import { LlamaText, SpecialToken, SpecialTokensText } from "../../../utils/LlamaText.js";
|
||||
import { getFirstValidResult } from "./getFirstValidResult.js";
|
||||
export function extractFunctionCallSettingsFromJinjaTemplate({ idsGenerator, renderTemplate }) {
|
||||
const idToStaticContent = new Map();
|
||||
const bosTokenId = idsGenerator.generateId();
|
||||
const eosTokenId = idsGenerator.generateId();
|
||||
const eotTokenId = idsGenerator.generateId();
|
||||
idToStaticContent.set(bosTokenId, new SpecialToken("BOS"));
|
||||
idToStaticContent.set(eosTokenId, new SpecialToken("EOS"));
|
||||
idToStaticContent.set(eotTokenId, new SpecialToken("EOT"));
|
||||
const contentIds = new Set();
|
||||
const addContentId = (id) => {
|
||||
contentIds.add(id);
|
||||
return id;
|
||||
};
|
||||
const systemMessage = addContentId(idsGenerator.generateId());
|
||||
const userMessage1 = addContentId(idsGenerator.generateId());
|
||||
const modelMessage1 = addContentId(idsGenerator.generateId());
|
||||
const func1name = addContentId(idsGenerator.generateId());
|
||||
const func1description = addContentId(idsGenerator.generateId());
|
||||
const func1params = addContentId(idsGenerator.generateId(true));
|
||||
const func1result = addContentId(idsGenerator.generateId(true));
|
||||
const func2name = addContentId(idsGenerator.generateId());
|
||||
const func2description = addContentId(idsGenerator.generateId());
|
||||
const func2params = addContentId(idsGenerator.generateId(true));
|
||||
const func2result = addContentId(idsGenerator.generateId(true));
|
||||
const modelMessage2 = addContentId(idsGenerator.generateId());
|
||||
const func1StringifyParam = addContentId(idsGenerator.generateId());
|
||||
const func1StringifyResult = addContentId(idsGenerator.generateId());
|
||||
const functions1 = {
|
||||
[func1name]: {
|
||||
description: func1description,
|
||||
params: {
|
||||
type: "number"
|
||||
}
|
||||
}
|
||||
};
|
||||
const functions2 = {
|
||||
...functions1,
|
||||
[func2name]: {
|
||||
description: func2description,
|
||||
params: {
|
||||
type: "number"
|
||||
}
|
||||
}
|
||||
};
|
||||
const baseChatHistory = [{
|
||||
type: "system",
|
||||
text: systemMessage
|
||||
}, {
|
||||
type: "user",
|
||||
text: userMessage1
|
||||
}];
|
||||
const chatHistory1Call = [...baseChatHistory, {
|
||||
type: "model",
|
||||
response: [
|
||||
modelMessage1,
|
||||
{
|
||||
type: "functionCall",
|
||||
name: func1name,
|
||||
// convert to number since this will go through JSON.stringify,
|
||||
// and we want to avoid escaping characters in the rendered output
|
||||
params: Number(func1params),
|
||||
result: Number(func1result),
|
||||
startsNewChunk: true
|
||||
},
|
||||
modelMessage2
|
||||
]
|
||||
}];
|
||||
const chatHistoryOnlyCall = [...baseChatHistory, {
|
||||
type: "model",
|
||||
response: [
|
||||
{
|
||||
type: "functionCall",
|
||||
name: func1name,
|
||||
// convert to number since this will go through JSON.stringify,
|
||||
// and we want to avoid escaping characters in the rendered output
|
||||
params: Number(func1params),
|
||||
result: Number(func1result),
|
||||
startsNewChunk: true
|
||||
},
|
||||
modelMessage2
|
||||
]
|
||||
}];
|
||||
const chatHistory2Calls = [...baseChatHistory, {
|
||||
type: "model",
|
||||
response: [
|
||||
modelMessage1,
|
||||
{
|
||||
type: "functionCall",
|
||||
name: func1name,
|
||||
// convert to number since this will go through JSON.stringify,
|
||||
// and we want to avoid escaping characters in the rendered output
|
||||
params: Number(func1params),
|
||||
result: Number(func1result),
|
||||
startsNewChunk: true
|
||||
},
|
||||
{
|
||||
type: "functionCall",
|
||||
name: func2name,
|
||||
params: Number(func2params),
|
||||
result: Number(func2result),
|
||||
startsNewChunk: false
|
||||
},
|
||||
modelMessage2
|
||||
]
|
||||
}];
|
||||
const chatHistory2CallsNewChunk = [...baseChatHistory, {
|
||||
type: "model",
|
||||
response: [
|
||||
modelMessage1,
|
||||
{
|
||||
type: "functionCall",
|
||||
name: func1name,
|
||||
// convert to number since this will go through JSON.stringify,
|
||||
// and we want to avoid escaping characters in the rendered output
|
||||
params: Number(func1params),
|
||||
result: Number(func1result),
|
||||
startsNewChunk: true
|
||||
},
|
||||
{
|
||||
type: "functionCall",
|
||||
name: func2name,
|
||||
params: Number(func2params),
|
||||
result: Number(func2result),
|
||||
startsNewChunk: true
|
||||
},
|
||||
modelMessage2
|
||||
]
|
||||
}];
|
||||
const additionalParams = {
|
||||
"bos_token": bosTokenId,
|
||||
"eos_token": eosTokenId,
|
||||
"eot_token": eotTokenId
|
||||
};
|
||||
let combineModelMessageAndToolCalls = true;
|
||||
let stringifyParams = true;
|
||||
let stringifyResult = true;
|
||||
try {
|
||||
const paramsObjectTest = renderTemplate({
|
||||
chatHistory: [...baseChatHistory, {
|
||||
type: "model",
|
||||
response: [
|
||||
modelMessage1,
|
||||
{
|
||||
type: "functionCall",
|
||||
name: func1name,
|
||||
params: { [func1StringifyParam]: "test" },
|
||||
result: func1StringifyResult,
|
||||
startsNewChunk: true
|
||||
},
|
||||
modelMessage2
|
||||
]
|
||||
}],
|
||||
functions: functions1,
|
||||
additionalParams,
|
||||
stringifyFunctionParams: false,
|
||||
stringifyFunctionResults: false,
|
||||
combineModelMessageAndToolCalls
|
||||
});
|
||||
stringifyParams = (!paramsObjectTest.includes(`"${func1StringifyParam}":`) &&
|
||||
!paramsObjectTest.includes(`'${func1StringifyParam}':`));
|
||||
}
|
||||
catch (err) {
|
||||
// do nothing
|
||||
}
|
||||
try {
|
||||
const resultObjectTest = renderTemplate({
|
||||
chatHistory: [...baseChatHistory, {
|
||||
type: "model",
|
||||
response: [
|
||||
modelMessage1,
|
||||
{
|
||||
type: "functionCall",
|
||||
name: func1name,
|
||||
params: func1StringifyParam,
|
||||
result: { [func1StringifyResult]: "test" },
|
||||
startsNewChunk: true
|
||||
},
|
||||
modelMessage2
|
||||
]
|
||||
}],
|
||||
functions: functions1,
|
||||
additionalParams,
|
||||
stringifyFunctionParams: false,
|
||||
stringifyFunctionResults: false,
|
||||
combineModelMessageAndToolCalls
|
||||
});
|
||||
stringifyResult = (!resultObjectTest.includes(`"${func1StringifyResult}":`) &&
|
||||
!resultObjectTest.includes(`'${func1StringifyResult}':`));
|
||||
}
|
||||
catch (err) {
|
||||
// do nothing
|
||||
}
|
||||
combineModelMessageAndToolCalls = renderTemplate({
|
||||
chatHistory: chatHistory1Call,
|
||||
functions: functions1,
|
||||
additionalParams,
|
||||
stringifyFunctionParams: true,
|
||||
stringifyFunctionResults: true,
|
||||
combineModelMessageAndToolCalls
|
||||
}).includes(modelMessage1);
|
||||
let textBetween2TextualModelResponses = LlamaText();
|
||||
if (!combineModelMessageAndToolCalls) {
|
||||
try {
|
||||
const betweenModelTextualResponsesTest = renderTemplate({
|
||||
chatHistory: [...baseChatHistory, {
|
||||
type: "model",
|
||||
response: [modelMessage1]
|
||||
}, {
|
||||
type: "model",
|
||||
response: [modelMessage2]
|
||||
}],
|
||||
functions: {},
|
||||
additionalParams,
|
||||
stringifyFunctionParams: false,
|
||||
stringifyFunctionResults: false,
|
||||
combineModelMessageAndToolCalls,
|
||||
squashModelTextResponses: false
|
||||
});
|
||||
const textDiff = getTextBetweenIds(betweenModelTextualResponsesTest, modelMessage1, modelMessage2).text ?? "";
|
||||
textBetween2TextualModelResponses = reviveSeparatorText(textDiff, idToStaticContent, contentIds);
|
||||
}
|
||||
catch (err) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
let usedNewChunkFor2Calls = false;
|
||||
const rendered1Call = renderTemplate({
|
||||
chatHistory: chatHistory1Call,
|
||||
functions: functions1,
|
||||
additionalParams,
|
||||
stringifyFunctionParams: stringifyParams,
|
||||
stringifyFunctionResults: stringifyResult,
|
||||
combineModelMessageAndToolCalls
|
||||
});
|
||||
const renderedOnlyCall = getFirstValidResult([
|
||||
() => renderTemplate({
|
||||
chatHistory: chatHistoryOnlyCall,
|
||||
functions: functions1,
|
||||
additionalParams,
|
||||
stringifyFunctionParams: stringifyParams,
|
||||
stringifyFunctionResults: stringifyResult,
|
||||
combineModelMessageAndToolCalls
|
||||
}),
|
||||
() => undefined
|
||||
]);
|
||||
const rendered2Calls = getFirstValidResult([
|
||||
() => renderTemplate({
|
||||
chatHistory: chatHistory2Calls,
|
||||
functions: functions2,
|
||||
additionalParams,
|
||||
stringifyFunctionParams: stringifyParams,
|
||||
stringifyFunctionResults: stringifyResult,
|
||||
combineModelMessageAndToolCalls
|
||||
}),
|
||||
() => {
|
||||
usedNewChunkFor2Calls = true;
|
||||
return renderTemplate({
|
||||
chatHistory: chatHistory2CallsNewChunk,
|
||||
functions: functions2,
|
||||
additionalParams,
|
||||
stringifyFunctionParams: stringifyParams,
|
||||
stringifyFunctionResults: stringifyResult,
|
||||
combineModelMessageAndToolCalls
|
||||
});
|
||||
}
|
||||
]);
|
||||
const modelMessage1ToFunc1Name = getTextBetweenIds(rendered2Calls, modelMessage1, func1name);
|
||||
const func1NameToFunc1Params = getTextBetweenIds(rendered2Calls, func1name, func1params, modelMessage1ToFunc1Name.endIndex);
|
||||
const func1ResultIndex = rendered2Calls.indexOf(func1result, func1NameToFunc1Params.endIndex);
|
||||
const func2NameIndex = rendered2Calls.indexOf(func2name, modelMessage1ToFunc1Name.endIndex);
|
||||
if (modelMessage1ToFunc1Name.text == null ||
|
||||
func1NameToFunc1Params.text == null ||
|
||||
func1ResultIndex < 0 ||
|
||||
func2NameIndex < 0)
|
||||
return { settings: null, stringifyParams, stringifyResult, combineModelMessageAndToolCalls };
|
||||
const supportsParallelCalls = func1ResultIndex > func2NameIndex;
|
||||
if (!supportsParallelCalls || usedNewChunkFor2Calls) {
|
||||
const prefix = getTextBetweenIds(rendered1Call, modelMessage1, func1name);
|
||||
const paramsPrefix = getTextBetweenIds(rendered1Call, func1name, func1params, prefix.endIndex);
|
||||
const resultPrefix = getTextBetweenIds(rendered1Call, func1params, func1result, paramsPrefix.endIndex);
|
||||
const resultSuffix = getTextBetweenIds(rendered1Call, func1result, modelMessage2, resultPrefix.endIndex);
|
||||
if (prefix.text == null || prefix.text === "" || paramsPrefix.text == null || resultPrefix.text == null || resultSuffix.text == null)
|
||||
return { settings: null, stringifyParams, stringifyResult, combineModelMessageAndToolCalls };
|
||||
return {
|
||||
stringifyParams,
|
||||
stringifyResult,
|
||||
combineModelMessageAndToolCalls,
|
||||
settings: {
|
||||
call: {
|
||||
optionalPrefixSpace: true,
|
||||
prefix: removeCommonRevivedPrefix(reviveSeparatorText(prefix.text, idToStaticContent, contentIds), !combineModelMessageAndToolCalls
|
||||
? textBetween2TextualModelResponses
|
||||
: LlamaText()),
|
||||
paramsPrefix: reviveSeparatorText(paramsPrefix.text, idToStaticContent, contentIds),
|
||||
suffix: "",
|
||||
emptyCallParamsPlaceholder: {}
|
||||
},
|
||||
result: {
|
||||
prefix: reviveSeparatorText(resultPrefix.text, new Map([
|
||||
...idToStaticContent.entries(),
|
||||
[func1name, LlamaText("{{functionName}}")],
|
||||
[func1params, LlamaText("{{functionParams}}")]
|
||||
]), contentIds),
|
||||
suffix: reviveSeparatorText(resultSuffix.text, new Map([
|
||||
...idToStaticContent.entries(),
|
||||
[func1name, LlamaText("{{functionName}}")],
|
||||
[func1params, LlamaText("{{functionParams}}")]
|
||||
]), contentIds)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
const func1ParamsToFunc2Name = getTextBetweenIds(rendered2Calls, func1params, func2name, func1NameToFunc1Params.endIndex);
|
||||
const func2ParamsToFunc1Result = getTextBetweenIds(rendered2Calls, func2params, func1result, func1ParamsToFunc2Name.endIndex);
|
||||
const func1ResultToFunc2Result = getTextBetweenIds(rendered2Calls, func1result, func2result, func2ParamsToFunc1Result.endIndex);
|
||||
const func2ResultToModelMessage2 = getTextBetweenIds(rendered2Calls, func2result, modelMessage2, func1ResultToFunc2Result.endIndex);
|
||||
if (func1ParamsToFunc2Name.text == null || func2ParamsToFunc1Result.text == null || func1ResultToFunc2Result.text == null ||
|
||||
func2ResultToModelMessage2.text == null)
|
||||
return { settings: null, stringifyParams, stringifyResult, combineModelMessageAndToolCalls };
|
||||
const callPrefixLength = findCommonEndLength(modelMessage1ToFunc1Name.text, func1ParamsToFunc2Name.text);
|
||||
const callPrefixText = func1ParamsToFunc2Name.text.slice(-callPrefixLength);
|
||||
const parallelismCallPrefix = modelMessage1ToFunc1Name.text.slice(0, -callPrefixLength);
|
||||
const callSuffixLength = findCommandStartLength(func1ParamsToFunc2Name.text, func2ParamsToFunc1Result.text);
|
||||
const callSuffixText = func1ParamsToFunc2Name.text.slice(0, callSuffixLength);
|
||||
const parallelismBetweenCallsText = func1ParamsToFunc2Name.text.slice(callSuffixLength, -callPrefixLength);
|
||||
const callParamsPrefixText = func1NameToFunc1Params.text;
|
||||
const resultPrefixLength = findCommonEndLength(func2ParamsToFunc1Result.text, func1ResultToFunc2Result.text);
|
||||
const resultPrefixText = func2ParamsToFunc1Result.text.slice(-resultPrefixLength);
|
||||
const resultSuffixLength = findCommandStartLength(func1ResultToFunc2Result.text, func2ResultToModelMessage2.text);
|
||||
const resultSuffixText = func1ResultToFunc2Result.text.slice(0, resultSuffixLength);
|
||||
const parallelismResultBetweenResultsText = func1ResultToFunc2Result.text.slice(resultSuffixLength, -resultPrefixLength);
|
||||
const parallelismResultSuffixText = func2ResultToModelMessage2.text.slice(resultSuffixLength);
|
||||
const resolveParallelismBetweenSectionsParts = (betweenSectionsText) => {
|
||||
const { index: endTokenIndex, text: endTokenId } = findFirstTextMatch(betweenSectionsText, [eosTokenId, eosTokenId]);
|
||||
if (endTokenIndex >= 0 && endTokenId != null)
|
||||
return {
|
||||
parallelismCallSuffixText: betweenSectionsText.slice(0, endTokenIndex + endTokenId.length),
|
||||
parallelismResultPrefix: betweenSectionsText.slice(endTokenIndex + endTokenId.length)
|
||||
};
|
||||
const bosIndex = betweenSectionsText.indexOf(bosTokenId);
|
||||
if (bosIndex >= 0)
|
||||
return {
|
||||
parallelismCallSuffixText: betweenSectionsText.slice(0, bosIndex),
|
||||
parallelismResultPrefix: betweenSectionsText.slice(bosIndex)
|
||||
};
|
||||
return {
|
||||
parallelismCallSuffixText: betweenSectionsText,
|
||||
parallelismResultPrefix: ""
|
||||
};
|
||||
};
|
||||
const { parallelismCallSuffixText, parallelismResultPrefix } = resolveParallelismBetweenSectionsParts(func2ParamsToFunc1Result.text.slice(callSuffixLength, -resultPrefixLength));
|
||||
let revivedCallPrefix = reviveSeparatorText(callPrefixText, idToStaticContent, contentIds);
|
||||
const revivedParallelismCallSectionPrefix = removeCommonRevivedPrefix(reviveSeparatorText(parallelismCallPrefix, idToStaticContent, contentIds), !combineModelMessageAndToolCalls
|
||||
? textBetween2TextualModelResponses
|
||||
: LlamaText());
|
||||
let revivedParallelismCallBetweenCalls = reviveSeparatorText(parallelismBetweenCallsText, idToStaticContent, contentIds);
|
||||
if (revivedParallelismCallSectionPrefix.values.length === 0 && renderedOnlyCall != null) {
|
||||
const userMessage1ToModelMessage1Start = getTextBetweenIds(rendered1Call, userMessage1, modelMessage1);
|
||||
const onlyCallUserMessage1ToFunc1Name = getTextBetweenIds(renderedOnlyCall, userMessage1, func1name);
|
||||
if (userMessage1ToModelMessage1Start.text != null && onlyCallUserMessage1ToFunc1Name.text != null) {
|
||||
const onlyCallModelMessagePrefixLength = findCommandStartLength(userMessage1ToModelMessage1Start.text, onlyCallUserMessage1ToFunc1Name.text);
|
||||
const onlyCallCallPrefixText = onlyCallUserMessage1ToFunc1Name.text.slice(onlyCallModelMessagePrefixLength);
|
||||
const revivedOnlyCallCallPrefixText = reviveSeparatorText(onlyCallCallPrefixText, idToStaticContent, contentIds);
|
||||
const optionalCallPrefix = removeCommonRevivedSuffix(revivedCallPrefix, revivedOnlyCallCallPrefixText);
|
||||
if (optionalCallPrefix.values.length > 0) {
|
||||
revivedCallPrefix = removeCommonRevivedPrefix(revivedCallPrefix, optionalCallPrefix);
|
||||
revivedParallelismCallBetweenCalls = LlamaText([
|
||||
optionalCallPrefix,
|
||||
revivedParallelismCallBetweenCalls
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
stringifyParams,
|
||||
stringifyResult,
|
||||
combineModelMessageAndToolCalls,
|
||||
settings: {
|
||||
call: {
|
||||
optionalPrefixSpace: true,
|
||||
prefix: revivedCallPrefix,
|
||||
paramsPrefix: reviveSeparatorText(callParamsPrefixText, idToStaticContent, contentIds),
|
||||
suffix: reviveSeparatorText(callSuffixText, idToStaticContent, contentIds),
|
||||
emptyCallParamsPlaceholder: {}
|
||||
},
|
||||
result: {
|
||||
prefix: reviveSeparatorText(resultPrefixText, new Map([
|
||||
...idToStaticContent.entries(),
|
||||
[func1name, LlamaText("{{functionName}}")],
|
||||
[func1params, LlamaText("{{functionParams}}")]
|
||||
]), contentIds),
|
||||
suffix: reviveSeparatorText(resultSuffixText, new Map([
|
||||
...idToStaticContent.entries(),
|
||||
[func1name, LlamaText("{{functionName}}")],
|
||||
[func1params, LlamaText("{{functionParams}}")]
|
||||
]), contentIds)
|
||||
},
|
||||
parallelism: {
|
||||
call: {
|
||||
sectionPrefix: revivedParallelismCallSectionPrefix,
|
||||
betweenCalls: revivedParallelismCallBetweenCalls,
|
||||
sectionSuffix: reviveSeparatorText(parallelismCallSuffixText, idToStaticContent, contentIds)
|
||||
},
|
||||
result: {
|
||||
sectionPrefix: reviveSeparatorText(parallelismResultPrefix, idToStaticContent, contentIds),
|
||||
betweenResults: reviveSeparatorText(parallelismResultBetweenResultsText, idToStaticContent, contentIds),
|
||||
sectionSuffix: reviveSeparatorText(parallelismResultSuffixText, idToStaticContent, contentIds)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
function getTextBetweenIds(text, startId, endId, startIndex = 0) {
|
||||
const foundStartIndex = text.indexOf(startId, startIndex);
|
||||
if (foundStartIndex < 0)
|
||||
return { text: undefined, endIndex: -1 };
|
||||
const foundEndIndex = text.indexOf(endId, foundStartIndex + startId.length);
|
||||
if (foundEndIndex < 0)
|
||||
return { text: undefined, endIndex: -1 };
|
||||
return {
|
||||
text: text.slice(foundStartIndex + startId.length, foundEndIndex),
|
||||
endIndex: foundEndIndex
|
||||
};
|
||||
}
|
||||
function reviveSeparatorText(text, idMap, contentIds) {
|
||||
return LlamaText(splitText(text, [...new Set([...idMap.keys(), ...contentIds])])
|
||||
.map((item) => {
|
||||
if (typeof item === "string")
|
||||
return new SpecialTokensText(item);
|
||||
const mappedItem = idMap.get(item.separator);
|
||||
if (mappedItem != null)
|
||||
return mappedItem;
|
||||
if (contentIds.has(item.separator))
|
||||
throw new Error("Content ID found in separator text");
|
||||
return new SpecialTokensText(item.separator);
|
||||
}));
|
||||
}
|
||||
function removeCommonRevivedPrefix(target, matchStart) {
|
||||
for (let commonStartLength = 0; commonStartLength < target.values.length && commonStartLength < matchStart.values.length; commonStartLength++) {
|
||||
const targetValue = target.values[commonStartLength];
|
||||
const matchStartValue = matchStart.values[commonStartLength];
|
||||
if (typeof targetValue === "string" && typeof matchStartValue === "string") {
|
||||
if (targetValue === matchStartValue)
|
||||
continue;
|
||||
}
|
||||
else if (targetValue instanceof SpecialTokensText && matchStartValue instanceof SpecialTokensText) {
|
||||
const commonLength = findCommandStartLength(targetValue.value, matchStartValue.value);
|
||||
if (commonLength === targetValue.value.length && commonLength === matchStartValue.value.length)
|
||||
continue;
|
||||
return LlamaText([
|
||||
new SpecialTokensText(targetValue.value.slice(commonLength)),
|
||||
...target.values.slice(commonStartLength + 1)
|
||||
]);
|
||||
}
|
||||
else if (targetValue instanceof SpecialToken && matchStartValue instanceof SpecialToken) {
|
||||
if (targetValue.value === matchStartValue.value)
|
||||
continue;
|
||||
}
|
||||
else if (LlamaText(targetValue ?? "").compare(LlamaText(matchStartValue ?? "")))
|
||||
continue;
|
||||
return LlamaText(target.values.slice(commonStartLength));
|
||||
}
|
||||
return LlamaText(target.values.slice(matchStart.values.length));
|
||||
}
|
||||
function removeCommonRevivedSuffix(target, matchEnd) {
|
||||
for (let commonEndLength = 0; commonEndLength < target.values.length && commonEndLength < matchEnd.values.length; commonEndLength++) {
|
||||
const targetValue = target.values[target.values.length - commonEndLength - 1];
|
||||
const matchEndValue = matchEnd.values[matchEnd.values.length - commonEndLength - 1];
|
||||
if (typeof targetValue === "string" && typeof matchEndValue === "string") {
|
||||
if (targetValue === matchEndValue)
|
||||
continue;
|
||||
}
|
||||
else if (targetValue instanceof SpecialTokensText && matchEndValue instanceof SpecialTokensText) {
|
||||
const commonLength = findCommonEndLength(targetValue.value, matchEndValue.value);
|
||||
if (commonLength === targetValue.value.length && commonLength === matchEndValue.value.length)
|
||||
continue;
|
||||
return LlamaText([
|
||||
...target.values.slice(0, target.values.length - commonEndLength - 1),
|
||||
new SpecialTokensText(targetValue.value.slice(0, targetValue.value.length - commonLength))
|
||||
]);
|
||||
}
|
||||
else if (targetValue instanceof SpecialToken && matchEndValue instanceof SpecialToken) {
|
||||
if (targetValue.value === matchEndValue.value)
|
||||
continue;
|
||||
}
|
||||
else if (LlamaText(targetValue ?? "").compare(LlamaText(matchEndValue ?? "")))
|
||||
continue;
|
||||
return LlamaText(target.values.slice(0, target.values.length - commonEndLength - 1));
|
||||
}
|
||||
return LlamaText(target.values.slice(0, target.values.length - matchEnd.values.length));
|
||||
}
|
||||
function findCommandStartLength(text1, text2) {
|
||||
let commonStartLength = 0;
|
||||
while (commonStartLength < text1.length && commonStartLength < text2.length) {
|
||||
if (text1[commonStartLength] !== text2[commonStartLength])
|
||||
break;
|
||||
commonStartLength++;
|
||||
}
|
||||
return commonStartLength;
|
||||
}
|
||||
function findCommonEndLength(text1, text2) {
|
||||
let commonEndLength = 0;
|
||||
while (commonEndLength < text1.length && commonEndLength < text2.length) {
|
||||
if (text1[text1.length - commonEndLength - 1] !== text2[text2.length - commonEndLength - 1])
|
||||
break;
|
||||
commonEndLength++;
|
||||
}
|
||||
return commonEndLength;
|
||||
}
|
||||
function findFirstTextMatch(text, matchTexts, startIndex = 0) {
|
||||
for (const matchText of matchTexts) {
|
||||
const index = text.indexOf(matchText, startIndex);
|
||||
if (index >= 0)
|
||||
return { index, text: matchText };
|
||||
}
|
||||
return { index: -1, text: undefined };
|
||||
}
|
||||
//# sourceMappingURL=extractFunctionCallSettingsFromJinjaTemplate.js.map
|
||||
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/extractFunctionCallSettingsFromJinjaTemplate.js.map
generated
vendored
Normal file
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/extractFunctionCallSettingsFromJinjaTemplate.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
2
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/extractSegmentSettingsFromTokenizerAndChatTemplate.d.ts
generated
vendored
Normal file
2
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/extractSegmentSettingsFromTokenizerAndChatTemplate.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
import { ChatWrapperSettings, Tokenizer } from "../../../types.js";
|
||||
export declare function extractSegmentSettingsFromTokenizerAndChatTemplate(chatTemplate: string | undefined, tokenizer?: Tokenizer): ChatWrapperSettings["segments"];
|
||||
64
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/extractSegmentSettingsFromTokenizerAndChatTemplate.js
generated
vendored
Normal file
64
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/extractSegmentSettingsFromTokenizerAndChatTemplate.js
generated
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
import { LlamaText, SpecialTokensText } from "../../../utils/LlamaText.js";
|
||||
import { removeUndefinedFields } from "../../../utils/removeNullFields.js";
|
||||
export function extractSegmentSettingsFromTokenizerAndChatTemplate(chatTemplate, tokenizer) {
|
||||
function tryMatchPrefixSuffixPair(tryMatchGroups) {
|
||||
if (chatTemplate != null) {
|
||||
for (const [prefix, suffix] of tryMatchGroups) {
|
||||
if ((hasAll(chatTemplate.replaceAll(prefix + "\\n\\n" + suffix, ""), [
|
||||
prefix + "\\n\\n",
|
||||
"\\n\\n" + suffix
|
||||
])) || (hasAll(chatTemplate.replaceAll(prefix + "\n\n" + suffix, ""), [
|
||||
prefix + "\n\n",
|
||||
"\n\n" + suffix
|
||||
])))
|
||||
return {
|
||||
prefix: LlamaText(new SpecialTokensText(prefix + "\n\n")),
|
||||
suffix: LlamaText(new SpecialTokensText("\n\n" + suffix))
|
||||
};
|
||||
if ((hasAll(chatTemplate.replaceAll(prefix + "\\n" + suffix, ""), [
|
||||
prefix + "\\n",
|
||||
"\\n" + suffix
|
||||
])) || (hasAll(chatTemplate.replaceAll(prefix + "\n" + suffix, ""), [
|
||||
prefix + "\n",
|
||||
"\n" + suffix
|
||||
])))
|
||||
return {
|
||||
prefix: LlamaText(new SpecialTokensText(prefix + "\n")),
|
||||
suffix: LlamaText(new SpecialTokensText("\n" + suffix))
|
||||
};
|
||||
if (chatTemplate.includes(prefix) && chatTemplate.includes(suffix))
|
||||
return {
|
||||
prefix: LlamaText(new SpecialTokensText(prefix)),
|
||||
suffix: LlamaText(new SpecialTokensText(suffix))
|
||||
};
|
||||
}
|
||||
}
|
||||
if (tokenizer != null) {
|
||||
for (const [prefix, suffix] of tryMatchGroups) {
|
||||
const thinkTokens = tokenizer(prefix, true, "trimLeadingSpace");
|
||||
const thinkEndTokens = tokenizer(suffix, true, "trimLeadingSpace");
|
||||
const [thinkToken] = thinkTokens;
|
||||
const [thinkEndToken] = thinkEndTokens;
|
||||
if (thinkTokens.length === 1 && thinkEndTokens.length === 1 &&
|
||||
thinkToken != null && thinkEndToken != null) {
|
||||
return {
|
||||
prefix: LlamaText(new SpecialTokensText(prefix)),
|
||||
suffix: LlamaText(new SpecialTokensText(suffix))
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
return removeUndefinedFields({
|
||||
thought: tryMatchPrefixSuffixPair([
|
||||
["<think>", "</think>"], // DeepSeek, QwQ
|
||||
["<thought>", "</thought>"], // EXAONE Deep
|
||||
["<|START_THINKING|>", "<|END_THINKING|>"] // Command R7B
|
||||
])
|
||||
});
|
||||
}
|
||||
function hasAll(text, matches) {
|
||||
return matches.every((match) => text.includes(match));
|
||||
}
|
||||
//# sourceMappingURL=extractSegmentSettingsFromTokenizerAndChatTemplate.js.map
|
||||
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/extractSegmentSettingsFromTokenizerAndChatTemplate.js.map
generated
vendored
Normal file
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/extractSegmentSettingsFromTokenizerAndChatTemplate.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"extractSegmentSettingsFromTokenizerAndChatTemplate.js","sourceRoot":"","sources":["../../../../src/chatWrappers/generic/utils/extractSegmentSettingsFromTokenizerAndChatTemplate.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,SAAS,EAAE,iBAAiB,EAAC,MAAM,6BAA6B,CAAC;AACzE,OAAO,EAAC,qBAAqB,EAAC,MAAM,oCAAoC,CAAC;AAEzE,MAAM,UAAU,kDAAkD,CAC9D,YAAgC,EAAE,SAAqB;IAEvD,SAAS,wBAAwB,CAAC,cAAkD;QAChF,IAAI,YAAY,IAAI,IAAI,EAAE,CAAC;YACvB,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,cAAc,EAAE,CAAC;gBAC5C,IACI,CACI,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,MAAM,GAAG,QAAQ,GAAG,MAAM,EAAE,EAAE,CAAC,EAAE;oBAC5D,MAAM,GAAG,QAAQ;oBACjB,QAAQ,GAAG,MAAM;iBACpB,CAAC,CACL,IAAI,CACD,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,EAAE,CAAC,EAAE;oBAC1D,MAAM,GAAG,MAAM;oBACf,MAAM,GAAG,MAAM;iBAClB,CAAC,CACL;oBAED,OAAO;wBACH,MAAM,EAAE,SAAS,CAAC,IAAI,iBAAiB,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;wBACzD,MAAM,EAAE,SAAS,CAAC,IAAI,iBAAiB,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;qBAC5D,CAAC;gBAEN,IACI,CACI,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,MAAM,GAAG,KAAK,GAAG,MAAM,EAAE,EAAE,CAAC,EAAE;oBACzD,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wBACH,MAAM,EAAE,SAAS,CAAC,IAAI,iBAAiB,CAAC,MAAM,CAAC,CAAC;wBAChD,MAAM,EAAE,SAAS,CAAC,IAAI,iBAAiB,CAAC,MAAM,CAAC,CAAC;qBACnD,CAAC;gBACN,CAAC;YACL,CAAC;QACL,CAAC;QAED,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,OAAO,qBAAqB,CAAC;QACzB,OAAO,EAAE,wBAAwB,CAAC;YAC9B,CAAC,SAAS,EAAE,UAAU,CAAC,EAAE,gBAAgB;YACzC,CAAC,WAAW,EAAE,YAAY,CAAC,EAAE,cAAc;YAC3C,CAAC,oBAAoB,EAAE,kBAAkB,CAAC,CAAC,cAAc;SAC5D,CAAC;KACL,CAAC,CAAC;AACP,CAAC;AAED,SAAS,MAAM,CAAC,IAAY,EAAE,OAAiB;IAC3C,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;AAC1D,CAAC"}
|
||||
6
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/getFirstValidResult.d.ts
generated
vendored
Normal file
6
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/getFirstValidResult.d.ts
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Call the functions in the array one by one and return the result of the first one that doesn't throw an error.
|
||||
*
|
||||
* If all functions throw an error, throw the error of the last function.
|
||||
*/
|
||||
export declare function getFirstValidResult<const T extends (() => any)[]>(options: T): ReturnType<T[number]>;
|
||||
19
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/getFirstValidResult.js
generated
vendored
Normal file
19
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/getFirstValidResult.js
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Call the functions in the array one by one and return the result of the first one that doesn't throw an error.
|
||||
*
|
||||
* If all functions throw an error, throw the error of the last function.
|
||||
*/
|
||||
export function getFirstValidResult(options) {
|
||||
for (let i = 0; i < options.length; i++) {
|
||||
if (i === options.length - 1)
|
||||
return options[i]();
|
||||
try {
|
||||
return options[i]();
|
||||
}
|
||||
catch (err) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
throw new Error("All options failed");
|
||||
}
|
||||
//# sourceMappingURL=getFirstValidResult.js.map
|
||||
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/getFirstValidResult.js.map
generated
vendored
Normal file
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/getFirstValidResult.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"getFirstValidResult.js","sourceRoot":"","sources":["../../../../src/chatWrappers/generic/utils/getFirstValidResult.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAgC,OAAU;IACzE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACtC,IAAI,CAAC,KAAK,OAAO,CAAC,MAAM,GAAG,CAAC;YACxB,OAAO,OAAO,CAAC,CAAC,CAAE,EAAE,CAAC;QAEzB,IAAI,CAAC;YACD,OAAO,OAAO,CAAC,CAAC,CAAE,EAAE,CAAC;QACzB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACX,aAAa;QACjB,CAAC;IACL,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;AAC1C,CAAC"}
|
||||
2
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/squashChatHistoryItems.d.ts
generated
vendored
Normal file
2
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/squashChatHistoryItems.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
import { ChatHistoryItem } from "../../../types.js";
|
||||
export declare function squashChatHistoryItems(history: readonly ChatHistoryItem[]): ChatHistoryItem[];
|
||||
35
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/squashChatHistoryItems.js
generated
vendored
Normal file
35
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/squashChatHistoryItems.js
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
import { LlamaText } from "../../../utils/LlamaText.js";
|
||||
export function squashChatHistoryItems(history) {
|
||||
const res = [];
|
||||
for (const item of history) {
|
||||
const lastItem = res.at(-1);
|
||||
if (lastItem == null) {
|
||||
res.push(structuredClone(item));
|
||||
continue;
|
||||
}
|
||||
if (lastItem.type === "system" && item.type === "system")
|
||||
lastItem.text = LlamaText.joinValues("\n\n", [
|
||||
LlamaText.fromJSON(lastItem.text),
|
||||
LlamaText.fromJSON(item.text)
|
||||
]).toJSON();
|
||||
else if (lastItem.type === "user" && item.type === "user")
|
||||
lastItem.text += "\n\n" + item.text;
|
||||
else if (lastItem.type === "model" && item.type === "model") {
|
||||
const responsesToAdd = ["\n\n", ...item.response];
|
||||
while (typeof responsesToAdd[0] === "string" && typeof lastItem.response.at(-1) === "string") {
|
||||
const lastResponses = lastItem.response.pop();
|
||||
if (typeof lastResponses !== "string") {
|
||||
lastItem.response.push(lastResponses);
|
||||
break;
|
||||
}
|
||||
lastItem.response.push(lastResponses + responsesToAdd.shift());
|
||||
}
|
||||
while (responsesToAdd.length > 0)
|
||||
lastItem.response.push(responsesToAdd.shift());
|
||||
}
|
||||
else
|
||||
res.push(structuredClone(item));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
//# sourceMappingURL=squashChatHistoryItems.js.map
|
||||
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/squashChatHistoryItems.js.map
generated
vendored
Normal file
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/squashChatHistoryItems.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"squashChatHistoryItems.js","sourceRoot":"","sources":["../../../../src/chatWrappers/generic/utils/squashChatHistoryItems.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,S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,QAAQ,EAAE,CAAC;oBACpC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;oBACtC,MAAM;gBACV,CAAC;gBAED,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,aAAa,GAAG,cAAc,CAAC,KAAK,EAAG,CAAC,CAAC;YACpE,CAAC;YAED,OAAO,cAAc,CAAC,MAAM,GAAG,CAAC;gBAC5B,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,KAAK,EAAG,CAAC,CAAC;QACxD,CAAC;;YACG,GAAG,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC;IACxC,CAAC;IAED,OAAO,GAAG,CAAC;AACf,CAAC"}
|
||||
22
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/templateSegmentOptionsToChatWrapperSettings.d.ts
generated
vendored
Normal file
22
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/templateSegmentOptionsToChatWrapperSettings.d.ts
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
import { ChatWrapperSettings } from "../../../types.js";
|
||||
export declare function templateSegmentOptionsToChatWrapperSettings(templateOptions?: TemplateChatWrapperSegmentsOptions): ChatWrapperSettings["segments"];
|
||||
export type TemplateChatWrapperSegmentsOptions = {
|
||||
/** Template for a thought segment */
|
||||
thoughtTemplate?: `${string}{{content}}${string}`;
|
||||
/**
|
||||
* Automatically reopen a thought segment after function calls.
|
||||
*
|
||||
* Useful for aligning the output of models that assume that a thought segment is already open after function calls.
|
||||
*
|
||||
* Defaults to `false`.
|
||||
*/
|
||||
reopenThoughtAfterFunctionCalls?: boolean;
|
||||
/** Consider all segments to be closed when this text is detected */
|
||||
closeAllSegmentsTemplate?: string;
|
||||
/**
|
||||
* After function calls, reiterate the stack of the active segments to remind the model of the context.
|
||||
*
|
||||
* Defaults to `false`.
|
||||
*/
|
||||
reiterateStackAfterFunctionCalls?: boolean;
|
||||
};
|
||||
28
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/templateSegmentOptionsToChatWrapperSettings.js
generated
vendored
Normal file
28
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/templateSegmentOptionsToChatWrapperSettings.js
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
import { parseTextTemplate } from "../../../utils/parseTextTemplate.js";
|
||||
import { removeUndefinedFields } from "../../../utils/removeNullFields.js";
|
||||
export function templateSegmentOptionsToChatWrapperSettings(templateOptions) {
|
||||
if (templateOptions == null)
|
||||
return {};
|
||||
function getThoughtSegmentOptions() {
|
||||
if (templateOptions?.thoughtTemplate == null)
|
||||
return undefined;
|
||||
const parsedThoughtTemplate = parseTextTemplate(templateOptions.thoughtTemplate, [{
|
||||
text: "{{content}}",
|
||||
key: "content"
|
||||
}]);
|
||||
const prefix = parsedThoughtTemplate.content.prefix;
|
||||
if (prefix.length === 0)
|
||||
throw new Error("Thought template must have text before \"{{content}}\"");
|
||||
return removeUndefinedFields({
|
||||
prefix,
|
||||
suffix: parsedThoughtTemplate.content.suffix || undefined,
|
||||
reopenAfterFunctionCalls: templateOptions.reopenThoughtAfterFunctionCalls
|
||||
});
|
||||
}
|
||||
return removeUndefinedFields({
|
||||
closeAllSegments: templateOptions.closeAllSegmentsTemplate || undefined,
|
||||
reiterateStackAfterFunctionCalls: templateOptions.reiterateStackAfterFunctionCalls,
|
||||
thought: getThoughtSegmentOptions()
|
||||
});
|
||||
}
|
||||
//# sourceMappingURL=templateSegmentOptionsToChatWrapperSettings.js.map
|
||||
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/templateSegmentOptionsToChatWrapperSettings.js.map
generated
vendored
Normal file
1
node_modules/node-llama-cpp/dist/chatWrappers/generic/utils/templateSegmentOptionsToChatWrapperSettings.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"templateSegmentOptionsToChatWrapperSettings.js","sourceRoot":"","sources":["../../../../src/chatWrappers/generic/utils/templateSegmentOptionsToChatWrapperSettings.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,iBAAiB,EAAC,MAAM,qCAAqC,CAAC;AACtE,OAAO,EAAC,qBAAqB,EAAC,MAAM,oCAAoC,CAAC;AAEzE,MAAM,UAAU,2CAA2C,CACvD,eAAoD;IAEpD,IAAI,eAAe,IAAI,IAAI;QACvB,OAAO,EAAE,CAAC;IAEd,SAAS,wBAAwB;QAC7B,IAAI,eAAe,EAAE,eAAe,IAAI,IAAI;YACxC,OAAO,SAAS,CAAC;QAErB,MAAM,qBAAqB,GAAG,iBAAiB,CAAC,eAAe,CAAC,eAAe,EAAE,CAAC;gBAC9E,IAAI,EAAE,aAAa;gBACnB,GAAG,EAAE,SAAS;aACjB,CAAC,CAAC,CAAC;QAEJ,MAAM,MAAM,GAAG,qBAAqB,CAAC,OAAO,CAAC,MAAM,CAAC;QACpD,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;QAE9E,OAAO,qBAAqB,CAAC;YACzB,MAAM;YACN,MAAM,EAAE,qBAAqB,CAAC,OAAO,CAAC,MAAM,IAAI,SAAS;YACzD,wBAAwB,EAAE,eAAe,CAAC,+BAA+B;SAC5E,CAAC,CAAC;IACP,CAAC;IAED,OAAO,qBAAqB,CAAC;QACzB,gBAAgB,EAAE,eAAe,CAAC,wBAAwB,IAAI,SAAS;QACvE,gCAAgC,EAAE,eAAe,CAAC,gCAAgC;QAElF,OAAO,EAAE,wBAAwB,EAAE;KACtC,CAAC,CAAC;AACP,CAAC"}
|
||||
Reference in New Issue
Block a user