File size: 2,193 Bytes
64772a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from dataclasses import asdict, dataclass
from typing import Any, Dict, Mapping, Optional, Sequence

from aleph_alpha_client.document import Document


@dataclass(frozen=True)
class QaRequest:
    """DEPRECATED: `QaRequest` is deprecated and will be removed in the future. New
    methods of processing Q&A tasks will be provided before this is removed.

    Answers a question about a prompt.

    Parameters:
        query (str, required):
            The question to be answered about the documents by the model.

        documents (List[Document], required):
            A list of documents. This can be either docx documents or text/image prompts.

        max_answers (int, default None):
            The maximum number of answers.

        Examples:
            >>> request = QaRequest(
                    query = "What is a computer program?",
                    documents = [document]
                )
    """

    query: str
    documents: Sequence[Document]
    max_answers: Optional[int] = None

    def to_json(self) -> Mapping[str, Any]:
        payload = {
            **self._asdict(),
            "documents": [
                document._to_serializable_document() for document in self.documents
            ],
        }
        if self.max_answers is None:
            del payload["max_answers"]
        return payload

    def _asdict(self) -> Mapping[str, Any]:
        return asdict(self)


@dataclass(frozen=True)
class QaAnswer:
    """DEPRECATED: `QaAnswer` is deprecated and will be removed in the future. New
    methods of processing Q&A tasks will be provided before this is removed.
    """

    answer: str
    score: float
    evidence: str


@dataclass(frozen=True)
class QaResponse:
    """DEPRECATED: `QaResponse` is deprecated and will be removed in the future. New
    methods of processing Q&A tasks will be provided before this is removed.
    """

    answers: Sequence[QaAnswer]

    @staticmethod
    def from_json(json: Mapping[str, Any]) -> "QaResponse":
        return QaResponse(
            answers=[
                QaAnswer(item["answer"], item["score"], item["evidence"])
                for item in json["answers"]
            ],
        )