clean final: HF Inference API embeddings (no disk)
Browse files
rag.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# rag.py – bullet-proof: online fetch with fallback on any error
|
| 2 |
from __future__ import annotations
|
| 3 |
import os, re, json, requests
|
| 4 |
from functools import lru_cache
|
|
@@ -94,28 +93,22 @@ def get_texts() -> List[str]:
|
|
| 94 |
@lru_cache(maxsize=1)
|
| 95 |
def get_vectorstore() -> FAISS:
|
| 96 |
texts = get_texts()
|
| 97 |
-
embeddings = None
|
| 98 |
|
| 99 |
try:
|
| 100 |
-
# Try new API first
|
| 101 |
from langchain_huggingface import HuggingFaceInferenceAPIEmbeddings
|
| 102 |
embeddings = HuggingFaceInferenceAPIEmbeddings(
|
| 103 |
api_key=HF_TOKEN,
|
| 104 |
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 105 |
)
|
| 106 |
except ImportError:
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
)
|
| 113 |
-
print("⚙️ Using HuggingFaceEmbeddings fallback")
|
| 114 |
-
except Exception as e:
|
| 115 |
-
raise RuntimeError(f"❌ Failed to load embeddings: {e}")
|
| 116 |
|
| 117 |
if not texts:
|
| 118 |
-
return FAISS.from_texts([""], embeddings)
|
| 119 |
|
| 120 |
splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=50)
|
| 121 |
docs = splitter.create_documents(texts, metadatas=[{"source": DATASET}] * len(texts))
|
|
@@ -131,13 +124,11 @@ def get_llm():
|
|
| 131 |
huggingfacehub_api_token=HF_TOKEN
|
| 132 |
)
|
| 133 |
|
| 134 |
-
PROMPT = PromptTemplate.from_template("""
|
| 135 |
-
You are Amina, assistant for {company}.
|
| 136 |
Use only the context below. If unsure, say: "A human agent will follow up."
|
| 137 |
Context: {context}
|
| 138 |
Question: {question}
|
| 139 |
-
Answer:
|
| 140 |
-
""")
|
| 141 |
|
| 142 |
# ------------------------------------------------------------------ MAIN
|
| 143 |
def ask_question(phone: str, question: str) -> Tuple[str, List]:
|
|
@@ -169,4 +160,4 @@ def ask_question(phone: str, question: str) -> Tuple[str, List]:
|
|
| 169 |
|
| 170 |
def _save_chat(phone: str, q: str, a: str) -> None:
|
| 171 |
supabase.table("chat_memory").insert({"user_phone": phone, "role": "user", "message": q}).execute()
|
| 172 |
-
supabase.table("chat_memory").insert({"user_phone": phone, "role": "assistant", "message": a}).execute()
|
|
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
import os, re, json, requests
|
| 3 |
from functools import lru_cache
|
|
|
|
| 93 |
@lru_cache(maxsize=1)
|
| 94 |
def get_vectorstore() -> FAISS:
|
| 95 |
texts = get_texts()
|
|
|
|
| 96 |
|
| 97 |
try:
|
|
|
|
| 98 |
from langchain_huggingface import HuggingFaceInferenceAPIEmbeddings
|
| 99 |
embeddings = HuggingFaceInferenceAPIEmbeddings(
|
| 100 |
api_key=HF_TOKEN,
|
| 101 |
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 102 |
)
|
| 103 |
except ImportError:
|
| 104 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 105 |
+
embeddings = HuggingFaceEmbeddings(
|
| 106 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 107 |
+
)
|
| 108 |
+
print("⚠️ Falling back to HuggingFaceEmbeddings (local) – inference API not available")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
if not texts:
|
| 111 |
+
return FAISS.from_texts([""], embeddings)
|
| 112 |
|
| 113 |
splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=50)
|
| 114 |
docs = splitter.create_documents(texts, metadatas=[{"source": DATASET}] * len(texts))
|
|
|
|
| 124 |
huggingfacehub_api_token=HF_TOKEN
|
| 125 |
)
|
| 126 |
|
| 127 |
+
PROMPT = PromptTemplate.from_template("""You are Amina, assistant for {company}.
|
|
|
|
| 128 |
Use only the context below. If unsure, say: "A human agent will follow up."
|
| 129 |
Context: {context}
|
| 130 |
Question: {question}
|
| 131 |
+
Answer:""")
|
|
|
|
| 132 |
|
| 133 |
# ------------------------------------------------------------------ MAIN
|
| 134 |
def ask_question(phone: str, question: str) -> Tuple[str, List]:
|
|
|
|
| 160 |
|
| 161 |
def _save_chat(phone: str, q: str, a: str) -> None:
|
| 162 |
supabase.table("chat_memory").insert({"user_phone": phone, "role": "user", "message": q}).execute()
|
| 163 |
+
supabase.table("chat_memory").insert({"user_phone": phone, "role": "assistant", "message": a}).execute()
|