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 <div className="key-value-table"> 13 14 | | | 15 | --- | --- | 16 | **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) | 17 | **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. | 18 | **Output variables** | `answers`: A merged list of answers | 19 | **API reference** | [Joiners](/reference/joiners-api) | 20 | **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/joiners/answer_joiner.py | 21 22 </div> 23 24 ## Overvew 25 26 `AnswerJoiner` joins input lists of [`Answer`](../../concepts/data-classes.mdx#answer) objects from multiple connections and returns them as one list. 27 28 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. 29 30 ## Usage 31 32 In this simple example pipeline, the `AnswerJoiner` merges answers from two instances of Generators: 33 34 ```python 35 from haystack.components.builders import AnswerBuilder 36 from haystack.components.joiners import AnswerJoiner 37 38 from haystack.core.pipeline import Pipeline 39 40 from haystack.components.generators.chat import OpenAIChatGenerator 41 from haystack.dataclasses import ChatMessage 42 43 query = "What's Natural Language Processing?" 44 messages = [ 45 ChatMessage.from_system( 46 "You are a helpful, respectful and honest assistant. Be super concise.", 47 ), 48 ChatMessage.from_user(query), 49 ] 50 51 pipe = Pipeline() 52 pipe.add_component("gpt-4o", OpenAIChatGenerator(model="gpt-4o")) 53 pipe.add_component("llama", OpenAIChatGenerator(model="gpt-3.5-turbo")) 54 pipe.add_component("aba", AnswerBuilder()) 55 pipe.add_component("abb", AnswerBuilder()) 56 pipe.add_component("joiner", AnswerJoiner()) 57 58 pipe.connect("gpt-4o.replies", "aba") 59 pipe.connect("llama.replies", "abb") 60 pipe.connect("aba.answers", "joiner") 61 pipe.connect("abb.answers", "joiner") 62 63 results = pipe.run( 64 data={ 65 "gpt-4o": {"messages": messages}, 66 "llama": {"messages": messages}, 67 "aba": {"query": query}, 68 "abb": {"query": query}, 69 }, 70 ) 71 ```