import os import pytest from datasets import Dataset from evaluations.evaluators import LLMJudge from haiku.rag.client import HaikuRAG from haiku.rag.config import Config from haiku.rag.config.models import ModelConfig from haiku.rag.qa.agent import QuestionAnswerAgent OPENAI_AVAILABLE = bool(os.getenv("OPENAI_API_KEY")) ANTHROPIC_AVAILABLE = bool(os.getenv("ANTHROPIC_API_KEY")) VLLM_QA_AVAILABLE = bool(Config.providers.vllm.qa_base_url) @pytest.mark.asyncio async def test_qa_ollama(qa_corpus: Dataset, temp_db_path): """Test Ollama QA with LLM judge.""" client = HaikuRAG(temp_db_path, create=True) qa = QuestionAnswerAgent( client, ModelConfig(provider="ollama", name="gpt-oss", enable_thinking=True) ) llm_judge = LLMJudge() doc = qa_corpus[1] await client.create_document( content=doc["document_extracted"], uri=doc["document_id"] ) question = doc["question"] expected_answer = doc["answer"] answer, _ = await qa.answer(question) is_equivalent = await llm_judge.judge_answers(question, answer, expected_answer) assert is_equivalent, ( f"Generated answer not equivalent to expected answer.\nQuestion: {question}\nGenerated: {answer}\nExpected: {expected_answer}" ) @pytest.mark.asyncio @pytest.mark.skipif(not OPENAI_AVAILABLE, reason="OpenAI not available") async def test_qa_openai(qa_corpus: Dataset, temp_db_path): """Test OpenAI QA with LLM judge.""" client = HaikuRAG(temp_db_path, create=True) qa = QuestionAnswerAgent(client, ModelConfig(provider="openai", name="gpt-4o-mini")) llm_judge = LLMJudge() doc = qa_corpus[1] await client.create_document( content=doc["document_extracted"], uri=doc["document_id"] ) question = doc["question"] expected_answer = doc["answer"] answer, _ = await qa.answer(question) is_equivalent = await llm_judge.judge_answers(question, answer, expected_answer) assert is_equivalent, ( f"Generated answer not equivalent to expected answer.\nQuestion: {question}\nGenerated: {answer}\nExpected: {expected_answer}" ) @pytest.mark.asyncio @pytest.mark.skipif(not ANTHROPIC_AVAILABLE, reason="Anthropic not available") async def test_qa_anthropic(qa_corpus: Dataset, temp_db_path): """Test Anthropic QA with LLM judge.""" client = HaikuRAG(temp_db_path, create=True) qa = QuestionAnswerAgent( client, ModelConfig(provider="anthropic", name="claude-3-5-haiku-20241022") ) llm_judge = LLMJudge() doc = qa_corpus[1] await client.create_document( content=doc["document_extracted"], uri=doc["document_id"] ) question = doc["question"] expected_answer = doc["answer"] answer, _ = await qa.answer(question) is_equivalent = await llm_judge.judge_answers(question, answer, expected_answer) assert is_equivalent, ( f"Generated answer not equivalent to expected answer.\nQuestion: {question}\nGenerated: {answer}\nExpected: {expected_answer}" ) @pytest.mark.asyncio @pytest.mark.skipif(not VLLM_QA_AVAILABLE, reason="vLLM QA server not configured") async def test_qa_vllm(qa_corpus: Dataset, temp_db_path): """Test vLLM QA with LLM judge.""" client = HaikuRAG(temp_db_path, create=True) qa = QuestionAnswerAgent(client, ModelConfig(provider="vllm", name="Qwen/Qwen3-4B")) llm_judge = LLMJudge() doc = qa_corpus[1] await client.create_document( content=doc["document_extracted"], uri=doc["document_id"] ) question = doc["question"] expected_answer = doc["answer"] answer, _ = await qa.answer(question) is_equivalent = await llm_judge.judge_answers(question, answer, expected_answer) assert is_equivalent, ( f"Generated answer not equivalent to expected answer.\nQuestion: {question}\nGenerated: {answer}\nExpected: {expected_answer}" )