Spaces:
Runtime error
Runtime error
Create vector_store.py
Browse files- vector_store.py +20 -0
vector_store.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import faiss
|
2 |
+
import numpy as np
|
3 |
+
from langchain.vectorstores import FAISS
|
4 |
+
from langchain.schema import Document
|
5 |
+
from utils.embeddings import embedding_model
|
6 |
+
|
7 |
+
# Initialize FAISS index
|
8 |
+
dim = 384 # MiniLM embedding size
|
9 |
+
index = faiss.IndexFlatL2(dim)
|
10 |
+
vector_db = FAISS(embedding_function=embedding_model, index=index)
|
11 |
+
|
12 |
+
def add_document(text, doc_id):
|
13 |
+
"""Add document embeddings to FAISS."""
|
14 |
+
doc = Document(page_content=text, metadata={"id": doc_id})
|
15 |
+
vector_db.add_documents([doc])
|
16 |
+
|
17 |
+
def search(query, top_k=3):
|
18 |
+
"""Search FAISS index for similar documents."""
|
19 |
+
results = vector_db.similarity_search(query, k=top_k)
|
20 |
+
return results
|