Spaces:
Running on Zero
Running on Zero
Download tests/test_generate.py from Varun2007/document-qa-system: direct link, hf CLI and curl.
- Browser
- Download file 1.8 kB
-
https://huggingface.co/spaces/Varun2007/document-qa-system/resolve/main/tests/test_generate.py
- Command line
-
hf download hf://spaces/Varun2007/document-qa-system/tests/test_generate.py
-
curl -L -o test_generate.py https://huggingface.co/spaces/Varun2007/document-qa-system/resolve/main/tests/test_generate.py
1.8 kB
| """Unit tests for Answer Generation, Grounding, and Citations.""" | |
| import unittest | |
| from ingest import DocumentChunk | |
| from generate import Generator, ConversationMemory | |
| class TestGenerate(unittest.TestCase): | |
| def setUp(self): | |
| self.generator = Generator() | |
| self.chunk = DocumentChunk( | |
| chunk_id="test_01", | |
| doc_id="doc_test", | |
| file_name="report.pdf", | |
| text="In Q3 2023, Acme generated $4.2 billion in revenue, up 14.8% year over year.", | |
| page_number=1, | |
| section_title="Executive Summary", | |
| chunk_index=0, | |
| token_count=16 | |
| ) | |
| def test_grounded_answer_includes_citation(self): | |
| query = "What was Acme's revenue in Q3 2023?" | |
| answer, verification = self.generator.generate_answer(query, [self.chunk]) | |
| self.assertIn("$4.2 billion", answer) | |
| # Check citation formatting: [Page X, Section Y] | |
| self.assertIn("[Page 1, Section \"Executive Summary\"]", answer) | |
| def test_ungrounded_answer_refusal(self): | |
| query = "What is the capital of France?" | |
| answer, verification = self.generator.generate_answer(query, [self.chunk]) | |
| self.assertEqual(answer.strip(), "Not found in the document.") | |
| def test_conversation_memory(self): | |
| mem = ConversationMemory(max_turns=3) | |
| mem.add_user_message("Hello") | |
| mem.add_assistant_message("Hi there! How can I help with your documents?") | |
| mem.add_user_message("What is Acme's revenue?") | |
| prompt_msgs = mem.get_messages_for_prompt() | |
| self.assertEqual(len(prompt_msgs), 3) | |
| self.assertEqual(prompt_msgs[0]["role"], "user") | |
| self.assertEqual(prompt_msgs[1]["role"], "assistant") | |
| if __name__ == "__main__": | |
| unittest.main() | |