Download src/retrieval/embed_store.py from Ilaa-1505/Explainable-RAG-System: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/Ilaa-1505/Explainable-RAG-System/resolve/main/src/retrieval/embed_store.py
- Command line
-
hf download hf://spaces/Ilaa-1505/Explainable-RAG-System/src/retrieval/embed_store.py
-
curl -L -o embed_store.py https://huggingface.co/spaces/Ilaa-1505/Explainable-RAG-System/resolve/main/src/retrieval/embed_store.py
1.09 kB
| import json | |
| from tqdm import tqdm | |
| from sentence_transformers import SentenceTransformer | |
| from chromadb import PersistentClient | |
| model = SentenceTransformer("BAAI/bge-small-en-v1.5") | |
| client = PersistentClient(path="embeddings/") | |
| collection = client.get_or_create_collection(name="rag_docs") | |
| def embed_and_store(input_path="processed/chunks.json"): | |
| with open(input_path, "r") as f: | |
| chunks = json.load(f) | |
| documents, metadatas, ids = [], [], [] | |
| for i, chunk in enumerate(tqdm(chunks)): | |
| documents.append("passage: " + chunk["text"]) | |
| metadatas.append(chunk["metadata"]) | |
| ids.append(f"chunk_{i}") | |
| client.delete_collection(name="rag_docs") | |
| collection = client.get_or_create_collection(name="rag_docs") | |
| embeddings = model.encode( | |
| documents, | |
| batch_size=32, | |
| normalize_embeddings=True, | |
| show_progress_bar=True | |
| ) | |
| collection.add( | |
| documents=documents, | |
| metadatas=metadatas, | |
| ids=ids, | |
| embeddings=embeddings.tolist() | |
| ) | |
| if __name__ == "__main__": | |
| embed_and_store() |