2022-11-03 16:04:53 +01:00
from ensurepip import version
2021-01-08 14:29:46 +01:00
import pytest
2022-11-03 16:04:53 +01:00
import haystack
2021-10-25 15:50:23 +02:00
from haystack . schema import Document
2021-11-15 18:49:49 +01:00
from haystack . pipelines import SearchSummarizationPipeline
2022-07-11 20:58:36 +05:30
from haystack . nodes import DensePassageRetriever , EmbeddingRetriever , TransformersSummarizer
2022-11-03 16:04:53 +01:00
from haystack . nodes . other . document_merger import DocumentMerger
2021-01-08 14:29:46 +01:00
2023-02-23 09:50:24 +01:00
pytestmark = pytest . mark . skip ( " Tests are too heavy for Github runners, skipping for now " )
2021-01-08 14:29:46 +01:00
DOCS = [
Document (
2022-03-07 19:25:33 +01:00
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. """
2021-01-08 14:29:46 +01:00
) ,
Document (
2021-10-13 14:23:23 +02:00
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. """
2022-02-03 13:43:18 +01:00
) ,
2021-01-08 14:29:46 +01:00
]
EXPECTED_SUMMARIES = [
2022-10-18 16:23:57 +02:00
" California ' s largest electricity provider, PG&E, has shut down power supplies to thousands of customers. " ,
" The Eiffel Tower in Paris has officially opened its doors to the public. " ,
2021-01-08 14:29:46 +01:00
]
SPLIT_DOCS = [
Document (
2021-10-13 14:23:23 +02:00
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. """
2021-01-08 14:29:46 +01:00
) ,
Document (
2021-10-13 14:23:23 +02:00
content = """ 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. """
2022-02-03 13:43:18 +01:00
) ,
2021-01-08 14:29:46 +01:00
]
# Documents order is very important to produce summary.
# Different order of same documents produce different summary.
EXPECTED_ONE_SUMMARIES = [
2022-10-18 16:23:57 +02:00
" The Eiffel Tower in Paris has officially opened its doors to the public. " ,
" The Eiffel Tower in Paris has become the tallest man-made structure in the world. " ,
2021-01-08 14:29:46 +01:00
]
2022-06-07 09:23:03 +02:00
@pytest.mark.integration
2021-01-08 14:29:46 +01:00
@pytest.mark.summarizer
def test_summarization ( summarizer ) :
summarized_docs = summarizer . predict ( documents = DOCS )
assert len ( summarized_docs ) == len ( DOCS )
for expected_summary , summary in zip ( EXPECTED_SUMMARIES , summarized_docs ) :
2022-11-03 16:04:53 +01:00
assert expected_summary == summary . meta [ " summary " ]
2021-01-08 14:29:46 +01:00
2022-06-07 09:23:03 +02:00
@pytest.mark.integration
2022-05-11 11:11:00 +02:00
@pytest.mark.summarizer
def test_summarization_batch_single_doc_list ( summarizer ) :
summarized_docs = summarizer . predict_batch ( documents = DOCS )
assert len ( summarized_docs ) == len ( DOCS )
for expected_summary , summary in zip ( EXPECTED_SUMMARIES , summarized_docs ) :
2022-11-03 16:04:53 +01:00
assert expected_summary == summary . meta [ " summary " ]
2022-05-11 11:11:00 +02:00
2022-06-07 09:23:03 +02:00
@pytest.mark.integration
2022-05-11 11:11:00 +02:00
@pytest.mark.summarizer
def test_summarization_batch_multiple_doc_lists ( summarizer ) :
summarized_docs = summarizer . predict_batch ( documents = [ DOCS , DOCS ] )
assert len ( summarized_docs ) == 2 # Number of document lists
assert len ( summarized_docs [ 0 ] ) == len ( DOCS )
for expected_summary , summary in zip ( EXPECTED_SUMMARIES , summarized_docs [ 0 ] ) :
2022-11-03 16:04:53 +01:00
assert expected_summary == summary . meta [ " summary " ]
2022-05-11 11:11:00 +02:00
2022-06-07 09:23:03 +02:00
@pytest.mark.integration
2021-01-08 14:29:46 +01:00
@pytest.mark.summarizer
@pytest.mark.parametrize (
2022-11-22 09:24:52 +01:00
" retriever,document_store " , [ ( " embedding " , " memory " ) , ( " bm25 " , " elasticsearch " ) ] , indirect = True
2021-01-08 14:29:46 +01:00
)
def test_summarization_pipeline ( document_store , retriever , summarizer ) :
document_store . write_documents ( DOCS )
if isinstance ( retriever , EmbeddingRetriever ) or isinstance ( retriever , DensePassageRetriever ) :
document_store . update_embeddings ( retriever = retriever )
query = " Where is Eiffel Tower? "
2021-09-10 11:41:16 +02:00
pipeline = SearchSummarizationPipeline ( retriever = retriever , summarizer = summarizer , return_in_answer_format = True )
2021-10-19 15:22:44 +02:00
output = pipeline . run ( query = query , params = { " Retriever " : { " top_k " : 1 } } )
2021-01-08 14:29:46 +01:00
answers = output [ " answers " ]
assert len ( answers ) == 1
2022-10-18 16:23:57 +02:00
assert " The Eiffel Tower in Paris has officially opened its doors to the public. " == answers [ 0 ] [ " answer " ]
2021-01-08 14:29:46 +01:00
2022-11-03 16:04:53 +01:00
#
# Document Merger + Summarizer tests
#
@pytest.mark.integration
@pytest.mark.summarizer
def test_summarization_one_summary ( summarizer ) :
dm = DocumentMerger ( )
merged_document = dm . merge ( documents = SPLIT_DOCS )
summarized_docs = summarizer . predict ( documents = merged_document )
assert len ( summarized_docs ) == 1
assert EXPECTED_ONE_SUMMARIES [ 0 ] == summarized_docs [ 0 ] . meta [ " summary " ]
2022-06-07 09:23:03 +02:00
@pytest.mark.integration
2021-01-08 14:29:46 +01:00
@pytest.mark.summarizer
@pytest.mark.parametrize (
2022-11-22 09:24:52 +01:00
" retriever,document_store " , [ ( " embedding " , " memory " ) , ( " bm25 " , " elasticsearch " ) ] , indirect = True
2021-01-08 14:29:46 +01:00
)
def test_summarization_pipeline_one_summary ( document_store , retriever , summarizer ) :
document_store . write_documents ( SPLIT_DOCS )
if isinstance ( retriever , EmbeddingRetriever ) or isinstance ( retriever , DensePassageRetriever ) :
document_store . update_embeddings ( retriever = retriever )
query = " Where is Eiffel Tower? "
2022-11-03 16:04:53 +01:00
pipeline = SearchSummarizationPipeline (
retriever = retriever , summarizer = summarizer , generate_single_summary = True , return_in_answer_format = True
2022-02-03 13:43:18 +01:00
)
2022-11-03 16:04:53 +01:00
output = pipeline . run ( query = query , params = { " Retriever " : { " top_k " : 2 } } )
2021-01-08 14:29:46 +01:00
answers = output [ " answers " ]
assert len ( answers ) == 1
assert answers [ 0 ] [ " answer " ] in EXPECTED_ONE_SUMMARIES