-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathvector_store.py
40 lines (31 loc) · 1.31 KB
/
vector_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
import os
from dotenv import load_dotenv
from langchain_core.documents import Document
from langchain_openai import OpenAIEmbeddings
from langchain_ollama import OllamaEmbeddings
from langchain_redis import RedisConfig, RedisVectorStore
load_dotenv()
REDIS_URL = os.environ.get("REDIS_URL", "redis://localhost:6379/0")
INDEX_NAME = os.environ.get("VECTOR_INDEX_NAME", "oregon_trail")
config = RedisConfig(index_name=INDEX_NAME, redis_url=REDIS_URL)
redis_client = Redis.from_url(REDIS_URL)
doc = Document(
page_content="the northern trail, of the blue mountains, was destroyed by a flood and is no longer safe to traverse. It is recommended to take the southern trail although it is longer."
)
# TODO: participant can change to whatever desired model
embedding_model = OpenAIEmbeddings()
def _clean_existing(prefix):
for key in redis_client.scan_iter(f"{prefix}:*"):
redis_client.delete(key)
def get_vector_store():
try:
config.from_existing = True
vector_store = RedisVectorStore(embedding_model, config=config)
except:
print("Init vector store with document")
print("Clean any existing data in index")
_clean_existing(config.index_name)
config.from_existing = False
# TODO: define vector store
vector_store = None
return vector_store