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 = {
outline : 'lesson outline' ,
note : 'lesson note' ,
activities : 'interactive lesson activities'
};
export async function POST ({ request }) {
const { prompt } = await request . json ();
const { courseTitle , lessonTitle , type } = prompt ;
console . log ( 'prompt' , prompt );
2023-08-03 23:34:54 +01:00
console . time ( 'debugOpenAi' );
2023-08-03 15:52:20 +01:00
const response = await openai . createChatCompletion ({
model : 'gpt-3.5-turbo' ,
messages : [
{
role : 'user' ,
2023-08-22 11:01:09 +00:00
content : `Generate a ${ instruction [ type ] } given the course title is " ${ courseTitle } " and the lesson is " ${ lessonTitle } ". Format in HTML without any styling, don't include the title of the course and title of the lesson in the result & make sure the content is well detailed.`
2023-08-03 15:52:20 +01:00
}
],
stream : true
});
2023-08-03 23:34:54 +01:00
console . timeEnd ( 'debugOpenAi' );
2023-08-03 15:52:20 +01:00
// Convert the response into a friendly text-stream
const stream = OpenAIStream ( response );
// Respond with the stream
return new StreamingTextResponse ( stream );
}