File size: 6,074 Bytes
4ff79c6 | 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import io
from dataclasses import asdict, dataclass, field
from typing import Any, Dict, List, Optional, Protocol, runtime_checkable
from pandas import DataFrame, read_json
from haystack.core.serialization import default_from_dict, default_to_dict
from haystack.dataclasses.document import Document
@runtime_checkable
@dataclass
class Answer(Protocol):
data: Any
query: str
meta: Dict[str, Any]
def to_dict(self) -> Dict[str, Any]: # noqa: D102
...
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "Answer": # noqa: D102
...
@dataclass
class ExtractedAnswer:
query: str
score: float
data: Optional[str] = None
document: Optional[Document] = None
context: Optional[str] = None
document_offset: Optional["Span"] = None
context_offset: Optional["Span"] = None
meta: Dict[str, Any] = field(default_factory=dict)
@dataclass
class Span:
start: int
end: int
def to_dict(self) -> Dict[str, Any]:
"""
Serialize the object to a dictionary.
:returns:
Serialized dictionary representation of the object.
"""
document = self.document.to_dict(flatten=False) if self.document is not None else None
document_offset = asdict(self.document_offset) if self.document_offset is not None else None
context_offset = asdict(self.context_offset) if self.context_offset is not None else None
return default_to_dict(
self,
data=self.data,
query=self.query,
document=document,
context=self.context,
score=self.score,
document_offset=document_offset,
context_offset=context_offset,
meta=self.meta,
)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "ExtractedAnswer":
"""
Deserialize the object from a dictionary.
:param data:
Dictionary representation of the object.
:returns:
Deserialized object.
"""
init_params = data.get("init_parameters", {})
if (doc := init_params.get("document")) is not None:
data["init_parameters"]["document"] = Document.from_dict(doc)
if (offset := init_params.get("document_offset")) is not None:
data["init_parameters"]["document_offset"] = ExtractedAnswer.Span(**offset)
if (offset := init_params.get("context_offset")) is not None:
data["init_parameters"]["context_offset"] = ExtractedAnswer.Span(**offset)
return default_from_dict(cls, data)
@dataclass
class ExtractedTableAnswer:
query: str
score: float
data: Optional[str] = None
document: Optional[Document] = None
context: Optional[DataFrame] = None
document_cells: List["Cell"] = field(default_factory=list)
context_cells: List["Cell"] = field(default_factory=list)
meta: Dict[str, Any] = field(default_factory=dict)
@dataclass
class Cell:
row: int
column: int
def to_dict(self) -> Dict[str, Any]:
"""
Serialize the object to a dictionary.
:returns:
Serialized dictionary representation of the object.
"""
document = self.document.to_dict(flatten=False) if self.document is not None else None
context = self.context.to_json() if self.context is not None else None
document_cells = [asdict(c) for c in self.document_cells]
context_cells = [asdict(c) for c in self.context_cells]
return default_to_dict(
self,
data=self.data,
query=self.query,
document=document,
context=context,
score=self.score,
document_cells=document_cells,
context_cells=context_cells,
meta=self.meta,
)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "ExtractedTableAnswer":
"""
Deserialize the object from a dictionary.
:param data:
Dictionary representation of the object.
:returns:
Deserialized object.
"""
init_params = data.get("init_parameters", {})
if (doc := init_params.get("document")) is not None:
data["init_parameters"]["document"] = Document.from_dict(doc)
if (context := init_params.get("context")) is not None:
data["init_parameters"]["context"] = read_json(io.StringIO(context))
if (cells := init_params.get("document_cells")) is not None:
data["init_parameters"]["document_cells"] = [ExtractedTableAnswer.Cell(**c) for c in cells]
if (cells := init_params.get("context_cells")) is not None:
data["init_parameters"]["context_cells"] = [ExtractedTableAnswer.Cell(**c) for c in cells]
return default_from_dict(cls, data)
@dataclass
class GeneratedAnswer:
data: str
query: str
documents: List[Document]
meta: Dict[str, Any] = field(default_factory=dict)
def to_dict(self) -> Dict[str, Any]:
"""
Serialize the object to a dictionary.
:returns:
Serialized dictionary representation of the object.
"""
documents = [doc.to_dict(flatten=False) for doc in self.documents]
return default_to_dict(self, data=self.data, query=self.query, documents=documents, meta=self.meta)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "GeneratedAnswer":
"""
Deserialize the object from a dictionary.
:param data:
Dictionary representation of the object.
:returns:
Deserialized object.
"""
init_params = data.get("init_parameters", {})
if (documents := init_params.get("documents")) is not None:
data["init_parameters"]["documents"] = [Document.from_dict(d) for d in documents]
return default_from_dict(cls, data)
|