Repository: deepset-ai/haystack

Issue:
Agent disregards multiline 'Final Answer' 
It seems that the agent disregards the contents after the `final_answer_pattern` if it contains more than one line. 
You will see the answer in the stream output, but it does not get assigned to the result['answers'] past the first line. 

Example:
We run 
```
result = agent.run(question)
```
Agent ends with
"Final Answer: 
acegikmo@mastodon.social: "Did you see the news about Joe Jonas and Sophie Turner? Quite the shocker, wasn't it? Felt like they were solid."

aeva@mastodon.gamedev.place: "Indeed, their split was surprising. It's a stark reminder that public perceptions often don't mirror private realities. On a related note, it's interesting how personal relationships can impact one's professional life, even in our field of game development."

```
result["answers"]
```
the above is

```
[
0:"<Answer {'answer': 'acegikmo@mastodon.social: "Did you see the news about Joe Jonas and Sophie Turner? Quite the shocker, wasn\'t it? Felt like they were solid.', 'type': 'generative', 'score': None, 'context': None, 'offsets_in_document': None, 'offsets_in_context': None, 'document_ids': None, 'meta': {}}>"
]
```



Discussion:
I think this is because of the [final_answer_pattern](https://github.com/deepset-ai/haystack/blob/81b2e83d04dc9e695d2bd02c5031c20fb53bf841/haystack/agents/base.py#L219) ends at a newline.
ChatGPT is really cool in explaining what those nasty regexes do:

The regex pattern Final Answer\s*:\s*(.*) is designed to match and extract certain text from a given string. Let's break down this pattern piece by piece:

Final Answer: This matches the exact sequence of characters "Final Answer". It ensures that any string the regex processes should contain this exact sequence for the pattern to match.

\s*: This matches zero or more whitespace characters. The character \s stands for any whitespace character (like spaces, tabs, or newlines), and the * allows for any number (including zero) of them.

:: This matches the colon character.

\s*: Again, this matches zero or more whitespace characters.

(.*): This is a capturing group that matches any sequence of characters **(except for a newline).**

.: Matches any character except for a newline.
*: Matches zero or more occurrences of the preceding element.

Resolving patch:
diff --git a/haystack/agents/base.py b/haystack/agents/base.py
--- a/haystack/agents/base.py
+++ b/haystack/agents/base.py
@@ -214,7 +214,7 @@ def __init__(
         memory: Optional[Memory] = None,
         prompt_parameters_resolver: Optional[Callable] = None,
         max_steps: int = 8,
-        final_answer_pattern: str = r"Final Answer\s*:\s*(.*)",
+        final_answer_pattern: str = r"(?s)Final Answer\s*:\s*(.*)",
         streaming: bool = True,
     ):
         """


Test patch:
diff --git a/test/agents/test_agent.py b/test/agents/test_agent.py
--- a/test/agents/test_agent.py
+++ b/test/agents/test_agent.py
@@ -149,6 +149,7 @@ def test_extract_final_answer():
         "Final Answer:42",
         "Final Answer:   ",
         "Final Answer:    The answer is 99    ",
+        "Final Answer:42 should be the answer\n\nBut it's not the only one\n\n\n\n the answer is 1948",
     ]
     expected_answers = [
         "Florida",
@@ -159,10 +160,11 @@ def test_extract_final_answer():
         "42",
         "",
         "The answer is 99",
+        "42 should be the answer\n\nBut it's not the only one\n\n\n\n the answer is 1948",
     ]
 
     for example, expected_answer in zip(match_examples, expected_answers):
-        agent_step = AgentStep(prompt_node_response=example, final_answer_pattern=r"Final Answer\s*:\s*(.*)")
+        agent_step = AgentStep(prompt_node_response=example, final_answer_pattern=r"(?s)Final Answer\s*:\s*(.*)")
         final_answer = agent_step.final_answer(query="irrelevant")
         assert final_answer["answers"][0].answer == expected_answer