Add Automatic Context Summarization to ClientSession #1175
Closed
+432
−0
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.
Summary
Adds the
ClientSessionSummarizing
class, extendingClientSession
to provide automatic summarization of conversation context, preventing token overflow in long-running sessions. Introduces precise token counting with thetiktoken
library and provides flexible configuration options.Motivation and Context
In extended conversations, maintaining large message histories can lead to exceeding token limits, resulting in potential session interruptions or model failures. This feature ensures that conversations are automatically summarized once a defined token threshold is reached, ensuring smooth and uninterrupted operation.
How Has This Been Tested?
Unit Tests: Added 7 unit tests covering scenarios like:
max_tokens
,summarize_threshold
,summary_prompt
)Local Testing: All tests have passed successfully in the local environment with:
Breaking Changes
No breaking changes. Users will not be forced to update existing configurations or code.
However, a new optional dependency (
tiktoken
) has been added to enable precise token management.Types of changes
Checklist
Additional context
New Dependency:
tiktoken
for accurate token counting, ensuring session stability.Configurable Parameters:
max_tokens
(default: 4000 tokens)summarize_threshold
(default: 0.8 – 80%)summary_prompt
(default provided, user customizable)This implementation is production-ready and provides flexibility and robust performance for users managing extended interactions with language models.