Spaces:
Running
Running
File size: 907 Bytes
083f13e 8bd40a9 083f13e 8bd40a9 |
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 27 28 29 30 31 32 33 |
from typing import Dict, List
from haystack import component
import json
import json_repair
@component
class QuizParser:
@component.output_types(quiz=Dict)
def run(self, replies: List[str]):
reply = replies[0]
# even if prompted to respond with JSON only, sometimes the model returns a mix of JSON and text
first_index = min(reply.find("{"), reply.find("["))
last_index = max(reply.rfind("}"), reply.rfind("]")) + 1
json_portion = reply[first_index:last_index]
try:
quiz = json.loads(json_portion)
except json.JSONDecodeError:
# if the JSON is not well-formed, try to repair it
quiz = json_repair.loads(json_portion)
# sometimes the JSON contains a list instead of a dictionary
if isinstance(quiz, list):
quiz = quiz[0]
print(quiz)
return {"quiz": quiz}
|