fix: add example

This commit is contained in:
ikechan8370 2024-10-11 23:07:09 +08:00
parent 479028584e
commit aa2ac7b5d3
2 changed files with 53 additions and 26 deletions

View file

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

View file

@ -0,0 +1,29 @@
import { BaseClient } from './BaseClient.js'
export class OpenAILikeClient extends BaseClient {
constructor (props) {
super(props)
this.model = props.model
this.key = props.key
this.baseUrl = props.baseUrl
this.debug = props.debug
}
async sendMessageRaw (text, opt = {}) {
const messages = await this.getHistory(opt.parentMessageId, opt.conversationId)
const response = await fetch(this.baseUrl, {
method: 'POST',
headers: {
'Content-Type': 'application-json',
Authorization: `Bearer ${this.key}`
},
body: JSON.stringify({
model: this.model,
messages,
stream: false,
...opt.completionParams || {}
})
})
return await response.json()
}
}