UNPKG

@difizen/libro-ai-native

Version:
39 lines (37 loc) 1.36 kB
import { lineBasedCompletionModelConfigs } from "./contant.js"; // 去除多余的空行,并且限制前文的长度 function processPrefix(prompt) { // remove all empty lines prompt = prompt.replace(/^s*[\n]/gm, ''); var arr = prompt.split('\n'); // if the number of lines is greater than n, take the last n lines if (arr.length > lineBasedCompletionModelConfigs.completionPromptMaxLineSize) { prompt = arr.slice(-lineBasedCompletionModelConfigs.completionPromptMaxLineSize).join('\n'); } return prompt; } // 去除多余的空行,并且限制后文的长度 function processSuffix(suffix) { suffix = suffix.replace(/^s*[\n]/gm, ''); var arr = suffix.split('\n'); if (arr.length > lineBasedCompletionModelConfigs.completionSuffixMaxLineSize) { suffix = arr.slice(-lineBasedCompletionModelConfigs.completionSuffixMaxLineSize).join('\n'); } return suffix; } export var lineBasedPromptProcessor = { processPrefix: processPrefix, processSuffix: processSuffix }; export function sleep(time) { return new Promise(function (resolve) { return setTimeout(resolve, time); }); } export function raceCancellation(promise, token, defaultValue) { return Promise.race([promise, new Promise(function (resolve) { return token.onCancellationRequested(function () { return resolve(defaultValue); }); })]); }