Repository navigation
Commit ecb80bd
authored
Python: keep existing summaries after the system message in the summa… (#14661)
### Motivation and Context
`ChatHistorySummarizationReducer(use_single_summary=False)` is meant to
keep earlier summaries and add a new one on each reduction. That doesn't
happen when the chat history starts with a system prompt, which is the
common case.
After the first reduction the reducer puts the preserved system message
at index 0: `[system, summary1, ...]`. On the next reduction
`locate_summarization_boundary(history)` starts at index 0, sees the
system message (not a summary) and returns 0. So
`keep_existing_summaries` stays empty, `summary1` falls into the range
that gets summarized, and the result is `[system, summary2(summary1 +
messages), ...]`, exactly what `use_single_summary=True` produces.
### Description
When `use_single_summary=False` and the preserved system message is at
index 0, locate the summary boundary after it: `insertion_point = 1 +
locate_summarization_boundary(history[1:])`. `keep_existing_summaries`
then contains `[system, summary1]`, the old summary is no longer sent to
the summarization call, and the existing identity check still prevents
the system message from being prepended twice. `use_single_summary=True`
(the default) is unchanged, so its `offset_count` passed to
`locate_safe_reduction_index` doesn't move.
Test: `test_summarization_keeps_existing_summaries_after_system_message`
builds `[system, earlier summary, 6 messages]` with `target_count=2,
use_single_summary=False` and checks that the result starts with the
same system message and earlier summary objects followed by the new
summary, and that the earlier summary is not in the history sent to
`get_chat_message_content`. It fails on `main` and passes with this
change.
```
pytest tests/unit/contents
395 passed
```
### Contribution Checklist
- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations (ruff 0.9.6 check and format clean)
- [x] All unit tests pass, and I have added new tests where possible (I
ran `tests/unit/contents`, not the full suite)
- [x] I didn't break anyone 😄1 parent df34878 commit ecb80bd
2 files changed
Lines changed: 39 additions & 0 deletions
File tree
- python
- semantic_kernel/contents/history_reducer
- tests/unit/contents
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
110 | 113 | | |
111 | 114 | | |
112 | 115 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
312 | 312 | | |
313 | 313 | | |
314 | 314 | | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
0 commit comments