mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-11-30 17:06:08 +00:00
fix: append runtime meta to ChatMessage's extracted meta in AnswerBuilder (#8544)
* append runtime meta to extracted meta * add pylint ignore flag to .run() * explicitly convert reply to string
This commit is contained in:
parent
2cc45dd5b9
commit
b1353f4f0f
@ -58,7 +58,7 @@ class AnswerBuilder:
|
||||
self.reference_pattern = reference_pattern
|
||||
|
||||
@component.output_types(answers=List[GeneratedAnswer])
|
||||
def run(
|
||||
def run( # pylint: disable=too-many-positional-arguments
|
||||
self,
|
||||
query: str,
|
||||
replies: Union[List[str], List[ChatMessage]],
|
||||
@ -111,10 +111,13 @@ class AnswerBuilder:
|
||||
pattern = pattern or self.pattern
|
||||
reference_pattern = reference_pattern or self.reference_pattern
|
||||
all_answers = []
|
||||
for reply, metadata in zip(replies, meta):
|
||||
for reply, given_metadata in zip(replies, meta):
|
||||
# Extract content from ChatMessage objects if reply is a ChatMessages, else use the string as is
|
||||
extracted_reply: str = reply.content if isinstance(reply, ChatMessage) else reply # type: ignore
|
||||
extracted_metadata = reply.meta if isinstance(reply, ChatMessage) else metadata
|
||||
extracted_reply = reply.content if isinstance(reply, ChatMessage) else str(reply)
|
||||
extracted_metadata = reply.meta if isinstance(reply, ChatMessage) else {}
|
||||
|
||||
extracted_metadata = {**extracted_metadata, **given_metadata}
|
||||
|
||||
referenced_docs = []
|
||||
if documents:
|
||||
if reference_pattern:
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
---
|
||||
fixes:
|
||||
- |
|
||||
When meta is passed into AnswerBuilder.run(), it is now merged into GeneratedAnswer meta
|
||||
@ -36,6 +36,16 @@ class TestAnswerBuilder:
|
||||
assert answers[0].documents == []
|
||||
assert isinstance(answers[0], GeneratedAnswer)
|
||||
|
||||
def test_run_with_meta(self):
|
||||
component = AnswerBuilder()
|
||||
output = component.run(query="query", replies=["reply1"], meta=[{"test": "meta"}])
|
||||
answers = output["answers"]
|
||||
assert answers[0].data == "reply1"
|
||||
assert answers[0].meta == {"test": "meta"}
|
||||
assert answers[0].query == "query"
|
||||
assert answers[0].documents == []
|
||||
assert isinstance(answers[0], GeneratedAnswer)
|
||||
|
||||
def test_run_without_pattern(self):
|
||||
component = AnswerBuilder()
|
||||
output = component.run(query="test query", replies=["Answer: AnswerString"], meta=[{}])
|
||||
@ -272,3 +282,45 @@ class TestAnswerBuilder:
|
||||
assert answers[0].query == "test query"
|
||||
assert answers[0].documents == []
|
||||
assert isinstance(answers[0], GeneratedAnswer)
|
||||
|
||||
def test_run_with_chat_message_replies_with_meta_set_at_run_time(self):
|
||||
component = AnswerBuilder()
|
||||
replies = [
|
||||
ChatMessage(
|
||||
content="AnswerString",
|
||||
role=ChatRole.ASSISTANT,
|
||||
name=None,
|
||||
meta={
|
||||
"model": "gpt-4o-mini",
|
||||
"index": 0,
|
||||
"finish_reason": "stop",
|
||||
"usage": {"prompt_tokens": 32, "completion_tokens": 153, "total_tokens": 185},
|
||||
},
|
||||
)
|
||||
]
|
||||
output = component.run(query="test query", replies=replies, meta=[{"test": "meta"}])
|
||||
answers = output["answers"]
|
||||
assert len(answers) == 1
|
||||
assert answers[0].data == "AnswerString"
|
||||
assert answers[0].meta == {
|
||||
"model": "gpt-4o-mini",
|
||||
"index": 0,
|
||||
"finish_reason": "stop",
|
||||
"usage": {"prompt_tokens": 32, "completion_tokens": 153, "total_tokens": 185},
|
||||
"test": "meta",
|
||||
}
|
||||
assert answers[0].query == "test query"
|
||||
assert answers[0].documents == []
|
||||
assert isinstance(answers[0], GeneratedAnswer)
|
||||
|
||||
def test_run_with_chat_message_no_meta_with_meta_set_at_run_time(self):
|
||||
component = AnswerBuilder()
|
||||
replies = [ChatMessage(content="AnswerString", role=ChatRole.ASSISTANT, name=None, meta={})]
|
||||
output = component.run(query="test query", replies=replies, meta=[{"test": "meta"}])
|
||||
answers = output["answers"]
|
||||
assert len(answers) == 1
|
||||
assert answers[0].data == "AnswerString"
|
||||
assert answers[0].meta == {"test": "meta"}
|
||||
assert answers[0].query == "test query"
|
||||
assert answers[0].documents == []
|
||||
assert isinstance(answers[0], GeneratedAnswer)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user