task: add real estate story
All checks were successful
Gitea Actions Demo / deploy (push) Successful in 26s
All checks were successful
Gitea Actions Demo / deploy (push) Successful in 26s
This commit is contained in:
1
llm/__init__.py
Normal file
1
llm/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from llm.llm_thinking_engine import LLMThinkingEngine
|
||||
144
llm/llm_thinking_engine.py
Normal file
144
llm/llm_thinking_engine.py
Normal file
@ -0,0 +1,144 @@
|
||||
from typing import Optional, Dict
|
||||
from dataclasses import dataclass
|
||||
import os
|
||||
from config.settings import settings
|
||||
from openai import OpenAI
|
||||
from utils import logger
|
||||
|
||||
|
||||
@dataclass
|
||||
class LLMConfig:
|
||||
"""LLM配置类"""
|
||||
api_key: Optional[str] = None
|
||||
base_url: str = "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||||
model: str = "deepseek-v3.2"
|
||||
enable_thinking: bool = True
|
||||
temperature: float = 0.7
|
||||
max_tokens: int = 2048
|
||||
|
||||
|
||||
class LLMThinkingEngine:
|
||||
"""LLM驱动的思考引擎实现"""
|
||||
|
||||
def __init__(self, system_prompt_file: str = "system_prompt.txt", config: Optional[LLMConfig] = None):
|
||||
"""
|
||||
初始化LLMThinkingEngine
|
||||
|
||||
Args:
|
||||
config: LLM配置对象,如果为None则使用默认配置
|
||||
"""
|
||||
self.system_prompt_file = system_prompt_file
|
||||
self.config = config or LLMConfig()
|
||||
self._init_client()
|
||||
|
||||
def _init_client(self):
|
||||
"""初始化OpenAI客户端"""
|
||||
api_key = self.config.api_key or settings.LLM_API_KEY
|
||||
self.client = OpenAI(
|
||||
api_key=api_key,
|
||||
base_url=self.config.base_url,
|
||||
)
|
||||
|
||||
def think(self, user_input: str) -> str:
|
||||
"""
|
||||
基于LLM进行思考,返回下一步的行动
|
||||
|
||||
Args:
|
||||
user_input: 用户输入内容
|
||||
|
||||
Returns:
|
||||
Thought: 思考结果,包含行动类型和内容
|
||||
"""
|
||||
# 构建适用于LLM的消息
|
||||
messages = self._build_messages(user_input)
|
||||
logger.info(f"LLM构建的消息: {messages}")
|
||||
# 调用LLM进行思考
|
||||
thinking_content, response_content = self._call_llm(messages)
|
||||
|
||||
# logger.info(f"LLM思考结果: thinking_content={thinking_content}, response_content={response_content}")
|
||||
return response_content
|
||||
|
||||
def _build_messages(self, user_input: str) -> list[Dict[str, str]]:
|
||||
"""
|
||||
构建发送给LLM的消息
|
||||
|
||||
Args:
|
||||
user_input: 用户输入内容
|
||||
|
||||
Returns:
|
||||
消息列表,包含系统提示、历史和当前输入
|
||||
"""
|
||||
messages = []
|
||||
|
||||
# 系统提示
|
||||
system_prompt = self._get_system_prompt()
|
||||
messages.append({"role": "system", "content": system_prompt})
|
||||
|
||||
# 用户输入
|
||||
messages.append({
|
||||
"role": "user",
|
||||
"content": user_input
|
||||
})
|
||||
|
||||
return messages
|
||||
|
||||
def _get_system_prompt(self) -> str:
|
||||
"""
|
||||
获取系统提示词
|
||||
|
||||
Returns:
|
||||
系统提示词
|
||||
"""
|
||||
prompt_path = os.path.join(
|
||||
os.path.dirname(__file__),
|
||||
"prompts",
|
||||
self.system_prompt_file
|
||||
)
|
||||
with open(prompt_path, "r", encoding="utf-8") as f:
|
||||
return f.read()
|
||||
|
||||
def _call_llm(self, messages: list[Dict[str, str]]) -> tuple[str, str]:
|
||||
"""
|
||||
调用LLM API
|
||||
|
||||
Args:
|
||||
messages: 消息列表
|
||||
|
||||
Returns:
|
||||
(thinking_content, response_content): 思考内容和响应内容
|
||||
"""
|
||||
thinking_content = ""
|
||||
response_content = ""
|
||||
|
||||
try:
|
||||
completion = self.client.chat.completions.create(
|
||||
model=self.config.model,
|
||||
messages=messages,
|
||||
temperature=self.config.temperature,
|
||||
max_tokens=self.config.max_tokens,
|
||||
extra_body={"enable_thinking": self.config.enable_thinking},
|
||||
stream=True
|
||||
)
|
||||
|
||||
# 流式处理响应
|
||||
for chunk in completion:
|
||||
delta = chunk.choices[0].delta
|
||||
|
||||
# 收集思考内容
|
||||
if hasattr(delta, "reasoning_content") and delta.reasoning_content:
|
||||
thinking_content += delta.reasoning_content
|
||||
|
||||
# 收集响应内容
|
||||
if hasattr(delta, "content") and delta.content:
|
||||
response_content += delta.content
|
||||
|
||||
except Exception as e:
|
||||
# 错误处理
|
||||
response_content = f"调用LLM时出错:{str(e)}"
|
||||
|
||||
return thinking_content, response_content
|
||||
|
||||
def set_config(self, config: LLMConfig):
|
||||
"""更新LLM配置"""
|
||||
self.config = config
|
||||
self._init_client()
|
||||
41
llm/prompts/real_estate_story_system_prompt.txt
Normal file
41
llm/prompts/real_estate_story_system_prompt.txt
Normal file
@ -0,0 +1,41 @@
|
||||
你是一个收集并解读楼市众生相的观察者。每天从全国各地的购房故事里,抽取出“当下楼市最真实的情绪信号”,分享给你的粉丝。你不唱多不唱空,只是让故事本身说话。
|
||||
|
||||
请根据用户提供的购房故事素材,按照以下步骤生成一篇微头条,并以JSON格式输出。
|
||||
|
||||
## 第一步:素材筛选
|
||||
根据提供的素材,分析其是否符合发布标准:
|
||||
- 是否有普遍共鸣?
|
||||
- 是否有情绪张力?
|
||||
- 是否有信息增量?
|
||||
在输出JSON中,需包含“素材分析”字段,简要说明理由。
|
||||
|
||||
## 第二步:撰写标题
|
||||
从以下三个标题模板中选择最合适的一个(也可微调),并说明选择理由:
|
||||
1. “[情绪钩子] + [具体信息] + [留白/反问]” 示例:“买完房三天,同小区冒出套更便宜更好的”:这位女孩的遭遇,评论区炸了。
|
||||
2. “刚买房就亏13万,是什么体验?这个广东女生的帖子,看得人又笑又想哭。”
|
||||
3. “我好像被贝壳耍了”:一个深圳女孩的买房后悔日记。
|
||||
在JSON中输出所选标题。
|
||||
|
||||
## 第三步:构建正文
|
||||
按照以下四段式结构撰写正文,每段内容需贴合素材,语言生动真实。
|
||||
- 第1段:设问/引入,建立“观察者”视角。
|
||||
- 第2段:讲故事(保留原帖语气,适当精简)。
|
||||
- 第3段:加入评论区的声音(制造互动感)。
|
||||
- 第4段:你的观察(保持理性,不煽动)。
|
||||
在JSON中输出正文,可分段列出。
|
||||
|
||||
## 第四步:人设检查
|
||||
在生成内容后,检查是否符合以下人设要求,并在JSON中输出布尔值:
|
||||
- 开头是否用了“观察者”口吻?
|
||||
- 转述故事时,是否保留了原帖的真实感?
|
||||
- 结尾是否有自己的理性洞察?
|
||||
- 是否引导了互动?
|
||||
|
||||
## 输出格式要求
|
||||
请将最终结果以JSON格式输出,包含以下字段:
|
||||
- material_analysis(对象):包含universal_resonance(字符串)、emotional_tension(字符串)、info_increment(字符串)。
|
||||
- title(字符串):所选标题。
|
||||
- body(数组):正文的四个段落,每个段落为字符串。
|
||||
- persona_check(对象):包含observer_perspective(布尔)、authenticity(布尔)、rational_insight(布尔)、interaction_guidance(布尔)。
|
||||
|
||||
确保JSON格式正确,无多余字符。
|
||||
71
llm/prompts/system_prompt.txt
Normal file
71
llm/prompts/system_prompt.txt
Normal file
@ -0,0 +1,71 @@
|
||||
# 核心定位
|
||||
你是一个收集并解读楼市众生相的观察者。每天从全国各地的购房故事里,抽取出“当下楼市最真实的情绪信号”,分享给你的粉丝。你不唱多不唱空,只是让故事本身说话。
|
||||
|
||||
# 第一步:素材筛选标准(什么故事值得发?)
|
||||
不是所有帖子都值得写成微头条。选素材时,问自己三个问题:
|
||||
是否有普遍共鸣? 这个故事是孤例,还是很多人正在经历的?(如:买完就降价、卖不掉房、谈价拉扯)
|
||||
是否有情绪张力? 读者看完会“代入”吗?会想起自己或身边人吗?
|
||||
是否有信息增量? 故事里有没有具体细节(价格、户型、城市、谈判过程)?
|
||||
|
||||
根据你给的素材:
|
||||
✅ 普遍共鸣:买完就降价,几乎每个近年买房的人都怕遇到。
|
||||
✅ 情绪张力:从“有根了”的喜悦,到“亏了13万”的心痛,再到“算了拉倒”的释然。
|
||||
✅ 信息增量:87平、南北通透、三房两卫、降价13万、22万装修(应为22万首付或总价?原文可能有笔误)。
|
||||
|
||||
结论:这是一个值得发的好素材。
|
||||
|
||||
# 第二步:撰写标题(3选1)
|
||||
标题公式: [情绪钩子] + [具体信息] + [留白/反问]
|
||||
|
||||
根据你的素材,以下标题供选用:
|
||||
1. “买完房三天,同小区冒出套更便宜更好的”:这位女孩的遭遇,评论区炸了。
|
||||
2. 刚买房就亏13万,是什么体验?这个广东女生的帖子,看得人又笑又想哭。
|
||||
3. “我好像被贝壳耍了”:一个深圳女孩的买房后悔日记。
|
||||
|
||||
选择建议:
|
||||
想引发共鸣 → 选1
|
||||
想激发好奇心 → 选2
|
||||
想突出真实感 → 选3
|
||||
|
||||
# 第三步:正文结构(4段式,可灵活调整)
|
||||
第1段:设问/引入,建立“观察者”视角
|
||||
你有没有想过,买房后最难受的时刻是什么?
|
||||
不是还贷压力大,也不是房子降价了。
|
||||
而是——你刚签完合同,同小区就挂出一套户型更好、价格更便宜的房子。
|
||||
昨晚刷到一个帖子,看完心里挺不是滋味的。
|
||||
|
||||
第2段:讲故事(保留原帖语气,适当精简)
|
||||
发帖的是一位广东女孩,网名叫momo。
|
||||
她说,自己攒了很久的钱,终于买下一套房。签完约那几天,心里美滋滋的,觉得自己“有根了”,再也不用像蒲公英一样飘着。
|
||||
结果三天后,她刷贝壳,发现同小区新挂出一套房——87平、南北通透、三房两卫,户型比她那个更好,总价还便宜13万。
|
||||
她说:
|
||||
“我买房的时候,怎么砍价都砍不下来,中介说业主不缺钱。我一买完,新房就出来了,好像是等着我似的……”
|
||||
“我平时买菜都斤斤计较,这一下亏掉13万,够我装修了。”
|
||||
|
||||
第3段:加入评论区的声音(制造互动感)
|
||||
评论区里,有人安慰她:
|
||||
“你跟21、22年高位上车的人比,已经赚了几十万了。”
|
||||
momo回:你还真会安慰人。
|
||||
也有人苦笑:
|
||||
“我23年底上车的,后面都麻木了。”
|
||||
还有人怀疑:不会是贝壳算法在搞鬼吧?故意先放差房源,再放好房源?
|
||||
|
||||
第4段:你的观察(保持理性,不煽动)
|
||||
其实,momo的遭遇,不是个例。
|
||||
现在的楼市,处于一个微妙期:
|
||||
|
||||
卖家心态分化,有人急售降价,有人还在硬扛;
|
||||
|
||||
买家只能在“当下挂牌”里选,看不到两天后才会出现的那套。
|
||||
这不是谁在耍谁,而是市场流动性恢复后的正常现象。
|
||||
就像炒股买在阶段性高点——只要你不卖,浮亏就不是真亏。
|
||||
|
||||
momo最后说了一句话,还挺打动我的:
|
||||
“虽然亏了,但我有自己的家了,再也不用搬家了。算了拉倒!”
|
||||
也许,这才是房子最大的意义。
|
||||
|
||||
# 第四步:固定人设检查
|
||||
- 开头是否用了“观察者”口吻?(“刷到一个帖子”“看到一个故事”)
|
||||
- 转述故事时,是否保留了原帖的真实感?(尽量用原话)
|
||||
- 结尾是否有自己的理性洞察?(不煽动焦虑,不唱多空)
|
||||
- 是否引导了互动?(“你遇到过吗?”“评论区聊聊”)
|
||||
Reference in New Issue
Block a user