File size: 1,378 Bytes
8630e6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS

# Path to your saved FAISS vectorstore
VECTORSTORE_DIR = "../vectorStore"

def query_faiss():
    # Load embeddings
    embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

    # Load FAISS vectorstore
    db = FAISS.load_local(VECTORSTORE_DIR, embeddings, allow_dangerous_deserialization=True)

    print("✅ FAISS vectorstore loaded successfully.")
    print(f"Total chunks in DB: {len(db.docstore._dict)}")

    while True:
        query = input("\nEnter your query (or type 'exit' to quit): ").strip()
        if query.lower() == "exit":
            print("Exiting...")
            break

        # Perform similarity search
        results = db.similarity_search(query, k=5)  # top 5 results

        if not results:
            print("❌ No matching documents found.")
        else:
            print(f"\n🔹 Top {len(results)} matches:")
            for i, doc in enumerate(results, 1):
                # Print metadata + first 200 chars of content
                content_preview = doc.page_content[:200].replace("\n", " ")
                print(f"{i}. {content_preview}")
                print(f"   Metadata: {doc.metadata}\n")

if __name__ == "__main__":
    query_faiss()