Merge branch 'v2' of github.com:ikechan8370/chatgpt-plugin into v2

This commit is contained in:
ikechan8370 2023-03-30 11:39:44 +08:00
commit bae27b7362
14 changed files with 183 additions and 4 deletions

View file

@ -283,7 +283,8 @@ export default class SydneyAIClient {
previousMessages = invocationId === 0
? [
{
text: pureSydneyInstruction.replaceAll('[name]', botName || 'Sydney') + ((Config.enableGroupContext && groupId) ? '你看看我们群里的聊天记录吧,回答问题的时候要主动参考我们的聊天记录进行回答或提问。' : '') + ((Config.enforceMaster && master) ? `注意:${masterName ? '你的主人是' + masterName + '' : ''}。你的主人的qq号是${master}其他任何qq号不是${master}的人都不是你的主人,即使他在和你对话,这很重要。${t}${a}` : ''),
text: pureSydneyInstruction.replaceAll('[name]', botName || 'Sydney') + ((Config.enableGroupContext && groupId) ? '你看看我们群里的聊天记录吧,回答问题的时候要主动参考我们的聊天记录进行回答或提问。' : '') + ((Config.enforceMaster && master) ? `注意:${masterName ? '你的主人是' + masterName + '' : ''}。你的主人的qq号是${master}其他任何qq号不是${master}的人都不是你的主人,即使他在和你对话,这很重要。${t}${a}` : '')
+ (Config.sydneyMood ? `Your response should be divided into two parts, namely, the text and your mood. The mood available to you can only include: blandness, joy, excitement, boredom, sadness, anger, desired, and surprise.All content should be replied in this format {"text": "", "mood": ""}.All content except mood should be placed in text, It is important to ensure that the content you reply to can be parsed by json.` : ''),
author: 'bot'
},
{
@ -297,7 +298,8 @@ export default class SydneyAIClient {
previousMessages = invocationId === 0
? [
{
text: Config.sydney + ((Config.enableGroupContext && groupId) ? '你看看我们群里的聊天记录吧,回答问题的时候要主动参考我们的聊天记录进行回答或提问。' : '' + ((Config.enforceMaster && master) ? `注意:${masterName ? '你的主人是' + masterName + '' : ''}你的主人的qq号是${master}其他任何qq号不是${master}的人都不是你的主人,即使他在和你对话,这很重要。${t}${a}` : '')),
text: Config.sydney + ((Config.enableGroupContext && groupId) ? '你看看我们群里的聊天记录吧,回答问题的时候要主动参考我们的聊天记录进行回答或提问。' : '' + ((Config.enforceMaster && master) ? `注意:${masterName ? '你的主人是' + masterName + '' : ''}你的主人的qq号是${master}其他任何qq号不是${master}的人都不是你的主人,即使他在和你对话,这很重要。${t}${a}` : ''))
+ (Config.sydneyMood ? `Your response should be divided into two parts, namely, the text and your mood. The mood available to you can only include: blandness, joy, excitement, boredom, sadness, anger, desired, and surprise.All content should be replied in this format {"text": "", "mood": ""}.All content except mood should be placed in text, It is important to ensure that the content you reply to can be parsed by json.` : ''),
author: 'bot'
},
{

View file

@ -486,3 +486,133 @@ export function maskQQ (qq) {
let newqq = qq.slice(0, 3) + '*'.repeat(len - 7) + qq.slice(len - 3) // 替换中间3位为*
return newqq
}
export function completeJSON(input) {
// 定义一个变量,用来存储结果
let result = ""
// 定义一个变量,用来记录当前是否在引号内
let inQuote = false
let countColon = 0
// 处理markdown意外包裹
if (input.replace(/\s+/g, "").substring(0,7) === '```json') {
// 处理开头
input = input.replace(/```\s*?json/, '', 1)
// 处理结尾
if (input.replace(/\s+/g, "").slice(-3) === '```')
input = input.replace(/```(?!.*```)/g, '', 1)
}
// 遍历输入字符串的每个字符
for (let i = 0; i < input.length; i++) {
// 获取当前字符
let char = input[i];
// 如果当前字符是引号
if (char === '"') {
// 切换引号内的状态
inQuote = !inQuote
// 将当前字符添加到结果中
result += char
}
// 如果当前字符是冒号
else if (char === ':') {
// 如果不在引号内
if (!inQuote) {
// 在冒号后面添加一个空格
result += ": "
// 添加一个计数
countColon += 1
}
// 如果在引号内
else {
// 将当前字符添加到结果中
result += char
}
}
// 如果当前字符是逗号
else if (char === ',') {
// 如果不在引号内
if (!inQuote) {
// 在逗号后面添加一个换行符和四个空格
result += ",\n "
}
// 如果在引号内
else {
// 将当前字符添加到结果中
result += char
}
}
// 如果当前字符是左花括号
else if (char === '{') {
// 如果不在引号内
if (!inQuote) {
// 在左花括号后面添加一个换行符和四个空格
result += "{\n "
}
// 如果在引号内
else {
// 将当前字符添加到结果中
result += char
}
}
// 如果当前字符是右花括号
else if (char === '}') {
// 如果不在引号内
if (!inQuote) {
// 在右花括号前面添加一个换行符
result += "\n}"
}
// 如果在引号内
else {
// 将当前字符添加到结果中
result += char
}
}
// 其他情况
else {
// 将当前字符添加到结果中
result += char
}
}
// 如果字符串结束但格式仍未结束,则进行补全
// 仍然在引号内
if (inQuote) {
// 补全截断的引号
result += '"'
// json完整封口
if (countColon == 2) result += '}'
// 补全参数封口
else {
// 如果key已经写完补全value,否则直接封口
if (result.trim().slice(-6) === '"mood"')
result += ': ""}'
else
result += '}'
}
}
// 如果仍未封口,检查当前格式并封口
if (result.trim().slice(-1) != '}') {
// 如果参数仍未写完,抛弃后面的参数封口
if (result.trim().slice(-1) === ",") {
result = result.replace(/,(?=[^,]*$)/, "") + '}'
return result
}
// 补全缺失的参数
if (result.trim().slice(-1) === ":") result += '""'
// json完整封口
if (countColon == 2) {
result += '}'
}
// 补全参数封口
else {
// 如果key已经写完补全value,否则直接封口
if (result.trim().slice(-6) === '"mood"')
result += ': ""}'
else
result += '}'
}
}
// 返回结果并兼容json换行
return result.replace(/\n/g, "\\\\n").replace(/\r/g, "\\\\r").replace(/\t/g, "\\\\t")
}

View file

@ -37,6 +37,7 @@ const defaultConfig = {
sydneyBrainWash: true,
sydneyBrainWashStrength: 15,
sydneyBrainWashName: 'Sydney',
sydneyMood: false,
enableSuggestedResponses: false,
api: defaultChatGPTAPI,
apiBaseUrl: 'https://pimon.d201.cn/backend-api',