Skip to content

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

Merged
Dmytro Struk (dmytrostruk) merged 5 commits into
microsoft:mainfrom
dmytrostruk:chat-history-in-function
Nov 5, 2024
Merged

Dmytro Struk (dmytrostruk) merged 5 commits into
microsoft:mainfrom
dmytrostruk:chat-history-in-function

Conversation

@dmytrostruk

Copy link
Copy Markdown
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

Copy link
Copy Markdown
Member

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
Copy Markdown
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.

Roger Barreto (@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
Dmytro Struk (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
Dmytro Struk (dmytrostruk) deleted the chat-history-in-function branch November 5, 2024 19:12

This branch was previously deployed

1 inactive deployment
integration — a5a45ca0 Deployed Nov 5, 2024 by dmytrostruk
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