Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update vecstore.py #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions vecstore.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,56 @@
"""Utilities for creating and using vector indexes."""
from pathlib import Path

from typing import List
from utils import pretty_log

INDEX_NAME = "openai-ada-fsdl"
VECTOR_DIR = Path("/vectors")


def connect_to_vector_index(index_name, embedding_engine):
def connect_to_vector_index(index_name: str, embedding_engine) -> 'FAISS':
"""Adds the texts and metadatas to the vector index."""
from langchain.vectorstores import FAISS

vector_index = FAISS.load_local(VECTOR_DIR, embedding_engine, index_name)

return vector_index
try:
vector_index = FAISS.load_local(VECTOR_DIR, embedding_engine, index_name)
return vector_index
except Exception as e:
pretty_log(f"Error connecting to vector index: {e}")
return None


def get_embedding_engine(model="text-embedding-ada-002", **kwargs):
def get_embedding_engine(model: str = "text-embedding-ada-002", **kwargs) -> 'OpenAIEmbeddings':
"""Retrieves the embedding engine."""
from langchain.embeddings import OpenAIEmbeddings

embedding_engine = OpenAIEmbeddings(model=model, **kwargs)

return embedding_engine
try:
embedding_engine = OpenAIEmbeddings(model=model, **kwargs)
return embedding_engine
except Exception as e:
pretty_log(f"Error initializing embedding engine: {e}")
return None


def create_vector_index(index_name, embedding_engine, documents, metadatas):
def create_vector_index(index_name: str, embedding_engine, documents: List[str], metadatas: List[dict]) -> 'FAISS':
"""Creates a vector index that offers similarity search."""
from langchain import FAISS
from langchain.vectorstores import FAISS

files = VECTOR_DIR.glob(f"{index_name}.*")
# Clean up existing index files if any
files = list(VECTOR_DIR.glob(f"{index_name}.*"))
if files:
for file in files:
file.unlink()
pretty_log("existing index wiped")

index = FAISS.from_texts(
texts=documents, embedding=embedding_engine, metadatas=metadatas
)

return index
try:
file.unlink()
pretty_log(f"Deleted existing index file: {file.name}")
except Exception as e:
pretty_log(f"Error deleting file {file.name}: {e}")

# Create a new index
try:
index = FAISS.from_texts(
texts=documents, embedding=embedding_engine, metadatas=metadatas
)
return index
except Exception as e:
pretty_log(f"Error creating vector index: {e}")
return None