@@ -7,11 +7,11 @@ slug: "/builders-api"
77
88<a id =" answer_builder " ></a >
99
10- # Module answer\_ builder
10+ ## Module answer\_ builder
1111
1212<a id =" answer_builder.AnswerBuilder " ></a >
1313
14- ## AnswerBuilder
14+ ### AnswerBuilder
1515
1616Converts a query and Generator replies into a ` GeneratedAnswer ` object.
1717
@@ -22,12 +22,47 @@ AnswerBuilder works with both non-chat and chat Generators.
2222
2323### Usage example
2424
25+
26+ ### Usage example with documents and reference pattern
27+
2528``` python
2629from haystack.components.builders import AnswerBuilder
2730
2831builder = AnswerBuilder(pattern = " Answer: (.*)" )
2932builder.run(query = " What's the answer?" , replies = [" This is an argument. Answer: This is the answer." ])
3033```
34+ ``` python
35+ from haystack import Document
36+ from haystack.components.builders import AnswerBuilder
37+
38+ replies = [" The capital of France is Paris [2]." ]
39+
40+ docs = [
41+ Document(content = " Berlin is the capital of Germany." ),
42+ Document(content = " Paris is the capital of France." ),
43+ Document(content = " Rome is the capital of Italy." ),
44+ ]
45+
46+ builder = AnswerBuilder(reference_pattern = " \[(\d+)\]" , return_only_referenced_documents = False )
47+ result = builder.run(query = " What is the capital of France?" , replies = replies, documents = docs)[" answers" ][0 ]
48+
49+ print (f " Answer: { result.data} " )
50+ print (" References:" )
51+ for doc in result.documents:
52+ if doc.meta[" referenced" ]:
53+ print (f " [ { doc.meta[' source_index' ]} ] { doc.content} " )
54+ print (" Other sources:" )
55+ for doc in result.documents:
56+ if not doc.meta[" referenced" ]:
57+ print (f " [ { doc.meta[' source_index' ]} ] { doc.content} " )
58+
59+ # Answer: The capital of France is Paris
60+ # References:
61+ # [2] Paris is the capital of France.
62+ # Other sources:
63+ # [1] Berlin is the capital of Germany.
64+ # [3] Rome is the capital of Italy.
65+ ```
3166
3267<a id =" answer_builder.AnswerBuilder.__init__ " ></a >
3368
@@ -36,7 +71,9 @@ builder.run(query="What's the answer?", replies=["This is an argument. Answer: T
3671``` python
3772def __init__ (pattern : Optional[str ] = None ,
3873 reference_pattern : Optional[str ] = None ,
39- last_message_only : bool = False )
74+ last_message_only : bool = False ,
75+ * ,
76+ return_only_referenced_documents : bool = True )
4077```
4178
4279Creates an instance of the AnswerBuilder component.
@@ -52,11 +89,16 @@ Examples:
5289 ` [^\n]+$ ` finds "this is an answer" in a string "this is an argument.\nthis is an answer".
5390 ` Answer: (.*) ` finds "this is an answer" in a string "this is an argument. Answer: this is an answer".
5491- ` reference_pattern ` : The regular expression pattern used for parsing the document references.
55- If not specified, no parsing is done, and all documents are referenced .
92+ If not specified, no parsing is done, and all documents are returned .
5693References need to be specified as indices of the input documents and start at [ 1] .
5794Example: ` \[(\d+)\] ` finds "1" in a string "this is an answer[ 1] ".
95+ If this parameter is provided, documents metadata will contain a "referenced" key with a boolean value.
5896- ` last_message_only ` : If False (default value), all messages are used as the answer.
5997If True, only the last message is used as the answer.
98+ - ` return_only_referenced_documents ` : To be used in conjunction with ` reference_pattern ` .
99+ If True (default value), only the documents that were actually referenced in ` replies ` are returned.
100+ If False, all documents are returned.
101+ If ` reference_pattern ` is not provided, this parameter has no effect, and all documents are returned.
60102
61103<a id =" answer_builder.AnswerBuilder.run " ></a >
62104
@@ -80,9 +122,12 @@ Turns the output of a Generator into `GeneratedAnswer` objects using regular exp
80122- ` replies ` : The output of the Generator. Can be a list of strings or a list of ` ChatMessage ` objects.
81123- ` meta ` : The metadata returned by the Generator. If not specified, the generated answer will contain no metadata.
82124- ` documents ` : The documents used as the Generator inputs. If specified, they are added to
83- the` GeneratedAnswer ` objects.
84- If both ` documents ` and ` reference_pattern ` are specified, the documents referenced in the
85- Generator output are extracted from the input documents and added to the ` GeneratedAnswer ` objects.
125+ the ` GeneratedAnswer ` objects.
126+ Each Document.meta includes a "source_index" key, representing its 1-based position in the input list.
127+ When ` reference_pattern ` is provided:
128+ - "referenced" key is added to the Document.meta, indicating if the document was referenced in the output.
129+ - ` return_only_referenced_documents ` init parameter controls if all or only referenced documents are
130+ returned.
86131- ` pattern ` : The regular expression pattern to extract the answer text from the Generator.
87132If not specified, the entire response is used as the answer.
88133The regular expression can have one capture group at most.
@@ -93,7 +138,7 @@ is used as the answer. If no capture group is present, the whole match is used a
93138 ` Answer: (.*) ` finds "this is an answer" in a string
94139 "this is an argument. Answer: this is an answer".
95140- ` reference_pattern ` : The regular expression pattern used for parsing the document references.
96- If not specified, no parsing is done, and all documents are referenced .
141+ If not specified, no parsing is done, and all documents are returned .
97142References need to be specified as indices of the input documents and start at [ 1] .
98143Example: ` \[(\d+)\] ` finds "1" in a string "this is an answer[ 1] ".
99144
@@ -104,11 +149,11 @@ A dictionary with the following keys:
104149
105150<a id =" prompt_builder " ></a >
106151
107- # Module prompt\_ builder
152+ ## Module prompt\_ builder
108153
109154<a id =" prompt_builder.PromptBuilder " ></a >
110155
111- ## PromptBuilder
156+ ### PromptBuilder
112157
113158Renders a prompt filling in any variables so that it can send it to a Generator.
114159
@@ -306,11 +351,11 @@ A dictionary with the following keys:
306351
307352<a id =" chat_prompt_builder " ></a >
308353
309- # Module chat\_ prompt\_ builder
354+ ## Module chat\_ prompt\_ builder
310355
311356<a id =" chat_prompt_builder.ChatPromptBuilder " ></a >
312357
313- ## ChatPromptBuilder
358+ ### ChatPromptBuilder
314359
315360Renders a chat prompt from a template using Jinja2 syntax.
316361
0 commit comments