Skip to content

Commit

Permalink
🐛 fix: Fix client fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Nov 15, 2023
1 parent 1ec2701 commit 9262608
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 21 deletions.
6 changes: 5 additions & 1 deletion api/open-stt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ export default async (req: Request) => {
if (!OPENAI_API_KEY) return new Response('OPENAI_API_KEY is not set', { status: 500 });
const openai = new OpenAI({ apiKey: OPENAI_API_KEY, baseURL: OPENAI_PROXY_URL });
const res = await createOpenaiAudioTranscriptionsCompletion({ openai, payload });
return res;
return new Response(JSON.stringify(res), {
headers: {
'content-type': 'application/json;charset=UTF-8',
},
});
};
6 changes: 6 additions & 0 deletions src/const/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import urlJoin from 'url-join';

export const MICROSOFT_SPEECH_URL =
'https://southeastasia.api.speech.microsoft.com/accfreetrial/texttospeech/acc/v3.0-beta1/vcg/speak';
export const EDGE_SPEECH_URL =
Expand All @@ -19,3 +21,7 @@ export const OPENAI_PROXY_URL =
process.env.OPENAI_PROXY_URL ||
process.env.NEXT_PUBLIC_OPENAI_PROXY_URL ||
'https://api.openai.com/v1';

export const OPENAI_TTS_URL = (api?: string) => urlJoin(api || OPENAI_PROXY_URL, 'audio/speech');
export const OPENAI_STT_URL = (api?: string) =>
urlJoin(api || OPENAI_PROXY_URL, 'audio/transcriptions');
2 changes: 1 addition & 1 deletion src/server/createOpenaiAudioTranscriptionsCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ export const createOpenaiAudioTranscriptionsCompletion = async ({
{ headers: { Accept: '*/*' } },
);

return response.text;
return response;
};
4 changes: 4 additions & 0 deletions src/services/fetchEdgeSpeech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export const fetchEdgeSpeech = async (
? fetch(api.url, { body: JSON.stringify(payload), method: 'POST' })
: await createEdgeSpeechComletion({ payload }));

if (!response.ok) {
throw new Error('Network response was not ok');
}

const arrayBuffer = await response.arrayBuffer();
const audioBuffer = await arrayBufferConvert(arrayBuffer);
return audioBuffer;
Expand Down
39 changes: 27 additions & 12 deletions src/services/fetchOpenaiSTT.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import OpenAI from 'openai';

import { OPENAI_API_KEY, OPENAI_PROXY_URL } from '@/const/api';
import { createOpenaiAudioTranscriptionsCompletion } from '@/server/createOpenaiAudioTranscriptionsCompletion';
import { OPENAI_API_KEY, OPENAI_PROXY_URL, OPENAI_STT_URL } from '@/const/api';
import { OpenAISTTPayload } from '@/server/types';
import { RecordMineType, getRecordMineType } from '@/utils/getRecordMineType';

const genSTTBody = ({ blob, options }: OpenAISTTPayload) => {
const filename = `${Date.now()}.${options.mineType.extension}`;
const file = new File([blob], filename, {
type: options.mineType.mineType,
});

const body = new FormData();
body.append('file', file);
body.append('model', options.model);
return body;
};
export interface OpenaiSttOptions {
api?: {
key?: string;
Expand All @@ -14,8 +22,6 @@ export interface OpenaiSttOptions {
mineType?: RecordMineType;
model?: 'whisper-1';
}

// 纯文本生成语音
export const fetchOpenaiSTT = async (
speech: Blob,
{ api = {}, model = 'whisper-1', mineType }: OpenaiSttOptions,
Expand All @@ -31,12 +37,21 @@ export const fetchOpenaiSTT = async (
},
};

const response = (await (api?.url
const response = await (api?.url
? fetch(api.url, { body: JSON.stringify(payload), method: 'POST' })
: createOpenaiAudioTranscriptionsCompletion({
openai: new OpenAI({ apiKey: key, baseURL: url }),
payload,
}))) as string;
: fetch(OPENAI_STT_URL(url), {
body: genSTTBody(payload),
headers: new Headers({
Authorization: `Bearer ${key}`,
}),
method: 'POST',
}));

if (!response.ok) {
throw new Error('Network response was not ok');
}

const json = await response.json();

return response;
return json.text;
};
19 changes: 12 additions & 7 deletions src/services/fetchOpenaiTTS.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import OpenAI from 'openai';

import { OPENAI_API_KEY, OPENAI_PROXY_URL } from '@/const/api';
import { createOpenaiAudioSpeechCompletion } from '@/server/createOpenaiAudioSpeechCompletion';
import { OPENAI_API_KEY, OPENAI_PROXY_URL, OPENAI_TTS_URL } from '@/const/api';
import { OpenAITTSPayload } from '@/server/types';
import { arrayBufferConvert } from '@/utils/arrayBufferConvert';
import { type SsmlOptions } from '@/utils/genSSML';
Expand Down Expand Up @@ -34,9 +31,17 @@ export const fetchOpenaiTTS = async (

const response = await (api?.url
? fetch(api.url, { body: JSON.stringify(payload), method: 'POST' })
: await createOpenaiAudioSpeechCompletion({
openai: new OpenAI({ apiKey: key, baseURL: url }),
payload,
: fetch(OPENAI_TTS_URL(url), {
body: JSON.stringify({
input,
model,
voice,
}),
headers: new Headers({
'Authorization': `Bearer ${key}`,
'Content-Type': 'application/json',
}),
method: 'POST',
}));

if (!response.ok) {
Expand Down

0 comments on commit 9262608

Please sign in to comment.