Debdeep30 commited on
Commit
e870cb3
·
verified ·
1 Parent(s): d797bdc

Upload pipeline/memory.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. pipeline/memory.py +138 -0
pipeline/memory.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ChromaDB-backed session memory for Lumi.
3
+
4
+ Each session is stored as a summary document with patient facts in its metadata.
5
+ Session summaries only — no raw conversation logs (privacy).
6
+
7
+ At conversation start, get_context() injects the last N session summaries + all
8
+ known facts into the system prompt, giving Lumi full continuity without the
9
+ patient needing to repeat themselves.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import json
15
+ from datetime import datetime
16
+
17
+ import chromadb
18
+
19
+ _client: chromadb.Client | None = None
20
+ _collection: chromadb.Collection | None = None
21
+
22
+
23
+ def _get_collection() -> chromadb.Collection:
24
+ global _client, _collection
25
+ if _collection is None:
26
+ _client = chromadb.Client()
27
+ _collection = _client.get_or_create_collection("patient_memories")
28
+ return _collection
29
+
30
+
31
+ # ---------------------------------------------------------------------------
32
+ # Write
33
+ # ---------------------------------------------------------------------------
34
+
35
+ def save_session(
36
+ patient_id: str,
37
+ facts: list[str],
38
+ emotional_state: str,
39
+ confusion_level: str,
40
+ summary: str,
41
+ ) -> None:
42
+ col = _get_collection()
43
+ doc_id = f"{patient_id}_{datetime.now().timestamp()}"
44
+ col.add(
45
+ documents=[summary],
46
+ metadatas=[{
47
+ "patient_id": patient_id,
48
+ "timestamp": datetime.now().isoformat(),
49
+ "facts": json.dumps(facts),
50
+ "emotional_state": emotional_state,
51
+ "confusion_level": confusion_level,
52
+ }],
53
+ ids=[doc_id],
54
+ )
55
+
56
+
57
+ # ---------------------------------------------------------------------------
58
+ # Read
59
+ # ---------------------------------------------------------------------------
60
+
61
+ def get_context(patient_id: str, n_sessions: int = 3) -> dict:
62
+ """
63
+ Returns a dict with:
64
+ - summaries : list of the last N session summary strings
65
+ - facts : deduplicated list of all known facts for this patient
66
+ """
67
+ col = _get_collection()
68
+ results = col.query(
69
+ query_texts=["recent sessions"],
70
+ where={"patient_id": patient_id},
71
+ n_results=n_sessions,
72
+ )
73
+
74
+ summaries: list[str] = []
75
+ facts: list[str] = []
76
+
77
+ if results["documents"]:
78
+ for doc, meta in zip(results["documents"][0], results["metadatas"][0]):
79
+ summaries.append(doc)
80
+ raw_facts = json.loads(meta.get("facts", "[]"))
81
+ facts.extend(raw_facts)
82
+
83
+ seen: set[str] = set()
84
+ deduped_facts = []
85
+ for f in facts:
86
+ if f not in seen:
87
+ seen.add(f)
88
+ deduped_facts.append(f)
89
+
90
+ return {
91
+ "summaries": summaries,
92
+ "facts": deduped_facts,
93
+ }
94
+
95
+
96
+ # ---------------------------------------------------------------------------
97
+ # System prompt builder
98
+ # ---------------------------------------------------------------------------
99
+
100
+ SYSTEM_PROMPT_TEMPLATE = """\
101
+ You are Lumi, a warm and patient companion for {patient_name}.
102
+
103
+ What you know about {patient_name}:
104
+ {facts_block}
105
+
106
+ Recent sessions:
107
+ {sessions_block}
108
+
109
+ Always respond in this exact format:
110
+ [avatar_tag] Short opening line.
111
+ <think>Your internal reasoning here. Never sent to user.</think>
112
+ Full warm response.
113
+
114
+ Be gentle and patient. Never correct temporal confusion — focus on the emotion, not the timeline.
115
+ """
116
+
117
+
118
+ def build_system_prompt(
119
+ patient_id: str,
120
+ patient_name: str = "the patient",
121
+ n_sessions: int = 3,
122
+ ) -> str:
123
+ ctx = get_context(patient_id, n_sessions)
124
+
125
+ facts_block = (
126
+ "\n".join(f"- {f}" for f in ctx["facts"])
127
+ if ctx["facts"] else "- No personal facts recorded yet."
128
+ )
129
+ sessions_block = (
130
+ "\n\n".join(ctx["summaries"])
131
+ if ctx["summaries"] else "No previous sessions recorded."
132
+ )
133
+
134
+ return SYSTEM_PROMPT_TEMPLATE.format(
135
+ patient_name=patient_name,
136
+ facts_block=facts_block,
137
+ sessions_block=sessions_block,
138
+ )