123410 unmodified lines1516171819202156 unmodified lines787980818283847 unmodified lines92939495969798991001011021031041051 unmodified line10710810911011111211332 unmodified lines146147148149150151152# rag.py – full unbiased, zero-disk, HF-Space-safe editionfrom __future__ import annotationsimport os, refrom functools import lru_cache10 unmodified lines# ------------------------------------------------------------------# CONFIG# ------------------------------------------------------------------HF_DS = "NimrodDev/LD_Events2"EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"LLM_MODEL = "microsoft/DialoGPT-medium"SUPABASE_URL = os.getenv("SUPABASE_URL")56 unmodified linesreturn "LD Events"if any(k in t for k in ("lamaki", "construction", "build", "site", "bungalow", "architect")):return "Lamaki Designs"return "LD Events" # default when nothing matchesdef _detect_intent(text: str) -> str:if GREETING_RE.search(text): return "greeting"7 unmodified linesreturn FALLBACKS[company].get(intent, FALLBACKS[company]["default"])# ------------------------------------------------------------------# RAM-ONLY DOCUMENT LOADER# ------------------------------------------------------------------def load_texts() -> List[str]:ds = load_dataset(HF_DS, split="train")return [row["text"] for row in ds if row.get("text")]# ------------------------------------------------------------------# RAM-ONLY VECTOR STORE# ------------------------------------------------------------------@lru_cache(maxsize=1)def get_vectorstore() -> FAISS:1 unmodified linesplitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=50)docs = splitter.create_documents(texts, metadatas=[{"source": HF_DS}] * len(texts))embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)return FAISS.from_documents(docs, embeddings)# ------------------------------------------------------------------# LLM32 unmodified lines_save_chat(phone, question, answer)return answer, []# RAG pathvs = get_vectorstore()docs = vs.similarity_search(question, k=3)if not docs:123410 unmodified lines1516171819202156 unmodified lines787980818283847 unmodified lines92939495969798991001011021031041051 unmodified line10710810911011111211332 unmodified lines146147148149150151152# rag.py – single-index, zero-disk, HF-Space-safe editionfrom __future__ import annotationsimport os, refrom functools import lru_cache10 unmodified lines# ------------------------------------------------------------------# CONFIG# ------------------------------------------------------------------HF_DS = "NimrodDev/LD_Events2" # parquet branch auto-convertedEMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"LLM_MODEL = "microsoft/DialoGPT-medium"SUPABASE_URL = os.getenv("SUPABASE_URL")56 unmodified linesreturn "LD Events"if any(k in t for k in ("lamaki", "construction", "build", "site", "bungalow", "architect")):return "Lamaki Designs"return "LD Events"def _detect_intent(text: str) -> str:if GREETING_RE.search(text): return "greeting"7 unmodified linesreturn FALLBACKS[company].get(intent, FALLBACKS[company]["default"])# ------------------------------------------------------------------# RAM-ONLY DOCUMENT LOADER – PARQUET BRANCH# ------------------------------------------------------------------def load_texts() -> List[str]:ds = load_dataset(HF_DS, revision="refs/convert/parquet", split="train")return [row["text"] for row in ds if row.get("text")]# ------------------------------------------------------------------# SINGLE-BUILD VECTOR STORE (cached for life of worker)# ------------------------------------------------------------------@lru_cache(maxsize=1)def get_vectorstore() -> FAISS:1 unmodified linesplitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=50)docs = splitter.create_documents(texts, metadatas=[{"source": HF_DS}] * len(texts))embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)return FAISS.from_documents(docs, embeddings) # <- built ONCE# ------------------------------------------------------------------# LLM32 unmodified lines_save_chat(phone, question, answer)return answer, []# RAG path – re-uses the *same* index every callvs = get_vectorstore()docs = vs.similarity_search(question, k=3)if not docs:
rag.py+8-8