File size: 1,822 Bytes
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from .pipelines import (
    quiz_generation_pipeline,
    web_rag_pipeline,
    closed_book_answer_pipeline,
)
from typing import Dict, Any, List, Tuple
import random


def generate_quiz(url: str) -> Dict[str, Any]:
    return quiz_generation_pipeline.run({"link_content_fetcher": {"urls": [url]}})[
        "quiz_parser"
    ]["quiz"]


def get_closed_book_answers(quiz: Dict[str, Any]) -> List[str]:
    topic = quiz["topic"]
    questions = quiz["questions"]

    answers = []

    for question in questions:
        answer = closed_book_answer_pipeline.run(
            {"prompt_builder": {"topic": topic, "question": question}}
        )["generator"]["replies"][0]
        # in some rare cases, the model answers "I don't know" or something similar
        if answer not in ["a", "b", "c", "d"]:
            answer = random.choice(["a", "b", "c", "d"])
        answers.append(answer)

    return answers


def get_web_rag_answers_and_snippets(quiz: Dict[str, Any]) -> Tuple:
    topic = quiz["topic"]
    questions = quiz["questions"]

    answers, snippets = [], []

    for question in questions:
        result = web_rag_pipeline.run(
            data={
                "websearch": {"query": question["question"]},
                "prompt_builder": {"topic": topic, "question": question},
            },
            include_outputs_from=["websearch", "generator"],
        )

        print(result)

        answer = result["generator"]["replies"][0]
        # in some rare cases, the model answers "I don't know" or something similar
        if answer not in ["a", "b", "c", "d"]:
            answer = random.choice(["a", "b", "c", "d"])
        snippets_ = [doc.content for doc in result["websearch"]["documents"]]
        answers.append(answer)
        snippets.append(snippets_)

    return answers, snippets