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

.Net: Added IChatHistoryReducer for ChatCompletion with AzureOpenAI and OpenAI #8894

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

sandrohanea
Copy link
Member

Motivation and Context

When using Auto-Invocation for Tools with OpenAI and AzureOpenAI, there is no way to intercept and reduce the chat history after tools are being invoked.

This is needed as some tools might return long responses and we might want to execute different prompt compression / optimization based on the tool result.

Note: AutoFunctionInvocationFilters cannot be used as they are invoked only before the result of the tools is added to the chat history.

The alternative to this approach would be to intercept the http call to Open AI and alter the request directly but that will cause multiple allocation, serializations and deserializations and won't scale to other Connectors.

Description

This change is extracting IChatHistoryReducer from Agents.Core project and moving it to SK.Abstrations.AI.ChatCompletions.

IChatHistoryReducer was renamed to IAgentChatHistoryReducer in order to avoid confusion as it requires 2 more methods: Equals and GetHashCode that are used to compute the agent channel key. It now inherits IChatHistoryReducer.

Added tests to ensure that a DI registered reducer is used in the ChatCompletionService of both OpenAI and AzureOpenAI connectors.

Important notice: the reducer is not mutating the ChatHistory, as it might be that new tool invocation or chat turns will require different reducing logic. (e.g. context-aware summarization)

Contribution Checklist

@sandrohanea sandrohanea requested a review from a team as a code owner September 18, 2024 13:53
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel kernel.core labels Sep 18, 2024
@github-actions github-actions bot changed the title Added IChatHistoryReducer for ChatCompletion with AzureOpenAI and OpenAI .Net: Added IChatHistoryReducer for ChatCompletion with AzureOpenAI and OpenAI Sep 18, 2024
@crickman
Copy link
Contributor

Would it make sense to provide both implementations of the reducer in the core framework:

  • Truncation
  • Summarization

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernel.core kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code
Projects
Status: Community PRs
Development

Successfully merging this pull request may close these issues.

4 participants