-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantic.py
More file actions
39 lines (31 loc) · 1.6 KB
/
Copy pathsemantic.py
File metadata and controls
39 lines (31 loc) · 1.6 KB
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
from pathlib import Path
from langchain_core.documents import Document
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
def build_semantic_search(documents: list[Document], save_path: str = "../data/processed/faiss_index") -> tuple:
"""
Builds a dense retrieval system using Transformer embeddings and FAISS.
"""
print("Initializing Transformer Embeddings...")
# This uses the Transformer architecture discussed in Lecture 5 & 6
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
print("Building FAISS Vector Store...")
vector_store = FAISS.from_documents(documents, embeddings)
# Persist the vector store
Path(save_path).mkdir(parents=True, exist_ok=True)
vector_store.save_local(save_path)
print(f"Semantic FAISS index successfully saved to {save_path}")
return vector_store, embeddings
def load_semantic(path: str) -> tuple:
"""Load a saved FAISS vector store from disk."""
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
vector_store = FAISS.load_local(path, embeddings, allow_dangerous_deserialization=True)
return vector_store, embeddings
def execute_semantic_search(vector_store: FAISS, query: str, k: int = 20):
"""
Executes a semantic search and returns documents with their L2 distance scores.
"""
# Returns a list of (Document, score) tuples.
# Note: Lower score indicates higher similarity (closer distance in vector space).
docs_and_scores = vector_store.similarity_search_with_score(query, k=k)
return docs_and_scores