Skip to content

Commit 139a72e

Browse files
author
Sam Partee
authored
Llmcache example (#21)
LLMCache notebook example in docs. Other changes - additions to the test suite for testing the index - Readme Update
1 parent 71b0bb8 commit 139a72e

File tree

17 files changed

+534
-62
lines changed

17 files changed

+534
-62
lines changed

README.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ Indices can be defined through yaml specification that corresponds directly to t
4242

4343
```yaml
4444
index:
45-
name: users
45+
name: user_index
4646
storage_type: hash
47-
prefix: "user:"
48-
key_field: "id"
47+
prefix: users
48+
key_field: user
4949

5050
fields:
5151
# define tag fields
5252
tag:
53-
- name: users
53+
- name: user
5454
- name: job
5555
- name: credit_store
5656
# define numeric fields
@@ -65,7 +65,7 @@ fields:
6565
6666
This would correspond to a dataset that looked something like
6767
68-
| users | age | job | credit_score | user_embedding |
68+
| user | age | job | credit_score | user_embedding |
6969
|-------|-----|------------|--------------|-----------------------------------|
7070
| john | 1 | engineer | high | \x3f\x8c\xcc\x3f\x8c\xcc?@ |
7171
| mary | 2 | doctor | low | \x3f\x8c\xcc\x3f\x8c\xcc?@ |
@@ -74,6 +74,8 @@ 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
78+
7779
from redisvl.index import SearchIndex
7880
from redisvl.query import create_vector_query
7981

@@ -82,47 +84,45 @@ index = SearchIndex.from_yaml("./users_schema.yml"))
8284
index.connect("redis://localhost:6379")
8385
index.create()
8486

85-
index.load(pd.read_csv("./users.csv").to_records())
87+
index.load(pd.read_csv("./users.csv").to_dict("records"))
8688

8789
query = create_vector_query(
88-
["users", "age", "job", "credit_score"],
90+
["user", "age", "job", "credit_score"],
8991
number_of_results=2,
9092
vector_field_name="user_embedding",
9193
)
9294

9395
query_vector = np.array([0.1, 0.1, 0.5]).tobytes()
9496
results = index.search(query, query_params={"vector": query_vector})
9597

98+
9699
```
97100
98101
### Semantic cache
99102
100103
The ``LLMCache`` Interface in RedisVL can be used as follows.
101104
102105
```python
103-
# init open ai client
104-
import openai
105-
openai.api_key = "sk-xxx"
106-
107106
from redisvl.llmcache.semantic import SemanticCache
108-
cache = SemanticCache(redis_host="localhost", redis_port=6379, redis_password=None)
109-
110-
def ask_gpt3(question):
111-
response = openai.Completion.create(
112-
engine="text-davinci-003",
113-
prompt=question,
114-
max_tokens=100
115-
)
116-
return response.choices[0].text.strip()
117-
118-
def answer_question(question: str):
119-
results = cache.check(question)
120-
if results:
121-
return results[0]
122-
else:
123-
answer = ask_gpt3(question)
124-
cache.store(question, answer)
125-
return answer
107+
cache = SemanticCache(
108+
redis_url="redis://localhost:6379",
109+
threshold=0.9, # semantic similarity threshold
110+
)
111+
112+
# check if the cache has a result for a given query
113+
cache.check("What is the capital of France?")
114+
[ ]
115+
116+
# store a result for a given query
117+
cache.store("What is the capital of France?", "Paris")
118+
119+
# Cache will now have the query
120+
cache.check("What is the capital of France?")
121+
["Paris"]
122+
123+
# Cache will return the result if the query is similar enough
124+
cache.get("What really is the capital of France?")
125+
["Paris"]
126126
```
127127

128128

conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import pytest
3+
import asyncio
34

45
from redisvl.utils.connection import (
56
get_async_redis_connection,
@@ -23,3 +24,13 @@ def client():
2324
@pytest.fixture
2425
def openai_key():
2526
return os.getenv("OPENAI_KEY")
27+
28+
29+
@pytest.fixture(scope="session")
30+
def event_loop():
31+
try:
32+
loop = asyncio.get_running_loop()
33+
except RuntimeError:
34+
loop = asyncio.new_event_loop()
35+
yield loop
36+
loop.close()

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
add_module_names = False
103103

104104
nbsphinx_execute = 'never'
105+
jupyter_execute_notebooks = "off"
105106

106107
# -- Options for autosummary/autodoc output ------------------------------------
107108
autosummary_generate = True
@@ -129,4 +130,4 @@
129130
"android-chrome-192x192.png",
130131
# apple icons
131132
{"rel": "apple-touch-icon", "href": "apple-touch-icon.png"},
132-
]
133+
]

docs/user_guide/getting_started_01.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"4. Performing queries\n",
1717
"\n",
1818
"Before running this notebook, be sure to\n",
19-
"1. Gave installed ``rvl`` and have that environment active for this notebook.\n",
19+
"1. Have installed ``redisvl`` and have that environment active for this notebook.\n",
2020
"2. Have a running Redis instance with RediSearch > 2.4 running."
2121
]
2222
},

docs/user_guide/index.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ embedding_creation
2626
```
2727

2828
```{toctree}
29-
:maxdepth: 2
3029
:caption: LLMCache
3130
32-
llm_cache
31+
llmcache_03
3332
```

docs/user_guide/llm_cache.rst

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)