Download rag/faiss_index.py from devserg/medi-bot: direct link, hf CLI and curl.
- Browser
- Download file 999 Bytes
-
https://huggingface.co/spaces/devserg/medi-bot/resolve/main/rag/faiss_index.py
- Command line
-
hf download hf://spaces/devserg/medi-bot/rag/faiss_index.py
-
curl -L -o faiss_index.py https://huggingface.co/spaces/devserg/medi-bot/resolve/main/rag/faiss_index.py
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) | |