Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

2.8 KB
Raw Blame
"""
Summary module tests
"""

import unittest

from txtai.pipeline import Summary


class TestSummary(unittest.TestCase):
    """
    Summary tests.
    """

    @classmethod
    def setUpClass(cls):
        """
        Create single summary instance.
        """

        cls.text = (
            "Search is the base of many applications. Once data starts to pile up, users want to be able to find it. It's the foundation "
            "of the internet and an ever-growing challenge that is never solved or done. The field of Natural Language Processing (NLP) is "
            "rapidly evolving with a number of new developments. Large-scale general language models are an exciting new capability "
            "allowing us to add amazing functionality quickly with limited compute and people. Innovation continues with new models "
            "and advancements coming in at what seems a weekly basis. This article introduces txtai, an AI-powered search engine "
            "that enables Natural Language Understanding (NLU) based search in any application."
        )

        cls.summary = Summary("t5-small")

    def testSummary(self):
        """
        Test summarization of text
        """

        self.assertEqual(self.summary(self.text, minlength=15, maxlength=15), "the field of natural language processing (NLP) is rapidly evolving")

    def testSummaryBatch(self):
        """
        Test batch summarization of text
        """

        summaries = self.summary([self.text, self.text], maxlength=15)
        self.assertEqual(len(summaries), 2)

    def testSummaryBatchLengths(self):
        """
        Test batch summarization of texts with different lengths
        """

        texts = [self.text, self.text + " " + self.text, self.text]
        summaries = self.summary(texts, maxlength=15)
        self.assertEqual(len(summaries), 3)
        self.assertTrue(all(summary and summary not in texts for summary in summaries))

        # Inputs that span multiple batches keep their order
        batchsize = self.summary.batchsize
        try:
            self.summary.batchsize = 2
            self.assertEqual(self.summary(texts, maxlength=15), summaries)
        finally:
            self.summary.batchsize = batchsize

    def testSummaryNoLength(self):
        """
        Test summary with no max length set
        """

        self.assertEqual(
            self.summary(self.text + self.text),
            "search is the base of many applications. Once data starts to pile up, users want to be able to find it. "
            + "Large-scale general language models are an exciting new capability allowing us to add amazing functionality quickly "
            + "with limited compute and people.",
        )

    def testSummaryShort(self):
        """
        Test that summarization is skipped
        """

        self.assertEqual(self.summary("Text", maxlength=15), "Text")