mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Implement annotation-based context compaction (#4469)
* Implement annotation-based context compaction Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Handle missing compaction attributes in BaseChatClient Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix CI typing and bandit issues Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Optimize incremental compaction annotation pass Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * refinement * Python: add ToolResultCompactionStrategy and CompactionProvider Add ToolResultCompactionStrategy that collapses older tool-call groups into short summary messages (e.g. [Tool calls: get_weather]) while keeping the most recent groups verbatim. This mirrors the .NET ToolResultCompactionStrategy from PR #4533. Add CompactionProvider as a context-provider that auto-applies compaction before each agent turn and stores compacted history in session state after each turn. Includes tests and samples for both features. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * refinement and alignment with dotnet PR * updated tool result compaction * updated tool result compaction * Python: add ToolResultCompactionStrategy, CompactionProvider, and skip_excluded - ToolResultCompactionStrategy collapses older tool-call groups into [Tool results: func_name: result] summaries with bidirectional tracing (same pattern as SummarizationStrategy). - CompactionProvider as BaseContextProvider with separate before_strategy and after_strategy parameters. before_strategy compacts loaded context; after_strategy compacts stored history via history_source_id. - InMemoryHistoryProvider gains skip_excluded flag to filter out messages marked as excluded by compaction strategies. - Tests, samples, and exports updated. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fixed checks * fix mypy * Fix: ensure summary messages from both strategies get full compaction annotations SummarizationStrategy was not calling annotate_message_groups after inserting its summary message, so the summary lacked core group annotations (id, kind, index, has_reasoning, _excluded). Added the missing call. ToolResultCompactionStrategy already had it. Added tests verifying both strategies produce fully annotated summaries. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * updated propagation * fix mypy --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
565c0b1623
commit
3e03a305f6
@@ -0,0 +1,89 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import (
|
||||
Message,
|
||||
annotate_message_groups,
|
||||
apply_compaction,
|
||||
included_messages,
|
||||
)
|
||||
|
||||
"""This sample demonstrates authoring a custom compaction strategy.
|
||||
|
||||
The custom strategy keeps system messages and the most recent user turn while
|
||||
excluding older non-system groups.
|
||||
"""
|
||||
|
||||
EXCLUDED_KEY = "_excluded"
|
||||
GROUP_ANNOTATION_KEY = "_group"
|
||||
|
||||
|
||||
class KeepLastUserTurnStrategy:
|
||||
async def __call__(self, messages: list[Message]) -> bool:
|
||||
group_ids = annotate_message_groups(messages)
|
||||
group_kinds: dict[str, str] = {}
|
||||
for message in messages:
|
||||
group_annotation = message.additional_properties.get(GROUP_ANNOTATION_KEY)
|
||||
group_id = group_annotation.get("id") if isinstance(group_annotation, dict) else None
|
||||
kind = group_annotation.get("kind") if isinstance(group_annotation, dict) else None
|
||||
if (
|
||||
isinstance(group_id, str)
|
||||
and isinstance(kind, str)
|
||||
and group_id not in group_kinds
|
||||
):
|
||||
group_kinds[group_id] = kind
|
||||
user_group_ids = [
|
||||
group_id for group_id in group_ids if group_kinds.get(group_id) == "user"
|
||||
]
|
||||
if not user_group_ids:
|
||||
return False
|
||||
keep_user_group_id = user_group_ids[-1]
|
||||
|
||||
changed = False
|
||||
for message in messages:
|
||||
group_annotation = message.additional_properties.get(GROUP_ANNOTATION_KEY)
|
||||
group_id = group_annotation.get("id") if isinstance(group_annotation, dict) else None
|
||||
if message.role == "system":
|
||||
continue
|
||||
if group_id == keep_user_group_id:
|
||||
continue
|
||||
if message.additional_properties.get(EXCLUDED_KEY) is not True:
|
||||
changed = True
|
||||
message.additional_properties[EXCLUDED_KEY] = True
|
||||
return changed
|
||||
|
||||
|
||||
def _messages() -> list[Message]:
|
||||
return [
|
||||
Message(role="system", text="You are concise."),
|
||||
Message(role="user", text="first request"),
|
||||
Message(role="assistant", text="first response"),
|
||||
Message(role="user", text="second request"),
|
||||
Message(role="assistant", text="second response"),
|
||||
]
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# 1. Build a short conversation.
|
||||
messages = _messages()
|
||||
print(f"Number of messages before compaction: {len(messages)}")
|
||||
# 2. Apply custom strategy.
|
||||
await apply_compaction(messages, strategy=KeepLastUserTurnStrategy())
|
||||
# 3. Print projected messages.
|
||||
projected = included_messages(messages)
|
||||
print(f"Number of messages after compaction: {len(projected)}")
|
||||
for msg in projected:
|
||||
print(f"[{msg.role}] {msg.text}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
"""
|
||||
Sample output:
|
||||
Number of messages before compaction: 5
|
||||
Number of messages after compaction: 2
|
||||
[system] You are concise.
|
||||
[user] second request
|
||||
"""
|
||||
Reference in New Issue
Block a user