Skip to content

Commit ff083c7

Browse files
author
Sam Partee
authored
Update query interface (#22)
Introduce new Query design with Filters and notebooks to show examples includes - documenation updates - examples - semantic cache and SearchIndex updates - tests
1 parent 139a72e commit ff083c7

22 files changed

+1300
-233
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,27 @@ This would correspond to a dataset that looked something like
7474
7575
With the schema, the RedisVL library can be used to create, load vectors and perform vector searches
7676
```python
77-
import pandas as pd
7877

7978
from redisvl.index import SearchIndex
80-
from redisvl.query import create_vector_query
79+
from redisvl.query import VectorQuery
8180

82-
# define and create the index
83-
index = SearchIndex.from_yaml("./users_schema.yml"))
81+
# initialize the index and connect to Redis
82+
index = SearchIndex.from_dict(schema)
8483
index.connect("redis://localhost:6379")
85-
index.create()
8684

87-
index.load(pd.read_csv("./users.csv").to_dict("records"))
85+
# create the index in Redis
86+
index.create(overwrite=True)
8887

89-
query = create_vector_query(
90-
["user", "age", "job", "credit_score"],
91-
number_of_results=2,
88+
# load data into the index in Redis (list of dicts)
89+
index.load(data)
90+
91+
query = VectorQuery(
92+
vector=[0.1, 0.1, 0.5],
9293
vector_field_name="user_embedding",
94+
return_fields=["user", "age", "job", "credit_score"],
95+
num_results=3,
9396
)
94-
95-
query_vector = np.array([0.1, 0.1, 0.5]).tobytes()
96-
results = index.search(query, query_params={"vector": query_vector})
97-
97+
results = index.search(query.query, query_params=query.params)
9898

9999
```
100100

conftest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@ def event_loop():
3333
except RuntimeError:
3434
loop = asyncio.new_event_loop()
3535
yield loop
36-
loop.close()
36+
loop.close()
37+
38+
@pytest.fixture
39+
def clear_db():
40+
redis.flushall()
41+
yield
42+
redis.flushall()

docs/api/cache.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
********
3+
LLMCache
4+
********
5+
6+
SemanticCache
7+
=============
8+
9+
.. _semantic_cache_api:
10+
11+
.. currentmodule:: redisvl.llmcache.semantic
12+
13+
.. autosummary::
14+
15+
SemanticCache.__init__
16+
SemanticCache.check
17+
SemanticCache.store
18+
SemanticCache.set_threshold
19+
SemanticCache.threshold
20+
SemanticCache.index
21+
SemanticCache.ttl
22+
SemanticCache.set_ttl
23+
24+
25+
.. autoclass:: SemanticCache
26+
:show-inheritance:
27+
:members:
28+
:inherited-members:

docs/api/filter.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
******
2+
Filter
3+
******
4+
5+
.. _filter_api:
6+
7+
TagFilter
8+
=========
9+
10+
11+
.. currentmodule:: redisvl.query
12+
13+
.. autosummary::
14+
15+
TagFilter.__init__
16+
TagFilter.to_string
17+
18+
19+
.. autoclass:: TagFilter
20+
:show-inheritance:
21+
:members:
22+
:inherited-members:
23+
24+
25+
26+
TextFilter
27+
==========
28+
29+
30+
.. currentmodule:: redisvl.query
31+
32+
.. autosummary::
33+
34+
TextFilter.__init__
35+
TextFilter.to_string
36+
37+
38+
.. autoclass:: TextFilter
39+
:show-inheritance:
40+
:members:
41+
:inherited-members:
42+
43+
44+
NumericFilter
45+
=============
46+
47+
48+
.. currentmodule:: redisvl.query
49+
50+
.. autosummary::
51+
52+
NumericFilter.__init__
53+
NumericFilter.to_string
54+
55+
56+
.. autoclass:: NumericFilter
57+
:show-inheritance:
58+
:members:
59+
:inherited-members:

docs/api/index.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ myst:
99

1010
```{toctree}
1111
:caption: RedisVL
12+
:maxdepth: 2
1213
13-
redisvl_api
14+
searchindex
15+
query
16+
filter
17+
cache
1418
```
1519

docs/api/query.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
*****
3+
Query
4+
*****
5+
6+
VectorQuery
7+
===========
8+
9+
.. _query_api:
10+
11+
.. currentmodule:: redisvl.query
12+
13+
.. autosummary::
14+
15+
VectorQuery.__init__
16+
VectorQuery.set_filter
17+
VectorQuery.get_filter
18+
VectorQuery.query
19+
VectorQuery.params
20+
21+
22+
.. autoclass:: VectorQuery
23+
:show-inheritance:
24+
:members:
25+
:inherited-members:

docs/api/redisvl_api.rst renamed to docs/api/searchindex.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

2-
***********
3-
RedisVL API
4-
***********
5-
2+
*****
3+
Index
4+
*****
65

76
SearchIndex
87
===========

docs/user_guide/getting_started_01.ipynb

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
{
3434
"cell_type": "code",
35-
"execution_count": 9,
35+
"execution_count": 1,
3636
"metadata": {},
3737
"outputs": [],
3838
"source": [
@@ -58,7 +58,7 @@
5858
},
5959
{
6060
"cell_type": "code",
61-
"execution_count": 10,
61+
"execution_count": 2,
6262
"metadata": {},
6363
"outputs": [
6464
{
@@ -131,7 +131,7 @@
131131
},
132132
{
133133
"cell_type": "code",
134-
"execution_count": 11,
134+
"execution_count": 3,
135135
"metadata": {},
136136
"outputs": [],
137137
"source": [
@@ -171,7 +171,7 @@
171171
},
172172
{
173173
"cell_type": "code",
174-
"execution_count": 12,
174+
"execution_count": 4,
175175
"metadata": {},
176176
"outputs": [],
177177
"source": [
@@ -189,15 +189,16 @@
189189
},
190190
{
191191
"cell_type": "code",
192-
"execution_count": 13,
192+
"execution_count": 5,
193193
"metadata": {},
194194
"outputs": [
195195
{
196196
"name": "stdout",
197197
"output_type": "stream",
198198
"text": [
199-
"\u001b[32m14:26:12\u001b[0m \u001b[35msam.partee-NW9MQX5Y74\u001b[0m \u001b[34mredisvl.cli.index[17001]\u001b[0m \u001b[1;30mINFO\u001b[0m Indices:\n",
200-
"\u001b[32m14:26:12\u001b[0m \u001b[35msam.partee-NW9MQX5Y74\u001b[0m \u001b[34mredisvl.cli.index[17001]\u001b[0m \u001b[1;30mINFO\u001b[0m 1. user_index\n"
199+
"\u001b[32m00:29:48\u001b[0m \u001b[35msam.partee-NW9MQX5Y74\u001b[0m \u001b[34mredisvl.cli.index[40909]\u001b[0m \u001b[1;30mINFO\u001b[0m Indices:\n",
200+
"\u001b[32m00:29:48\u001b[0m \u001b[35msam.partee-NW9MQX5Y74\u001b[0m \u001b[34mredisvl.cli.index[40909]\u001b[0m \u001b[1;30mINFO\u001b[0m 1. user_index\n",
201+
"\u001b[32m00:29:48\u001b[0m \u001b[35msam.partee-NW9MQX5Y74\u001b[0m \u001b[34mredisvl.cli.index[40909]\u001b[0m \u001b[1;30mINFO\u001b[0m 2. my_index\n"
201202
]
202203
}
203204
],
@@ -217,7 +218,7 @@
217218
},
218219
{
219220
"cell_type": "code",
220-
"execution_count": 14,
221+
"execution_count": 6,
221222
"metadata": {},
222223
"outputs": [],
223224
"source": [
@@ -237,48 +238,46 @@
237238
},
238239
{
239240
"cell_type": "code",
240-
"execution_count": 15,
241+
"execution_count": 7,
241242
"metadata": {},
242243
"outputs": [],
243244
"source": [
244-
"from redisvl.query import create_vector_query\n",
245+
"from redisvl.query import VectorQuery\n",
245246
"\n",
246247
"# create a vector query returning a number of results\n",
247248
"# with specific fields to return.\n",
248-
"query = create_vector_query(\n",
249-
" return_fields=[\"users\", \"age\", \"job\", \"credit_score\", \"vector_score\"],\n",
250-
" number_of_results=3,\n",
251-
" vector_field_name=\"user_embedding\"\n",
249+
"query = VectorQuery(\n",
250+
" vector=[0.1, 0.1, 0.5],\n",
251+
" vector_field_name=\"user_embedding\",\n",
252+
" return_fields=[\"user\", \"age\", \"job\", \"credit_score\", \"vector_distance\"],\n",
253+
" num_results=3\n",
252254
")\n",
253255
"\n",
254-
"# establish a query vector to search against the data in Redis\n",
255-
"query_vector = np.array([0.1, 0.1, 0.5], dtype=np.float32).tobytes()\n",
256-
"\n",
257256
"# use the SearchIndex instance (or Redis client) to execute the query\n",
258-
"results = index.search(query, query_params={\"vector\": query_vector})"
257+
"results = index.search(query.query, query_params=query.params)"
259258
]
260259
},
261260
{
262261
"cell_type": "code",
263-
"execution_count": 16,
262+
"execution_count": 8,
264263
"metadata": {},
265264
"outputs": [
266265
{
267266
"name": "stdout",
268267
"output_type": "stream",
269268
"text": [
270269
"Score: 0\n",
271-
"Document {'id': 'v1:john', 'payload': None, 'vector_score': '0', 'age': '1', 'job': 'engineer', 'credit_score': 'high'}\n",
270+
"Document {'id': 'v1:john', 'payload': None, 'vector_distance': '0', 'user': 'john', 'age': '1', 'job': 'engineer', 'credit_score': 'high'}\n",
272271
"Score: 0\n",
273-
"Document {'id': 'v1:mary', 'payload': None, 'vector_score': '0', 'age': '2', 'job': 'doctor', 'credit_score': 'low'}\n",
272+
"Document {'id': 'v1:mary', 'payload': None, 'vector_distance': '0', 'user': 'mary', 'age': '2', 'job': 'doctor', 'credit_score': 'low'}\n",
274273
"Score: 0.653301358223\n",
275-
"Document {'id': 'v1:joe', 'payload': None, 'vector_score': '0.653301358223', 'age': '3', 'job': 'dentist', 'credit_score': 'medium'}\n"
274+
"Document {'id': 'v1:joe', 'payload': None, 'vector_distance': '0.653301358223', 'user': 'joe', 'age': '3', 'job': 'dentist', 'credit_score': 'medium'}\n"
276275
]
277276
}
278277
],
279278
"source": [
280279
"for doc in results.docs:\n",
281-
" print(\"Score:\", doc.vector_score)\n",
280+
" print(\"Score:\", doc.vector_distance)\n",
282281
" print(doc)\n"
283282
]
284283
},

0 commit comments

Comments
 (0)