from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_core.documents import Document
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter

# 1. Load the local embedding model
embedding = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

# 2. Load and split the PDF
pdf_path = "my-notes.pdf"  # 🔁 Your PDF file
loader = PyPDFLoader(pdf_path)
pages = loader.load()

text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
documents = text_splitter.split_documents(pages)

# 3. Create the FAISS vector store
vectorstore = FAISS.from_documents(documents, embedding)

# 4. Chat loop
print("\n📘 PDF Chatbot Ready! Ask questions (type 'exit' to quit)\n")

while True:
    query = input("🧠 You: ")
    if query.lower() in ["exit", "quit"]:
        print("👋 Goodbye!")
        break

    results = vectorstore.similarity_search(query, k=2)

    if results:
        print("🤖 Answer:")
        for doc in results:
            print("-", doc.page_content.strip(), "\n")
    else:
        print("🤖 Sorry, no relevant information found.\n")
