diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index 4a6364979..648b2bc9a 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -50,6 +50,7 @@ DEFAULT_CONFIG = { "default_personality": "default", "prompt_prefix": "", "max_context_length": -1, + "dequeue_context_length": 1, "streaming_response": False, }, "provider_stt_settings": { @@ -997,6 +998,11 @@ CONFIG_METADATA_2 = { "type": "int", "hint": "超出这个数量时将丢弃最旧的部分,用户和AI的一轮聊天记为 1 条。-1 表示不限制,默认为不限制。", }, + "dequeue_context_length": { + "description": "丢弃对话数量(条)", + "type": "int", + "hint": "超出 最多携带对话数量(条) 时,丢弃多少条记录,用户和AI的一轮聊天记为 1 条。适宜的配置,可以提高超长上下文对话 deepseek 命中缓存效果,理想情况下计费将降低到1/3以下", + }, "streaming_response": { "description": "启用流式回复", "type": "bool", diff --git a/astrbot/core/pipeline/process_stage/method/llm_request.py b/astrbot/core/pipeline/process_stage/method/llm_request.py index a9f793626..547099e21 100644 --- a/astrbot/core/pipeline/process_stage/method/llm_request.py +++ b/astrbot/core/pipeline/process_stage/method/llm_request.py @@ -38,6 +38,10 @@ class LLMRequestSubStage(Stage): self.max_context_length = ctx.astrbot_config["provider_settings"][ "max_context_length" ] # int + self.dequeue_context_length = min( + max(1,ctx.astrbot_config["provider_settings"]["dequeue_context_length"]), + self.max_context_length - 1 + ) # int self.streaming_response = ctx.astrbot_config["provider_settings"][ "streaming_response" ] # bool @@ -137,7 +141,7 @@ class LLMRequestSubStage(Stage): and len(req.contexts) // 2 > self.max_context_length ): logger.debug("上下文长度超过限制,将截断。") - req.contexts = req.contexts[-self.max_context_length * 2 :] + req.contexts = req.contexts[-(self.max_context_length - self.dequeue_context_length) * 2 :] # session_id if not req.session_id: