Skip to content

Commit aa0b7a2

Browse files
Pleiades1726AstrBot
andauthored
feat: add fallback_max_context_tokens config for context compression (#7942)
- New config item fallback_max_context_tokens (default 128k) - When max_context_tokens is 0 and model not in LLM_METADATAS, use fallback_max_context_tokens as the context window limit - Unified global config under provider_settings, in truncate_and_compress section - i18n: zh-CN, en-US, ru-RU Co-authored-by: AstrBot <astrbot@container>
1 parent a1ccb02 commit aa0b7a2

6 files changed

Lines changed: 32 additions & 1 deletion

File tree

astrbot/core/astr_main_agent.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ class MainAgentBuildConfig:
152152
This enforce max turns before compression"""
153153
dequeue_context_length: int = 1
154154
"""The number of oldest turns to remove when context length limit is reached."""
155+
fallback_max_context_tokens: int = 128000
156+
"""Fallback max context tokens. When max_context_tokens is 0 and the model is not in LLM_METADATAS, use this value."""
155157
llm_safety_mode: bool = True
156158
"""This will inject healthy and safe system prompt into the main agent,
157159
to prevent LLM output harmful information"""
@@ -1367,6 +1369,11 @@ async def build_main_agent(
13671369
provider.provider_config["max_context_tokens"] = model_info["limit"][
13681370
"context"
13691371
]
1372+
else:
1373+
# fallback: default to configured fallback value
1374+
provider.provider_config["max_context_tokens"] = (
1375+
config.fallback_max_context_tokens
1376+
)
13701377

13711378
if event.get_platform_name() == "webchat":
13721379
asyncio.create_task(_handle_webchat(event, req, provider))

astrbot/core/config/default.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ class ChatProviderTemplate(TypedDict):
309309
"model": "",
310310
"modalities": [],
311311
"custom_extra_body": {},
312-
"max_context_tokens": 0,
312+
"max_context_tokens": 128000,
313313
}
314314

315315
"""
@@ -3544,6 +3544,14 @@ class ChatProviderTemplate(TypedDict):
35443544
"provider_settings.agent_runner_type": "local",
35453545
},
35463546
},
3547+
"provider_settings.fallback_max_context_tokens": {
3548+
"description": "上下文窗口兜底值",
3549+
"type": "int",
3550+
"hint": "当 max_context_tokens 为 0 且模型不在内置元数据中时,使用此值作为上下文窗口大小。默认 128000。",
3551+
"condition": {
3552+
"provider_settings.agent_runner_type": "local",
3553+
},
3554+
},
35473555
},
35483556
"condition": {
35493557
"provider_settings.agent_runner_type": "local",

astrbot/core/pipeline/process_stage/method/agent_sub_stages/internal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ async def initialize(self, ctx: PipelineContext) -> None:
107107
)
108108
if self.dequeue_context_length <= 0:
109109
self.dequeue_context_length = 1
110+
self.fallback_max_context_tokens: int = settings.get(
111+
"fallback_max_context_tokens", 128000
112+
)
110113

111114
self.llm_safety_mode = settings.get("llm_safety_mode", True)
112115
self.safety_mode_strategy = settings.get(
@@ -136,6 +139,7 @@ async def initialize(self, ctx: PipelineContext) -> None:
136139
llm_compress_provider_id=self.llm_compress_provider_id,
137140
max_context_length=self.max_context_length,
138141
dequeue_context_length=self.dequeue_context_length,
142+
fallback_max_context_tokens=self.fallback_max_context_tokens,
139143
llm_safety_mode=self.llm_safety_mode,
140144
safety_mode_strategy=self.safety_mode_strategy,
141145
computer_use_runtime=self.computer_use_runtime,

dashboard/src/i18n/locales/en-US/features/config-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@
272272
"llm_compress_provider_id": {
273273
"description": "Model Provider ID for Context Compression",
274274
"hint": "When left empty, will fall back to the 'Truncate by Turns' strategy."
275+
},
276+
"fallback_max_context_tokens": {
277+
"description": "Fallback context window size",
278+
"hint": "When max_context_tokens is 0 and the model is not in built-in metadata, use this value as the context window size. Default: 128000."
275279
}
276280
}
277281
},

dashboard/src/i18n/locales/ru-RU/features/config-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@
272272
"llm_compress_provider_id": {
273273
"description": "Модель для сжатия контекста",
274274
"hint": "Если не выбрано, произойдет откат к стратегии удаления сообщений."
275+
},
276+
"fallback_max_context_tokens": {
277+
"description": "Запасной размер окна контекста",
278+
"hint": "Если max_context_tokens равен 0 и модель отсутствует во встроенных метаданных, используется это значение. По умолчанию: 128000."
275279
}
276280
}
277281
},

dashboard/src/i18n/locales/zh-CN/features/config-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@
274274
"llm_compress_provider_id": {
275275
"description": "用于上下文压缩的模型提供商 ID",
276276
"hint": "留空时将降级为\"按对话轮数截断\"的策略。"
277+
},
278+
"fallback_max_context_tokens": {
279+
"description": "上下文窗口兜底值",
280+
"hint": "当 max_context_tokens 为 0 且模型不在内置元数据中时,使用此值作为上下文窗口大小。默认 128000。"
277281
}
278282
}
279283
},

0 commit comments

Comments
 (0)