32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
from langchain_community.vectorstores import FAISS
|
|
|
|
from embedding import LocalLMEmbeddings
|
|
|
|
|
|
def retrieve_enriched_context(query, db_path="./local_faiss_db"):
|
|
# 1. Re-initialize the same embedding model
|
|
embeddings_model = LocalLMEmbeddings(
|
|
model="text-embedding-qwen3-embedding-8b", base_url="http://192.168.0.49:1234"
|
|
)
|
|
|
|
# 2. Load the index from disk
|
|
# allow_dangerous_deserialization is required because FAISS uses pickle
|
|
vectorstore = FAISS.load_local(db_path, embeddings_model, allow_dangerous_deserialization=True)
|
|
|
|
# 3. Perform the search
|
|
# k=4 means "bring back the top 4 most relevant chunks"
|
|
results_with_scores = vectorstore.similarity_search_with_score(query, k=4)
|
|
|
|
return results_with_scores
|
|
|
|
|
|
# --- Example Usage ---
|
|
query = "the party get free bread but i cant remember why?"
|
|
hits = retrieve_enriched_context(query)
|
|
|
|
for doc, score in hits:
|
|
print(f"\n🎯 [Score: {score:.4f}]")
|
|
print(f"📄 Content: {doc.page_content[:200]}...")
|
|
print(f"🛠️ Metadata (Enrichment): {doc.metadata}")
|
|
# print(f"doc: {doc}")
|