Spaces:
Runtime error

medi-bot / rag /faiss_index.py
devserg's picture
Claude Sonnet 4.6
Initial MediBot deployment
4adf7aa
Raw History Blame Contribute Delete
999 Bytes
"""Build, save, and load the FAISS vector index for disease documents."""
from pathlib import Path
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from config import EMBEDDING_MODEL, FAISS_INDEX_PATH
def get_embeddings() -> OpenAIEmbeddings:
return OpenAIEmbeddings(model=EMBEDDING_MODEL)
def build_and_save(docs: list[str], metadata: list[dict]) -> FAISS:
embeddings = get_embeddings()
store = FAISS.from_texts(docs, embeddings, metadatas=metadata)
store.save_local(str(FAISS_INDEX_PATH))
return store
def load_index() -> FAISS:
embeddings = get_embeddings()
return FAISS.load_local(
str(FAISS_INDEX_PATH),
embeddings,
allow_dangerous_deserialization=True,
)
def get_or_build_index(docs: list[str], metadata: list[dict]) -> FAISS:
index_file = Path(FAISS_INDEX_PATH) / "index.faiss"
if index_file.exists():
return load_index()
return build_and_save(docs, metadata)