14
14
@component
15
15
class AnswerBuilder :
16
16
"""
17
- Takes a query and the replies a Generator returns as input and parses them into GeneratedAnswer objects .
17
+ Converts a query and Generator replies into a ` GeneratedAnswer` object .
18
18
19
- Optionally, it also takes Documents and metadata from the Generator as inputs to enrich the GeneratedAnswer objects.
19
+ AnswerBuilder parses Generator replies using custom regular expressions.
20
+ Check out the usage example below to see how it works.
21
+ Optionally, it can also take documents and metadata from the Generator to add to the `GeneratedAnswer` object.
22
+ AnswerBuilder works with both non-chat and chat Generators.
23
+
24
+ ### Usage example
20
25
21
- Usage example:
22
26
```python
23
27
from haystack.components.builders import AnswerBuilder
24
28
@@ -32,20 +36,20 @@ def __init__(self, pattern: Optional[str] = None, reference_pattern: Optional[st
32
36
Creates an instance of the AnswerBuilder component.
33
37
34
38
:param pattern:
35
- The regular expression pattern to use to extract the answer text from the generator output.
36
- If not specified, the whole string is used as the answer. The regular expression can have at
37
- most one capture group. If a capture group is present, the text matched by the capture group
39
+ The regular expression pattern to extract the answer text from the Generator.
40
+ If not specified, the entire response is used as the answer.
41
+ The regular expression can have one capture group at most.
42
+ If present, the capture group text
38
43
is used as the answer. If no capture group is present, the whole match is used as the answer.
39
44
Examples:
40
45
`[^\\ n]+$` finds "this is an answer" in a string "this is an argument.\\ nthis is an answer".
41
46
`Answer: (.*)` finds "this is an answer" in a string "this is an argument. Answer: this is an answer".
42
47
43
48
:param reference_pattern:
44
- The regular expression pattern to use for parsing the document references.
45
- We assume that references are specified as indices of the input documents and that
46
- indices start at 1.
47
- Example: `\\ [(\\ d+)\\ ]` finds "1" in a string "this is an answer[1]".
49
+ The regular expression pattern used for parsing the document references.
48
50
If not specified, no parsing is done, and all documents are referenced.
51
+ References need to be specified as indices of the input documents and start at [1].
52
+ Example: `\\ [(\\ d+)\\ ]` finds "1" in a string "this is an answer[1]".
49
53
"""
50
54
if pattern :
51
55
AnswerBuilder ._check_num_groups_in_regex (pattern )
@@ -64,35 +68,37 @@ def run(
64
68
reference_pattern : Optional [str ] = None ,
65
69
):
66
70
"""
67
- Turns the output of a Generator into `Answer ` objects using regular expressions.
71
+ Turns the output of a Generator into `GeneratedAnswer ` objects using regular expressions.
68
72
69
73
:param query:
70
- The query used in the prompts for the Generator.
74
+ The input query used as the Generator prompt .
71
75
:param replies:
72
- The output of the Generator. Can be a list of strings or a list of ChatMessage objects.
76
+ The output of the Generator. Can be a list of strings or a list of ` ChatMessage` objects.
73
77
:param meta:
74
78
The metadata returned by the Generator. If not specified, the generated answer will contain no metadata.
75
79
:param documents:
76
- The documents used as input to the Generator. If `documents` are specified, they are added to the `Answer`
77
- objects. If both `documents` and `reference_pattern` are specified, the documents referenced in the
78
- Generator output are extracted from the input documents and added to the `Answer` objects.
80
+ The documents used as the Generator inputs. If specified, they are added to
81
+ the`GeneratedAnswer` objects.
82
+ If both `documents` and `reference_pattern` are specified, the documents referenced in the
83
+ Generator output are extracted from the input documents and added to the `GeneratedAnswer` objects.
79
84
:param pattern:
80
- The regular expression pattern to use to extract the answer text from the generator output.
81
- If not specified, the whole string is used as the answer. The regular expression can have at
82
- most one capture group. If a capture group is present, the text matched by the capture group
85
+ The regular expression pattern to extract the answer text from the Generator.
86
+ If not specified, the entire response is used as the answer.
87
+ The regular expression can have one capture group at most.
88
+ If present, the capture group text
83
89
is used as the answer. If no capture group is present, the whole match is used as the answer.
84
90
Examples:
85
91
`[^\\ n]+$` finds "this is an answer" in a string "this is an argument.\\ nthis is an answer".
86
92
`Answer: (.*)` finds "this is an answer" in a string
87
93
"this is an argument. Answer: this is an answer".
88
94
:param reference_pattern:
89
- The regular expression pattern to use for parsing the document references.
90
- We assume that references are specified as indices of the input documents and that indices start at 1.
91
- Example: `\\ [(\\ d+)\\ ]` finds "1" in a string "this is an answer[1]".
95
+ The regular expression pattern used for parsing the document references.
92
96
If not specified, no parsing is done, and all documents are referenced.
97
+ References need to be specified as indices of the input documents and start at [1].
98
+ Example: `\\ [(\\ d+)\\ ]` finds "1" in a string "this is an answer[1]".
93
99
94
100
:returns: A dictionary with the following keys:
95
- - `answers`: The answers obtained from the output of the generator
101
+ - `answers`: The answers received from the output of the Generator.
96
102
"""
97
103
if not meta :
98
104
meta = [{}] * len (replies )
0 commit comments