Spaces:
Configuration error
Configuration error
Download tests/test_llm_service.py from anu-122006/hackrx: direct link, hf CLI and curl.
- Browser
- Download file 1.83 kB
-
https://huggingface.co/spaces/anu-122006/hackrx/resolve/main/tests/test_llm_service.py
- Command line
-
hf download hf://spaces/anu-122006/hackrx/tests/test_llm_service.py
-
curl -L -o test_llm_service.py https://huggingface.co/spaces/anu-122006/hackrx/resolve/main/tests/test_llm_service.py
1.83 kB
| # ============================================================================ | |
| import pytest | |
| from unittest.mock import AsyncMock, MagicMock, patch | |
| from src.llm_service import LLMService | |
| class TestLLMService: | |
| """Test suite for LLM service""" | |
| def llm_service(self): | |
| with patch.dict('os.environ', {'OPENAI_API_KEY': 'test-key'}): | |
| return LLMService() | |
| async def test_answer_generation(self, llm_service): | |
| """Test answer generation with context""" | |
| # Mock OpenAI response | |
| mock_response = MagicMock() | |
| mock_response.choices = [MagicMock()] | |
| mock_response.choices[0].message.content = "Test answer based on provided context." | |
| mock_response.model = "gpt-4" | |
| mock_response.usage = MagicMock() | |
| mock_response.usage.prompt_tokens = 100 | |
| mock_response.usage.completion_tokens = 50 | |
| mock_response.usage.total_tokens = 150 | |
| with patch.object(llm_service.client.chat.completions, 'create', return_value=mock_response): | |
| context_chunks = [ | |
| { | |
| 'content': 'Sample context about insurance policies.', | |
| 'similarity': 0.85, | |
| 'chunk_id': 'test_chunk' | |
| } | |
| ] | |
| result = await llm_service.answer_question( | |
| question="What is covered by this policy?", | |
| context_chunks=context_chunks, | |
| document_url="https://example.com/test.pdf" | |
| ) | |
| assert 'answer' in result | |
| assert 'confidence' in result | |
| assert 'model' in result | |
| assert result['confidence'] > 0 | |
| # ============================================================================ | |