chatgpt-plugin/apps/example_handler.js
2024-10-11 23:04:54 +08:00

48 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 示例后处理器。你可以在example下面写一个新的。默认会调用所有此key的处理器
*/
export class ChatGPTResponsePostHandler extends plugin {
constructor () {
super({
name: 'chatgpt文本回复后处理器',
priority: -100,
namespace: 'chatgpt-plugin',
handler: [{
key: 'chatgpt.response.post', // key必须是chatgpt.response.post
fn: 'postHandler'
}]
})
}
async postHandler (e, options, reject) {
const { content, use, prompt } = options
// 你可以在这里处理返回的文本比如使用自定义的语音api来合成语音
// const audio = customTTS(content)
// e.reply(segment.record(audio))
// 返回值会被忽略
const response = await fetch('https://api.fish.audio/v1/tts', {
method: 'POST',
headers: {
Authorization: 'Bearer 5e614bcc80a34789837fdb0f1269b2c4',
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: content,
reference_id: '1aacaeb1b840436391b835fd5513f4c4',
format: 'mp3',
latency: 'normal'
})
})
if (!response.ok) {
throw new Error(`无法从服务器获取音频数据:${response.statusText}`)
}
const audio = await response.blob()
// to Buffer
const buffer = await audio.arrayBuffer()
e.reply(segment.record(Buffer.from(buffer)))
// e.reply(segment.record(audio))
}
}