rag.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# rag.py –
|
| 2 |
from __future__ import annotations
|
| 3 |
import os, re
|
| 4 |
from functools import lru_cache
|
|
@@ -15,7 +15,7 @@ from supabase import create_client
|
|
| 15 |
# ------------------------------------------------------------------
|
| 16 |
# CONFIG
|
| 17 |
# ------------------------------------------------------------------
|
| 18 |
-
HF_DS = "NimrodDev/LD_Events2"
|
| 19 |
EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 20 |
LLM_MODEL = "microsoft/DialoGPT-medium"
|
| 21 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
|
@@ -78,7 +78,7 @@ def _company_from_text(text: str) -> str:
|
|
| 78 |
return "LD Events"
|
| 79 |
if any(k in t for k in ("lamaki", "construction", "build", "site", "bungalow", "architect")):
|
| 80 |
return "Lamaki Designs"
|
| 81 |
-
return "LD Events"
|
| 82 |
|
| 83 |
def _detect_intent(text: str) -> str:
|
| 84 |
if GREETING_RE.search(text): return "greeting"
|
|
@@ -92,14 +92,14 @@ def _fallback_answer(company: str, intent: str) -> str:
|
|
| 92 |
return FALLBACKS[company].get(intent, FALLBACKS[company]["default"])
|
| 93 |
|
| 94 |
# ------------------------------------------------------------------
|
| 95 |
-
# RAM-ONLY DOCUMENT LOADER
|
| 96 |
# ------------------------------------------------------------------
|
| 97 |
def load_texts() -> List[str]:
|
| 98 |
-
ds = load_dataset(HF_DS, split="train")
|
| 99 |
return [row["text"] for row in ds if row.get("text")]
|
| 100 |
|
| 101 |
# ------------------------------------------------------------------
|
| 102 |
-
#
|
| 103 |
# ------------------------------------------------------------------
|
| 104 |
@lru_cache(maxsize=1)
|
| 105 |
def get_vectorstore() -> FAISS:
|
|
@@ -107,7 +107,7 @@ def get_vectorstore() -> FAISS:
|
|
| 107 |
splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=50)
|
| 108 |
docs = splitter.create_documents(texts, metadatas=[{"source": HF_DS}] * len(texts))
|
| 109 |
embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)
|
| 110 |
-
return FAISS.from_documents(docs, embeddings)
|
| 111 |
|
| 112 |
# ------------------------------------------------------------------
|
| 113 |
# LLM
|
|
@@ -146,7 +146,7 @@ def ask_question(phone: str, question: str) -> Tuple[str, List]:
|
|
| 146 |
_save_chat(phone, question, answer)
|
| 147 |
return answer, []
|
| 148 |
|
| 149 |
-
# RAG path
|
| 150 |
vs = get_vectorstore()
|
| 151 |
docs = vs.similarity_search(question, k=3)
|
| 152 |
if not docs:
|
|
|
|
| 1 |
+
# rag.py – single-index, zero-disk, HF-Space-safe edition
|
| 2 |
from __future__ import annotations
|
| 3 |
import os, re
|
| 4 |
from functools import lru_cache
|
|
|
|
| 15 |
# ------------------------------------------------------------------
|
| 16 |
# CONFIG
|
| 17 |
# ------------------------------------------------------------------
|
| 18 |
+
HF_DS = "NimrodDev/LD_Events2" # parquet branch auto-converted
|
| 19 |
EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 20 |
LLM_MODEL = "microsoft/DialoGPT-medium"
|
| 21 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
|
|
|
| 78 |
return "LD Events"
|
| 79 |
if any(k in t for k in ("lamaki", "construction", "build", "site", "bungalow", "architect")):
|
| 80 |
return "Lamaki Designs"
|
| 81 |
+
return "LD Events"
|
| 82 |
|
| 83 |
def _detect_intent(text: str) -> str:
|
| 84 |
if GREETING_RE.search(text): return "greeting"
|
|
|
|
| 92 |
return FALLBACKS[company].get(intent, FALLBACKS[company]["default"])
|
| 93 |
|
| 94 |
# ------------------------------------------------------------------
|
| 95 |
+
# RAM-ONLY DOCUMENT LOADER – PARQUET BRANCH
|
| 96 |
# ------------------------------------------------------------------
|
| 97 |
def load_texts() -> List[str]:
|
| 98 |
+
ds = load_dataset(HF_DS, revision="refs/convert/parquet", split="train")
|
| 99 |
return [row["text"] for row in ds if row.get("text")]
|
| 100 |
|
| 101 |
# ------------------------------------------------------------------
|
| 102 |
+
# SINGLE-BUILD VECTOR STORE (cached for life of worker)
|
| 103 |
# ------------------------------------------------------------------
|
| 104 |
@lru_cache(maxsize=1)
|
| 105 |
def get_vectorstore() -> FAISS:
|
|
|
|
| 107 |
splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=50)
|
| 108 |
docs = splitter.create_documents(texts, metadatas=[{"source": HF_DS}] * len(texts))
|
| 109 |
embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)
|
| 110 |
+
return FAISS.from_documents(docs, embeddings) # <- built ONCE
|
| 111 |
|
| 112 |
# ------------------------------------------------------------------
|
| 113 |
# LLM
|
|
|
|
| 146 |
_save_chat(phone, question, answer)
|
| 147 |
return answer, []
|
| 148 |
|
| 149 |
+
# RAG path – re-uses the *same* index every call
|
| 150 |
vs = get_vectorstore()
|
| 151 |
docs = vs.similarity_search(question, k=3)
|
| 152 |
if not docs:
|