-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: add summarization compactor #12296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
39baf35
Add new class variable for generation kwarg conversion and utils to u…
sjrl facd5f8
Merge branch 'main' into codex/chat-generator-parameter-mapping
sjrl d92b982
PR comments
sjrl 6882115
Merge branch 'codex/chat-generator-parameter-mapping' of github.com:d…
sjrl 64dddb8
refactor
sjrl f7ad0d3
Merge branch 'main' of github.com:deepset-ai/haystack into feat/summa…
sjrl ab2651a
updates
sjrl 14ec1b5
fixes and better documentation on how the summarization progresses as…
sjrl e3d4a36
changes
sjrl 9ace71b
some improvements
sjrl f86d596
pivot to using approximate_summary_tokens which is just an estimate u…
sjrl 2552570
changes
sjrl 3103893
fix tests and reno
sjrl 2ae9650
improve reno
sjrl 7b14536
refactoring tests
sjrl e06c4f8
more refactoring
sjrl dc1e336
test refinement
sjrl 7e9cd70
test refinement
sjrl 3821b2f
refinement
sjrl 7ed3e1b
changes
sjrl e6b0680
remove smallets const
sjrl 3ce957b
improve summarization logic so that we don't summarize messages outsi…
sjrl aa8b249
update test
sjrl 682b187
improve rendered prompt for summarization
sjrl 80c6fc4
improvements
sjrl c02eb27
PR comments
sjrl 92d42ce
improve docs and shorten reno
sjrl 1fb9c96
remove function
sjrl 81866cb
Reduce number tokenizer calls
sjrl 6c90527
PR comments
sjrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
releasenotes/notes/add-summarization-compactor-91b6be6855f478df.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| --- | ||
|
sjrl marked this conversation as resolved.
|
||
| features: | ||
| - | | ||
| Added the experimental ``SummarizationCompactor`` (use with ``CompactionHook``), which progressively summarizes a | ||
| conversation until it fits a target token budget, preserving useful context from long-running Agents instead of | ||
| dropping older messages. | ||
|
|
||
| It uses four summarization tiers in order until the target token budget is reached: | ||
|
|
||
| 1. ``historical_turns``: Starting with the oldest, summarize as few complete historical turns as needed. | ||
| 2. ``historical_summaries``: When no complete historical turns remain, combine as few of the oldest historical | ||
| summaries as needed. | ||
| 3. ``current_task_steps``: Summarize the fewest oldest steps needed to reach the target while preserving the | ||
| ``min_keep_steps`` newest steps. | ||
| 4. ``current_task_summaries``: When no more steps can be summarized, combine as few of the current task's oldest | ||
| summaries as needed to reach the target. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from typing import Annotated | ||
| from haystack.components.agents import Agent | ||
| from haystack.components.generators.chat import OpenAIResponsesChatGenerator | ||
| from haystack.hooks.compaction import CompactionHook, SummarizationCompactor | ||
| from haystack.tools import tool | ||
|
|
||
| @tool | ||
| def web_search(query: Annotated[str, "The search query"]) -> str: | ||
| """Search the web for current information.""" | ||
| return f"Search results for: {query}" | ||
|
|
||
| agent_generator = OpenAIResponsesChatGenerator(model="gpt-5.4") | ||
| summary_generator = OpenAIResponsesChatGenerator(model="gpt-5.4-nano") | ||
|
|
||
| compaction_hook = CompactionHook( | ||
| compactor=SummarizationCompactor( | ||
| chat_generator=summary_generator, | ||
| min_keep_steps=2, | ||
| approximate_summary_tokens=1_024, | ||
| ), | ||
| context_window=400_000, | ||
| compact_at=0.7, | ||
| compact_to=0.4, | ||
| ) | ||
|
|
||
| agent = Agent( | ||
|
sjrl marked this conversation as resolved.
|
||
| chat_generator=agent_generator, | ||
| tools=[web_search], | ||
| hooks={"before_llm": [compaction_hook]}, | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.