放宽最近上下文到30条并取消中途截断
This commit is contained in:
@@ -94,38 +94,14 @@ class ContextBuilder:
|
||||
) -> List[Dict]:
|
||||
if not recent_messages:
|
||||
return []
|
||||
# 这里直接把“最近 N 条”原样交给后续提示词层,而不是再做一次相关性裁剪:
|
||||
# 1. 用户明确要求给模型 30 条最近消息,方便推断群里正在讨论的上下文;
|
||||
# 2. 之前的“相关性筛选 + 尾部保留”虽然更省 token,但会打断对话连续性;
|
||||
# 3. 对群聊场景来说,连续现场通常比少量高分片段更有利于模型判断谁在接谁的话。
|
||||
#
|
||||
# 这里仍保留签名参数不动,是为了兼容上层调用,避免后续改动牵连太多。
|
||||
window = recent_messages[-self.recent_context_size:]
|
||||
if len(window) <= 8:
|
||||
return window
|
||||
|
||||
current_tokens = self._extract_topic_tokens(current_content)
|
||||
quote_tokens = self._extract_topic_tokens(
|
||||
f"{quote_context.get('title', '')} {quote_context.get('quote_body', '')}"
|
||||
)
|
||||
focus_tokens = current_tokens | quote_tokens
|
||||
quote_sender_name = str(quote_context.get("quote_sender_name", "") or "").strip().lower()
|
||||
|
||||
scored: List[tuple[int, int, Dict]] = []
|
||||
for idx, item in enumerate(window):
|
||||
score = self._message_relevance(
|
||||
item,
|
||||
current_sender=current_sender,
|
||||
focus_tokens=focus_tokens,
|
||||
quote_sender_name=quote_sender_name,
|
||||
)
|
||||
if score > 0:
|
||||
scored.append((score, idx, item))
|
||||
|
||||
# 总是保留尾部几条,维持现场感;再拼上与当前话题最相关的消息。
|
||||
tail_indexes = set(range(max(len(window) - 4, 0), len(window)))
|
||||
keep_indexes = set(tail_indexes)
|
||||
for _, idx, _ in sorted(scored, key=lambda x: (-x[0], -x[1]))[:10]:
|
||||
keep_indexes.add(idx)
|
||||
|
||||
selected = [window[idx] for idx in sorted(keep_indexes)]
|
||||
if len(selected) < 6:
|
||||
return window[-6:]
|
||||
return selected[-12:]
|
||||
return window
|
||||
|
||||
@classmethod
|
||||
def _message_relevance(
|
||||
|
||||
Reference in New Issue
Block a user