Skip to content

Commit 21de1f8

Browse files
dfokinaagnieszka-m
andauthored
docs: clean up docstrings of AnswerBuilder (#8094)
* answerbuilder docstrings * update the `replies` * Apply suggestions from code review Co-authored-by: Agnieszka Marzec <[email protected]> * Update answer_builder.py --------- Co-authored-by: Agnieszka Marzec <[email protected]>
1 parent e8598be commit 21de1f8

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

haystack/components/builders/answer_builder.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
@component
1515
class AnswerBuilder:
1616
"""
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.
1818
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
2025
21-
Usage example:
2226
```python
2327
from haystack.components.builders import AnswerBuilder
2428
@@ -32,20 +36,20 @@ def __init__(self, pattern: Optional[str] = None, reference_pattern: Optional[st
3236
Creates an instance of the AnswerBuilder component.
3337
3438
: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
3843
is used as the answer. If no capture group is present, the whole match is used as the answer.
3944
Examples:
4045
`[^\\n]+$` finds "this is an answer" in a string "this is an argument.\\nthis is an answer".
4146
`Answer: (.*)` finds "this is an answer" in a string "this is an argument. Answer: this is an answer".
4247
4348
: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.
4850
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]".
4953
"""
5054
if pattern:
5155
AnswerBuilder._check_num_groups_in_regex(pattern)
@@ -64,35 +68,37 @@ def run(
6468
reference_pattern: Optional[str] = None,
6569
):
6670
"""
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.
6872
6973
:param query:
70-
The query used in the prompts for the Generator.
74+
The input query used as the Generator prompt.
7175
: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.
7377
:param meta:
7478
The metadata returned by the Generator. If not specified, the generated answer will contain no metadata.
7579
: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.
7984
: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
8389
is used as the answer. If no capture group is present, the whole match is used as the answer.
8490
Examples:
8591
`[^\\n]+$` finds "this is an answer" in a string "this is an argument.\\nthis is an answer".
8692
`Answer: (.*)` finds "this is an answer" in a string
8793
"this is an argument. Answer: this is an answer".
8894
: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.
9296
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]".
9399
94100
: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.
96102
"""
97103
if not meta:
98104
meta = [{}] * len(replies)

0 commit comments

Comments
 (0)