townlet / game /sentiment.py
Budlee's picture
Townlet: initial private upload with Zero-GPU fixes
37b5712 verified
Raw
History Blame Contribute Delete
1.11 kB
"""Cheap sentiment classifier for the message board's UI tint.
One LLM call (default model) every time the board changes — classifies the
recent posts as positive | neutral | negative. The board's background colour
maps to that label client-side. Characters do not see this signal — it's a
user-facing affordance only.
"""
from __future__ import annotations
from backend.factory import get_llm
from game.models import DEFAULT_MODEL_ID
from .world import WorldState
LABELS = ("positive", "neutral", "negative")
def classify_board(world: WorldState) -> str:
if not world.board:
return "neutral"
tail = "\n".join(f"{p.author}: {p.text}" for p in world.board[-10:])
backend = get_llm(DEFAULT_MODEL_ID)
raw = backend.generate(
system_prompt=(
"Classify the overall mood of the following message-board posts. "
"Reply with exactly one word: positive, neutral, or negative."
),
user_prompt=tail,
)
word = (raw or "").strip().lower().split()[0:1]
if word and word[0] in LABELS:
return word[0]
return "neutral"