Repository navigation
Commit 05b6569
authored
### Motivation and Context
Fixes #12303
1. **Why is this change required?**
Currently, configuring a `ChatHistorySummarizationReducer` or
`ChatHistoryTruncationReducer` on a `ChatCompletionAgent` (e.g. via
`arguments=KernelArguments(chat_history=reducer)` or thread injection)
fails to reduce the chat history before dispatching calls to the LLM.
The full, unbounded conversation history is passed in the request
payload, causing context window overflows and unnecessary token
consumption.
2. **What problem does it solve?**
- Enables `ChatCompletionAgent` to respect active `ChatHistoryReducer`
implementations (such as summarization and truncation reducers).
- Allows users and multi-agent orchestrations to manage context size
efficiently without token explosion.
3. **What scenario does it contribute to?**
Multi-turn agent interactions and multi-agent group chats where
long-running conversations require summarizing or truncating older
messages to stay within LLM context boundaries.
---
### Description
- **Auto-reduction on thread updates:** In
`ChatHistoryAgentThread._on_new_message`, check if `self._chat_history`
is a `ChatHistoryReducer` with `auto_reduce=True`. If so, invoke `await
self._chat_history.add_message_async(new_message)` so the reduction
boundary triggers as designed.
- **Reducer propagation from KernelArguments:** In
`ChatCompletionAgent.get_response()`, `invoke()`, and `invoke_stream()`,
check if `arguments` contains an active `chat_history` reducer and pass
it into the constructor when initializing `ChatHistoryAgentThread`.
- **Pre-invocation reduction pass:** Before constructing the final
`chat_history` payload for `_inner_invoke()` / `_inner_invoke_stream()`,
check if the active thread contains a `ChatHistoryReducer` and call
`await thread.reduce()`.
- **Unit test suite:** Added unit tests verifying `ChatCompletionAgent`
respects both thread-injected and argument-injected reducers during
`get_response()`.
---
### 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
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
---------
Signed-off-by: sparkysolutions10-pixelmy <sparkysolutions10@gmail.com>
1 parent 7a7c92f commit 05b6569
2 files changed
Lines changed: 126 additions & 4 deletions
File tree
- python
- semantic_kernel/agents/chat_completion
- tests/unit/agents/chat_completion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
93 | 96 | | |
94 | 97 | | |
95 | 98 | | |
| |||
295 | 298 | | |
296 | 299 | | |
297 | 300 | | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
298 | 307 | | |
299 | 308 | | |
300 | 309 | | |
301 | | - | |
| 310 | + | |
302 | 311 | | |
303 | 312 | | |
304 | 313 | | |
305 | 314 | | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
306 | 318 | | |
307 | 319 | | |
308 | 320 | | |
| |||
349 | 361 | | |
350 | 362 | | |
351 | 363 | | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
352 | 370 | | |
353 | 371 | | |
354 | 372 | | |
355 | | - | |
| 373 | + | |
356 | 374 | | |
357 | 375 | | |
358 | 376 | | |
359 | 377 | | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
360 | 381 | | |
361 | 382 | | |
362 | 383 | | |
| |||
398 | 419 | | |
399 | 420 | | |
400 | 421 | | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
401 | 428 | | |
402 | 429 | | |
403 | 430 | | |
404 | | - | |
| 431 | + | |
405 | 432 | | |
406 | 433 | | |
407 | 434 | | |
408 | 435 | | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
409 | 439 | | |
410 | 440 | | |
411 | 441 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
469 | 469 | | |
470 | 470 | | |
471 | 471 | | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
0 commit comments