Skip to content

Commit

Permalink
Add support for multiline answers in ReActOutputParser (Fixes #7690) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jensmeder authored Oct 9, 2023
1 parent 1425fe4 commit 6c60b86
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Enable `codespell` for markdown docs (#7972)

### Bug Fixes / Nits
- Parse multi-line outputs in react agent answers (#8029)
- Add properly named kwargs to keyword `as_retriever` calls (#8011)

## [0.8.41] - 2023-10-07
Expand Down
2 changes: 1 addition & 1 deletion llama_index/agent/react/output_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def extract_tool_use(input_text: str) -> Tuple[str, str, str]:


def extract_final_response(input_text: str) -> Tuple[str, str]:
pattern = r"\s*Thought:(.*?)Answer:(.*?)(?:\n|$)"
pattern = r"\s*Thought:(.*?)Answer:(.*?)(?:$)"

match = re.search(pattern, input_text, re.DOTALL)
if not match:
Expand Down
22 changes: 22 additions & 0 deletions tests/agent/react/test_react_output_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,25 @@ def test_extract_final_response() -> None:
thought, answer = extract_final_response(mock_input_text)
assert thought == expected_thought
assert answer == "2"


def test_extract_final_response_multiline_answer() -> None:
mock_input_text = """\
Thought: I have enough information to answer the question without using any more tools.
Answer: Here is the answer:
This is the second line.
"""

expected_thought = (
"I have enough information to answer the question "
"without using any more tools."
)
thought, answer = extract_final_response(mock_input_text)
assert thought == expected_thought
assert (
answer
== """Here is the answer:
This is the second line."""
)

0 comments on commit 6c60b86

Please sign in to comment.