From ac8094e98279b27d02d8fcb732e99f4c73709717 Mon Sep 17 00:00:00 2001 From: bincooo Date: Sat, 28 Sep 2024 17:47:11 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E5=88=A0=E5=87=8Fjs=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/plugin/llm/gemini/fetch.go | 30 ++++++++-- internal/vars/js/dist/index.js | 92 +++++++++-------------------- internal/vars/js/src/script.es6.js | 53 ++++++----------- 3 files changed, 71 insertions(+), 104 deletions(-) diff --git a/internal/plugin/llm/gemini/fetch.go b/internal/plugin/llm/gemini/fetch.go index 5b11e6e3..cb7e91f9 100644 --- a/internal/plugin/llm/gemini/fetch.go +++ b/internal/plugin/llm/gemini/fetch.go @@ -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 } @@ -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 @@ -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) } } diff --git a/internal/vars/js/dist/index.js b/internal/vars/js/dist/index.js index 41d19d58..84ebed2d 100644 --- a/internal/vars/js/dist/index.js +++ b/internal/vars/js/dist/index.js @@ -25,8 +25,7 @@ var Config = { "PreserveChats": false, "FullColon": true, "xmlPlot": true, - "SkipRestricted": false, - "padtxt": "1000,1000,15000" + "SkipRestricted": false } }; var Replacements = { @@ -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*)(? 1 && arguments[1] !== undefined ? arguments[1] : false; //一次正则 content = xmlPlot_regex(content, 1); //一次role合并 @@ -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; @@ -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); @@ -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, @@ -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 ? /(? 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", diff --git a/internal/vars/js/src/script.es6.js b/internal/vars/js/src/script.es6.js index 7dc89089..29862b9e 100644 --- a/internal/vars/js/src/script.es6.js +++ b/internal/vars/js/src/script.es6.js @@ -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*)(? { } }); return content; -}, xmlPlot = (content, nonsys = false) => { +}, xmlPlot = (content) => { //一次正则 content = xmlPlot_regex(content, 1); //一次role合并 @@ -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) { @@ -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); @@ -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) => { @@ -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 ? /(? { - 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 ];