feat(config): configurable conversation history window (B3)
FastGPT maintains full history server-side; LangGraph truncated the injected history at a hardcoded 50 messages, so long conversations behaved differently across backends. - Config.AGENT_HISTORY_MESSAGES (env): max messages injected into the model prompt (recent N, both roles counted); default 50 unchanged; larger values support long conversations; 0 = no truncation, matching FastGPT's full-history semantics. Only the injected window is affected - dialogue stays fully persisted in the checkpoint. - validate(): rejects negative values; 0 and positive accepted - conversation_history reads the config (import-time class attribute, monkeypatched in tests); explicit None-check keeps 0 falsy-safe - .env.example documents the knob Verified: 225 unit tests (6 new history-window tests)
This commit is contained in:
+17
-4
@@ -108,13 +108,26 @@ async def validated_reply(
|
||||
raise CollectReplyError("模型回复格式、阶段转换或时间依据不合规(一次修复后仍失败)")
|
||||
|
||||
|
||||
# 工作流 chatNode 的 history 配置为 50;保留独立消息及其角色。
|
||||
_HISTORY_MESSAGES = 50
|
||||
# 工作流 chatNode 的 history 配置为 50;默认对齐(Config.
|
||||
# AGENT_HISTORY_MESSAGES,env 可调大,0 = 不截断对齐 FastGPT 全量语义)。
|
||||
_HISTORY_MESSAGES_DEFAULT = 50
|
||||
|
||||
|
||||
def conversation_history(state: AgentState) -> list[dict[str, str]]:
|
||||
"""历史不拼入 user 消息,不剥除历史 assistant 的状态前缀。"""
|
||||
return [dict(message) for message in (state.get("dialogue") or [])[-_HISTORY_MESSAGES:]]
|
||||
"""历史不拼入 user 消息,不剥除历史 assistant 的状态前缀。
|
||||
|
||||
截断窗口取 Config.AGENT_HISTORY_MESSAGES(import 时固化的类属性,
|
||||
测试用 monkeypatch 调整);0 或负数视为不截断。
|
||||
"""
|
||||
from ..core.config import Config
|
||||
|
||||
limit = getattr(Config, "AGENT_HISTORY_MESSAGES", None)
|
||||
if limit is None:
|
||||
limit = _HISTORY_MESSAGES_DEFAULT
|
||||
dialogue = state.get("dialogue") or []
|
||||
if limit > 0:
|
||||
dialogue = dialogue[-limit:]
|
||||
return [dict(message) for message in dialogue]
|
||||
|
||||
|
||||
def context_prompt(state: AgentState, *, time_context: dict | None = None) -> str:
|
||||
|
||||
Reference in New Issue
Block a user