autoquizzer / backend /custom_components.py
anakin87's picture
update Haystack and simplify
083f13e
raw
history blame
907 Bytes
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}