Skip to content
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

Python: ChatHistory Reduction #10102

Closed
markwallace-microsoft opened this issue Jan 7, 2025 · 4 comments · Fixed by #10190
Closed

Python: ChatHistory Reduction #10102

markwallace-microsoft opened this issue Jan 7, 2025 · 4 comments · Fixed by #10190
Assignees
Labels
Build Features planned for next Build conference python Pull requests for the Python Semantic Kernel SK-H2-Planning Issues tagged with this label are listed in SK H2 Planning loop

Comments

@markwallace-microsoft
Copy link
Member

Minimum is to create Python samples equivalent to what we have in .NET

@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code python Pull requests for the Python Semantic Kernel triage Build Features planned for next Build conference and removed .NET Issue or Pull requests regarding .NET code triage labels Jan 7, 2025
@github-actions github-actions bot changed the title ChatHistory Reduction .Net: ChatHistory Reduction Jan 7, 2025
@github-actions github-actions bot changed the title .Net: ChatHistory Reduction Python: ChatHistory Reduction Jan 7, 2025
@markwallace-microsoft markwallace-microsoft moved this to Backlog: Planned in Semantic Kernel Jan 7, 2025
@eavanvalkenburg
Copy link
Member

is this related to #6771 #6772 #6773 #6770?

@markwallace-microsoft markwallace-microsoft added the SK-H2-Planning Issues tagged with this label are listed in SK H2 Planning loop label Jan 13, 2025
@moonbox3
Copy link
Contributor

moonbox3 commented Jan 14, 2025

I've already got this underway for the agent framework based on #7969. Is this something separate?

@markwallace-microsoft
Copy link
Member Author

We currently have this Blog post https://devblogs.microsoft.com/semantic-kernel/managing-chat-history-for-large-language-models-llms/ which describes how to manage chat history and includes samples for .Net only. The purpose of this task is to extend this to Python also.
On the .Net the Agent support for managing chat history is internal at present and the samples don't require any SK changes so we don't have an official API. We may want to take advantage of the pattern in M.E.AI to support this officially in .Net i.e. create middleware implementations which support different chat history management strategies.

@moonbox3
Copy link
Contributor

Thank you. Very helpful.

github-merge-queue bot pushed a commit that referenced this issue Jan 23, 2025
### Motivation and Context

The SK Python framework has been missing the ability to configure a chat
history reducer of type `ChatHistoryTruncationReducer` and
`ChatHistorySummarizationReducer` which have existed in the .Net SK
Agent framework for some time.

The goal of this PR is to introduce the chat history reducers and allow
them for use for not only the agent framework, but also anything else
that uses a chat history (chat completion, for example). The
ChatHistoryReducer extends the ChatHistory class, and so it's simple to
include a reducer and logic to reduce messages as one manages the chat
history either in an agent framework setting or in a chat completion
setting.

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

This PR:
- Introduces the chat history reducer functionality in Python -- both
the `ChatHistoryTruncationReducer` and
`ChatHistorySummarizationReducer`.
- Add unit tests for code coverage.
- Adds a sample `Chat Completion History Reducer` to show how to
configure both reducers and what each parameter does.
- Add chat completion samples showing how to manage a chat history
reducer, including how to specify that function call content and
function result content should be part of the summarization payload.
- Updates the Agent SelectionStrategy, KernelFunctionSelectionStrategy
and KernelFunctionTermination strategy to use the reducer.
- Additionally updates the classes above to use a new `select_agent`
abstract method so that one can define an initial agent to run in a
particular scenario.
- Removes the deprecated `FunctionCallBehavior` class, and removes some
nasty circular dependencies that we had lurking in the code base for
some time. This `FunctionCallBehavior` has been marked with a
deprecation warning for 6+ months now. All samples and docs have moved
over to use `FunctionChoiceBehavior` - developers using
`FunctionCallBehavior` should have had enough time to switch.
- Closes #7969
- Closes #10102

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [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 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Build Features planned for next Build conference python Pull requests for the Python Semantic Kernel SK-H2-Planning Issues tagged with this label are listed in SK H2 Planning loop
Projects
Status: Backlog: Planned
Development

Successfully merging a pull request may close this issue.

4 participants