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:
|
||||
|
||||
@@ -33,6 +33,11 @@ class Config:
|
||||
# 运行时选择:LLM_PROVIDER=<名>;缺省或 fake = 离线脚本模型。
|
||||
LLM_PROVIDERS = os.getenv("LLM_PROVIDERS", "")
|
||||
LLM_PROVIDER = os.getenv("LLM_PROVIDER", "")
|
||||
# 对话历史注入模型时的最大消息条数(最近 N 条,双向计数)。
|
||||
# 默认 50(约 25 轮);调大支持长对话;0 = 不截断(与 FastGPT
|
||||
# 服务端全量历史语义对齐)。对话正文仍随 checkpoint 全量保存,
|
||||
# 本参数只影响注入提示词的历史窗口。
|
||||
AGENT_HISTORY_MESSAGES = int(os.getenv("AGENT_HISTORY_MESSAGES", "50"))
|
||||
# LLM 客户端是否信任系统代理(Windows 注册表)。缺省 False:
|
||||
# 本地网关(127.0.0.1)必须直连;外部网关若需代理设为 true。
|
||||
LLM_HTTP_TRUST_ENV = os.getenv("LLM_HTTP_TRUST_ENV", "false").lower() in (
|
||||
@@ -149,6 +154,8 @@ class Config:
|
||||
f"Unknown LLM_PROVIDER {cls.LLM_PROVIDER!r}; "
|
||||
f"configured: {sorted(profiles)}"
|
||||
)
|
||||
if cls.AGENT_HISTORY_MESSAGES < 0:
|
||||
raise ValueError("AGENT_HISTORY_MESSAGES must be >= 0 (0 = 不截断)")
|
||||
if cls.AGENT_BACKEND != "fastgpt":
|
||||
return
|
||||
required = [
|
||||
|
||||
Reference in New Issue
Block a user