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: Introduce the chat history reducer #10190

Merged
merged 26 commits into from
Jan 23, 2025

Conversation

moonbox3
Copy link
Contributor

@moonbox3 moonbox3 commented Jan 15, 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.

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 Python Agents: ChatHistoryReducer #7969
  • Closes Python: ChatHistory Reduction #10102

Contribution Checklist

@moonbox3 moonbox3 self-assigned this Jan 15, 2025
@moonbox3 moonbox3 requested a review from a team as a code owner January 15, 2025 10:40
@markwallace-microsoft markwallace-microsoft added python Pull requests for the Python Semantic Kernel documentation labels Jan 15, 2025
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Jan 15, 2025

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
semantic_kernel/agents
   agent.py451176%48–59, 72–73
semantic_kernel/agents/chat_completion
   chat_completion_agent.py87199%82
semantic_kernel/agents/strategies/selection
   kernel_function_selection_strategy.py57493%62–65
   selection_strategy.py20290%41, 57
semantic_kernel/agents/strategies/termination
   kernel_function_termination_strategy.py47491%54–57
semantic_kernel/connectors/ai
   chat_completion_client_base.py124298%393, 403
semantic_kernel/connectors/ai/anthropic/services
   anthropic_chat_completion.py162895%159, 165, 178, 184, 188, 245–247, 380
   utils.py45393%67, 100–103
semantic_kernel/connectors/ai/azure_ai_inference/services
   azure_ai_inference_base.py44198%99
   azure_ai_inference_chat_completion.py110695%120–123, 132, 154, 178
semantic_kernel/connectors/ai/bedrock/services
   bedrock_chat_completion.py1351490%117, 139, 164, 168–171, 229, 247–266, 325
semantic_kernel/connectors/ai/bedrock/services/model_provider
   utils.py782074%70, 73, 104, 108–119, 136–154, 175–178
semantic_kernel/connectors/ai/google/google_ai/services
   google_ai_chat_completion.py118497%126, 153, 179, 181
semantic_kernel/connectors/ai/google/vertex_ai/services
   vertex_ai_chat_completion.py117497%123, 150, 176, 178
semantic_kernel/connectors/ai/mistral_ai/services
   mistral_ai_chat_completion.py1203868%121–124, 134, 149–152, 167, 183–187, 202–210, 227–235, 248–261, 267, 276–280, 325–328
semantic_kernel/connectors/ai/ollama/services
   ollama_chat_completion.py1373475%116, 141, 145–146, 156, 169, 186, 206–207, 211, 224–234, 245–247, 258–267, 279, 289–290, 312, 323–324, 350, 359–367
   utils.py472840%31, 46–54, 66–88, 100–104, 125–133
semantic_kernel/connectors/ai/open_ai/prompt_execution_settings
   open_ai_prompt_execution_settings.py84298%119, 133
semantic_kernel/connectors/ai/open_ai/services
   open_ai_chat_completion_base.py128795%71, 81, 102, 122, 143, 179, 320
semantic_kernel/contents
   function_call_content.py107199%196
semantic_kernel/contents/history_reducer
   chat_history_reducer_utils.py93990%164–165, 175–180, 193, 198–199
   chat_history_summarization_reducer.py90990%111, 113, 129–130, 155–156, 163, 171, 208
   chat_history_truncation_reducer.py34391%55–58, 74
TOTAL16950182489% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3020 4 💤 0 ❌ 0 🔥 1m 25s ⏱️

@moonbox3 moonbox3 added agents experimental Associated with an experimental feature labels Jan 15, 2025
Copy link
Member

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of questions raised, most importantly why this isn't part of the whole thing instead of part of agents!

@moonbox3 moonbox3 changed the title Python: Introduce the agent chat history reducer Python: Introduce the chat history reducer Jan 16, 2025
Copy link
Member

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple of small comments!

Copy link
Member

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only further improvement would be to test with the function call settings, but that can come later

@moonbox3
Copy link
Contributor Author

Only further improvement would be to test with the function call settings, but that can come later

What do you mean?

@moonbox3 moonbox3 enabled auto-merge January 23, 2025 20:59
@moonbox3 moonbox3 added this pull request to the merge queue Jan 23, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 23, 2025
@moonbox3 moonbox3 enabled auto-merge January 23, 2025 22:07
@moonbox3 moonbox3 added this pull request to the merge queue Jan 23, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 23, 2025
@moonbox3 moonbox3 added this pull request to the merge queue Jan 23, 2025
Merged via the queue into microsoft:main with commit 4d29656 Jan 23, 2025
25 checks passed
@moonbox3 moonbox3 deleted the py-chat-history-reduce branch January 23, 2025 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agents documentation experimental Associated with an experimental feature python Pull requests for the Python Semantic Kernel
Projects
Status: Sprint: Done
Development

Successfully merging this pull request may close these issues.

Python: ChatHistory Reduction Python Agents: ChatHistoryReducer
5 participants