feat(思维导图助手):思维导图助手增加智能整理总结-返回参数

develop
panyy 2026-06-25 09:39:25 +08:00
parent 965781213e
commit 64ac579c2a
1 changed files with 44 additions and 7 deletions

View File

@ -367,14 +367,37 @@ class MindmapOrganizeRequest(BaseModel):
prompt: Optional[str] = None prompt: Optional[str] = None
def _extract_json_object(text: str) -> str: def _extract_json_object(text: Optional[str]) -> str:
content = text.strip() content = (text or "").strip()
if content.startswith("```"): if content.startswith("```"):
content = re.sub(r"^```(?:json|markdown|md)?\s*", "", content, flags=re.IGNORECASE) content = re.sub(r"^```(?:json|markdown|md)?\s*", "", content, flags=re.IGNORECASE)
content = re.sub(r"\s*```$", "", content) content = re.sub(r"\s*```$", "", content)
return content.strip() return content.strip()
def _extract_llm_message_content(message: dict) -> str:
content = message.get("content")
if isinstance(content, str):
return content
if isinstance(content, list):
parts = []
for item in content:
if isinstance(item, str):
parts.append(item)
elif isinstance(item, dict):
text = item.get("text") or item.get("content")
if isinstance(text, str):
parts.append(text)
if parts:
return "\n".join(parts)
for key in ("reasoning_content", "reasoning", "output_text", "text"):
value = message.get(key)
if isinstance(value, str) and value.strip():
return value
return ""
DEFAULT_MINDMAP_ORGANIZE_PROMPT = """你是文档结构整理助手。请基于用户提供的 Markdown 生成适合思维导图展示的 Markdown。 DEFAULT_MINDMAP_ORGANIZE_PROMPT = """你是文档结构整理助手。请基于用户提供的 Markdown 生成适合思维导图展示的 Markdown。
要求 要求
@ -551,14 +574,28 @@ def _call_mindmap_llm(markdown: str, mode: str = "smart", custom_prompt: Optiona
except Exception as exc: except Exception as exc:
raise RuntimeError(f"智能整理模型请求失败: {exc}") from exc raise RuntimeError(f"智能整理模型请求失败: {exc}") from exc
message = result.get("choices", [{}])[0].get("message", {}) choices = result.get("choices") or []
content = message.get("content", "") choice = choices[0] if choices else {}
message = choice.get("message") or {}
finish_reason = choice.get("finish_reason")
usage = result.get("usage")
content = _extract_llm_message_content(message)
if not content:
logger.warning(
"Mindmap LLM returned empty content task_id={} role={} finish_reason={} usage={} message_keys={}",
task_id or "-", request_role, finish_reason, usage, list(message.keys())
)
raise RuntimeError(f"智能整理模型返回空内容finish_reason={finish_reason}")
organized = _extract_json_object(content) organized = _extract_json_object(content)
if not organized: if not organized:
raise RuntimeError("智能整理模型未返回有效内容") logger.warning(
"Mindmap LLM returned invalid organized content task_id={} role={} finish_reason={} usage={}",
task_id or "-", request_role, finish_reason, usage
)
raise RuntimeError(f"智能整理模型未返回有效内容finish_reason={finish_reason}")
logger.info( logger.info(
"Mindmap LLM request completed task_id={} role={} output_chars={} output_tokens_est={}", "Mindmap LLM request completed task_id={} role={} finish_reason={} usage={} output_chars={} output_tokens_est={}",
task_id or "-", request_role, len(organized), _estimate_tokens(organized) task_id or "-", request_role, finish_reason, usage, len(organized), _estimate_tokens(organized)
) )
return organized return organized