Commit ·
3e7e287
0
Parent(s):
working rag + filters + fallbacks
Browse filesFiles changed (5)
- .gitignoreAdded+4
- DockerfileAdded+6
- app.pyAdded+16
- rag.pyAdded+161
- requirements.txtAdded+10
1234.envfaiss_dbdata/__pycache__/123456FROM python:3.11-slimWORKDIR /codeCOPY requirements.txt .RUN pip install -r requirements.txtCOPY . .CMD ["gunicorn", "app:app", "-b", "0.0.0.0:7860"]12345678910111213141516# app.pyfrom flask import Flask, request, jsonifyfrom rag import ask_question
app = Flask(__name__)
@app.route("/webhook", methods=["POST"])def webhook(): payload = request.get_json(force=True) phone = payload["phone"] question = payload["question"] answer, docs = ask_question(phone, question) return jsonify({"answer": answer, "docs": len(docs)})
if __name__ == "__main__": app.run(host="0.0.0.0", port=7860)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161# rag.py (v2 – with filters + fallbacks)from __future__ import annotationsimport os, uuid, tempfile, requests, shutil, refrom pathlib import Pathfrom functools import lru_cachefrom typing import List, Tuplefrom datasets import load_datasetfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom langchain_community.vectorstores import FAISSfrom langchain_huggingface import HuggingFaceEmbeddingsfrom langchain_core.prompts import PromptTemplatefrom langchain.chains import RetrievalQAfrom langchain_huggingface import HuggingFaceEndpointfrom supabase import create_client
# ---------- config ----------HF_DS = "NimrodDev/LD_Events2"EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"LLM_MODEL = "microsoft/DialoGPT-medium"FAISS_PATH = Path("faiss_db")SUPABASE_URL = os.getenv("SUPABASE_URL")SUPABASE_KEY = os.getenv("SUPABASE_KEY")HF_TOKEN = os.getenv("HF_TOKEN")
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
# ---------- keyword filters ----------GREETING_RE = re.compile(r"\b(hi|hello|hey|good morning|good afternoon|good evening)\b", re.I)THANKS_RE = re.compile(r"\b(thank|thanks|appreciate)\b", re.I)BYE_RE = re.compile(r"\b(bye|goodbye|see you|later)\b", re.I)MONEY_RE = re.compile(r"\b(price|cost|budget|cheap|expensive|money|usd|ksh|payment|deposit)\b", re.I)COMPLAIN_RE = re.compile(r"\b(complain|bad|terrible|awful|disappointed|angry|slow|rude)\b", re.I)
# ---------- company-specific fallbacks ----------FALLBACKS = { "LD Events": { "greeting": "Hello! 👋 Welcome to LD Events – your trusted partner for weddings, graduations and corporate events.", "money": "Our pricing depends on venue, guest count and package. Please share a few details so we can give you a tailored quote.", "complain": "We’re sorry to hear this. A senior agent will contact you within 30 minutes to resolve the issue.", "thanks": "You’re welcome! If you need anything else, just text back.", "bye": "Thanks for chatting with LD Events. Have a lovely day!", "default": "I’m not sure about that, but a human agent will follow up shortly." }, "Lamaki Designs": { "greeting": "Karibu! 🏗️ Lamaki Designs here – quality construction, architectural plans and project management.", "money": "Cost varies by project size and materials. Kindly share your plot size / plan so we can estimate for you.", "complain": "We apologise for the inconvenience. Our site manager will call you within 30 minutes to sort it out.", "thanks": "Asante! Feel free to text any time.", "bye": "Good-bye and stay safe!", "default": "Let me get back to you on that." }}
# ---------- helpers ----------def _company_from_text(text: str) -> str: t = text.lower() if any(k in t for k in ("ld events", "event", "wedding", "venue", "graduation")): return "LD Events" if any(k in t for k in ("lamaki", "construction", "build", "site", "bungalow", "architect")): return "Lamaki Designs" return "LD Events" # default
def _detect_intent(text: str) -> str: if GREETING_RE.search(text): return "greeting" if THANKS_RE.search(text): return "thanks" if BYE_RE.search(text): return "bye" if MONEY_RE.search(text): return "money" if COMPLAIN_RE.search(text): return "complain" return "normal"
def _fallback_answer(company: str, intent: str) -> str: return FALLBACKS[company].get(intent, FALLBACKS[company]["default"])
# ---------- pdf loader ----------def download_pdfs() -> List[Path]: data_dir = Path("data") data_dir.mkdir(exist_ok=True) ds = load_dataset(HF_DS, split="train", streaming=True) paths = [] for row in ds: url = row["pdf_url"] name = row.get("name", uuid.uuid4().hex) + ".pdf" dest = data_dir / name if not dest.exists(): r = requests.get(url, stream=True, timeout=30) r.raise_for_status() with open(dest, "wb") as f: shutil.copyfileobj(r.raw, f) paths.append(dest) return paths
# ---------- vector store ----------@lru_cache(maxsize=1)def get_vectorstore() -> FAISS: if FAISS_PATH.exists(): return FAISS.load_local(str(FAISS_PATH), HuggingFaceEmbeddings(model_name=EMBED_MODEL), allow_dangerous_deserialization=True) docs = [] splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=50) for pdf in download_pdfs(): text = Path(pdf).read_text(encoding="utf-8", errors="ignore") docs.extend(splitter.create_documents([text], metadatas=[{"source": pdf.name}])) vs = FAISS.from_documents(docs, HuggingFaceEmbeddings(model_name=EMBED_MODEL)) vs.save_local(str(FAISS_PATH)) return vs
# ---------- llm ----------@lru_cache(maxsize=1)def get_llm(): return HuggingFaceEndpoint( repo_id=LLM_MODEL, temperature=0.1, max_new_tokens=150, huggingfacehub_api_token=HF_TOKEN )
PROMPT = PromptTemplate.from_template("""You are Amina, assistant for {company}.Use only the context below. If unsure, say: “A human agent will follow up.”Context: {context}Question: {question}Answer:""")
# ---------- main entry ----------def ask_question(phone: str, question: str) -> Tuple[str, List]: intent = _detect_intent(question) company = _company_from_text(question)
# short-circuit greetings/thanks/bye – no LLM, no context needed if intent in ("greeting", "thanks", "bye"): answer = _fallback_answer(company, intent) _save_chat(phone, question, answer) return answer, []
# money or complaints: fallback if no docs vs = get_vectorstore() docs = vs.similarity_search(question, k=3) if not docs: answer = _fallback_answer(company, intent if intent in ("money", "complain") else "default") _save_chat(phone, question, answer) return answer, []
# normal RAG qa = RetrievalQA.from_chain_type( llm=get_llm(), retriever=vs.as_retriever(search_kwargs={"k": 3}), return_source_documents=True, chain_type_kwargs={"prompt": PROMPT} ) result = qa({"query": question, "company": company}) answer = result["result"].strip() _save_chat(phone, question, answer) return answer, result.get("source_documents", [])
def _save_chat(phone: str, q: str, a: str) -> None: supabase.table("chat_memory").insert({"user_phone": phone, "role": "user", "message": q}).execute() supabase.table("chat_memory").insert({"user_phone": phone, "role": "assistant", "message": a}).execute()12345678910flask==3.0.2langchain==0.3.0langchain-community==0.3.0langchain-huggingface==0.3.0sentence-transformers==3.0.0faiss-cpu==1.8.0datasets==2.20.0supabase==2.6.1python-dotenv==1.0.1gunicorn==22.0.0.gitignoreAdded+4
DockerfileAdded+6
app.pyAdded+16
rag.pyAdded+161
requirements.txtAdded+10