Passing the meta-data in the summerizer response (#2179)

* Passing the all the meta-data in the summerizer

* Disable metadata forwarding if `generate_single_summary` is `True`

* Update Documentation & Code Style

* simplify tests

* Update Documentation & Code Style

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Sowmiya Jaganathan 2022-07-11 20:58:36 +05:30 committed by GitHub
parent 1706729e26
commit 4d8f40425b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 4 deletions

View File

@ -158,9 +158,15 @@ class TransformersSummarizer(BaseSummarizer):
result: List[Document] = []
for context, summarized_answer in zip(contexts, summaries):
cur_doc = Document(content=summarized_answer["summary_text"], meta={"context": context})
result.append(cur_doc)
if generate_single_summary:
for context, summarized_answer in zip(contexts, summaries):
cur_doc = Document(content=summarized_answer["summary_text"], meta={"context": context})
result.append(cur_doc)
else:
for context, summarized_answer, document in zip(contexts, summaries, documents):
cur_doc = Document(content=summarized_answer["summary_text"], meta=document.meta)
cur_doc.meta.update({"context": context})
result.append(cur_doc)
return result

View File

@ -1,8 +1,10 @@
from copy import deepcopy
import pytest
from haystack.schema import Document
from haystack.pipelines import SearchSummarizationPipeline
from haystack.nodes import DensePassageRetriever, EmbeddingRetriever
from haystack.nodes import DensePassageRetriever, EmbeddingRetriever, TransformersSummarizer
DOCS = [
Document(
@ -109,3 +111,38 @@ def test_summarization_pipeline_one_summary(document_store, retriever, summarize
answers = output["answers"]
assert len(answers) == 1
assert answers[0]["answer"] in EXPECTED_ONE_SUMMARIES
@pytest.mark.summarizer
def add_metadata_summerizer():
docs = [
Document(
content="""PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.""",
meta={
"sub_content": "Pegasus Example",
"topic": "California's Electricity",
"context": "Dummy - PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires.",
},
),
Document(
content="""The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.""",
meta={"sub_content": "Paris best tour best tour", "topic": "Eiffel tower"},
),
]
# Original input is overwrote after the "predict". So adding the same input as check_output to assess the output
check_output = deepcopy(docs)
summarizer = TransformersSummarizer(model_name_or_path="google/pegasus-xsum")
summary = summarizer.predict(documents=docs)
assert len(summary[0].meta) == len(check_output[0].meta)
assert len(summary[1].meta) - 1 == len(check_output[1].meta)
assert (
summary[0].meta["context"]
== """PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow."""
)
summary = summarizer.predict(documents=docs, generate_single_summary=True)
assert len(summary) == 1
assert not summary[0].meta # Metadata is not returned in case of a single summary