hlsitechbot / pages /api /chatAPI.ts
hlsitech's picture
Upload 93 files
7bbd534 verified
raw
history blame contribute delete
755 Bytes
import { ChatBody } from '@/types/types';
import { OpenAIStream } from '@/utils/chatStream';
export const config = {
runtime: 'edge',
};
const handler = async (req: Request): Promise<Response> => {
try {
const { inputCode, model, apiKey } = (await req.json()) as ChatBody;
let apiKeyFinal;
if (apiKey) {
apiKeyFinal = apiKey;
} else {
apiKeyFinal = process.env.NEXT_PUBLIC_OPENAI_API_KEY;
}
if (!apiKey) {
return new Response('API key not found', { status: 500 });
}
const stream = await OpenAIStream(inputCode, model, apiKeyFinal);
return new Response(stream);
} catch (error) {
console.error(error);
return new Response('Error', { status: 500 });
}
};
export default handler;