Skip to content

.Net: Added an example how to access ChatHistory in SK function#9527

Merged
dmytrostruk merged 5 commits intomicrosoft:mainfrom
dmytrostruk:chat-history-in-function
Nov 5, 2024
Merged

.Net: Added an example how to access ChatHistory in SK function#9527
dmytrostruk merged 5 commits intomicrosoft:mainfrom
dmytrostruk:chat-history-in-function

Conversation

@dmytrostruk
Copy link
Member

Motivation and Context

Resolves: #7263

This PR contains an example how to access ChatHistory in SK function using Kernel.Data property or KernelArguments and IAutoFunctionInvocationFilter.

This scenario can be useful with auto function calling, when logic in SK function depends on results from previous messages in the same chat history.

Contribution Checklist

@dmytrostruk dmytrostruk self-assigned this Nov 4, 2024
@dmytrostruk dmytrostruk requested a review from a team as a code owner November 4, 2024 20:37
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code documentation labels Nov 4, 2024
Copy link
Member

@rogerbarreto rogerbarreto left a comment

Choose a reason for hiding this comment

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

Although today being possible to do with current demonstration I would prefer to see this implemented as supported scenario in here:

Where if you add ChatHistory as the argument type of a function it is properly injected in the function.

@dmytrostruk
Copy link
Member Author

Although today being possible to do with current demonstration I would prefer to see this implemented as supported scenario in here:
Where if you add ChatHistory as the argument type of a function it is properly injected in the function.

@rogerbarreto Thanks for feedback! Yes, I agree that such approach would work in the best way for end users, and I've investigated it in more detail. It appeared that in order to support that, we will have to add ChatHistory parameter to the abstraction in places where it doesn't look like it belongs there.

For example, we will need to update kernel.InvokeAsync(function, arguments, cancellationToken) to kernel.InvokeAsync(function, arguments, chatHistory, cancellationToken), which I'm not sure is a good approach, since a function may be invoked outside of chat completion context. Even adding an optional ChatHistory dependency to KernelFunction looks like an unnecessary high coupling.

I was also thinking about adding ChatHistory property to KernelArguments class, but again, I'm not sure if KernelArguments should be aware of any chat-related models in compile time.

Taking into account that today it's possible to pass ChatHistory through KernelArguments in runtime as one of the dictionary values, I think we can keep this as a solution for now, and we can think about improving it if we will see some problems associated with it.

Hope that makes sense!

@dmytrostruk dmytrostruk added this pull request to the merge queue Nov 5, 2024
Merged via the queue into microsoft:main with commit ca4badf Nov 5, 2024
@dmytrostruk dmytrostruk deleted the chat-history-in-function branch November 5, 2024 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation .NET Issue or Pull requests regarding .NET code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.Net: Access to Chat history in semantic kernel function

4 participants