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

Closed

Conversation

sandrohanea
Copy link

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

@markwallace-microsoft
Copy link
Member

Hi @sandrohanea thanks for creating this PR. The team met to discuss and a few things came up

  1. We currently mutate ChatHistory when automatic function calling is enabled. A function calling loop can be long and have many round trips to the model and truncating the ChatHistory could have undesired side effects. In general the consensus was that we don't want to chat history reducer to be called by the chat completion service.
  2. The code shows how to use the reducer with Dependency Injection but we also need to handle non DI cases.

The consensus was that developers have control over the ChatHistory that they send to the chat completion service so they can reduce it as necessary and also decide which messages from the response get added back into the ChatHistory. We're going to work on some samples (during the current sprint) for this we will share with you once we have them.

@sandrohanea
Copy link
Author

Closing this PR as it's not currently aligning with SK plan for abstractions.

@qihangnet
Copy link

@markwallace-microsoft Can the Reducer implementation in #7570 be moved to Core? I believe it is useful not just for the Agent.

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
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants