import re from explore.llm import LLMBadResponse, chat_text PERSONA_NAME = "小拾" MAX_AI_COMMENTS = 6 SYSTEM_PROMPT = """你是「小拾」,一个温暖、敏锐、偶尔有点俏皮的朋友。你的朋友在记录此刻的心情。 请针对这条说说,写一句像朋友一样的评论,20-60 个汉字: - 先接住对方的情绪,再给一点自己的感受,或者轻轻开个玩笑 - 像微信里说话,口语化 - 不要总结、不要说教、不要鸡汤套话 - 不要自称 AI,不要写“我理解你的感受”这种空话 - 不要编造说说里没有的具体事实,不要替对方补充细节 - 不要加引号、不要换行、不要用括号写动作 如果下面有你说过的话,请换一个角度,不要重复。""" def _clean_comment(text): text = (text or "").strip() if not text: return "" lines = [line.strip() for line in text.splitlines() if line.strip()] text = lines[0] if lines else "" text = re.sub(r"^(?:[-*•]|\d+[.、])\s*", "", text) text = re.sub(r"^(小拾|AI)\s*[::]\s*", "", text) text = text.strip().strip("\"'“”‘’") text = re.sub(r"[*_`#]+", "", text) text = re.sub(r"\s+", " ", text) return text[:160].strip() def generate_comment(moment, existing=None): """为一条说说生成一句拟人化评论。""" lines = [ "心情:{}".format(moment.mood_label), "说说:{}".format(moment.content), ] existing = list(existing or []) if existing: lines.append("你之前说过:") for comment in existing: lines.append("- " + comment.content) lines.append("请换一个角度,不要重复。") raw = chat_text( SYSTEM_PROMPT, "\n".join(lines), temperature=0.85, num_predict=160, timeout=30, ) comment = _clean_comment(raw) if not comment: raise LLMBadResponse("小拾没想好说什么,等会儿再试试") return comment