2023-08-03 15:52:20 +01:00
import { Configuration , OpenAIApi } from 'openai-edge' ;
import { OpenAIStream , StreamingTextResponse } from 'ai' ;
import { OPENAI_API_KEY } from '$env/static/private' ;
// Create an OpenAI API client (that's edge friendly!)
const openAIConfig = new Configuration ({
apiKey : OPENAI_API_KEY
});
const openai = new OpenAIApi ( openAIConfig );
// Set the runtime to edge for best performance
export const config = {
runtime : 'edge'
};
const instruction = {
2023-09-02 19:40:09 +01:00
outline : 'a structured lesson outline' ,
note : 'a sample lesson note' ,
activities : 'few interactive lesson activities to my lecture'
2023-08-03 15:52:20 +01:00
};
export async function POST ({ request }) {
const { prompt } = await request . json ();
2024-04-19 05:45:48 +01:00
const { courseTitle , lessonTitle , type , locale } = JSON . parse ( prompt );
2023-08-03 15:52:20 +01:00
const response = await openai . createChatCompletion ({
2024-05-15 23:49:51 +01:00
model : 'gpt-4o' ,
2023-08-03 15:52:20 +01:00
messages : [
2023-09-07 10:47:25 +00:00
{
role : 'system' ,
content : 'Act like an assistant of a lecturer'
},
2023-08-03 15:52:20 +01:00
{
role : 'user' ,
2024-04-19 05:45:48 +01:00
content : `Generate ${ instruction [ type ] } given the course title is " ${ courseTitle } " and the title of the lesson is " ${ lessonTitle } ". Format in HTML without any styling. MOST IMPORTANT DON'T include the title of the course and don't include the lesson title: " ${ lessonTitle } " in your output. Please make sure the content is well detailed and you output the content in this locale: ${ locale } `
2023-08-03 15:52:20 +01:00
}
],
stream : true
});
// Convert the response into a friendly text-stream
const stream = OpenAIStream ( response );
// Respond with the stream
return new StreamingTextResponse ( stream );
}