-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis_store.py
71 lines (58 loc) · 2.09 KB
/
redis_store.py
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
# Check one of the products
# product_metadata[0]
import os
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores.redis import Redis as RedisVectorStore
from create_meta import createMeta
from redisearch import Client, IndexDefinition
import redis
# set your openAI api key as an environment variable
# os.environ['OPENAI_API_KEY'] = ""
# Function to check if the index exists
def checkIndex(client, index_name):
try:
client.info()
return True
except Exception as e:
if "Unknown index name" in str(e):
return False
else:
raise
def getRedisStore(index_name=None, product_metadata=None):
# Connect to Redis server
redisURL = 'redis://localhost:6379'
embedding = OpenAIEmbeddings()
index_exists = False
if index_name:
print(f"Looking for index '{index_name}'.")
redis_client = redis.Redis.from_url(redisURL)
search_client = Client(index_name, conn=redis_client)
if checkIndex(search_client, index_name):
index_exists = True
print(f"The index '{index_name}' found.")
else:
print(f"Index '{index_name}' not found")
else:
print(f"Index name '{index_name}' does not provided")
return None
# Check if the index exists
if index_exists:
# Create RedisVectorStore object from existing index
vectorstore = RedisVectorStore.from_existing_index(embedding, redis_url=redisURL, index_name=index_name)
else:
if product_metadata is None:
product_metadata = createMeta()
texts = [
v['item_name'] for k, v in product_metadata.items()
]
# product metadata that we'll store along our vectors
metadatas = list(product_metadata.values())
# create and load redis with documents
vectorstore = RedisVectorStore.from_texts(
texts=texts,
metadatas=metadatas,
embedding=embedding,
index_name=index_name,
redis_url=redisURL
)
return vectorstore