|
| 1 | +import unittest |
| 2 | + |
| 3 | +from dotenv import load_dotenv |
| 4 | + |
| 5 | +from vectorq import ( |
| 6 | + DynamicThresholdPolicy, |
| 7 | + HNSWLibVectorDB, |
| 8 | + InMemoryEmbeddingMetadataStorage, |
| 9 | + LangChainEmbeddingEngine, |
| 10 | + OpenAIInferenceEngine, |
| 11 | + StringComparisonSimilarityEvaluator, |
| 12 | + VectorQ, |
| 13 | + VectorQConfig, |
| 14 | +) |
| 15 | + |
| 16 | +load_dotenv() |
| 17 | + |
| 18 | + |
| 19 | +def create_default_config_and_policy(): |
| 20 | + config = VectorQConfig( |
| 21 | + inference_engine=OpenAIInferenceEngine( |
| 22 | + model_name="gpt-4.1-nano-2025-04-14", |
| 23 | + temperature=0.0, |
| 24 | + ), |
| 25 | + embedding_engine=LangChainEmbeddingEngine( |
| 26 | + model_name="sentence-transformers/all-mpnet-base-v2" |
| 27 | + ), |
| 28 | + vector_db=HNSWLibVectorDB(), |
| 29 | + embedding_metadata_storage=InMemoryEmbeddingMetadataStorage(), |
| 30 | + system_prompt="Please answer in a single word with the first letter capitalized. Example: London", |
| 31 | + ) |
| 32 | + policy = DynamicThresholdPolicy( |
| 33 | + delta=0.05, |
| 34 | + is_global=False, |
| 35 | + similarity_evaluator=StringComparisonSimilarityEvaluator(), |
| 36 | + ) |
| 37 | + return config, policy |
| 38 | + |
| 39 | + |
| 40 | +class TestVectorQDynamicThreshold(unittest.TestCase): |
| 41 | + def test_basic_functionality(self): |
| 42 | + """Test that the cache correctly identifies hits and misses.""" |
| 43 | + config, policy = create_default_config_and_policy() |
| 44 | + vectorq = VectorQ(config, policy) |
| 45 | + |
| 46 | + # First request should be a miss |
| 47 | + cache_hit, response, _ = vectorq.infer_with_cache_info( |
| 48 | + prompt="What is the capital of France?" |
| 49 | + ) |
| 50 | + self.assertFalse(cache_hit, "First request should be a cache miss") |
| 51 | + self.assertTrue(len(response) > 0, "Response should not be empty") |
| 52 | + |
| 53 | + # The 2nd to 5th request should be miss because it's still adjusting the threshold |
| 54 | + cache_hit, response, _ = vectorq.infer_with_cache_info( |
| 55 | + prompt="What's France's capital city?" |
| 56 | + ) |
| 57 | + self.assertFalse(cache_hit, "Second request should be a cache miss") |
| 58 | + self.assertTrue(len(response) > 0, "Response should not be empty") |
| 59 | + cache_hit, response, _ = vectorq.infer_with_cache_info( |
| 60 | + prompt="France's capital city is called what?" |
| 61 | + ) |
| 62 | + self.assertFalse(cache_hit, "Identical request should be a cache hit") |
| 63 | + self.assertTrue(len(response) > 0, "Response should not be empty") |
| 64 | + cache_hit, response, _ = vectorq.infer_with_cache_info( |
| 65 | + prompt="Tell me the capital city of France" |
| 66 | + ) |
| 67 | + cache_hit, response, _ = vectorq.infer_with_cache_info( |
| 68 | + prompt="Which city is the capital of France?" |
| 69 | + ) |
| 70 | + |
| 71 | + # After several tries with the Bayesian policy, we should now get a hit |
| 72 | + cache_hit, response, _ = vectorq.infer_with_cache_info( |
| 73 | + prompt="The capital of France is?" |
| 74 | + ) |
| 75 | + self.assertTrue(cache_hit, "Similar request should now be a cache hit") |
| 76 | + self.assertTrue(len(response) > 0, "Response should not be empty") |
| 77 | + |
| 78 | + cache_hit, response, _ = vectorq.infer_with_cache_info( |
| 79 | + prompt="Can you tell me what the capital of France is?" |
| 80 | + ) |
| 81 | + self.assertTrue(cache_hit, "Similar request should now be a cache hit") |
| 82 | + self.assertTrue(len(response) > 0, "Response should not be empty") |
| 83 | + |
| 84 | + def test_high_delta(self): |
| 85 | + # TODO: Implement this |
| 86 | + self.assertTrue(True) |
| 87 | + |
| 88 | + def test_low_delta(self): |
| 89 | + # TODO: Implement this |
| 90 | + self.assertTrue(True) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + unittest.main() |
0 commit comments