Added option to choose openai provider

This commit is contained in:
zaidmukaddam 2024-08-26 21:00:45 +05:30
parent 9498bf1c97
commit 56dd60105d
2 changed files with 80 additions and 32 deletions

View File

@ -38,6 +38,10 @@ Never use pronouns in the questions as they blur the context.`,
}
export async function generateSpeech(text: string, voice: 'alloy' | 'echo' | 'fable' | 'onyx' | 'nova' | 'shimmer' = "alloy") {
if (process.env.OPENAI_PROVIDER === 'azure') {
if (!process.env.AZURE_OPENAI_API_KEY || !process.env.AZURE_OPENAI_API_URL) {
throw new Error('Azure OpenAI API key and URL are required.');
}
const url = process.env.AZURE_OPENAI_API_URL!;
const response = await fetch(url, {
@ -60,7 +64,39 @@ export async function generateSpeech(text: string, voice: 'alloy' | 'echo' | 'fa
const arrayBuffer = await response.arrayBuffer();
const base64Audio = Buffer.from(arrayBuffer).toString('base64');
return {
audio: `data:audio/mp3;base64,${base64Audio}`,
};
} else if (process.env.OPENAI_PROVIDER === 'openai') {
const openai = new OpenAI();
const response = await openai.audio.speech.create({
model: "tts-1",
voice: voice,
input: text,
});
const arrayBuffer = await response.arrayBuffer();
const base64Audio = Buffer.from(arrayBuffer).toString('base64');
return {
audio: `data:audio/mp3;base64,${base64Audio}`,
};
} else {
const openai = new OpenAI();
const response = await openai.audio.speech.create({
model: "tts-1",
voice: voice,
input: text,
});
const arrayBuffer = await response.arrayBuffer();
const base64Audio = Buffer.from(arrayBuffer).toString('base64');
return {
audio: `data:audio/mp3;base64,${base64Audio}`,
};
}
}

View File

@ -7,7 +7,7 @@ import FirecrawlApp from '@mendable/firecrawl-js';
import { z } from "zod";
import { geolocation } from "@vercel/functions";
// Allow streaming responses up to 30 seconds
// Allow streaming responses up to 60 seconds
export const maxDuration = 60;
const azure = createAzure({
@ -15,12 +15,24 @@ const azure = createAzure({
apiKey: process.env.AZURE_API_KEY,
});
const provider = process.env.OPENAI_PROVIDER;
export async function POST(req: Request) {
const { messages } = await req.json();
const { latitude, longitude, city } = geolocation(req)
let model;
if (provider === "azure") {
model = azure.chat("gpt-4o-mini");
} else if (provider === "openai") {
model = openai.chat("gpt-4o-mini");
} else {
model = openai.chat("gpt-4o-mini");
}
const result = await streamText({
model: azure.chat("gpt-4o-mini"),
model,
messages: convertToCoreMessages(messages),
temperature: 0.72,
topP: 0.95,
@ -269,7 +281,7 @@ When asked a "What is" question, maintain the same format as the question and an
});
const timeout = setTimeout(() => {
// Abort the request after 10 seconds
// Abort the request after 2 seconds
abortController.abort();
}, 2000);