Spaces:
Sleeping
Sleeping
File size: 7,783 Bytes
433f3f1 0b170f9 433f3f1 0b170f9 433f3f1 0b170f9 433f3f1 0b170f9 433f3f1 0b170f9 433f3f1 0b170f9 433f3f1 0b170f9 433f3f1 | 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
#from langchain.schema import Document as LangchainDocument
from langchain_core.documents import Document as LangchainDocument
import config
from pathlib import Path
import os
import json
from datetime import datetime, timedelta
from supabase import create_client
from config import SUPABASE_URL, SUPABASE_SERVICE_KEY, HF_EMBEDDING_MODEL
from tempfile import TemporaryDirectory
embedding_model = HuggingFaceEmbeddings(model_name=HF_EMBEDDING_MODEL)
supabase = create_client(SUPABASE_URL, SUPABASE_SERVICE_KEY)
from supabase_ie import _load_history, _save_history
# --- LOAD CHAT HISTORY ---
def _prepare_chat_history_for_retrieval(user_id: str = "None", max_turns: int = 10):
"""Return recent chat messages (flattened) from short-term for prompt context."""
short = _load_history("chat_history_short", user_id)
if not short["sessions"]:
return []
current = short["sessions"][-1]["messages"]
return current[-max_turns:]
# --- SESSION EXPIRY ---
def close_current_session_if_expired(max_idle_minutes: int = 360, user_id: str = "None"): #TBC
"""Close session if idle too long in short-term history."""
short = _load_history("chat_history_short", user_id)
if not short["sessions"]:
return short
current = short["sessions"][-1]
if not current["messages"]:
return short
last_msg_time = datetime.fromisoformat(current["messages"][-1]["time"])
if datetime.utcnow() - last_msg_time > timedelta(minutes=max_idle_minutes):
current["status"] = "closed"
current["date_closed"] = datetime.utcnow().isoformat()
_save_history("chat_history_short", short, user_id)
return short
# --- SAVE CHAT ---
def save_chat_message(message_en: dict, message_user_language: dict, user_id: str = "None"):
"""Append a message to both short-term and total history in Supabase."""
now = datetime.utcnow().isoformat()
if isinstance(message_user_language, str):
message_user_language = {"lang": message_user_language}
# Ensure timestamp is present
if "time" not in message_en:
message_en["time"] = now
if "time" not in message_user_language:
message_user_language["time"] = now
# --- TOTAL (user language for UI) ---
total = _load_history("chat_history_total", user_id)
if not total["sessions"]:
total["sessions"].append({
"session_id": 1,
"status": "open",
"date_opened": now,
"date_closed": None,
"messages": []
})
total["sessions"][-1]["messages"].append(message_user_language)
_save_history("chat_history_total", total, user_id)
# --- SHORT (English for reasoning) ---
short = _load_history("chat_history_short", user_id)
if not short["sessions"]:
short["sessions"].append({
"session_id": 1,
"status": "open",
"date_opened": now,
"date_closed": None,
"messages": []
})
short["sessions"][-1]["messages"].append(message_en)
_save_history("chat_history_short", short, user_id)
print(f"[DEBUG][SAVE_CHAT] total_user_language += {message_user_language}")
print(f"[DEBUG][SAVE_CHAT] short_english += {message_en}")
# --- SESSION MANAGEMENT HELPERS ---
def on_pre_message_tick(user_id: str, username: str):
"""Call before new user message: close session if expired, rotate/archive if needed."""
closed = close_current_session_if_expired(user_id=user_id)
if closed:
rotate_archive_if_needed(max_sessions=2, user_id=user_id, username=username)
# --- ARCHIVE ---
def rotate_archive_if_needed(max_sessions: int = 2, user_id: str = "None", username: str = "None"):
"""Keep only recent sessions in short-term, archive old closed ones to FAISS."""
short = _load_history("chat_history_short", user_id)
while len(short["sessions"]) > max_sessions:
old_session = short["sessions"].pop(0)
if old_session["status"] == "closed":
# Build lightweight summary
summary_dict = _summarise_batch_with_llm([old_session])
summary_text = summary_dict["summary"]
# Metadata for traceability
metadata = {
"session_id": old_session["session_id"],
"date_opened": old_session.get("date_opened"),
"date_closed": old_session.get("date_closed"),
"user_id": user_id
}
# Archive into FAISS with metadata
_faiss_from_summary_and_merge(summary_dict, db_name="db5", username=username)
_save_history("chat_history_short", short, user_id)
return short
# --- SUMMARISER HELPERS ---
def _summarise_batch_with_llm(batch_sessions: list[dict]) -> dict:
"""
Lightweight summariser for sessions: just collect recent user prompts.
Returns a dict with summary text (string), date, and session_ids.
Suitable for FAISS storage without an LLM call.
"""
lines = []
for s in batch_sessions:
for m in s.get("messages", []):
if m.get("role") == "user":
txt = m.get("content", "").strip()
if txt:
lines.append(txt)
# Take only the last 20 user prompts
highlights = lines[-20:]
summary_text = (
f"Batch of {len(batch_sessions)} sessions "
f"(IDs {batch_sessions[0]['session_id']}–{batch_sessions[-1]['session_id']}).\n"
f"Recent user prompts:\n- " + "\n- ".join(highlights)
)
return {
"summary": summary_text,
"date": datetime.now().strftime("%Y-%m-%d"),
"session_ids": [s["session_id"] for s in batch_sessions]
}
def _faiss_from_summary_and_merge(summary: dict, db_name="db5", username: str = "None"):
"""
Add a summary dict to FAISS stored in Supabase.
Path: users/user_<name>/{db_name}/index.faiss, index.pkl
"""
user_folder = f"user_{username}"
embeddings = HuggingFaceEmbeddings(model_name=HF_EMBEDDING_MODEL)
with TemporaryDirectory() as tmp_dir:
tmp_path = Path(tmp_dir)
# 1. Create new FAISS index from summary
doc = LangchainDocument(
page_content=summary["summary"],
metadata={
"date": summary["date"],
"session_ids": summary["session_ids"],
"user": user_folder
}
)
new_db = FAISS.from_documents([doc], embeddings)
new_db.save_local(str(tmp_path))
# 2. Try to download existing FAISS DB from Supabase
existing_path = tmp_path / "existing"
existing_path.mkdir(exist_ok=True)
files = ["index.faiss", "index.pkl"]
has_existing = True
for f in files:
try:
res = supabase.storage.from_("vector_dbs").download(f"users/{user_folder}/{db_name}/{f}")
with open(existing_path / f, "wb") as out:
out.write(res)
except Exception:
has_existing = False
# 3. Merge if existing DB was found
if has_existing:
base = FAISS.load_local(str(existing_path), embeddings, allow_dangerous_deserialization=True)
incr = FAISS.load_local(str(tmp_path), embeddings, allow_dangerous_deserialization=True)
base.merge_from(incr)
base.save_local(str(tmp_path))
# 4. Upload merged FAISS back to Supabase
for f in files:
with open(tmp_path / f, "rb") as fh:
supabase.storage.from_("vector_dbs").upload(
f"users/{user_folder}/{db_name}/{f}", fh, {"upsert": "true"}
)
|