Skip to content

Commit be765dc

Browse files
Refactor core logic and policy abstraction
* Refactored core logic and policy abstraction * Added integration tests * Fixed openai initialization * Fixed chroma db implementation * Unified structure * Formatting fixes --------- Co-authored-by: Luis Gaspar Schroeder <luis.gasparschroeder@gmail.com>
1 parent fca83c4 commit be765dc

32 files changed

Lines changed: 749 additions & 439 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ benchmarks/results/*
1212
temp/*
1313
*.log
1414
bin/*
15-
.venv/
15+
.venv/
16+
.env

benchmarks/benchmark.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ def get_vectorQ_answer(
291291
vectorQ_prompt = f"{task} {review_text}"
292292
latency_vectorq_logic: float = time.time()
293293
try:
294-
is_cache_hit, cache_response, nn_response = self.vectorq.create(
294+
is_cache_hit, cache_response, nn_response = self.vectorq.infer(
295295
prompt=vectorQ_prompt,
296-
output_format=output_format,
296+
system_prompt=output_format,
297297
benchmark=vectorQ_benchmark,
298298
)
299299
except Exception as e:

poetry.lock

Lines changed: 75 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ dependencies = [
2222
"accelerate (>=1.6.0,<2.0.0)",
2323
"typing-extensions (>=4.13.2,<5.0.0)",
2424
"torchvision (>=0.22.0,<0.23.0)",
25+
"statsmodels (>=0.14.4,<0.15.0)",
2526
]
2627

2728

@@ -44,6 +45,7 @@ ruff = "^0.11.6"
4445
mypy = "^1.15.0"
4546
pre-commit = "^4.2.0"
4647
pytest = "^8.0.0"
48+
python-dotenv = "^1.1.0"
4749

4850

4951
[tool.ruff]

tests/ReadMe.md

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,9 @@ The unit tests are supposed to soley test the logic of an invidual module strate
66
## Integration Tests
77
The integration tests are supposed to test the combination and interaction of all module strategies.
88

9-
## Run All Tests
9+
### Run Integration Tests
10+
Set `OPEN_AI_APIKEY` in `.env`, and run:
1011

11-
```bash
12-
pip install -e .
13-
```
14-
15-
```bash
16-
export OPENAI_API_KEY="your_api_key_here"
17-
```
18-
19-
```bash
20-
python3 runner.py
21-
```
22-
23-
## Run Individual Tests
24-
25-
```bash
26-
pytest unit/VectorDBStrategy/test.py
27-
```
28-
29-
With print terminal output enabled
30-
```bash
31-
pytest -vs unit/VectorDBStrategy/test.py
12+
```base
13+
poetry run pytest tests/integration
3214
```

tests/integration/test_1.py

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)