检索逻辑修改
This commit is contained in:
1 parent
294d06f22b
commit
2402a84c71
5 files changed
+346
-15
No files matched your search
+33
-6
@@ -18,7 +18,10 @@ from ._utils import llm_json
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_MAX_MATERIALS = 12
|
||||
# 素材总量上限边界:config.yaml 的 ai_writing.max_materials,非法值回退默认 20
|
||||
_DEFAULT_MAX_MATERIALS = 20
|
||||
_MIN_MAX_MATERIALS = 1
|
||||
_MAX_MAX_MATERIALS = 100
|
||||
|
||||
# 素材检索关键词数量边界:用户可指定 1~10 个,非法值回退默认 6
|
||||
_DEFAULT_KEYWORD_COUNT = 6
|
||||
@@ -34,6 +37,26 @@ def _resolve_keyword_count(raw: object) -> int:
|
||||
return max(1, min(_MAX_KEYWORD_COUNT, n))
|
||||
|
||||
|
||||
def _resolve_max_materials(raw: object) -> int:
|
||||
"""把 config.yaml 配置的素材总量上限规整到 [1, 100],非法值回退默认 20。"""
|
||||
try:
|
||||
n = int(raw) # type: ignore[arg-type]
|
||||
except (TypeError, ValueError):
|
||||
return _DEFAULT_MAX_MATERIALS
|
||||
return max(_MIN_MAX_MATERIALS, min(_MAX_MAX_MATERIALS, n))
|
||||
|
||||
|
||||
def _per_keyword_limit(max_materials: int, total_keywords: int) -> int:
|
||||
"""把素材总量上限均分到每个检索词上的检索条数(向上取整)。
|
||||
|
||||
向上取整保证合并去重后素材数仍能接近总量上限;最终会再统一截断到上限。
|
||||
total_keywords <= 0 时回退为总量上限本身。
|
||||
"""
|
||||
if total_keywords <= 0:
|
||||
return max_materials
|
||||
return (max_materials + total_keywords - 1) // total_keywords
|
||||
|
||||
|
||||
def _notice(title: str, detail: str, raw: str = "") -> dict:
|
||||
"""构造一个非致命告警事件,前端在时间线显示「标题 + 真实原因」,不打断写作流程。
|
||||
|
||||
@@ -67,6 +90,8 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
ai_writing_config = cfg.get("ai_writing_config", {}) or {}
|
||||
keyword_model: str | None = ai_writing_config.get("keyword_model") or None
|
||||
rank_model: str | None = ai_writing_config.get("rank_model") or None
|
||||
# 素材总量上限(config.yaml: ai_writing.max_materials),按检索词数均分到每个词
|
||||
max_materials = _resolve_max_materials(ai_writing_config.get("max_materials"))
|
||||
|
||||
async def _emit(message: str) -> None:
|
||||
if sse_queue is None:
|
||||
@@ -103,6 +128,8 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
search_limit = max(search_limit, len(keywords))
|
||||
search_keywords = keywords[:search_limit]
|
||||
total = len(search_keywords)
|
||||
# 每个检索词分到的检索条数 = 总量上限 / 检索词数(向上取整)
|
||||
per_keyword = _per_keyword_limit(max_materials, total)
|
||||
await _emit(f"检索词已生成,开始检索 {total} 个方向…")
|
||||
|
||||
# 并发搜索,但每完成一个就推送进度(计数在单线程 asyncio 下无需加锁)
|
||||
@@ -111,7 +138,7 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
async def _search_one(kw: str) -> list[SearchResult]:
|
||||
nonlocal completed
|
||||
try:
|
||||
return await provider.search(kw, max_results=3)
|
||||
return await provider.search(kw, max_results=per_keyword)
|
||||
finally:
|
||||
completed += 1
|
||||
if completed < total:
|
||||
@@ -159,7 +186,7 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
# Step 3: 相关度排序(如素材较多,让 LLM 排序)
|
||||
await _emit("检索完成,正在筛选排序素材…")
|
||||
rank_error = ""
|
||||
if len(raw_materials) > _MAX_MATERIALS:
|
||||
if len(raw_materials) > max_materials:
|
||||
materials_list = "\n".join(
|
||||
f"- ID: {m['id']} 标题: {m['title']} 摘要: {m['content'][:100]}"
|
||||
for m in raw_materials
|
||||
@@ -167,7 +194,7 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
rank_result = await llm_json(
|
||||
RANK_MATERIALS_SYSTEM,
|
||||
RANK_MATERIALS_USER.format(user_intent=intent, materials_list=materials_list),
|
||||
fallback={"ranked_ids": [m["id"] for m in raw_materials[:_MAX_MATERIALS]], "summary": f"共搜索到{len(raw_materials)}条素材"},
|
||||
fallback={"ranked_ids": [m["id"] for m in raw_materials[:max_materials]], "summary": f"共搜索到{len(raw_materials)}条素材"},
|
||||
config=config,
|
||||
model_name=rank_model,
|
||||
)
|
||||
@@ -180,10 +207,10 @@ async def researcher_node(state: AIWritingState, config: RunnableConfig) -> dict
|
||||
for m in raw_materials:
|
||||
if m["id"] not in sorted_ids_set:
|
||||
sorted_materials.append(m)
|
||||
materials = sorted_materials[:_MAX_MATERIALS]
|
||||
materials = sorted_materials[:max_materials]
|
||||
summary = rank_result.get("summary", f"共搜索到{len(raw_materials)}条素材,精选{len(materials)}条")
|
||||
else:
|
||||
materials = sorted(raw_materials, key=lambda m: m.get("relevance_score", 0), reverse=True)[:_MAX_MATERIALS]
|
||||
materials = sorted(raw_materials, key=lambda m: m.get("relevance_score", 0), reverse=True)[:max_materials]
|
||||
summary = f"共搜索到{len(raw_materials)}条素材,精选{len(materials)}条"
|
||||
|
||||
# 空结果兜底:未检索到任何素材时给出明确文案,前端据此弹出「无数据继续」选项
|
||||
|
||||
Reference in new issue
Block a user