提交代码
This commit is contained in:
1 parent
d21dbeb214
commit
54190d7a83
8 files changed
+1205
-11
No files matched your search
+65
-7
@@ -25,15 +25,18 @@ import re
|
||||
import time
|
||||
from types import SimpleNamespace
|
||||
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
|
||||
from ..search import get_search_provider, SearchResult
|
||||
from ..prompts.researcher_prompts import (
|
||||
EXTRACT_KEYWORDS_SYSTEM,
|
||||
EXTRACT_KEYWORDS_USER,
|
||||
RANK_MATERIALS_SYSTEM,
|
||||
RANK_MATERIALS_USER,
|
||||
)
|
||||
from ..search import SearchResult, get_search_provider
|
||||
from ..search.skill_search import SkillPicker
|
||||
from ..state import AIWritingState, Material, MaterialPackage
|
||||
from ..prompts.researcher_prompts import (
|
||||
EXTRACT_KEYWORDS_SYSTEM, EXTRACT_KEYWORDS_USER,
|
||||
RANK_MATERIALS_SYSTEM, RANK_MATERIALS_USER,
|
||||
)
|
||||
from ._agent_runner import (
|
||||
KEYWORDS_READY_MARKER,
|
||||
MATERIALS_RANKED_MARKER,
|
||||
@@ -45,7 +48,16 @@ from ._agent_runner import (
|
||||
use_builtin_agent,
|
||||
)
|
||||
from ._skill_agent import run_skill_via_real_agent, use_skill_search_agent
|
||||
from ._utils import get_ai_writing_config, llm_json, safe_get_stream_writer
|
||||
from ._utils import (
|
||||
ThinkTagParser,
|
||||
_content_to_text,
|
||||
get_ai_writing_config,
|
||||
get_model,
|
||||
llm_json,
|
||||
parse_json,
|
||||
reasoning_delta,
|
||||
safe_get_stream_writer,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -227,6 +239,40 @@ def _per_keyword_limit(max_materials: int, total_keywords: int) -> int:
|
||||
return (max_materials + total_keywords - 1) // total_keywords
|
||||
|
||||
|
||||
async def _stream_llm_json(
|
||||
system: str,
|
||||
user: str,
|
||||
*,
|
||||
fallback: dict,
|
||||
config: RunnableConfig,
|
||||
model_name: str | None,
|
||||
disable_thinking: bool,
|
||||
on_text,
|
||||
) -> dict:
|
||||
"""流式生成一段 JSON,并把正文 / 思考片段透传给前端过程卡。"""
|
||||
messages = [SystemMessage(content=system), HumanMessage(content=user)]
|
||||
full_text = ""
|
||||
parser = ThinkTagParser()
|
||||
try:
|
||||
model = get_model(config, model_name=model_name, disable_thinking=disable_thinking)
|
||||
async for chunk in model.astream(messages):
|
||||
reasoning = reasoning_delta(chunk)
|
||||
if reasoning:
|
||||
on_text(reasoning, 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
|
||||
if text:
|
||||
on_text(text, is_thinking)
|
||||
return parse_json(full_text, fallback)
|
||||
except Exception as e:
|
||||
logger.error("stream_llm_json failed: %s", e, exc_info=True)
|
||||
return {**fallback, "_llm_error": str(e)}
|
||||
|
||||
|
||||
# 「再追加 N 条素材」之类的数量指令:动词 + 数字(+ 量词 + 名词)。
|
||||
_APPEND_COUNT_VERBS = "追加|新增|增加|补充|再来|多来|多给|多找|多搜|多检索|再给|再要|再找|再搜|再加"
|
||||
_APPEND_COUNT_RE = re.compile(
|
||||
@@ -467,6 +513,15 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
def _emit(message: str) -> None:
|
||||
writer({"type": "research_progress", "message": message})
|
||||
|
||||
def _emit_keyword_chunk(text: str = "", is_thinking: bool = False) -> None:
|
||||
writer({
|
||||
"type": "keyword_chunk",
|
||||
"agent_name": researcher_label,
|
||||
"message": "生成检索词",
|
||||
"text": text,
|
||||
"is_thinking": is_thinking,
|
||||
})
|
||||
|
||||
# 跳过素材收集:用户在初始表单勾选「不收集素材,直接用通用知识写作」。
|
||||
# 仅在**首次**进入(无任何 pause 干预 = intervention 为空)时短路,避免后续
|
||||
# 「补充检索 / 按输入追加」等再入研究节点时被误跳过。直接产出空素材包、强制
|
||||
@@ -501,6 +556,7 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
_emit(f"追加检索 {len(keywords)} 个关键词…")
|
||||
else:
|
||||
_emit("正在生成检索词…")
|
||||
_emit_keyword_chunk()
|
||||
if use_agent:
|
||||
kw_result = await run_writing_agent(
|
||||
agent_id=RESEARCHER_AGENT_ID,
|
||||
@@ -514,12 +570,13 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
fallback={"keywords": [intent]},
|
||||
state=state,
|
||||
config=config,
|
||||
on_text=_emit_keyword_chunk,
|
||||
explicit_model_fallback=keyword_model,
|
||||
strict_model=False,
|
||||
disable_thinking=True,
|
||||
)
|
||||
else:
|
||||
kw_result = await llm_json(
|
||||
kw_result = await _stream_llm_json(
|
||||
EXTRACT_KEYWORDS_SYSTEM,
|
||||
EXTRACT_KEYWORDS_USER.format(
|
||||
user_intent=intent,
|
||||
@@ -531,6 +588,7 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
config=config,
|
||||
model_name=keyword_model,
|
||||
disable_thinking=True,
|
||||
on_text=_emit_keyword_chunk,
|
||||
)
|
||||
# 清洗关键词:剔除占位符 / 空值,全空时回退写作意图
|
||||
keywords = _clean_keywords(kw_result.get("keywords"), intent)
|
||||
|
||||
Reference in new issue
Block a user