File size: 1,000 Bytes
085eaee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from typing import List, Dict
import json
import re

def parse_llm_response(content: str) -> List[Dict]:
        try:
            content = re.sub(r'```json\s*|\s*```', '', content)
            content = content.strip()
            if not content.startswith('['):
                content = f'[{content}]'
            if content.endswith(',]'):
                content = content[:-1] + ']'
            qa_pairs = json.loads(content)
            if not isinstance(qa_pairs, list):
                raise json.JSONDecodeError("Not a list", content, 0)
            return qa_pairs
        except json.JSONDecodeError as e:
            print(f"Error parsing JSON: {e}")
            return [{
                "question": "What is the main topic discussed in this text?",
                "answer": content[:150] + "...",
                "difficulty": 3,
                "type": "factual",
                "tags": ["auto-generated"],
                "metadata": {"error": "JSON parsing failed"}
            }]