ai写作
This commit is contained in:
1 parent
cc8703577d
commit
10d7190ea4
12 files changed
+643
-43
No files matched your search
@@ -69,6 +69,9 @@ def _make_config(session_id: str, model_name: str = "") -> dict:
|
||||
"thread_id": session_id,
|
||||
"model_name": model_name or _session_model_names.get(session_id, ""),
|
||||
"ai_writing_config": {},
|
||||
# 把 SSE 队列注入,供草稿/大纲节点直接推流式 chunk
|
||||
"sse_queue": _event_queues.get(session_id),
|
||||
"sse_session_id": session_id,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,7 +243,8 @@ async def _stream_graph_segment(
|
||||
def _event_type(evt: dict) -> str:
|
||||
t = evt.get("type", "progress")
|
||||
# 过滤掉已在上方单独推送的事件类型,避免重复
|
||||
if t in ("materials_ready", "outline_ready", "draft_ready", "review_ready", "await_user"):
|
||||
if t in ("materials_ready", "outline_ready", "draft_ready", "review_ready", "await_user",
|
||||
"draft_chunk", "outline_chunk"):
|
||||
return "step_done"
|
||||
return t
|
||||
|
||||
|
||||
+67
@@ -134,6 +134,73 @@ def _content_to_text(content) -> str:
|
||||
return str(content)
|
||||
|
||||
|
||||
class ThinkTagParser:
|
||||
"""流式 <think>...</think> 标签解析器。
|
||||
|
||||
模型可能在任意 chunk 边界截断标签,通过尾部缓冲处理跨 chunk 边界情况。
|
||||
调用 feed(chunk) 返回 list[tuple[bool, str]],每项为 (is_thinking, text)。
|
||||
"""
|
||||
|
||||
_OPEN_TAG = "<think>"
|
||||
_CLOSE_TAG = "</think>"
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._in_think = False
|
||||
self._buf = "" # 潜在标签的部分匹配缓冲
|
||||
|
||||
def feed(self, chunk: str) -> list[tuple[bool, str]]:
|
||||
"""处理一个流式 chunk,返回 (is_thinking, text) 片段列表。"""
|
||||
self._buf += chunk
|
||||
result: list[tuple[bool, str]] = []
|
||||
|
||||
while self._buf:
|
||||
if self._in_think:
|
||||
tag = self._CLOSE_TAG
|
||||
idx = self._buf.find(tag)
|
||||
if idx != -1:
|
||||
if idx > 0:
|
||||
result.append((True, self._buf[:idx]))
|
||||
self._buf = self._buf[idx + len(tag):]
|
||||
self._in_think = False
|
||||
else:
|
||||
partial = self._partial_suffix(self._buf, tag)
|
||||
if partial:
|
||||
result.append((True, self._buf[:-partial]))
|
||||
self._buf = self._buf[-partial:]
|
||||
else:
|
||||
result.append((True, self._buf))
|
||||
self._buf = ""
|
||||
break
|
||||
else:
|
||||
tag = self._OPEN_TAG
|
||||
idx = self._buf.find(tag)
|
||||
if idx != -1:
|
||||
if idx > 0:
|
||||
result.append((False, self._buf[:idx]))
|
||||
self._buf = self._buf[idx + len(tag):]
|
||||
self._in_think = True
|
||||
else:
|
||||
partial = self._partial_suffix(self._buf, tag)
|
||||
if partial:
|
||||
result.append((False, self._buf[:-partial]))
|
||||
self._buf = self._buf[-partial:]
|
||||
else:
|
||||
result.append((False, self._buf))
|
||||
self._buf = ""
|
||||
break
|
||||
|
||||
return [(is_t, t) for (is_t, t) in result if t]
|
||||
|
||||
@staticmethod
|
||||
def _partial_suffix(text: str, tag: str) -> int:
|
||||
"""返回 text 尾部与 tag 前缀匹配的最长长度(>0 说明可能是部分标签)。"""
|
||||
max_check = min(len(tag) - 1, len(text))
|
||||
for length in range(max_check, 0, -1):
|
||||
if tag.startswith(text[-length:]):
|
||||
return length
|
||||
return 0
|
||||
|
||||
|
||||
async def llm_json(system: str, user: str, fallback: dict, config: RunnableConfig) -> dict:
|
||||
"""调用 LLM,解析 JSON 输出,失败用 fallback。"""
|
||||
model = get_model(config)
|
||||
|
||||
+43
-11
@@ -1,6 +1,7 @@
|
||||
"""作家节点 Phase 2:按大纲逐段写正文。"""
|
||||
"""作家节点 Phase 2:按大纲逐段写正文,支持流式 chunk 输出。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
@@ -8,7 +9,7 @@ from langchain_core.runnables import RunnableConfig
|
||||
|
||||
from ..state import AIWritingState, DraftArticle
|
||||
from ..prompts.writer_prompts import DRAFT_SECTION_SYSTEM, DRAFT_SECTION_USER
|
||||
from ._utils import get_model, _content_to_text
|
||||
from ._utils import get_model, _content_to_text, ThinkTagParser
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -21,7 +22,6 @@ def _get_section_materials(material_ids: list[str], pkg: dict) -> str:
|
||||
m = mat_map[mid]
|
||||
lines.append(f"[{mid}] {m['title']}: {m['content'][:300]}")
|
||||
if not lines:
|
||||
# 没有指定素材时,使用前两条
|
||||
for m in pkg.get("materials", [])[:2]:
|
||||
lines.append(f"[{m['id']}] {m['title']}: {m['content'][:300]}")
|
||||
return "\n".join(lines) if lines else "(无相关素材)"
|
||||
@@ -39,6 +39,11 @@ async def writer_draft_node(state: AIWritingState, config: RunnableConfig) -> di
|
||||
intervention = state.get("last_user_intervention") or {}
|
||||
revision_count = state.get("revision_count", 0)
|
||||
|
||||
# SSE 流式推送所需
|
||||
cfg = config.get("configurable", {})
|
||||
sse_queue: asyncio.Queue | None = cfg.get("sse_queue")
|
||||
session_id: str = cfg.get("sse_session_id", "")
|
||||
|
||||
# 修改意见(来自用户或编辑)
|
||||
revision_notes = ""
|
||||
if intervention.get("action") == "user_revise":
|
||||
@@ -82,23 +87,50 @@ async def writer_draft_node(state: AIWritingState, config: RunnableConfig) -> di
|
||||
target_words=words_per_section,
|
||||
)
|
||||
|
||||
section_buf = "" # 只累积正文(非思考)用于拼接 Markdown
|
||||
parser = ThinkTagParser()
|
||||
try:
|
||||
resp = await model.ainvoke([
|
||||
async for chunk in model.astream([
|
||||
SystemMessage(content=DRAFT_SECTION_SYSTEM),
|
||||
HumanMessage(content=user_msg),
|
||||
])
|
||||
# 部分模型(如 DeepSeek thinking 模式)返回列表结构的 content,统一转为字符串
|
||||
text = _content_to_text(resp.content).strip()
|
||||
section_texts.append(f"## {sec_title}\n\n{text}")
|
||||
]):
|
||||
delta = _content_to_text(chunk.content)
|
||||
if not delta:
|
||||
continue
|
||||
for is_thinking, text in parser.feed(delta):
|
||||
if not is_thinking:
|
||||
section_buf += text
|
||||
if sse_queue is not None:
|
||||
await sse_queue.put(("draft_chunk", {
|
||||
"session_id": session_id,
|
||||
"section_index": i,
|
||||
"section_title": sec_title,
|
||||
"text": text,
|
||||
"is_thinking": is_thinking,
|
||||
"is_section_done": False,
|
||||
}))
|
||||
|
||||
# 章节完成
|
||||
if sse_queue is not None:
|
||||
await sse_queue.put(("draft_chunk", {
|
||||
"session_id": session_id,
|
||||
"section_index": i,
|
||||
"section_title": sec_title,
|
||||
"text": "",
|
||||
"is_section_done": True,
|
||||
}))
|
||||
|
||||
section_texts.append(f"## {sec_title}\n\n{section_buf.strip()}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error("draft section [%s] failed: %s", sec_title, e, exc_info=True)
|
||||
section_texts.append(f"## {sec_title}\n\n(内容生成失败,请重试)")
|
||||
|
||||
full_markdown = f"# {outline['title']}\n\n" + "\n\n".join(section_texts)
|
||||
full_markdown = f"# {outline.get('title', '文章')}\n\n" + "\n\n".join(section_texts)
|
||||
word_count = len([c for c in full_markdown if '一' <= c <= '鿿'])
|
||||
|
||||
draft = DraftArticle(
|
||||
title=outline["title"],
|
||||
title=outline.get("title", "文章"),
|
||||
outline=outline,
|
||||
full_markdown=full_markdown,
|
||||
word_count=word_count,
|
||||
@@ -115,7 +147,7 @@ async def writer_draft_node(state: AIWritingState, config: RunnableConfig) -> di
|
||||
"type": "draft_ready",
|
||||
"agent_name": "作家",
|
||||
"message": f"草稿完成,共 {word_count} 字",
|
||||
"draft_title": outline["title"],
|
||||
"draft_title": outline.get("title", "文章"),
|
||||
"draft_markdown": full_markdown,
|
||||
"revision_count": revision_count,
|
||||
},
|
||||
|
||||
+53
-23
@@ -1,12 +1,17 @@
|
||||
"""作家节点 Phase 1:生成文章大纲。"""
|
||||
"""作家节点 Phase 1:生成文章大纲,支持流式 chunk 输出。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
|
||||
from ..state import AIWritingState, Outline, OutlineSection
|
||||
from ..prompts.writer_prompts import OUTLINE_SYSTEM, OUTLINE_USER
|
||||
from ._utils import llm_json
|
||||
from ._utils import get_model, parse_json, _content_to_text, ThinkTagParser
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 上下文压缩:传给大纲规划的素材摘要做条数和单条长度双重截断,防止 token 溢出。
|
||||
_MAX_OUTLINE_MATERIALS = 12
|
||||
@@ -43,33 +48,58 @@ async def writer_outline_node(state: AIWritingState, config: RunnableConfig) ->
|
||||
elif intervention.get("outline_feedback"):
|
||||
feedback_section = f"\n重新规划要求:{intervention['outline_feedback']}"
|
||||
|
||||
result = await llm_json(
|
||||
OUTLINE_SYSTEM,
|
||||
OUTLINE_USER.format(
|
||||
user_intent=state["user_intent"],
|
||||
article_type=state.get("article_type", "analysis"),
|
||||
target_audience=state.get("target_audience", "普通读者"),
|
||||
word_count_target=state.get("word_count_target", 800),
|
||||
materials_summary=_materials_summary(pkg),
|
||||
feedback_section=feedback_section,
|
||||
),
|
||||
fallback={
|
||||
"title": "未命名文章",
|
||||
"sections": [
|
||||
{"section_title": "引言", "key_points": ["背景介绍"], "material_ids": []},
|
||||
{"section_title": "正文", "key_points": ["核心内容"], "material_ids": []},
|
||||
{"section_title": "结论", "key_points": ["总结"], "material_ids": []},
|
||||
],
|
||||
},
|
||||
config=config,
|
||||
user_prompt = OUTLINE_USER.format(
|
||||
user_intent=state["user_intent"],
|
||||
article_type=state.get("article_type", "analysis"),
|
||||
target_audience=state.get("target_audience", "普通读者"),
|
||||
word_count_target=state.get("word_count_target", 800),
|
||||
materials_summary=_materials_summary(pkg),
|
||||
feedback_section=feedback_section,
|
||||
)
|
||||
|
||||
if llm_err := result.pop("_llm_error", None):
|
||||
# SSE 流式推送
|
||||
cfg = config.get("configurable", {})
|
||||
sse_queue: asyncio.Queue | None = cfg.get("sse_queue")
|
||||
session_id: str = cfg.get("sse_session_id", "")
|
||||
|
||||
model = get_model(config)
|
||||
full_text = "" # 只累积正文(非思考)用于 JSON 解析
|
||||
parser = ThinkTagParser()
|
||||
|
||||
try:
|
||||
async for chunk in model.astream([
|
||||
SystemMessage(content=OUTLINE_SYSTEM),
|
||||
HumanMessage(content=user_prompt),
|
||||
]):
|
||||
delta = _content_to_text(chunk.content)
|
||||
if not delta:
|
||||
continue
|
||||
for is_thinking, text in parser.feed(delta):
|
||||
if not is_thinking:
|
||||
full_text += text
|
||||
if sse_queue is not None:
|
||||
await sse_queue.put(("outline_chunk", {
|
||||
"session_id": session_id,
|
||||
"text": text,
|
||||
"is_thinking": is_thinking,
|
||||
}))
|
||||
except Exception as e:
|
||||
logger.error("outline streaming failed: %s", e, exc_info=True)
|
||||
return {
|
||||
"status": "error",
|
||||
"progress_events": [{"type": "error", "agent_name": "作家", "message": f"大纲生成失败:{llm_err}"}],
|
||||
"progress_events": [{"type": "error", "agent_name": "作家", "message": f"大纲生成失败:{e}"}],
|
||||
}
|
||||
|
||||
fallback = {
|
||||
"title": "未命名文章",
|
||||
"sections": [
|
||||
{"section_title": "引言", "key_points": ["背景介绍"], "material_ids": []},
|
||||
{"section_title": "正文", "key_points": ["核心内容"], "material_ids": []},
|
||||
{"section_title": "结论", "key_points": ["总结"], "material_ids": []},
|
||||
],
|
||||
}
|
||||
result = parse_json(full_text, fallback)
|
||||
|
||||
outline = _parse_outline(result)
|
||||
section_count = len(outline["sections"])
|
||||
|
||||
|
||||
Reference in new issue
Block a user