|
| 1 | +""" |
| 2 | +Comprehensive end-to-end test of the GoodMem + DeepEval integration. |
| 3 | +
|
| 4 | +Uses a GoodMem space pre-loaded with SQuAD 2.0 articles covering: |
| 5 | + - Energy (physics) |
| 6 | + - American Idol |
| 7 | + - FBI history |
| 8 | + - Greek diaspora |
| 9 | + - Universal Studios |
| 10 | +
|
| 11 | +Evaluates a full RAG pipeline: retrieve from GoodMem → generate with OpenAI → score |
| 12 | +with multiple DeepEval metrics across answerable and unanswerable queries. |
| 13 | +
|
| 14 | +Required env vars: |
| 15 | + GOODMEM_BASE_URL, GOODMEM_API_KEY, GOODMEM_SPACE_ID, OPENAI_API_KEY |
| 16 | +""" |
| 17 | + |
| 18 | +import os |
| 19 | + |
| 20 | +from deepeval import evaluate |
| 21 | +from deepeval.evaluate import AsyncConfig |
| 22 | +from deepeval.integrations.goodmem import GoodMemConfig, GoodMemChunk, GoodMemRetriever |
| 23 | +from deepeval.metrics import ( |
| 24 | + AnswerRelevancyMetric, |
| 25 | + ContextualRelevancyMetric, |
| 26 | + FaithfulnessMetric, |
| 27 | +) |
| 28 | +from deepeval.test_case import LLMTestCase |
| 29 | +from openai import OpenAI |
| 30 | + |
| 31 | +# --------------------------------------------------------------------------- |
| 32 | +# 1. Setup |
| 33 | +# --------------------------------------------------------------------------- |
| 34 | +retriever = GoodMemRetriever( |
| 35 | + GoodMemConfig( |
| 36 | + base_url=os.environ["GOODMEM_BASE_URL"], |
| 37 | + api_key=os.environ["GOODMEM_API_KEY"], |
| 38 | + space_id=os.environ["GOODMEM_SPACE_ID"], |
| 39 | + top_k=3, |
| 40 | + ) |
| 41 | +) |
| 42 | +client = OpenAI() |
| 43 | +GENERATION_MODEL = "gpt-4o-mini" |
| 44 | + |
| 45 | +SYSTEM_PROMPT = ( |
| 46 | + "Answer the question accurately based only on the provided context. " |
| 47 | + "If the context doesn't contain enough information, say so." |
| 48 | +) |
| 49 | + |
| 50 | +# --------------------------------------------------------------------------- |
| 51 | +# 2. Test queries — one per SQuAD article, plus an unanswerable query |
| 52 | +# --------------------------------------------------------------------------- |
| 53 | +test_queries = [ |
| 54 | + # --- Energy (physics) --- |
| 55 | + { |
| 56 | + "query": "What are the main forms of energy in physics?", |
| 57 | + "expected": ( |
| 58 | + "Common energy forms include kinetic energy of a moving object, " |
| 59 | + "potential energy stored by position in a force field, elastic energy, " |
| 60 | + "and other forms like chemical, thermal, and electromagnetic energy." |
| 61 | + ), |
| 62 | + }, |
| 63 | + # --- American Idol --- |
| 64 | + { |
| 65 | + "query": "Who created American Idol and when did it first air?", |
| 66 | + "expected": ( |
| 67 | + "American Idol was created by Simon Fuller, produced by " |
| 68 | + "19 Entertainment, and first aired on Fox on June 11, 2002." |
| 69 | + ), |
| 70 | + }, |
| 71 | + # --- FBI --- |
| 72 | + { |
| 73 | + "query": "What was the FBI's role in enforcing civil rights laws?", |
| 74 | + "expected": ( |
| 75 | + "The FBI is charged with the responsibility of enforcing compliance " |
| 76 | + "with United States Civil Rights Acts." |
| 77 | + ), |
| 78 | + }, |
| 79 | + # --- Greeks --- |
| 80 | + { |
| 81 | + "query": "Where have Greek colonies been historically established?", |
| 82 | + "expected": ( |
| 83 | + "Greek colonies and communities have been historically established " |
| 84 | + "on the shores of the Mediterranean Sea and Black Sea, centered " |
| 85 | + "around the Aegean and Ionian seas." |
| 86 | + ), |
| 87 | + }, |
| 88 | + # --- Universal Studios --- |
| 89 | + { |
| 90 | + "query": "When did Carl Laemmle open Universal's production facility?", |
| 91 | + "expected": ( |
| 92 | + "On March 15, 1915, Carl Laemmle opened the world's largest motion " |
| 93 | + "picture production facility, Universal City Studios." |
| 94 | + ), |
| 95 | + }, |
| 96 | + # --- Unanswerable (no relevant content in the space) --- |
| 97 | + { |
| 98 | + "query": "What is the capital of Mongolia?", |
| 99 | + "expected": ( |
| 100 | + "The context does not contain information about the capital of Mongolia." |
| 101 | + ), |
| 102 | + }, |
| 103 | +] |
| 104 | + |
| 105 | +# --------------------------------------------------------------------------- |
| 106 | +# 3. Demonstrate retrieve_chunks() — structured retrieval with scores/IDs |
| 107 | +# --------------------------------------------------------------------------- |
| 108 | +print("\n=== Structured Retrieval Demo (retrieve_chunks) ===") |
| 109 | +demo_chunks = retriever.retrieve_chunks(test_queries[0]["query"]) |
| 110 | +print(f"\nQuery: {test_queries[0]['query']}") |
| 111 | +for i, chunk in enumerate(demo_chunks): |
| 112 | + print(f" Chunk {i + 1}: score={chunk.score:.4f} chunk_id={chunk.chunk_id[:16]}... memory_id={chunk.memory_id[:16]}...") |
| 113 | + print(f" {chunk.content[:80]}...") |
| 114 | + |
| 115 | +# --------------------------------------------------------------------------- |
| 116 | +# 4. Build test cases: retrieve → generate → package |
| 117 | +# --------------------------------------------------------------------------- |
| 118 | +test_cases = [] |
| 119 | + |
| 120 | +for item in test_queries: |
| 121 | + query = item["query"] |
| 122 | + |
| 123 | + # Retrieve context from GoodMem (plain text for LLMTestCase) |
| 124 | + chunks = retriever.retrieve(query) |
| 125 | + print(f"\n--- Query: {query} ---") |
| 126 | + print(f" Retrieved {len(chunks)} chunks") |
| 127 | + for i, c in enumerate(chunks): |
| 128 | + preview = c[:100].replace("\n", " ") |
| 129 | + print(f" Chunk {i + 1}: {preview}...") |
| 130 | + |
| 131 | + # Generate answer grounded in retrieved context |
| 132 | + response = client.chat.completions.create( |
| 133 | + model=GENERATION_MODEL, |
| 134 | + messages=[ |
| 135 | + {"role": "system", "content": SYSTEM_PROMPT}, |
| 136 | + { |
| 137 | + "role": "user", |
| 138 | + "content": f"Context:\n{chr(10).join(chunks)}\n\nQuestion: {query}", |
| 139 | + }, |
| 140 | + ], |
| 141 | + ) |
| 142 | + answer = response.choices[0].message.content |
| 143 | + print(f" Answer: {answer[:200]}...") |
| 144 | + |
| 145 | + test_cases.append( |
| 146 | + LLMTestCase( |
| 147 | + input=query, |
| 148 | + actual_output=answer, |
| 149 | + expected_output=item["expected"], |
| 150 | + retrieval_context=chunks, |
| 151 | + ) |
| 152 | + ) |
| 153 | + |
| 154 | +# --------------------------------------------------------------------------- |
| 155 | +# 5. Evaluate with multiple RAG metrics |
| 156 | +# --------------------------------------------------------------------------- |
| 157 | +print("\n\n=== Running DeepEval Evaluation ===\n") |
| 158 | +metrics = [ |
| 159 | + AnswerRelevancyMetric(model="gpt-4o-mini"), |
| 160 | + ContextualRelevancyMetric(model="gpt-4o-mini"), |
| 161 | + # FaithfulnessMetric processes all retrieval chunks per claim and can |
| 162 | + # exceed OpenAI timeouts on lower-tier keys. Uncomment with a higher-tier key: |
| 163 | + # FaithfulnessMetric(model="gpt-4o-mini"), |
| 164 | +] |
| 165 | +results = evaluate( |
| 166 | + test_cases, |
| 167 | + metrics, |
| 168 | + async_config=AsyncConfig(max_concurrent=2, throttle_value=1), |
| 169 | +) |
0 commit comments