answerjoiner.mdx
1 --- 2 title: "AnswerJoiner" 3 id: answerjoiner 4 slug: "/answerjoiner" 5 description: "Merges multiple answers from different Generators into a single list." 6 --- 7 8 # AnswerJoiner 9 10 Merges multiple answers from different Generators into a single list. 11 12 | | | 13 | :------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 14 | **Most common position in a pipeline** | In query pipelines, after [Generators](../generators.mdx) and, subsequently, components that return a list of answers such as [`AnswerBuilder`](../builders/answerbuilder.mdx) | 15 | **Mandatory run variables** | “answers”: A nested list of answers to be merged, received from the Generator. This input is `variadic`, meaning you can connect a variable number of components to it. | 16 | **Output variables** | “answers”: A merged list of answers | 17 | **API reference** | [Joiners](/reference/joiners-api) | 18 | **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/joiners/answer_joiner.py | 19 20 ## Overvew 21 22 `AnswerJoiner` joins input lists of [`Answer`](../../concepts/data-classes.mdx#answer) objects from multiple connections and returns them as one list. 23 24 You can optionally set the `top_k` parameter, which specifies the maximum number of answers to return. If you don’t set this parameter, the component returns all answers it receives. 25 26 ## Usage 27 28 In this simple example pipeline, the `AnswerJoiner` merges answers from two instances of Generators: 29 30 ```python 31 from haystack.components.builders import AnswerBuilder 32 from haystack.components.joiners import AnswerJoiner 33 34 from haystack.core.pipeline import Pipeline 35 36 from haystack.components.generators.chat import OpenAIChatGenerator 37 from haystack.dataclasses import ChatMessage 38 39 query = "What's Natural Language Processing?" 40 messages = [ 41 ChatMessage.from_system( 42 "You are a helpful, respectful and honest assistant. Be super concise.", 43 ), 44 ChatMessage.from_user(query), 45 ] 46 47 pipe = Pipeline() 48 pipe.add_component("gpt-4o", OpenAIChatGenerator(model="gpt-4o")) 49 pipe.add_component("llama", OpenAIChatGenerator(model="gpt-3.5-turbo")) 50 pipe.add_component("aba", AnswerBuilder()) 51 pipe.add_component("abb", AnswerBuilder()) 52 pipe.add_component("joiner", AnswerJoiner()) 53 54 pipe.connect("gpt-4o.replies", "aba") 55 pipe.connect("llama.replies", "abb") 56 pipe.connect("aba.answers", "joiner") 57 pipe.connect("abb.answers", "joiner") 58 59 results = pipe.run( 60 data={ 61 "gpt-4o": {"messages": messages}, 62 "llama": {"messages": messages}, 63 "aba": {"query": query}, 64 "abb": {"query": query}, 65 }, 66 ) 67 ```