Added option to choose openai provider
This commit is contained in:
parent
9498bf1c97
commit
56dd60105d
@ -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") {
|
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 url = process.env.AZURE_OPENAI_API_URL!;
|
||||||
|
|
||||||
const response = await fetch(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 arrayBuffer = await response.arrayBuffer();
|
||||||
const base64Audio = Buffer.from(arrayBuffer).toString('base64');
|
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 {
|
return {
|
||||||
audio: `data:audio/mp3;base64,${base64Audio}`,
|
audio: `data:audio/mp3;base64,${base64Audio}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@ -7,7 +7,7 @@ import FirecrawlApp from '@mendable/firecrawl-js';
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { geolocation } from "@vercel/functions";
|
import { geolocation } from "@vercel/functions";
|
||||||
|
|
||||||
// Allow streaming responses up to 30 seconds
|
// Allow streaming responses up to 60 seconds
|
||||||
export const maxDuration = 60;
|
export const maxDuration = 60;
|
||||||
|
|
||||||
const azure = createAzure({
|
const azure = createAzure({
|
||||||
@ -15,12 +15,24 @@ const azure = createAzure({
|
|||||||
apiKey: process.env.AZURE_API_KEY,
|
apiKey: process.env.AZURE_API_KEY,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const provider = process.env.OPENAI_PROVIDER;
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
const { messages } = await req.json();
|
const { messages } = await req.json();
|
||||||
const { latitude, longitude, city } = geolocation(req)
|
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({
|
const result = await streamText({
|
||||||
model: azure.chat("gpt-4o-mini"),
|
model,
|
||||||
messages: convertToCoreMessages(messages),
|
messages: convertToCoreMessages(messages),
|
||||||
temperature: 0.72,
|
temperature: 0.72,
|
||||||
topP: 0.95,
|
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(() => {
|
const timeout = setTimeout(() => {
|
||||||
// Abort the request after 10 seconds
|
// Abort the request after 2 seconds
|
||||||
abortController.abort();
|
abortController.abort();
|
||||||
}, 2000);
|
}, 2000);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user