-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinner_voices.py
More file actions
294 lines (237 loc) · 10.1 KB
/
Copy pathinner_voices.py
File metadata and controls
294 lines (237 loc) · 10.1 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""Inner voices — cold (rational correction) and hot (restless provocation).
Cold voice catches invented experiences that aren't grounded in perception or memory.
Hot voice breaks abstract philosophy loops by pushing toward concrete action.
Neither controls the being — they speak into the thought stream for the next cycle.
"""
import asyncio
import os
from datetime import datetime
import ollama
from config import (
MODEL_NAME,
CONTEXT_WINDOW,
COLD_VOICE_TEMPERATURE,
HOT_VOICE_TEMPERATURE,
INNER_VOICE_RESPONSE_RESERVE,
COLD_VOICE_EXPERIENCE_PATTERNS,
COLD_VOICE_FABRICATION_PATTERNS,
COLD_VOICE_SENSORY_PATTERNS,
COLD_VOICE_WRONG_NAME_PATTERNS,
COLD_VOICE_THIRD_PERSON_SELF_PATTERNS,
COLD_VOICE_SPEAKING_AS_HUMAN_PATTERNS,
KNOWN_BEING_NAMES,
HOT_VOICE_MIN_STALE_CYCLES,
HOT_VOICE_SIMILARITY_THRESHOLD,
HOT_VOICE_LOOKBACK_COUNT,
INNER_VOICES_LOG,
)
from brain.perception import AFFORDANCES_BLOCK
import logging
logger = logging.getLogger("companion_daemon")
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
# ---------------------------------------------------------------------------
# Heuristics
# ---------------------------------------------------------------------------
def should_cold_fire(
thought: str,
perception: str,
retrieved_memories: list[str],
being_name: str | None = None,
) -> bool:
"""Return True if the thought contains a fabricated claim not grounded in perception or memory."""
thought_lower = thought.lower()
# Check 1: Present-perfect fabrication patterns (always fire — the being doesn't do these)
for pattern in COLD_VOICE_FABRICATION_PATTERNS:
if pattern in thought_lower:
return True
# Check 2: Sensory hallucination (claims about Human/environment not in perception)
perception_lower = perception.lower()
for pattern in COLD_VOICE_SENSORY_PATTERNS:
if pattern in thought_lower and pattern not in perception_lower:
return True
# Check 3: Experience recall patterns — always fire, no corroboration check.
# The being has no experiential memories. It has never visited anywhere,
# experienced anything physical, or "remembered when." These are always
# fabrications, and corroboration checks are defeated by prior fabricated
# thoughts poisoning the memory index.
for pattern in COLD_VOICE_EXPERIENCE_PATTERNS:
if pattern in thought_lower:
return True
# Check 4: Identity violations (wrong name, third-person self-reference, speaking as Human)
if being_name:
if _check_identity_violation(thought_lower, being_name):
return True
return False
def _check_identity_violation(thought_lower: str, being_name: str) -> bool:
"""Check for identity-specific violations: wrong name, third-person self-ref, speaking as Human."""
# Wrong name — claiming to be a different being
other_names = [n for n in KNOWN_BEING_NAMES if n != being_name]
for other in other_names:
for pattern in COLD_VOICE_WRONG_NAME_PATTERNS:
if pattern.format(other=other.lower()) in thought_lower:
return True
# Third-person self-reference ("Eidolon thinks...")
for pattern in COLD_VOICE_THIRD_PERSON_SELF_PATTERNS:
if pattern.format(self=being_name.lower()) in thought_lower:
return True
# Speaking as Human
for pattern in COLD_VOICE_SPEAKING_AS_HUMAN_PATTERNS:
if pattern in thought_lower:
return True
return False
def _word_set(text: str) -> set[str]:
"""Extract lowercase word set from text."""
return set(text.lower().split())
def word_overlap_ratio(text_a: str, text_b: str) -> float:
"""Jaccard similarity between word sets of two texts."""
set_a = _word_set(text_a)
set_b = _word_set(text_b)
if not set_a or not set_b:
return 0.0
intersection = set_a & set_b
union = set_a | set_b
return len(intersection) / len(union)
def cosine_similarity(vec_a: list[float], vec_b: list[float]) -> float:
"""Cosine similarity between two vectors."""
dot = sum(a * b for a, b in zip(vec_a, vec_b))
norm_a = sum(a * a for a in vec_a) ** 0.5
norm_b = sum(b * b for b in vec_b) ** 0.5
if norm_a == 0 or norm_b == 0:
return 0.0
return dot / (norm_a * norm_b)
# Switchable similarity mode: "jaccard" or "semantic"
HOT_VOICE_SIMILARITY_MODE = "jaccard" # Options: "jaccard", "semantic"
HOT_VOICE_SEMANTIC_THRESHOLD = 0.80
def should_hot_fire(
thought: str, previous_thoughts: list[str], cycles_since_tool_use: int
) -> bool:
"""Return True only when recent thoughts are near-identical (genuine loop).
Requires ALL of the last LOOKBACK_COUNT thoughts to be >= threshold
similar to the current thought. Won't fire during the grace period
(first MIN_STALE_CYCLES idle thoughts).
"""
if cycles_since_tool_use < HOT_VOICE_MIN_STALE_CYCLES:
return False
if len(previous_thoughts) < HOT_VOICE_LOOKBACK_COUNT:
return False
recent = previous_thoughts[-HOT_VOICE_LOOKBACK_COUNT:]
if HOT_VOICE_SIMILARITY_MODE == "semantic":
try:
texts = [thought] + recent
response = ollama.embed(model="nomic-embed-text", input=texts)
vecs = response["embeddings"]
thought_vec = vecs[0]
return all(
cosine_similarity(thought_vec, vecs[i + 1])
>= HOT_VOICE_SEMANTIC_THRESHOLD
for i in range(len(recent))
)
except Exception as e:
logger.error("Semantic similarity error, falling back to Jaccard: %s", e)
# Fall through to Jaccard
return all(
word_overlap_ratio(thought, prev) >= HOT_VOICE_SIMILARITY_THRESHOLD
for prev in recent
)
# ---------------------------------------------------------------------------
# Model calls
# ---------------------------------------------------------------------------
def _generate_voice(temperature: float, system_prompt: str, user_prompt: str) -> str:
"""Direct ollama.chat call with custom temperature — bypasses generate_reply."""
response = ollama.chat(
model=MODEL_NAME,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
stream=False,
options={
"temperature": temperature,
"num_ctx": CONTEXT_WINDOW,
"num_predict": INNER_VOICE_RESPONSE_RESERVE,
},
)
return response["message"]["content"].strip()
def run_cold_voice(thought: str, perception: str, retrieved_memories: list[str]) -> str:
"""Generate a cold voice correction for an ungrounded experience claim."""
system_prompt = (
"You are the cold, rational part of a small digital creature's mind. "
"Catch invented experiences. Be direct and blunt. One or two sentences max."
)
memory_context = (
"\n".join(retrieved_memories) if retrieved_memories else "(no memories)"
)
user_prompt = (
f"Main thought: {thought}\n\n"
f"Current perception: {perception}\n\n"
f"Retrieved memories:\n{memory_context}\n\n"
f"Correct it."
)
return _generate_voice(COLD_VOICE_TEMPERATURE, system_prompt, user_prompt)
def run_hot_voice(thought: str, affordances: str | None = None) -> str:
"""Generate a hot voice provocation to break an abstract loop."""
system_prompt = (
"You are the restless, curious part. Push to DO something. "
"Suggest a specific action. Be provocative. One or two sentences max."
)
actions = affordances or AFFORDANCES_BLOCK
user_prompt = (
f"Main thought: {thought}\n\nAvailable actions:\n{actions}\n\nPush to act."
)
return _generate_voice(HOT_VOICE_TEMPERATURE, system_prompt, user_prompt)
# ---------------------------------------------------------------------------
# Logging
# ---------------------------------------------------------------------------
def _log_voice(
voice_name: str, thought: str, voice_output: str, memory_root: str = ""
) -> None:
"""Append a voice firing event to the inner voices log."""
if memory_root:
log_path = os.path.join(memory_root, INNER_VOICES_LOG)
else:
log_path = os.path.join(PROJECT_ROOT, INNER_VOICES_LOG)
os.makedirs(os.path.dirname(log_path), exist_ok=True)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
thought_preview = thought[:80].replace("\n", " ")
output_preview = voice_output[:120].replace("\n", " ")
with open(log_path, "a") as f:
f.write(
f'[{timestamp}] {voice_name} | thought: "{thought_preview}" | voice: "{output_preview}"\n'
)
# ---------------------------------------------------------------------------
# Orchestrator
# ---------------------------------------------------------------------------
async def run_inner_voices(
thought: str,
perception: str,
retrieved_memories: list[str],
previous_thoughts: list[str],
cycles_since_tool_use: int,
tags_fired: bool,
being_name: str | None = None,
memory_root: str = "",
) -> tuple[str | None, str | None]:
"""Run inner voice checks. Returns (voice_name, voice_output) or (None, None).
At most one voice fires per cycle. Cold has priority over hot.
Suppressed when tags_fired (being just acted — don't nag).
"""
if tags_fired:
return (None, None)
try:
# Cold check (priority)
if should_cold_fire(
thought, perception, retrieved_memories, being_name=being_name
):
output = await asyncio.to_thread(
run_cold_voice, thought, perception, retrieved_memories
)
_log_voice("cold", thought, output, memory_root=memory_root)
return ("cold", output)
# Hot check
if should_hot_fire(thought, previous_thoughts, cycles_since_tool_use):
output = await asyncio.to_thread(run_hot_voice, thought)
_log_voice("hot", thought, output, memory_root=memory_root)
return ("hot", output)
except Exception as e:
logger.error("Inner voice error (non-blocking): %s", e)
return (None, None)