feat: improve ai writing imitation flow
This commit is contained in:
1 parent
0547fb1715
commit
025e43f4c5
14 files changed
+927
-117
No files matched your search
+82
-11
@@ -5,10 +5,19 @@ import json
|
||||
import logging
|
||||
import re
|
||||
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
|
||||
from ..state import AIWritingState, SampleStyleProfile
|
||||
from ._utils import count_words, llm_json
|
||||
from ._utils import (
|
||||
ThinkTagParser,
|
||||
_content_to_text,
|
||||
count_words,
|
||||
get_model,
|
||||
parse_json,
|
||||
reasoning_delta,
|
||||
safe_get_stream_writer,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -146,6 +155,72 @@ def render_sample_style_section(state: AIWritingState) -> str:
|
||||
)
|
||||
|
||||
|
||||
async def _stream_sample_analysis(
|
||||
*,
|
||||
filename: str,
|
||||
sample_text: str,
|
||||
clipped: str,
|
||||
fallback: dict,
|
||||
config: RunnableConfig,
|
||||
model_name: str | None,
|
||||
) -> dict:
|
||||
writer = safe_get_stream_writer()
|
||||
writer({
|
||||
"type": "sample_chunk",
|
||||
"agent_name": "样文分析师",
|
||||
"text": (
|
||||
f"读取样文《{filename}》,约 {count_words(sample_text)} 字;"
|
||||
f"本轮提取前 {len(clipped)} 字用于文风画像。\n"
|
||||
"正在识别文章类型、结构骨架、语气、句式节奏、仿写建议和避抄边界...\n\n"
|
||||
),
|
||||
"is_thinking": False,
|
||||
})
|
||||
|
||||
messages = [
|
||||
SystemMessage(content=SAMPLE_ANALYZE_SYSTEM),
|
||||
HumanMessage(content=SAMPLE_ANALYZE_USER.format(
|
||||
filename=filename,
|
||||
word_count=count_words(sample_text),
|
||||
sample_text=clipped,
|
||||
)),
|
||||
]
|
||||
full_text = ""
|
||||
try:
|
||||
model = get_model(config, model_name=model_name, disable_thinking=True)
|
||||
parser = ThinkTagParser()
|
||||
async for chunk in model.astream(messages):
|
||||
reasoning = reasoning_delta(chunk)
|
||||
if reasoning:
|
||||
writer({
|
||||
"type": "sample_chunk",
|
||||
"agent_name": "样文分析师",
|
||||
"text": reasoning,
|
||||
"is_thinking": True,
|
||||
})
|
||||
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
|
||||
writer({
|
||||
"type": "sample_chunk",
|
||||
"agent_name": "样文分析师",
|
||||
"text": text,
|
||||
"is_thinking": is_thinking,
|
||||
})
|
||||
return parse_json(full_text, fallback) if full_text.strip() else fallback
|
||||
except Exception as exc:
|
||||
logger.error("sample_analyzer streaming failed: %s", exc, exc_info=True)
|
||||
writer({
|
||||
"type": "sample_chunk",
|
||||
"agent_name": "样文分析师",
|
||||
"text": "\n样文分析模型异常,已改用基础画像兜底。\n",
|
||||
"is_thinking": False,
|
||||
})
|
||||
return {**fallback, "_llm_error": str(exc)}
|
||||
|
||||
|
||||
async def sample_analyzer_node(state: AIWritingState, config: RunnableConfig) -> dict:
|
||||
if not _is_imitate_mode(state):
|
||||
return {}
|
||||
@@ -168,17 +243,13 @@ async def sample_analyzer_node(state: AIWritingState, config: RunnableConfig) ->
|
||||
fallback = _fallback_profile(clipped)
|
||||
configurable = (config or {}).get("configurable", {}) or {}
|
||||
model_name = (state.get("model_name") or configurable.get("model_name") or "").strip() or None
|
||||
raw = await llm_json(
|
||||
SAMPLE_ANALYZE_SYSTEM,
|
||||
SAMPLE_ANALYZE_USER.format(
|
||||
filename=filename,
|
||||
word_count=count_words(sample_text),
|
||||
sample_text=clipped,
|
||||
),
|
||||
fallback,
|
||||
config,
|
||||
raw = await _stream_sample_analysis(
|
||||
filename=filename,
|
||||
sample_text=sample_text,
|
||||
clipped=clipped,
|
||||
fallback=fallback,
|
||||
config=config,
|
||||
model_name=model_name,
|
||||
disable_thinking=True,
|
||||
)
|
||||
profile = _normalize_profile(raw, clipped)
|
||||
events = [
|
||||
|
||||
Reference in new issue
Block a user