feat: add custom route to export data for LLM
This commit is contained in:
@@ -2,7 +2,43 @@
|
||||
|
||||
const { createCoreController } = require('@strapi/strapi').factories;
|
||||
|
||||
const VALID_LANGS = new Set(['fr', 'en', 'es', 'de', 'it'])
|
||||
|
||||
module.exports = createCoreController('api::parole.parole', ({strapi}) => ({
|
||||
async export(ctx) {
|
||||
const { type = 'pairs', lang, format = 'jsonl' } = ctx.query
|
||||
|
||||
const langs = lang
|
||||
? lang.split(',').map(l => l.trim()).filter(l => VALID_LANGS.has(l))
|
||||
: null
|
||||
|
||||
if (lang && (!langs || langs.length === 0)) {
|
||||
return ctx.badRequest('Langue(s) invalide(s). Valeurs acceptées : fr, en, es, de, it.')
|
||||
}
|
||||
|
||||
if (!['pairs', 'instruct'].includes(type)) {
|
||||
return ctx.badRequest('type invalide. Valeurs acceptées : pairs, instruct.')
|
||||
}
|
||||
|
||||
const paroles = await strapi.service('api::parole.parole').fetchAllParoles()
|
||||
const { metadata, pairs } = strapi.service('api::parole.parole').buildExport(paroles, type, langs)
|
||||
|
||||
if (format === 'json') {
|
||||
return ctx.send({ metadata, data: pairs })
|
||||
}
|
||||
|
||||
// JSONL : première ligne = métadonnées, suivies des exemples d'entraînement.
|
||||
// Pour filtrer la ligne de métadonnées : jq 'select(._metadata | not)'
|
||||
const lines = [
|
||||
JSON.stringify({ _metadata: true, ...metadata }),
|
||||
...pairs.map(p => JSON.stringify(p)),
|
||||
]
|
||||
|
||||
ctx.set('Content-Type', 'application/x-ndjson')
|
||||
ctx.set('Content-Disposition', `attachment; filename="pawol-nu-export-${Date.now()}.jsonl"`)
|
||||
ctx.body = lines.join('\n')
|
||||
},
|
||||
|
||||
async findOne(documentId) {
|
||||
const parole = await strapi.documents('api::parole.parole').findOne({
|
||||
documentId,
|
||||
|
||||
Reference in New Issue
Block a user