Spaces:
Sleeping
Sleeping
Update rag_sessions.py
Browse files- rag_sessions.py +44 -3
rag_sessions.py
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
|
|
| 1 |
import uuid
|
| 2 |
|
| 3 |
SESSIONS = {} # session_id -> {thread_id, recent_turns, entity_memory}
|
| 4 |
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def start_session(thread_id: str) -> str:
|
| 7 |
"""Create a new session fixed to a given thread."""
|
| 8 |
sid = str(uuid.uuid4())
|
| 9 |
SESSIONS[sid] = {
|
| 10 |
"thread_id": thread_id,
|
| 11 |
"recent_turns": [],
|
| 12 |
-
"entity_memory":
|
| 13 |
}
|
| 14 |
return sid
|
| 15 |
|
|
@@ -25,5 +36,35 @@ def reset_session(session_id: str):
|
|
| 25 |
SESSIONS[session_id] = {
|
| 26 |
"thread_id": tid,
|
| 27 |
"recent_turns": [],
|
| 28 |
-
"entity_memory":
|
| 29 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# rag_sessions.py
|
| 2 |
import uuid
|
| 3 |
|
| 4 |
SESSIONS = {} # session_id -> {thread_id, recent_turns, entity_memory}
|
| 5 |
|
| 6 |
|
| 7 |
+
def _init_entity_memory():
|
| 8 |
+
"""Create a fresh entity memory structure."""
|
| 9 |
+
return {
|
| 10 |
+
"people": [],
|
| 11 |
+
"amounts": [],
|
| 12 |
+
"files": [],
|
| 13 |
+
"dates": [],
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
def start_session(thread_id: str) -> str:
|
| 18 |
"""Create a new session fixed to a given thread."""
|
| 19 |
sid = str(uuid.uuid4())
|
| 20 |
SESSIONS[sid] = {
|
| 21 |
"thread_id": thread_id,
|
| 22 |
"recent_turns": [],
|
| 23 |
+
"entity_memory": _init_entity_memory(),
|
| 24 |
}
|
| 25 |
return sid
|
| 26 |
|
|
|
|
| 36 |
SESSIONS[session_id] = {
|
| 37 |
"thread_id": tid,
|
| 38 |
"recent_turns": [],
|
| 39 |
+
"entity_memory": _init_entity_memory(),
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def update_entity_memory(session_id: str, new_entities: dict):
|
| 44 |
+
"""
|
| 45 |
+
Merge newly extracted entities into the session's entity_memory.
|
| 46 |
+
|
| 47 |
+
new_entities format:
|
| 48 |
+
{
|
| 49 |
+
"people": [...],
|
| 50 |
+
"amounts": [...],
|
| 51 |
+
"files": [...],
|
| 52 |
+
"dates": [...]
|
| 53 |
+
}
|
| 54 |
+
"""
|
| 55 |
+
session = get_session(session_id)
|
| 56 |
+
if session is None:
|
| 57 |
+
return
|
| 58 |
+
|
| 59 |
+
mem = session.get("entity_memory")
|
| 60 |
+
if not mem:
|
| 61 |
+
mem = _init_entity_memory()
|
| 62 |
+
session["entity_memory"] = mem
|
| 63 |
+
|
| 64 |
+
for key, values in new_entities.items():
|
| 65 |
+
if key not in mem:
|
| 66 |
+
mem[key] = []
|
| 67 |
+
# Append only unique values, preserve insertion order
|
| 68 |
+
for v in values:
|
| 69 |
+
if v not in mem[key]:
|
| 70 |
+
mem[key].append(v)
|