File size: 1,783 Bytes
e382003
 
 
d005419
 
 
 
 
88f9105
 
d005419
 
88f9105
 
 
 
 
d005419
88f9105
54af9e3
d005419
88f9105
 
d005419
 
 
 
54af9e3
d005419
88f9105
d005419
54af9e3
d005419
88f9105
d005419
54af9e3
 
88f9105
d005419
 
 
 
 
 
7dfaba7
d005419
88f9105
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from form.form import work_categories
from local_storage.entities import SavedDetails
from enums.enums import Questions


class LlmParser:

    @classmethod
    def parse_verification_prompt_answers(cls, llm_answer:str, details: list[SavedDetails] | None)\
            -> dict[Questions, str | None]:
        print(f"llm answer: {llm_answer}")
        answers = {}
        for d in details:
            answers.update(d.to_answers())

        unanswered_questions = sorted([k.value for k in Questions.values() if k not in answers])
        unanswered_questions = iter(unanswered_questions)
        i = 0
        question_id = next(unanswered_questions)
        lines = [l for l in llm_answer.split("\n") if len(l.strip()) > 0 and not l.strip().endswith("?")]
        while i < len(lines):
            if question_id is None:
                break
            line = lines[i].strip()
            if len(line) == 0:
                i += 1
            elif "null" in lines[i]:
                answers[Questions(question_id)] = None
                i += 1
                question_id = next(unanswered_questions, None)
            elif ":" in lines[i]:
                answers[Questions(question_id)] = line.split(":")[1]
                i += 1
                question_id = next(unanswered_questions, None)
            else:
                answers[Questions(question_id)] = line
                i += 1
                question_id = next(unanswered_questions, None)
        return answers

    @classmethod
    def parse_get_categories_answer(cls, category_answer) -> list[str]:
        categories = []
        for category in category_answer.split(";"):
            categories.extend([k for k, v in work_categories.items() if category.lower() in v.lower()])
        return categories