Spaces:
Paused
Paused
Malina Claude Opus 4.8 commited on
Commit ·
5ea523e
1
Parent(s): 6cf15a3
brain: switch to aya-expanse-32b (your 32B/gemma-4 weren't available; gemma-4 broke Aria entirely)
Browse files- model: CohereLabs/aya-expanse-32b (works, strong Polish; kept bill_to=NextNeural)
- restore reactions in build_user_frame (SYSTEM_PROMPT untouched), de-emphasized so the
model still replies with words; both directions (Aria reacts + comments user reactions)
- reply context + silent gender (never verbalizes the assumption)
- robust output parsing: accept content/text/message keys; split multi-sentence replies
into separate lowercase no-period bubbles (chat style, model-agnostic)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- brain/orchestrator.py +1 -1
- brain/persona.py +44 -14
- brain/schema.py +37 -6
brain/orchestrator.py
CHANGED
|
@@ -19,7 +19,7 @@ from huggingface_hub import InferenceClient
|
|
| 19 |
from .persona import SYSTEM_PROMPT, build_user_frame
|
| 20 |
from .schema import sanitize_bubbles
|
| 21 |
|
| 22 |
-
MODEL = os.environ.get("ARIA_MODEL", "
|
| 23 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 24 |
|
| 25 |
BILL_TO = os.environ.get("ARIA_BILL_TO", "NextNeural") or None
|
|
|
|
| 19 |
from .persona import SYSTEM_PROMPT, build_user_frame
|
| 20 |
from .schema import sanitize_bubbles
|
| 21 |
|
| 22 |
+
MODEL = os.environ.get("ARIA_MODEL", "CohereLabs/aya-expanse-32b")
|
| 23 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 24 |
|
| 25 |
BILL_TO = os.environ.get("ARIA_BILL_TO", "NextNeural") or None
|
brain/persona.py
CHANGED
|
@@ -166,31 +166,61 @@ zwracasz WYŁĄCZNIE JSON
|
|
| 166 |
|
| 167 |
|
| 168 |
def build_user_frame(ctx: dict) -> str:
|
| 169 |
-
"""
|
| 170 |
-
|
| 171 |
-
|
| 172 |
turn = ctx.get("turn_events", [])
|
| 173 |
media = ctx.get("media", {})
|
| 174 |
topic = ctx.get("topic")
|
| 175 |
-
lines = []
|
| 176 |
name = (ctx.get("user_name") or "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
if name:
|
| 178 |
lines.append(
|
| 179 |
-
f"
|
| 180 |
-
f"
|
| 181 |
-
f"
|
| 182 |
-
f"
|
| 183 |
)
|
| 184 |
if topic:
|
| 185 |
-
lines.append(f"(
|
| 186 |
if media:
|
| 187 |
for eid, m in media.items():
|
| 188 |
lines.append(f"- załącznik: {m.get('kind')} ({m.get('name')}) [id: {eid}]")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
lines.append(
|
| 190 |
-
"
|
| 191 |
-
"
|
| 192 |
-
"user ich nie widzi i nie ma o nich pojęcia."
|
| 193 |
)
|
| 194 |
-
lines.append(f"id zdarzeń tej tury (tylko do target_event_id): {turn}")
|
| 195 |
-
lines.append("Odpowiedz teraz. Tylko JSON (tablica obiektów).")
|
| 196 |
return "\n".join(lines)
|
|
|
|
| 166 |
|
| 167 |
|
| 168 |
def build_user_frame(ctx: dict) -> str:
|
| 169 |
+
"""Per-turn instruction frame appended after the reconstructed history.
|
| 170 |
+
(Separate from SYSTEM_PROMPT.) Carries gender handling, reply context, the
|
| 171 |
+
reaction contract, the react-to-reaction trigger, and id-safety."""
|
| 172 |
turn = ctx.get("turn_events", [])
|
| 173 |
media = ctx.get("media", {})
|
| 174 |
topic = ctx.get("topic")
|
|
|
|
| 175 |
name = (ctx.get("user_name") or "").strip()
|
| 176 |
+
reply_to = ctx.get("reply_to") or {}
|
| 177 |
+
user_reaction = ctx.get("user_reaction") or {}
|
| 178 |
+
lines = []
|
| 179 |
+
|
| 180 |
+
# gender — SILENT: use correct gendered forms, never verbalize the assumption
|
| 181 |
if name:
|
| 182 |
lines.append(
|
| 183 |
+
f"Rozmówca: {name}. Po nazwie wywnioskuj płeć (polskie imię na -a zwykle żeńskie, reszta zwykle "
|
| 184 |
+
f"męskie) i używaj poprawnych form rodzajowych w jego stronę ('zrobiłeś'/'zrobiłaś'). NIGDY nie "
|
| 185 |
+
f"komentuj płci ani założeń (żadnego 'zakładam że jesteś...') — po prostu używaj właściwej formy. "
|
| 186 |
+
f"Nie da się ocenić → formy neutralne. Imienia używaj bardzo rzadko."
|
| 187 |
)
|
| 188 |
if topic:
|
| 189 |
+
lines.append(f"(wątek: {topic})")
|
| 190 |
if media:
|
| 191 |
for eid, m in media.items():
|
| 192 |
lines.append(f"- załącznik: {m.get('kind')} ({m.get('name')}) [id: {eid}]")
|
| 193 |
+
|
| 194 |
+
# reply context (both directions)
|
| 195 |
+
if reply_to.get("text"):
|
| 196 |
+
whose = "Twoją wcześniejszą wiadomość" if reply_to.get("role") == "assistant" \
|
| 197 |
+
else "swoją wcześniejszą wiadomość"
|
| 198 |
+
lines.append(f'Rozmówca ODPOWIADA na {whose}: "{reply_to["text"]}". Odnieś się właśnie do niej.')
|
| 199 |
+
|
| 200 |
+
# triggered purely by the user reacting to one of Aria's messages (no text)
|
| 201 |
+
if user_reaction.get("emoji"):
|
| 202 |
+
lines.append(
|
| 203 |
+
f'Rozmówca nic nie napisał — tylko dodał reakcję {user_reaction["emoji"]} do Twojej wiadomości: '
|
| 204 |
+
f'"{user_reaction.get("on_text", "")}". Jeśli to zabawne/warte zaczepki (np. śmiech na coś '
|
| 205 |
+
f'niesmiesznego) skomentuj to krótko SŁOWAMI (np. "lmao co cię tak bawi"). Jak nie ma sensu — zwróć []. '
|
| 206 |
+
f'NIE odpowiadaj reakcją.'
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
# id safety
|
| 210 |
+
lines.append(
|
| 211 |
+
"id poniżej to WEWNĘTRZNE technikalia — używaj ich tylko w polu target_event_id, NIGDY w treści dymka."
|
| 212 |
+
)
|
| 213 |
+
lines.append(f"id zdarzeń tej tury: {turn}")
|
| 214 |
+
|
| 215 |
+
# OPTIONAL reaction add-on, intentionally de-emphasized (model over-reacted otherwise)
|
| 216 |
+
lines.append(
|
| 217 |
+
'(opcjonalnie, rzadko) MOŻESZ dodatkowo dorzucić reakcję emotką jako osobny obiekt '
|
| 218 |
+
'{"bubble":"😂","target_event_id":"<id>","type":"reaction"}, dozwolone tylko 👍 ❤️ 😂 😮 😢 😡 '
|
| 219 |
+
'(❤️="ok/spoko"). To tylko dodatek — NIGDY zamiast odpowiedzi.'
|
| 220 |
+
)
|
| 221 |
+
# PRIMARY task last (recency): real worded reply
|
| 222 |
lines.append(
|
| 223 |
+
"NAJWAŻNIEJSZE: odpowiedz naturalnie SŁOWAMI na ostatnią wiadomość rozmówcy "
|
| 224 |
+
"(dymki typu response/follow_up). Zwróć WYŁĄCZNIE JSON — tablicę obiektów."
|
|
|
|
| 225 |
)
|
|
|
|
|
|
|
| 226 |
return "\n".join(lines)
|
brain/schema.py
CHANGED
|
@@ -5,6 +5,29 @@ import re
|
|
| 5 |
# Internal event/bubble ids must never leak into visible text.
|
| 6 |
_ID_LEAK = re.compile(r'["\'„“”]?\b(?:evt|sys|bub|ac)_[A-Za-z0-9]+\b["\'„“”]?')
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
EVENT_TYPES = ("message", "image", "file", "system_idle")
|
| 9 |
BUBBLE_TYPES = ("response", "follow_up", "proactive", "reaction")
|
| 10 |
ALLOWED_REACTIONS = ("👍", "❤️", "😂", "😮", "😢", "😡")
|
|
@@ -48,19 +71,27 @@ def sanitize_bubbles(raw, valid_event_ids):
|
|
| 48 |
if not isinstance(b, dict):
|
| 49 |
continue
|
| 50 |
btype = b.get("type") if b.get("type") in BUBBLE_TYPES else "response"
|
| 51 |
-
target = b.get("target_event_id")
|
| 52 |
if target not in valid_event_ids:
|
| 53 |
target = None
|
|
|
|
|
|
|
|
|
|
| 54 |
# Reaction: keep the raw emoji (no messenger styling); must be allowed + targeted.
|
| 55 |
if btype == "reaction":
|
| 56 |
-
emoji = str(
|
| 57 |
if emoji in ALLOWED_REACTIONS and target:
|
| 58 |
out.append({"bubble": emoji, "target_event_id": target, "type": "reaction"})
|
| 59 |
continue
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
return out
|
| 65 |
|
| 66 |
|
|
|
|
| 5 |
# Internal event/bubble ids must never leak into visible text.
|
| 6 |
_ID_LEAK = re.compile(r'["\'„“”]?\b(?:evt|sys|bub|ac)_[A-Za-z0-9]+\b["\'„“”]?')
|
| 7 |
|
| 8 |
+
# Split a multi-sentence reply into separate messenger bubbles (chat style).
|
| 9 |
+
# Don't split right after a few common Polish abbreviations.
|
| 10 |
+
_ABBR = ("np", "itd", "itp", "tzn", "tj", "m.in", "dr", "mgr", "godz", "ok")
|
| 11 |
+
_SENT_SPLIT = re.compile(r'(?<=[.!?…])\s+')
|
| 12 |
+
|
| 13 |
+
def _split_sentences(text):
|
| 14 |
+
text = (text or "").strip()
|
| 15 |
+
if not text:
|
| 16 |
+
return []
|
| 17 |
+
out, buf = [], ""
|
| 18 |
+
for part in _SENT_SPLIT.split(text):
|
| 19 |
+
cand = (buf + " " + part).strip() if buf else part
|
| 20 |
+
low = buf.rstrip(".").split()[-1].lower() if buf else ""
|
| 21 |
+
if buf and low in _ABBR: # don't break after "np." etc.
|
| 22 |
+
buf = cand
|
| 23 |
+
continue
|
| 24 |
+
if buf:
|
| 25 |
+
out.append(buf)
|
| 26 |
+
buf = part
|
| 27 |
+
if buf:
|
| 28 |
+
out.append(buf)
|
| 29 |
+
return [s for s in (x.strip() for x in out) if s]
|
| 30 |
+
|
| 31 |
EVENT_TYPES = ("message", "image", "file", "system_idle")
|
| 32 |
BUBBLE_TYPES = ("response", "follow_up", "proactive", "reaction")
|
| 33 |
ALLOWED_REACTIONS = ("👍", "❤️", "😂", "😮", "😢", "😡")
|
|
|
|
| 71 |
if not isinstance(b, dict):
|
| 72 |
continue
|
| 73 |
btype = b.get("type") if b.get("type") in BUBBLE_TYPES else "response"
|
| 74 |
+
target = b.get("target_event_id") or b.get("target") or b.get("reply_to")
|
| 75 |
if target not in valid_event_ids:
|
| 76 |
target = None
|
| 77 |
+
# Models vary on the field name — accept bubble/content/text/message/emoji.
|
| 78 |
+
raw_text = (b.get("bubble") or b.get("content") or b.get("text")
|
| 79 |
+
or b.get("message") or b.get("emoji") or "")
|
| 80 |
# Reaction: keep the raw emoji (no messenger styling); must be allowed + targeted.
|
| 81 |
if btype == "reaction":
|
| 82 |
+
emoji = str(raw_text).strip()
|
| 83 |
if emoji in ALLOWED_REACTIONS and target:
|
| 84 |
out.append({"bubble": emoji, "target_event_id": target, "type": "reaction"})
|
| 85 |
continue
|
| 86 |
+
# split a multi-sentence reply into separate short bubbles (chat style)
|
| 87 |
+
for i, piece in enumerate(_split_sentences(str(raw_text)[:BUBBLE_CHARS])):
|
| 88 |
+
if len(out) >= BUBBLES_MAX:
|
| 89 |
+
break
|
| 90 |
+
t = _messenger_style(piece)
|
| 91 |
+
if not t:
|
| 92 |
+
continue
|
| 93 |
+
out.append({"bubble": t, "target_event_id": target,
|
| 94 |
+
"type": btype if i == 0 else "follow_up"})
|
| 95 |
return out
|
| 96 |
|
| 97 |
|