修改suno伪造生成策略,支持更多模型调用

This commit is contained in:
zyc404 2024-05-08 15:32:49 +08:00
parent 123e5304a7
commit d111d2625e
7 changed files with 86 additions and 30 deletions

View file

@ -69,10 +69,10 @@ export default class BingSunoClient {
sunoURL,
prompt: prompt.songPrompt
}
await e.reply('Bing Suno 生成中,请稍后')
await e.reply('Suno 生成中,请稍后')
this.replyMsg(sunoDisplayResult, e)
} else {
await e.reply('Bing Suno 数据获取失败')
await e.reply('Suno 数据获取失败')
redis.del(`CHATGPT:SUNO:${e.sender.user_id}`)
}
redis.del(`CHATGPT:SUNO:${e.sender.user_id}`)
@ -84,7 +84,7 @@ export default class BingSunoClient {
redis.del(`CHATGPT:SUNO:${e.sender.user_id}`)
return true
}
let description = prompt.songPrompt
let description = prompt.songPrompt || prompt.lyrics
await e.reply('正在生成,请稍后')
try {
let sessTokens = Config.sunoSessToken.split(',')

View file

@ -338,7 +338,7 @@ export default class SydneyAIClient {
((Config.enableGroupContext && groupId) ? groupContextTip : '') +
((Config.enforceMaster && master) ? masterTip : '') +
(Config.sydneyMood ? moodTip : '') +
((!Config.enableGenerateSuno && Config.bingSuno != 'bing' && Config.enableGenerateSunoForger) ? 'If I ask you to generate music or write songs, you need to reply with information suitable for Suno to generate music. Please use keywords such as Verse, Chorus, Bridge, Outro, and End to segment the lyrics, such as [Verse], The returned message is in JSON format, with a structure of {"option": "Suno", "tags": "style", "title": "title of the song", "lyrics": "lyrics"}.' : '')
((!Config.enableGenerateSuno && Config.enableChatSuno) ? 'If I ask you to generate music or write songs, you need to reply with information suitable for Suno to generate music. Please use keywords such as Verse, Chorus, Bridge, Outro, and End to segment the lyrics, such as [Verse], The returned message is in JSON format, with a structure of {"option": "Suno", "tags": "style", "title": "title of the song", "lyrics": "lyrics"}.' : '')
if (!text) {
previousMessages = pm
} else {
@ -835,17 +835,6 @@ export default class SydneyAIClient {
message.adaptiveCards = adaptiveCardsSoFar
message.text = replySoFar.join('')
}
// 伪造歌曲生成
if (Config.enableGenerateSunoForger) {
const sunoList = extractMarkdownJson(message.text)
for (let suno of sunoList) {
if (suno.json.option == 'Suno') {
message.text = message.text.replace(suno.markdown, `歌曲 《${suno.json.title}`)
logger.info(`开始生成歌曲${suno.json.tags}`)
onSunoCreateRequest(suno.json)
}
}
}
resolve({
message,
conversationExpiryTime: event?.item?.conversationExpiryTime

View file

@ -1243,6 +1243,34 @@ function maskString (str) {
return firstThreeChars + maskedChars + lastThreeChars
}
/**
* generated by ai
* @param rawJsonString
* @returns {string}
*/
function fixNewlinesInJsonString(rawJsonString) {
// 标记是否在字符串内
let inString = false
// 结果字符串
let result = ''
for (let i = 0; i < rawJsonString.length; i++) {
const currentChar = rawJsonString[i]
const nextChar = i + 1 < rawJsonString.length ? rawJsonString[i + 1] : ''
// 检查当前字符是否为双引号,且不是转义的双引号
if (currentChar === '"' && (i === 0 || rawJsonString[i - 1] !== '\\')) {
inString = !inString // 切换在字符串内的标记
}
// 如果在字符串内且遇到换行符,则替换为\\n
if (inString && (currentChar === '\n' || (currentChar === '\r' && nextChar === '\n'))) {
result += '\\n'
if (currentChar === '\r') i++ // 跳过\n
} else {
result += currentChar
}
}
return result
}
/**
* generated by ai
* @param text
@ -1259,7 +1287,7 @@ export function extractMarkdownJson(text) {
// 如果已经在一个 JSON 中,先结束当前的 JSON
if (currentJson) {
try {
const parsedJson = JSON.parse(currentJson)
const parsedJson = JSON.parse(fixNewlinesInJsonString(currentJson))
mdJsonPairs.push({ json: parsedJson, markdown: currentMd + '```' })
} catch (e) {
console.error('JSON解析错误:', e)
@ -1271,7 +1299,7 @@ export function extractMarkdownJson(text) {
} else if (line.startsWith('```') && currentJson) {
// 结束当前的 JSON
try {
const parsedJson = JSON.parse(currentJson)
const parsedJson = JSON.parse(fixNewlinesInJsonString(currentJson))
mdJsonPairs.push({ json: parsedJson, markdown: currentMd + line })
} catch (e) {
console.error('JSON解析错误:', e)

View file

@ -154,7 +154,6 @@ const defaultConfig = {
autoJapanese: false,
enableGenerateContents: false,
enableGenerateSuno: false,
enableGenerateSunoForger: false,
amapKey: '',
azSerpKey: '',
serpSource: 'ikechan8370',
@ -186,6 +185,8 @@ const defaultConfig = {
chatglmRefreshToken: '',
sunoSessToken: '',
sunoClientToken: '',
enableChatSuno: false,
SunoModel: 'local',
claudeApiKey: '',
claudeApiBaseUrl: 'http://claude-api.ikechan8370.com',

View file

@ -397,6 +397,11 @@ export default class XinghuoClient {
} else {
Prompt = option.system ? [{ role: 'system', content: option.system }] : []
}
if (Config.enableChatSuno) {
Prompt.unshift(
{ role: 'system', content: '如果我要求你生成音乐或写歌你需要回复适合Suno生成音乐的信息。请使用Verse、Chorus、Bridge、Outro和End等关键字对歌词进行分段如[Verse]。返回的消息需要使用markdown包裹的JSON格式结构为```json{"option": "Suno", "tags": "style", "title": "title of the song", "lyrics": "lyrics"}```。' }
)
}
if (Config.xhPromptEval) {
Prompt.forEach(obj => {
try {