Created
July 31, 2023 16:52
-
-
Save dmassiani/3a66f1c74fb206c80345cebf8853f5ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Configuration, OpenAIApi } from "openai"; | |
import _ from 'lodash' | |
import { serverSupabaseClient } from '#supabase/server' | |
let client | |
const configuration = new Configuration({ | |
apiKey: process.env.OPENAI_API_KEY, | |
}); | |
const openai = new OpenAIApi(configuration); | |
const prompt = `Dans quel contexte est utilisé cette compétence professionnelle ? | |
compétence : ` | |
async function extractPrompt(activite, capacite) { | |
let prompt = `Pour ${capacite} | |
Comment ${activite} ? | |
réponse : | |
` | |
return prompt | |
} | |
const apprentissageExist = async (activite) => { | |
const { data, error, count } = await client | |
.from('apprentissages') | |
.select(`*`, {count: 'exact'}) | |
.eq('activite', activite.uid) | |
if(count == 0){ | |
return false | |
}else{ | |
return data | |
} | |
} | |
async function saveApprentissage(activite, apprentissage) { | |
const { error } = await client | |
.from('apprentissages') | |
.insert({ | |
certification: activite.certification, | |
activite: activite.uid, | |
apprentissage: apprentissage, | |
capacite: activite.skill | |
}) | |
return true | |
} | |
async function getApprentissageFromIA(activite, capacite){ | |
let prompt = await extractPrompt(activite, capacite); | |
const completion = await openai.createCompletion({ | |
model: "text-davinci-003", | |
prompt: prompt, | |
temperature: 1, | |
max_tokens: 500, | |
top_p: 1, | |
frequency_penalty: 0.0, | |
presence_penalty: 0.0, | |
best_of: 5, | |
n: 1 | |
}); | |
return completion.data.choices[0].text.replace(/([0-9]{1,2})\.\s?/g,'').trim(); | |
} | |
export default defineEventHandler(async (event) => { | |
client = serverSupabaseClient(event) | |
const body = await readBody(event) | |
let skill = body.skill | |
let apprentissages = [] | |
for (var activite of skill.skill_activities) { | |
let activityExist = await apprentissageExist(activite.activites) | |
let apprentissage | |
if(activityExist == false){ | |
apprentissage = await getApprentissageFromIA(activite.activites.libelle, skill.intitule) | |
await saveApprentissage(activite.activites, apprentissage) | |
}else{ | |
apprentissage = activityExist | |
} | |
apprentissages.push({ | |
activite: activite.activites, | |
apprentissage: apprentissage | |
}) | |
} | |
return { | |
api: 'works', | |
result: apprentissages | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment