File size: 6,069 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 | # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import itertools
from enum import Enum
from math import inf
from typing import Any, Callable, Dict, List, Optional, Union
from haystack import component, default_from_dict, default_to_dict, logging
from haystack.core.component.types import Variadic
from haystack.dataclasses.answer import ExtractedAnswer, ExtractedTableAnswer, GeneratedAnswer
AnswerType = Union[GeneratedAnswer, ExtractedTableAnswer, ExtractedAnswer]
logger = logging.getLogger(__name__)
class JoinMode(Enum):
"""
Enum for AnswerJoiner join modes.
"""
CONCATENATE = "concatenate"
def __str__(self):
return self.value
@staticmethod
def from_str(string: str) -> "JoinMode":
"""
Convert a string to a JoinMode enum.
"""
enum_map = {e.value: e for e in JoinMode}
mode = enum_map.get(string)
if mode is None:
msg = f"Unknown join mode '{string}'. Supported modes in AnswerJoiner are: {list(enum_map.keys())}"
raise ValueError(msg)
return mode
@component
class AnswerJoiner:
"""
Merges multiple lists of `Answer` objects into a single list.
Use this component to combine answers from different Generators into a single list.
Currently, the component supports only one join mode: `CONCATENATE`.
This mode concatenates multiple lists of answers into a single list.
### Usage example
In this example, AnswerJoiner merges answers from two different Generators:
```python
from haystack.components.builders import AnswerBuilder
from haystack.components.joiners import AnswerJoiner
from haystack.core.pipeline import Pipeline
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
query = "What's Natural Language Processing?"
messages = [ChatMessage.from_system("You are a helpful, respectful and honest assistant. Be super concise."),
ChatMessage.from_user(query)]
pipe = Pipeline()
pipe.add_component("gpt-4o", OpenAIChatGenerator(model="gpt-4o"))
pipe.add_component("llama", OpenAIChatGenerator(model="gpt-3.5-turbo"))
pipe.add_component("aba", AnswerBuilder())
pipe.add_component("abb", AnswerBuilder())
pipe.add_component("joiner", AnswerJoiner())
pipe.connect("gpt-4o.replies", "aba")
pipe.connect("llama.replies", "abb")
pipe.connect("aba.answers", "joiner")
pipe.connect("abb.answers", "joiner")
results = pipe.run(data={"gpt-4o": {"messages": messages},
"llama": {"messages": messages},
"aba": {"query": query},
"abb": {"query": query}})
```
"""
def __init__(
self,
join_mode: Union[str, JoinMode] = JoinMode.CONCATENATE,
top_k: Optional[int] = None,
sort_by_score: bool = False,
):
"""
Creates an AnswerJoiner component.
:param join_mode:
Specifies the join mode to use. Available modes:
- `concatenate`: Concatenates multiple lists of Answers into a single list.
:param top_k:
The maximum number of Answers to return.
:param sort_by_score:
If `True`, sorts the documents by score in descending order.
If a document has no score, it is handled as if its score is -infinity.
"""
if isinstance(join_mode, str):
join_mode = JoinMode.from_str(join_mode)
join_mode_functions: Dict[JoinMode, Callable[[List[List[AnswerType]]], List[AnswerType]]] = {
JoinMode.CONCATENATE: self._concatenate
}
self.join_mode_function: Callable[[List[List[AnswerType]]], List[AnswerType]] = join_mode_functions[join_mode]
self.join_mode = join_mode
self.top_k = top_k
self.sort_by_score = sort_by_score
@component.output_types(answers=List[AnswerType])
def run(self, answers: Variadic[List[AnswerType]], top_k: Optional[int] = None):
"""
Joins multiple lists of Answers into a single list depending on the `join_mode` parameter.
:param answers:
Nested list of Answers to be merged.
:param top_k:
The maximum number of Answers to return. Overrides the instance's `top_k` if provided.
:returns:
A dictionary with the following keys:
- `answers`: Merged list of Answers
"""
answers_list = list(answers)
join_function = self.join_mode_function
output_answers: List[AnswerType] = join_function(answers_list)
if self.sort_by_score:
output_answers = sorted(
output_answers, key=lambda answer: answer.score if hasattr(answer, "score") else -inf, reverse=True
)
top_k = top_k or self.top_k
if top_k:
output_answers = output_answers[:top_k]
return {"answers": output_answers}
def _concatenate(self, answer_lists: List[List[AnswerType]]) -> List[AnswerType]:
"""
Concatenate multiple lists of Answers, flattening them into a single list and sorting by score.
:param answer_lists: List of lists of Answers to be flattened.
"""
return list(itertools.chain.from_iterable(answer_lists))
def to_dict(self) -> Dict[str, Any]:
"""
Serializes the component to a dictionary.
:returns:
Dictionary with serialized data.
"""
return default_to_dict(self, join_mode=str(self.join_mode), top_k=self.top_k, sort_by_score=self.sort_by_score)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "AnswerJoiner":
"""
Deserializes the component from a dictionary.
:param data:
The dictionary to deserialize from.
:returns:
The deserialized component.
"""
return default_from_dict(cls, data)
|