Skip to content

Commit

Permalink
fix: add example
Browse files Browse the repository at this point in the history
  • Loading branch information
ikechan8370 committed Oct 11, 2024
1 parent 4790285 commit aa2ac7b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
50 changes: 24 additions & 26 deletions apps/example_handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* 示例后处理器。你可以在example下面写一个新的。默认会调用所有此key的处理器
*/
Expand All @@ -18,31 +17,30 @@ export class ChatGPTResponsePostHandler extends plugin {
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))
// 返回值会被忽略
// 以下是一个简单的例子
// const response = await fetch('https://api.fish.audio/v1/tts', {
// method: 'POST',
// headers: {
// Authorization: 'Bearer + key',
// '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)))
}
}
29 changes: 29 additions & 0 deletions client/OpenAILikeClient.js
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit aa2ac7b

Please sign in to comment.