@@ -331,6 +331,51 @@ export const KnowledgeForm: React.FunctionComponent<KnowledgeFormProps> = ({ kno
331
331
) ;
332
332
} ;
333
333
334
+ // New function to handle the button click and generate Q&A pairs
335
+ const handleGenerateQA = async ( seedExampleIndex : number ) => {
336
+ try {
337
+ // Ensure seedExampleIndex is valid
338
+ if ( seedExampleIndex < 0 || seedExampleIndex >= seedExamples . length ) {
339
+ throw new Error ( 'Invalid seed example index' ) ;
340
+ }
341
+
342
+ const context = seedExamples [ seedExampleIndex ] . context ;
343
+ const prompt = `Generate 3 question and answer pairs from the provided context. The output should be in the form of "Question 1" and "Answer 1" and next "Question 2" and "Answer 2" and so on. Here is the context:\n${ context } ` ;
344
+
345
+ // Make a request to the server-side route
346
+ const response = await fetch ( '/api/pr/qnaGen' , {
347
+ method : 'POST' ,
348
+ headers : {
349
+ 'Content-Type' : 'application/json'
350
+ } ,
351
+ body : JSON . stringify ( { question : prompt , systemRole : 'user' } )
352
+ } ) ;
353
+
354
+ if ( ! response . ok ) {
355
+ throw new Error ( 'Failed to generate Q&A pairs' ) ;
356
+ }
357
+
358
+ const data = await response . json ( ) ;
359
+
360
+ // Parse the response to extract Q&A pairs
361
+ const updatedQAPairs = seedExamples [ seedExampleIndex ] . questionAndAnswers . map ( ( qaPair , i ) => {
362
+ const questionMatch = data . match ( new RegExp ( `Question ${ i + 1 } : (.*?)(?:Answer|$)` ) ) ;
363
+ const answerMatch = data . match ( new RegExp ( `Answer ${ i + 1 } : (.*?)(?:Question|$)` ) ) ;
364
+
365
+ return {
366
+ ...qaPair ,
367
+ question : questionMatch ? questionMatch [ 1 ] . trim ( ) : qaPair . question ,
368
+ answer : answerMatch ? answerMatch [ 1 ] . trim ( ) : qaPair . answer
369
+ } ;
370
+ } ) ;
371
+
372
+ // Update state with new Q&A pairs
373
+ setSeedExamples ( seedExamples . map ( ( example , i ) => ( i === seedExampleIndex ? { ...example , questionAndAnswers : updatedQAPairs } : example ) ) ) ;
374
+ } catch ( error ) {
375
+ console . error ( 'Error generating Q&A pairs:' , error ) ;
376
+ }
377
+ } ;
378
+
334
379
const onCloseActionGroupAlert = ( ) => {
335
380
setActionGroupAlertContent ( undefined ) ;
336
381
} ;
@@ -429,15 +474,25 @@ export const KnowledgeForm: React.FunctionComponent<KnowledgeFormProps> = ({ kno
429
474
setFilePath = { setFilePath }
430
475
/>
431
476
432
- < KnowledgeSeedExample
433
- seedExamples = { seedExamples }
434
- handleContextInputChange = { handleContextInputChange }
435
- handleContextBlur = { handleContextBlur }
436
- handleQuestionInputChange = { handleQuestionInputChange }
437
- handleQuestionBlur = { handleQuestionBlur }
438
- handleAnswerInputChange = { handleAnswerInputChange }
439
- handleAnswerBlur = { handleAnswerBlur }
440
- />
477
+ { /* Iterate over each seed example and display it */ }
478
+ { seedExamples . map ( ( seedExample , index ) => (
479
+ < div key = { index } >
480
+ < KnowledgeSeedExample
481
+ seedExamples = { [ seedExample ] } // Pass each individual seed example
482
+ handleContextInputChange = { ( contextValue ) => handleContextInputChange ( index , contextValue ) }
483
+ handleContextBlur = { ( ) => handleContextBlur ( index ) }
484
+ handleQuestionInputChange = { ( qaIndex , questionValue ) => handleQuestionInputChange ( index , qaIndex , questionValue ) }
485
+ handleQuestionBlur = { ( qaIndex ) => handleQuestionBlur ( index , qaIndex ) }
486
+ handleAnswerInputChange = { ( qaIndex , answerValue ) => handleAnswerInputChange ( index , qaIndex , answerValue ) }
487
+ handleAnswerBlur = { ( qaIndex ) => handleAnswerBlur ( index , qaIndex ) }
488
+ />
489
+
490
+ { /* New Button to Generate Q&A Pairs for each seed example */ }
491
+ < Button variant = "primary" onClick = { ( ) => handleGenerateQA ( index ) } >
492
+ Generate Q& A Pairs
493
+ </ Button >
494
+ </ div >
495
+ ) ) }
441
496
442
497
< DocumentInformation
443
498
reset = { reset }
0 commit comments