-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathtest_llmcache_schema.py
More file actions
189 lines (157 loc) · 5.91 KB
/
Copy pathtest_llmcache_schema.py
File metadata and controls
189 lines (157 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import json
import pytest
from pydantic import ValidationError
from redisvl.extensions.cache.llm import CacheEntry, CacheHit, SemanticCacheIndexSchema
from redisvl.extensions.constants import CACHE_VECTOR_FIELD_NAME
from redisvl.redis.utils import array_to_buffer, hashify
def test_valid_cache_entry_creation():
entry = CacheEntry(
prompt="What is AI?",
response="AI is artificial intelligence.",
prompt_vector=[0.1, 0.2, 0.3],
)
assert entry.entry_id == hashify("What is AI?")
assert entry.prompt == "What is AI?"
assert entry.response == "AI is artificial intelligence."
assert entry.prompt_vector == [0.1, 0.2, 0.3]
def test_cache_entry_with_given_entry_id():
entry = CacheEntry(
entry_id="custom_id",
prompt="What is AI?",
response="AI is artificial intelligence.",
prompt_vector=[0.1, 0.2, 0.3],
)
assert entry.entry_id == "custom_id"
def test_cache_entry_with_invalid_metadata():
with pytest.raises(ValidationError):
CacheEntry(
prompt="What is AI?",
response="AI is artificial intelligence.",
prompt_vector=[0.1, 0.2, 0.3],
metadata="invalid_metadata",
)
def test_cache_entry_to_dict():
entry = CacheEntry(
prompt="What is AI?",
response="AI is artificial intelligence.",
prompt_vector=[0.1, 0.2, 0.3],
metadata={"author": "John"},
filters={"category": "technology"},
)
result = entry.to_dict(dtype="float32")
assert result["entry_id"] == hashify("What is AI?", {"category": "technology"})
assert result["metadata"] == json.dumps({"author": "John"})
assert result["prompt_vector"] == array_to_buffer([0.1, 0.2, 0.3], "float32")
assert result["category"] == "technology"
assert "filters" not in result
def test_valid_cache_hit_creation():
hit = CacheHit(
entry_id="entry_1",
prompt="What is AI?",
response="AI is artificial intelligence.",
vector_distance=0.1,
inserted_at=1625819123.123,
updated_at=1625819123.123,
)
assert hit.entry_id == "entry_1"
assert hit.prompt == "What is AI?"
assert hit.response == "AI is artificial intelligence."
assert hit.vector_distance == 0.1
assert hit.inserted_at == hit.updated_at == 1625819123.123
def test_cache_hit_with_serialized_metadata():
hit = CacheHit(
entry_id="entry_1",
prompt="What is AI?",
response="AI is artificial intelligence.",
vector_distance=0.1,
inserted_at=1625819123.123,
updated_at=1625819123.123,
metadata=json.dumps({"author": "John"}),
)
assert hit.metadata == {"author": "John"}
def test_cache_hit_to_dict():
hit = CacheHit(
entry_id="entry_1",
prompt="What is AI?",
response="AI is artificial intelligence.",
vector_distance=0.1,
inserted_at=1625819123.123,
updated_at=1625819123.123,
filters={"category": "technology"},
)
result = hit.to_dict()
assert result["entry_id"] == "entry_1"
assert result["prompt"] == "What is AI?"
assert result["response"] == "AI is artificial intelligence."
assert result["vector_distance"] == 0.1
assert result["category"] == "technology"
assert "filters" not in result
def test_cache_entry_with_empty_optional_fields():
entry = CacheEntry(
prompt="What is AI?",
response="AI is artificial intelligence.",
prompt_vector=[0.1, 0.2, 0.3],
)
result = entry.to_dict(dtype="float32")
assert "metadata" not in result
assert "filters" not in result
def test_cache_hit_with_empty_optional_fields():
hit = CacheHit(
entry_id="entry_1",
prompt="What is AI?",
response="AI is artificial intelligence.",
vector_distance=0.1,
inserted_at=1625819123.123,
updated_at=1625819123.123,
)
result = hit.to_dict()
assert "metadata" not in result
assert "filters" not in result
def test_semantic_cache_schema_defaults_to_flat_vector_index():
schema = SemanticCacheIndexSchema.from_params(
"llmcache", "llmcache", vector_dims=768, dtype="float32"
)
vector_field = schema.fields[CACHE_VECTOR_FIELD_NAME]
assert vector_field.attrs.algorithm.value == "FLAT"
assert vector_field.attrs.dims == 768
assert vector_field.attrs.datatype.value == "FLOAT32"
assert vector_field.attrs.distance_metric.value == "COSINE"
def test_semantic_cache_schema_accepts_hnsw_vector_index_config():
schema = SemanticCacheIndexSchema.from_params(
"llmcache",
"llmcache",
vector_dims=768,
dtype="float32",
vector_index_config={
"algorithm": "hnsw",
"m": 32,
"ef_construction": 300,
"ef_runtime": 20,
},
)
vector_field = schema.fields[CACHE_VECTOR_FIELD_NAME]
assert vector_field.attrs.algorithm.value == "HNSW"
assert vector_field.attrs.dims == 768
assert vector_field.attrs.datatype.value == "FLOAT32"
assert vector_field.attrs.distance_metric.value == "COSINE"
assert vector_field.attrs.m == 32
assert vector_field.attrs.ef_construction == 300
assert vector_field.attrs.ef_runtime == 20
def test_semantic_cache_schema_rejects_unknown_vector_index_algorithm():
with pytest.raises(ValueError, match="Unknown vector field algorithm"):
SemanticCacheIndexSchema.from_params(
"llmcache",
"llmcache",
vector_dims=768,
dtype="float32",
vector_index_config={"algorithm": "unknown"},
)
def test_semantic_cache_schema_rejects_vectorizer_derived_config_overrides():
with pytest.raises(ValueError, match="dims"):
SemanticCacheIndexSchema.from_params(
"llmcache",
"llmcache",
vector_dims=768,
dtype="float32",
vector_index_config={"algorithm": "hnsw", "dims": 1536},
)