From aa2ac7b5d3b5ff33646166a44ff825a5ab73ea67 Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Fri, 11 Oct 2024 23:07:09 +0800 Subject: [PATCH] fix: add example --- apps/example_handler.js | 50 ++++++++++++++++++-------------------- client/OpenAILikeClient.js | 29 ++++++++++++++++++++++ 2 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 client/OpenAILikeClient.js diff --git a/apps/example_handler.js b/apps/example_handler.js index 205f9338..27f60c56 100644 --- a/apps/example_handler.js +++ b/apps/example_handler.js @@ -1,4 +1,3 @@ - /** * 示例后处理器。你可以在example下面写一个新的。默认会调用所有此key的处理器 */ @@ -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))) } } diff --git a/client/OpenAILikeClient.js b/client/OpenAILikeClient.js new file mode 100644 index 00000000..33a37187 --- /dev/null +++ b/client/OpenAILikeClient.js @@ -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() + } +}