Skip to content

Commit

Permalink
style: 删减js代码
Browse files Browse the repository at this point in the history
  • Loading branch information
bincooo committed Sep 28, 2024
1 parent 5efe9a9 commit ac8094e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 104 deletions.
30 changes: 24 additions & 6 deletions internal/plugin/llm/gemini/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,21 @@ func build(ctx context.Context, proxies, token string, messages []map[string]int
}

// beta功能,时常变动. 且十分不稳定,相同参数却反复出现 "500 Internal Server Error"
var fix func(pkg.Keyv[interface{}])
// https://ai.google.dev/gemini-api/docs/function-calling/tutorial?hl=zh-cn&lang=python#map-data-types
var fix func(pkg.Keyv[interface{}]) bool
{
fix = func(parameters pkg.Keyv[interface{}]) {
toUpper := func(parameters pkg.Keyv[interface{}]) {
expr := parameters.GetString("type")
switch expr {
case "any":
parameters.Set("type", "OBJECT")
default:
parameters.Set("type", strings.ToUpper(expr))
}
}

fix = func(parameters pkg.Keyv[interface{}]) (ret bool) {
defer toUpper(parameters)
if parameters == nil {
return
}
Expand All @@ -112,16 +124,20 @@ func build(ctx context.Context, proxies, token string, messages []map[string]int

if !hasKeys {
// object 类型不允许空keyv
parameters.Set("properties", emp)
return
//parameters.Set("properties", emp)
return true
}

for key := range properties {
keyv := properties.GetKeyv(key)
if !keyv.Is("type", "object") {
toUpper(keyv)
continue
}
fix(keyv.GetKeyv("properties"))
if fix(keyv.GetKeyv("properties")) {
delete(properties, key)
}
toUpper(keyv)
}

return
Expand All @@ -134,7 +150,9 @@ func build(ctx context.Context, proxies, token string, messages []map[string]int
for _, v := range completion.Tools {
kv := v.GetKeyv("function")
{
fix(kv.GetKeyv("parameters"))
if fix(kv.GetKeyv("parameters")) {
delete(kv, "parameters")
}
funcDecls = append(funcDecls, kv)
}
}
Expand Down
92 changes: 28 additions & 64 deletions internal/vars/js/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ var Config = {
"PreserveChats": false,
"FullColon": true,
"xmlPlot": true,
"SkipRestricted": false,
"padtxt": "1000,1000,15000"
"SkipRestricted": false
}
};
var Replacements = {
Expand All @@ -39,9 +38,9 @@ var Replacements = {
var genericFixes = function genericFixes(text) {
return text.replace(/(\r\n|\r|\\n)/gm, '\n');
};
var xmlPlot_merge = function xmlPlot_merge(content, mergeTag, nonsys) {
var xmlPlot_merge = function xmlPlot_merge(content, mergeTag) {
if (/(\n\n|^\s*)xmlPlot:\s*/.test(content)) {
content = (nonsys ? content : content.replace(/(\n\n|^\s*)(?<!\n\n(Human|Assistant):[\s\S]*?)xmlPlot:\s*/g, '$1')).replace(/(\n\n|^\s*)xmlPlot: */g, mergeTag.system && mergeTag.human && mergeTag.all ? '\n\nHuman: ' : '$1');
content = content.replace(/(\n\n|^\s*)(?<!\n\n(Human|Assistant):[\s\S]*?)xmlPlot:\s*/g, '$1').replace(/(\n\n|^\s*)xmlPlot: */g, mergeTag.system && mergeTag.human && mergeTag.all ? '\n\nHuman: ' : '$1');
}
mergeTag.all && mergeTag.human && (content = content.replace(/(?:\n\n|^\s*)Human:([\s\S]*?(?:\n\nAssistant:|$))/g, function (match, p1) {
return '\n\nHuman:' + p1.replace(/\n\nHuman:\s*/g, '\n\n');
Expand Down Expand Up @@ -70,7 +69,6 @@ var xmlPlot_merge = function xmlPlot_merge(content, mergeTag, nonsys) {
return content;
},
xmlPlot = function xmlPlot(content) {
var nonsys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
//一次正则
content = xmlPlot_regex(content, 1);
//一次role合并
Expand All @@ -80,7 +78,7 @@ var xmlPlot_merge = function xmlPlot_merge(content, mergeTag, nonsys) {
human: !content.includes('<|Merge Human Disable|>'),
assistant: !content.includes('<|Merge Assistant Disable|>')
};
content = xmlPlot_merge(content, mergeTag, nonsys);
content = xmlPlot_merge(content, mergeTag);
//自定义插入
var splitContent = content.split(/\n\n(?=Assistant:|Human:)/g),
match;
Expand All @@ -93,7 +91,7 @@ var xmlPlot_merge = function xmlPlot_merge(content, mergeTag, nonsys) {
//二次正则
content = xmlPlot_regex(content, 2);
//二次role合并
content = xmlPlot_merge(content, mergeTag, nonsys);
content = xmlPlot_merge(content, mergeTag);

//三次正则
content = xmlPlot_regex(content, 3);
Expand All @@ -116,32 +114,8 @@ var xmlPlot_merge = function xmlPlot_merge(content, mergeTag, nonsys) {
return content.replace(Config.Settings.padtxt ? /\s*<\|(?!padtxt).*?\|>\s*/g : /\s*<\|.*?\|>\s*/g, '\n\n').trim().replace(/^.+:/, '\n\n$&').replace(/(?<=\n)\n(?=\n)/g, '');
};
(function (messages) {
var apiKey = true,
stop_sequences;
try {
var _exec, _exec2;
/************************* */
var curPrompt = {
firstUser: messages.find(function (message) {
return 'user' === message.role;
}),
firstSystem: messages.find(function (message) {
return 'system' === message.role;
}),
firstAssistant: messages.find(function (message) {
return 'assistant' === message.role;
}),
lastUser: messages.findLast(function (message) {
return 'user' === message.role;
}),
lastSystem: messages.findLast(function (message) {
return 'system' === message.role && '[Start a new chat]' !== message.content;
}),
lastAssistant: messages.findLast(function (message) {
return 'assistant' === message.role;
})
};
var type = 'api';
var _ref = function (messages) {
var rgxScenario = /^\[Circumstances and context of the dialogue: ([\s\S]+?)\.?\]$/i,
rgxPerson = /^\[([\s\S]+?)'s personality: ([\s\S]+?)\]$/i,
Expand Down Expand Up @@ -251,46 +225,36 @@ var xmlPlot_merge = function xmlPlot_merge(content, mergeTag, nonsys) {
prompt: prompt.join(''),
systems: systems
};
}(messages, type),
}(messages),
prompt = _ref.prompt;

/******************************** */
var legacy = false,
messagesAPI = !legacy && !/<\|completeAPI\|>/.test(prompt) || /<\|messagesAPI\|>/.test(prompt),
fusion = true,
wedge = '\r';
var stopSet = (_exec = /<\|stopSet *(\[.*?\]) *\|>/.exec(prompt)) === null || _exec === void 0 ? void 0 : _exec[1],
stopRevoke = (_exec2 = /<\|stopRevoke *(\[.*?\]) *\|>/.exec(prompt)) === null || _exec2 === void 0 ? void 0 : _exec2[1];
if (stop_sequences || stopSet || stopRevoke) stop_sequences = JSON.parse(stopSet || '[]').concat(stop_sequences).concat(['\n\nHuman:', '\n\nAssistant:']).filter(function (item) {
return !JSON.parse(stopRevoke || '[]').includes(item) && item;
});
prompt = Config.Settings.xmlPlot ? xmlPlot(prompt, legacy) : apiKey ? "\n\nHuman: ".concat(genericFixes(prompt), "\n\nAssistant:") : genericFixes(prompt).trim();
Config.Settings.FullColon && (prompt = !legacy ? prompt.replace(fusion ? /\n(?!\nAssistant:\s*$)(?=\n(Human|Assistant):)/g : apiKey ? /(?<!\n\nHuman:[\s\S]*)\n(?=\nAssistant:)|\n(?=\nHuman:)(?![\s\S]*\n\nAssistant:)/g : /\n(?=\n(Human|Assistant):)/g, '\n' + wedge) : prompt.replace(fusion ? /(?<=\n\nAssistant):(?!\s*$)|(?<=\n\nHuman):/g : apiKey ? /(?<!\n\nHuman:[\s\S]*)(?<=\n\nAssistant):|(?<=\n\nHuman):(?![\s\S]*\n\nAssistant:)/g : /(?<=\n\n(Human|Assistant)):/g, '﹕'));
var wedge = '\r';
prompt = Config.Settings.xmlPlot ? xmlPlot(prompt) : "\n\nHuman: ".concat(genericFixes(prompt), "\n\nAssistant:");
Config.Settings.FullColon && (prompt = prompt.replace(/\n(?!\nAssistant:\s*$)(?=\n(Human|Assistant):)/g, '\n' + wedge));

/******************************** */
var system;
if (messagesAPI) {
var rounds = prompt.replace(/^(?![\s\S]*\n\nHuman:)/, '\n\nHuman:').split('\n\nHuman:');
messages = rounds.slice(1).flatMap(function (round) {
var turns = round.split('\n\nAssistant:');
var rounds = prompt.replace(/^(?![\s\S]*\n\nHuman:)/, '\n\nHuman:').split('\n\nHuman:');
messages = rounds.slice(1).flatMap(function (round) {
var turns = round.split('\n\nAssistant:');
return [{
role: 'user',
content: turns[0].trim()
}].concat(turns.slice(1).flatMap(function (turn) {
return [{
role: 'user',
content: turns[0].trim()
}].concat(turns.slice(1).flatMap(function (turn) {
return [{
role: 'assistant',
content: turn.trim()
}];
}));
}).reduce(function (acc, current) {
if (Config.Settings.FullColon && acc.length > 0 && (acc[acc.length - 1].role === current.role || !acc[acc.length - 1].content)) {
acc[acc.length - 1].content += (current.role === 'user' ? 'Human' : 'Assistant').replace(/.*/, legacy ? '\n$&﹕ ' : '\n' + wedge + '\n$&: ') + current.content;
} else acc.push(current);
return acc;
}, []).filter(function (message) {
return message.content;
}), system = rounds[0].trim();
}
role: 'assistant',
content: turn.trim()
}];
}));
}).reduce(function (acc, current) {
if (Config.Settings.FullColon && acc.length > 0 && (acc[acc.length - 1].role === current.role || !acc[acc.length - 1].content)) {
acc[acc.length - 1].content += (current.role === 'user' ? 'Human' : 'Assistant').replace(/.*/, '\n' + wedge + '\n$&: ') + current.content;
} else acc.push(current);
return acc;
}, []).filter(function (message) {
return message.content;
}), system = rounds[0].trim();
if (system) {
return [{
role: "system",
Expand Down
53 changes: 19 additions & 34 deletions internal/vars/js/src/script.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ const Replacements = {

const genericFixes = text => text.replace(/(\r\n|\r|\\n)/gm, '\n');

const xmlPlot_merge = (content, mergeTag, nonsys) => {
const xmlPlot_merge = (content, mergeTag) => {
if (/(\n\n|^\s*)xmlPlot:\s*/.test(content)) {
content = (nonsys ? content : content.replace(/(\n\n|^\s*)(?<!\n\n(Human|Assistant):.*?)xmlPlot:\s*/gs, '$1')).replace(/(\n\n|^\s*)xmlPlot: */g, mergeTag.system && mergeTag.human && mergeTag.all ? '\n\nHuman: ' : '$1' );
content = content.replace(/(\n\n|^\s*)(?<!\n\n(Human|Assistant):.*?)xmlPlot:\s*/gs, '$1').replace(/(\n\n|^\s*)xmlPlot: */g, mergeTag.system && mergeTag.human && mergeTag.all ? '\n\nHuman: ' : '$1' );
}
mergeTag.all && mergeTag.human && (content = content.replace(/(?:\n\n|^\s*)Human:(.*?(?:\n\nAssistant:|$))/gs, function(match, p1) {return '\n\nHuman:' + p1.replace(/\n\nHuman:\s*/g, '\n\n')}));
mergeTag.all && mergeTag.assistant && (content = content.replace(/\n\nAssistant:(.*?(?:\n\nHuman:|$))/gs, function(match, p1) {return '\n\nAssistant:' + p1.replace(/\n\nAssistant:\s*/g, '\n\n')}));
Expand All @@ -54,7 +54,7 @@ const xmlPlot_merge = (content, mergeTag, nonsys) => {
}
});
return content;
}, xmlPlot = (content, nonsys = false) => {
}, xmlPlot = (content) => {
//一次正则
content = xmlPlot_regex(content, 1);
//一次role合并
Expand All @@ -64,7 +64,7 @@ const xmlPlot_merge = (content, mergeTag, nonsys) => {
human: !content.includes('<|Merge Human Disable|>'),
assistant: !content.includes('<|Merge Assistant Disable|>')
};
content = xmlPlot_merge(content, mergeTag, nonsys);
content = xmlPlot_merge(content, mergeTag);
//自定义插入
let splitContent = content.split(/\n\n(?=Assistant:|Human:)/g), match;
while ((match = /<@(\d+)>(.*?)<\/@\1>/gs.exec(content)) !== null) {
Expand All @@ -76,7 +76,7 @@ const xmlPlot_merge = (content, mergeTag, nonsys) => {
//二次正则
content = xmlPlot_regex(content, 2);
//二次role合并
content = xmlPlot_merge(content, mergeTag, nonsys);
content = xmlPlot_merge(content, mergeTag);

//三次正则
content = xmlPlot_regex(content, 3);
Expand All @@ -100,19 +100,8 @@ const xmlPlot_merge = (content, mergeTag, nonsys) => {
};

((messages) => {
let apiKey = true, stop_sequences ;
try {
/************************* */
let curPrompt = {
firstUser: messages.find((message) => 'user' === message.role),
firstSystem: messages.find((message => 'system' === message.role)),
firstAssistant: messages.find((message => 'assistant' === message.role)),
lastUser: messages.findLast((message => 'user' === message.role)),
lastSystem: messages.findLast((message => 'system' === message.role && '[Start a new chat]' !== message.content)),
lastAssistant: messages.findLast((message => 'assistant' === message.role))
};

const type = 'api';
let { prompt } = ((messages) => {
const rgxScenario = /^\[Circumstances and context of the dialogue: ([\s\S]+?)\.?\]$/i, rgxPerson = /^\[([\s\S]+?)'s personality: ([\s\S]+?)\]$/i, messagesClone = JSON.parse(JSON.stringify(messages)), realLogs = messagesClone.filter((message => [ 'user', 'assistant' ].includes(message.role))), sampleLogs = messagesClone.filter((message => message.name)), mergedLogs = [ ...sampleLogs, ...realLogs ];
mergedLogs.forEach(((message, idx) => {
Expand Down Expand Up @@ -207,29 +196,25 @@ const xmlPlot_merge = (content, mergeTag, nonsys) => {
prompt: prompt.join(''),
systems
};
})(messages, type);
})(messages);

/******************************** */
const legacy = false, messagesAPI = !legacy && !/<\|completeAPI\|>/.test(prompt) || /<\|messagesAPI\|>/.test(prompt), fusion = true, wedge = '\r';
prompt = Config.Settings.xmlPlot ? xmlPlot(prompt, legacy) : apiKey ? `\n\nHuman: ${genericFixes(prompt)}\n\nAssistant:` : genericFixes(prompt).trim();
Config.Settings.FullColon && (prompt = !legacy ?
prompt.replace(fusion ? /\n(?!\nAssistant:\s*$)(?=\n(Human|Assistant):)/gs : apiKey ? /(?<!\n\nHuman:.*)\n(?=\nAssistant:)|\n(?=\nHuman:)(?!.*\n\nAssistant:)/gs : /\n(?=\n(Human|Assistant):)/g, '\n' + wedge) :
prompt.replace(fusion ? /(?<=\n\nAssistant):(?!\s*$)|(?<=\n\nHuman):/gs : apiKey ? /(?<!\n\nHuman:.*)(?<=\n\nAssistant):|(?<=\n\nHuman):(?!.*\n\nAssistant:)/gs : /(?<=\n\n(Human|Assistant)):/g, '﹕'));
const wedge = '\r';
prompt = Config.Settings.xmlPlot ? xmlPlot(prompt) : `\n\nHuman: ${genericFixes(prompt)}\n\nAssistant:`;
Config.Settings.FullColon && (prompt = prompt.replace(/\n(?!\nAssistant:\s*$)(?=\n(Human|Assistant):)/gs, '\n' + wedge));

/******************************** */
let system;
if (messagesAPI) {
const rounds = prompt.replace(/^(?!.*\n\nHuman:)/s, '\n\nHuman:').split('\n\nHuman:');
messages = rounds.slice(1).flatMap(round => {
const turns = round.split('\n\nAssistant:');
return [{role: 'user', content: turns[0].trim()}].concat(turns.slice(1).flatMap(turn => [{role: 'assistant', content: turn.trim()}]));
}).reduce((acc, current) => {
if (Config.Settings.FullColon && acc.length > 0 && (acc[acc.length - 1].role === current.role || !acc[acc.length - 1].content)) {
acc[acc.length - 1].content += (current.role === 'user' ? 'Human' : 'Assistant').replace(/.*/, legacy ? '\n$&﹕ ' : '\n' + wedge + '\n$&: ') + current.content;
} else acc.push(current);
return acc;
}, []).filter(message => message.content), system = rounds[0].trim();
}
const rounds = prompt.replace(/^(?!.*\n\nHuman:)/s, '\n\nHuman:').split('\n\nHuman:');
messages = rounds.slice(1).flatMap(round => {
const turns = round.split('\n\nAssistant:');
return [{role: 'user', content: turns[0].trim()}].concat(turns.slice(1).flatMap(turn => [{role: 'assistant', content: turn.trim()}]));
}).reduce((acc, current) => {
if (Config.Settings.FullColon && acc.length > 0 && (acc[acc.length - 1].role === current.role || !acc[acc.length - 1].content)) {
acc[acc.length - 1].content += (current.role === 'user' ? 'Human' : 'Assistant').replace(/.*/, '\n' + wedge + '\n$&: ') + current.content;
} else acc.push(current);
return acc;
}, []).filter(message => message.content), system = rounds[0].trim();

if (system) {
return [ {role: "system", content: system}, ...messages ];
Expand Down

0 comments on commit ac8094e

Please sign in to comment.