效果优化

This commit is contained in:
董宏凯 committed 2026-05-19 18:10:15 +08:00
1 parent 2402a84c71
commit ab85e4df72
15 files changed
+548 -35

No files matched your search

@@ -3,6 +3,7 @@ from __future__ import annotations
import asyncio
import logging
import re
import uuid
from typing import List
@@ -46,6 +47,25 @@ def _resolve_max_materials(raw: object) -> int:
return max(_MIN_MAX_MATERIALS, min(_MAX_MAX_MATERIALS, n))
# 占位/示意性关键词(模型误抄提示词模板时产生),检索前剔除
_PLACEHOLDER_KEYWORD_RE = re.compile(r"^(关键词\d*|keyword\d*|kw\d*|\.+|…+)$", re.IGNORECASE)
def _clean_keywords(raw: object, fallback: str) -> list[str]:
"""清洗 LLM 产出的关键词:去空白、丢弃占位符(如「关键词1」「...」)、去重。
全部被滤掉时回退为写作意图本身,避免拿占位词检索出一堆无关素材。
"""
cleaned: list[str] = []
for kw in raw if isinstance(raw, list) else []:
s = str(kw).strip()
if not s or _PLACEHOLDER_KEYWORD_RE.match(s):
continue
cleaned.append(s)
deduped = list(dict.fromkeys(cleaned))
return deduped or [fallback]
def _per_keyword_limit(max_materials: int, total_keywords: int) -> int:
"""把素材总量上限均分到每个检索词上的检索条数(向上取整)。
@@ -77,6 +97,9 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
intervention = state.get("last_user_intervention") or {}
extra_keywords: list[str] = intervention.get("_extra_keywords") or intervention.get("extra_keywords") or []
# 素材不足求助点触发的「补充检索」:保留原大纲,检索完直接回写作、不走素材确认/大纲
is_supplement = intervention.get("action") == "supplement_search"
# 用户指定的检索关键词数量(1~10),决定生成多少个检索词、检索多少个方向
keyword_count = _resolve_keyword_count(state.get("keyword_count"))
@@ -115,7 +138,8 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
config=config,
model_name=keyword_model,
)
keywords: list[str] = kw_result.get("keywords", [intent])
# 清洗关键词:剔除占位符 / 空值,全空时回退写作意图
keywords: list[str] = _clean_keywords(kw_result.get("keywords"), intent)
if extra_keywords:
keywords = list(dict.fromkeys(keywords + extra_keywords))
@@ -237,7 +261,8 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
return {
"material_package": pkg,
"status": "awaiting_material_confirm",
# 补充检索:直接回写作(沿用原大纲);常规检索:进入素材确认暂停点
"status": "writing_draft" if is_supplement else "awaiting_material_confirm",
"progress_events": [
{"type": "step_start", "agent_name": "素材收集专家", "message": f"搜索关键词:{', '.join(keywords[:3])}"},
*notices,
@@ -3,16 +3,53 @@ from __future__ import annotations
import asyncio
import logging
import re
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.runnables import RunnableConfig
from ..state import AIWritingState, DraftArticle
from ..prompts.writer_prompts import DRAFT_SECTION_SYSTEM, DRAFT_SECTION_USER, DRAFT_EXPAND_USER
from ..prompts.writer_prompts import (
DRAFT_SECTION_SYSTEM, DRAFT_SECTION_USER, DRAFT_EXPAND_USER,
WRITER_MODE_LOOSE, WRITER_MODE_STRICT, SECTION_BLOCKED_MARKER,
)
from ._utils import get_model, _content_to_text, count_words, ThinkTagParser
logger = logging.getLogger(__name__)
# 章节正文里的 Markdown 标题行(允许前导空白)
_HEADING_PREFIX_RE = re.compile(r"^[ \t]*#{1,6}[ \t]+", re.MULTILINE)
def _normalize_section_headings(text: str) -> str:
"""把章节正文里所有 Markdown 标题行统一压成三级标题(###)。
作家被要求只用 ### 作为「要点」小标题;模型偶尔误用 #/##/#### 时在此纠正,
保证文档标题层级符合「文章 # → 章节 ## → 要点 ###」,
前端目录栏据此才能把要点正确识别为二级标题。
"""
return _HEADING_PREFIX_RE.sub("### ", text)
# 疑似「拒绝 / 元话语」的强特征短语:模型把对用户说的话当成正文输出时出现,
# 这些短语几乎不可能出现在正常文章里,用作安全网识别。
_REFUSAL_MARKERS = (
"我无法完成", "无法完成此任务", "无法完成该任务", "无法完成这个任务",
"无法完成本次", "无法写出符合", "无法撰写这篇", "无法基于现有素材",
"无法基于当前素材", "无法基于提供的素材",
)
def _looks_like_refusal(text: str) -> bool:
"""判断一段章节产出是否疑似「拒绝 / 元话语」而非真正的文章正文。
模型遇到素材与意图不匹配时,可能把「我无法完成此任务」之类对用户说的话
当成正文输出。这里用强特征短语做启发式识别(安全网,非强校验)。
"""
if not text or not text.strip():
return False
return any(marker in text for marker in _REFUSAL_MARKERS)
def _get_section_materials(material_ids: list[str], pkg: dict) -> str:
mat_map = {m["id"]: m for m in pkg.get("materials", [])}
@@ -39,6 +76,10 @@ async def writer_draft_node(state: AIWritingState, config: RunnableConfig) -> di
intervention = state.get("last_user_intervention") or {}
revision_count = state.get("revision_count", 0)
# 写作模式;用户在「素材不足求助」里选「按通用知识续写」的章节强制走宽松
writing_mode = state.get("writing_mode", "loose")
loose_titles: set[str] = set(intervention.get("_loose_sections") or [])
# SSE 流式推送所需
cfg = config.get("configurable", {})
sse_queue: asyncio.Queue | None = cfg.get("sse_queue")
@@ -89,6 +130,7 @@ async def writer_draft_node(state: AIWritingState, config: RunnableConfig) -> di
section_titles: list[str] = []
section_bufs: list[str] = [] # 各章节正文(不含 ## 标题),供扩写兜底逐节替换
section_errors: list[str] = [] # 分节写作的模型异常,供完成后非致命提示
blocked_sections: list[dict] = [] # 严格模式下素材不足、无法写作的章节
for i, section in enumerate(sections):
sec_title = _section_title(i, section)
@@ -104,6 +146,10 @@ async def writer_draft_node(state: AIWritingState, config: RunnableConfig) -> di
for j, s in enumerate(sections)
)
# 本章节是否走严格模式(全局严格 且 未被用户标记为「通用知识续写」)
section_is_strict = writing_mode == "strict" and sec_title not in loose_titles
mode_section = WRITER_MODE_STRICT if section_is_strict else WRITER_MODE_LOOSE
user_msg = DRAFT_SECTION_USER.format(
article_title=article_title,
outline_context=outline_context,
@@ -111,6 +157,7 @@ async def writer_draft_node(state: AIWritingState, config: RunnableConfig) -> di
key_points=key_points_str,
section_materials=section_materials,
revision_section=revision_notes,
mode_section=mode_section,
target_words=words_per_section,
)
@@ -147,8 +194,27 @@ async def writer_draft_node(state: AIWritingState, config: RunnableConfig) -> di
"is_section_done": True,
}))
cleaned_buf = section_buf.strip()
blocked_reason = ""
# 严格模式:作家用哨兵声明素材不足,或仍输出了拒绝话术 → 标记为待求助章节
if section_is_strict and SECTION_BLOCKED_MARKER in cleaned_buf:
blocked_reason = (
cleaned_buf.split(SECTION_BLOCKED_MARKER, 1)[1].strip()[:120]
or "素材不足以支撑本章节写作"
)
elif _looks_like_refusal(cleaned_buf):
if section_is_strict:
blocked_reason = "素材不足以支撑本章节写作"
else:
# 宽松模式:模型仍拒绝,安全网占位,不打断流程
logger.warning("draft section [%s] looks like a refusal, replaced with placeholder", sec_title)
section_errors.append(f"「{sec_title}」生成内容疑似拒绝/无关,已占位")
cleaned_buf = "(本节内容生成异常,建议重新生成或补充相关素材)"
if blocked_reason:
blocked_sections.append({"section_title": sec_title, "reason": blocked_reason})
cleaned_buf = f"(本节素材不足:{blocked_reason})"
section_titles.append(sec_title)
section_bufs.append(section_buf.strip())
section_bufs.append(cleaned_buf)
except Exception as e:
logger.error("draft section [%s] failed: %s", sec_title, e, exc_info=True)
@@ -156,8 +222,26 @@ async def writer_draft_node(state: AIWritingState, config: RunnableConfig) -> di
section_bufs.append("(内容生成失败,请重试)")
section_errors.append(f"「{sec_title}」{type(e).__name__}: {e}")
# 严格模式下有章节无法基于素材写作 → 暂停求助用户,不产出半成品草稿
if blocked_sections:
return {
"blocked_sections": blocked_sections,
"status": "awaiting_section_help",
"progress_events": [
{"type": "step_start", "agent_name": "作家", "message": f"开始写作(第{revision_count+1}稿)"},
{
"type": "notice", "agent_name": "作家", "title": "素材不足",
"detail": f"有 {len(blocked_sections)} 个章节缺少足够素材支撑写作,需要你决定如何处理。",
},
],
}
def _assemble() -> str:
body = "\n\n".join(f"## {t}\n\n{b}" for t, b in zip(section_titles, section_bufs))
# 章节正文里的标题统一归一到 ###(要点小标题),章节标题用 ##,文章标题用 #
body = "\n\n".join(
f"## {t}\n\n{_normalize_section_headings(b)}"
for t, b in zip(section_titles, section_bufs)
)
return f"# {article_title}\n\n{body}"
full_markdown = _assemble()