From a624628e1d1f79f3710f7632aad320870cee4d11 Mon Sep 17 00:00:00 2001 From: Brent Salisbury Date: Fri, 26 Jul 2024 14:09:53 -0400 Subject: [PATCH] Update knowledge submissions and edits to the v3 schema - Schema can be viewed at: https://github.com/instructlab/schema/blob/main/src/instructlab/schema/v3/ Signed-off-by: Brent Salisbury --- .../edit-submission/knowledge/[id]/page.tsx | 158 +++++---- src/components/Contribute/Knowledge/index.tsx | 319 ++++++++++++------ src/types/index.ts | 12 +- 3 files changed, 313 insertions(+), 176 deletions(-) diff --git a/src/app/edit-submission/knowledge/[id]/page.tsx b/src/app/edit-submission/knowledge/[id]/page.tsx index b0091ab1..187fe623 100644 --- a/src/app/edit-submission/knowledge/[id]/page.tsx +++ b/src/app/edit-submission/knowledge/[id]/page.tsx @@ -37,7 +37,7 @@ const EditKnowledgePage: React.FunctionComponent<{ params: { id: string } }> = ( const [body, setBody] = React.useState(''); const [email, setEmail] = React.useState(''); const [name, setName] = React.useState(''); - const [task_description, setTaskDescription] = React.useState(''); + const [document_outline, setDocumentOutline] = React.useState(''); const [domain, setDomain] = React.useState(''); const [repo, setRepo] = React.useState(''); const [commit, setCommit] = React.useState(''); @@ -47,8 +47,16 @@ const EditKnowledgePage: React.FunctionComponent<{ params: { id: string } }> = ( const [revision, setRevision] = React.useState(''); const [license_work, setLicenseWork] = React.useState(''); const [creators, setCreators] = React.useState(''); - const [questions, setQuestions] = React.useState([]); - const [answers, setAnswers] = React.useState([]); + const [seedExamples, setSeedExamples] = React.useState([ + { + context: '', + questions_and_answers: [ + { question: '', answer: '' }, + { question: '', answer: '' }, + { question: '', answer: '' } + ] + } + ]); const [error, setError] = React.useState(null); const [yamlFile, setYamlFile] = React.useState(null); const [attributionFile, setAttributionFile] = React.useState(null); @@ -97,13 +105,12 @@ const EditKnowledgePage: React.FunctionComponent<{ params: { id: string } }> = ( console.log('Parsed YAML data:', yamlData); // Populate the form fields with YAML data - setTaskDescription(yamlData.task_description); + setDocumentOutline(yamlData.document_outline); setDomain(yamlData.domain); setRepo(yamlData.document.repo); setCommit(yamlData.document.commit); setPatterns(yamlData.document.patterns.join(', ')); - setQuestions(yamlData.seed_examples.map((example) => example.question)); - setAnswers(yamlData.seed_examples.map((example) => example.answer)); + setSeedExamples(yamlData.seed_examples); // Set the file path from the current YAML file const currentFilePath = foundYamlFile.filename.split('/').slice(0, -1).join('/'); @@ -154,15 +161,18 @@ const EditKnowledgePage: React.FunctionComponent<{ params: { id: string } }> = ( created_by: githubUsername, version: SchemaVersion, domain, - task_description, + document_outline, document: { repo, commit, patterns: patterns.split(',').map((pattern) => pattern.trim()) }, - seed_examples: questions.map((question, index) => ({ - question, - answer: answers[index] + seed_examples: seedExamples.map((example) => ({ + context: example.context, + questions_and_answers: example.questions_and_answers.map((qa) => ({ + question: qa.question, + answer: qa.answer + })) })) }; @@ -304,35 +314,44 @@ Creator names: ${updatedAttributionData.creator_names} } }; - const handleInputChange = (index: number, type: string, value: string) => { - switch (type) { - case 'question': - setQuestions((prevQuestions) => { - const updatedQuestions = [...prevQuestions]; - updatedQuestions[index] = value; - return updatedQuestions; - }); - break; - case 'answer': - setAnswers((prevAnswers) => { - const updatedAnswers = [...prevAnswers]; - updatedAnswers[index] = value; - return updatedAnswers; - }); - break; - default: - break; + const handleInputChange = (exampleIndex: number, type: string, value: string, qaIndex?: number) => { + const updatedSeedExamples = [...seedExamples]; + if (type === 'context') { + updatedSeedExamples[exampleIndex].context = value; + } else if (qaIndex !== undefined) { + if (type === 'question') { + updatedSeedExamples[exampleIndex].questions_and_answers[qaIndex].question = value; + } else if (type === 'answer') { + updatedSeedExamples[exampleIndex].questions_and_answers[qaIndex].answer = value; + } } + setSeedExamples(updatedSeedExamples); + }; + + const addQuestionAnswerPair = (exampleIndex: number) => { + const updatedSeedExamples = [...seedExamples]; + updatedSeedExamples[exampleIndex].questions_and_answers.push({ question: '', answer: '' }); + setSeedExamples(updatedSeedExamples); }; - const addQuestionAnswerPair = () => { - setQuestions([...questions, '']); - setAnswers([...answers, '']); + const deleteQuestionAnswerPair = (exampleIndex: number, qaIndex: number) => { + const updatedSeedExamples = [...seedExamples]; + updatedSeedExamples[exampleIndex].questions_and_answers = updatedSeedExamples[exampleIndex].questions_and_answers.filter((_, i) => i !== qaIndex); + setSeedExamples(updatedSeedExamples); }; - const deleteQuestionAnswerPair = (index: number) => { - setQuestions(questions.filter((_, i) => i !== index)); - setAnswers(answers.filter((_, i) => i !== index)); + const addSeedExample = () => { + setSeedExamples([ + ...seedExamples, + { + context: '', + questions_and_answers: [ + { question: '', answer: '' }, + { question: '', answer: '' }, + { question: '', answer: '' } + ] + } + ]); }; const parseAttributionContent = (content: string): AttributionData => { @@ -409,10 +428,10 @@ Creator names: ${updatedAttributionData.creator_names}