Skip to content

Commit b8e783e

Browse files
committed
支持多PROXY_KEY,以及支持TTS-1接口的Audio返回
1.支持多PROXY_KEY,环境变量中使用","分割。 2.支持TTS-1接口的Audio返回,原有代码中Fetch在调用TTS接口时,收到返回的Audio类型数据报错的处理。
1 parent 61a9c86 commit b8e783e

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

app.js

+40-21
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,14 @@ app.all(`*`, async (req, res) => {
4141
if( !openai_key ) return res.status(403).send('Forbidden');
4242
if( openai_key.startsWith("fk") ) url = url.replaceAll( "api.openai.com", "openai.api2d.net" );
4343

44-
const proxy_key = token.split(':')[1]||"";
45-
if( process.env.PROXY_KEY && proxy_key !== process.env.PROXY_KEY )
44+
const proxy_key = token.split(':')[1]||"";
45+
console.log("PROXY_KEY:" + proxy_key);
46+
const validProxyKeys = process.env.PROXY_KEY ? process.env.PROXY_KEY.split(',') : [];
47+
// 检查传入的proxy_key是否在有效的PROXY_KEY列表中
48+
if (process.env.PROXY_KEY && !validProxyKeys.includes(proxy_key)){
49+
console.log("拒绝访问, PROXY_KEY无效");
4650
return res.status(403).send('Forbidden');
51+
}
4752

4853
// console.log( req );
4954
const { moderation, moderation_level, ...restBody } = req.body;
@@ -228,27 +233,41 @@ app.all(`*`, async (req, res) => {
228233
{
229234
console.log("使用 fetch");
230235
const response = await myFetch(url, options);
231-
// console.log(response);
232-
const data = await response.json();
233-
// 审核结果
234-
if( moderation && mdClient )
235-
{
236-
const params = {"Content": Buffer.from(data.choices[0].message.content).toString('base64')};
237-
const md_result = await mdClient.TextModeration(params);
238-
// console.log("审核结果", md_result);
239-
let md_check = moderation_level == 'high' ? md_result.Suggestion != 'Pass' : md_result.Suggestion == 'Block';
240-
if( md_check )
241-
{
242-
// 终止输出
243-
console.log("审核不通过", data.choices[0].message.content, md_result);
244-
data.choices[0].message.content = "这个话题不适合讨论,换个话题吧。";
245-
}else
246-
{
247-
console.log("审核通过", data.choices[0].message.content);
236+
// 检查返回的内容类型
237+
const contentType = response.headers.get("Content-Type");
238+
// 根据内容类型处理返回的数据
239+
if (contentType.includes("application/json")) {
240+
// 处理JSON数据
241+
const data = await response.json();
242+
// 审核结果
243+
if (moderation && mdClient) {
244+
const params = {"Content": Buffer.from(data.choices[0].message.content).toString('base64')};
245+
const md_result = await mdClient.TextModeration(params);
246+
console.log("审核结果", md_result);
247+
let md_check = moderation_level == 'high' ? md_result.Suggestion != 'Pass' : md_result.Suggestion == 'Block';
248+
if (md_check) {
249+
// 终止输出
250+
console.log("审核不通过", data.choices[0].message.content, md_result);
251+
data.choices[0].message.content = "这个话题不适合讨论,换个话题吧。";
252+
} else {
253+
console.log("审核通过", data.choices[0].message.content);
254+
}
248255
}
256+
// 返回JSON数据
257+
res.json(data);
258+
} else if (contentType.includes("audio")) {
259+
// 处理Audio数据
260+
const audioBlob = await response.blob();
261+
// 需要设置正确的Content-Type
262+
res.setHeader('Content-Type', 'audio/mpeg');
263+
// 发送音频数据给客户端
264+
const audioStream = audioBlob.stream();
265+
audioStream.pipe(res);
266+
} else {
267+
// 处理其他类型的返回或抛出错误
268+
console.log("返回了未知类型的数据");
269+
res.status(500).send("返回了未知类型的数据");
249270
}
250-
251-
res.json(data);
252271
}
253272

254273

0 commit comments

Comments
 (0)