Semantic Functions with Microsoft.Extensions.AI #9825
-
I've been pouring over the Microsoft.Extensions.AI announcements along with reviewing my understanding of Semantic Kernel. Here's what I'm trying to figure out: Is there an intention to make Semantic Functions (e.g. prompt based functions, as opposed to native functions) work with IChatClient? As best I can tell so far, I can do something like the following: var songSuggesterFunction = kernel.CreateFunctionFromPrompt(
promptTemplate: @"Based on the user's recently played music:
{{$recentlyPlayedSongs}}
recommend a song to the user from the music library:
{{$musicLibrary}}",
functionName: "SuggestSong",
description: "Recommend a song from the library"
).AsAIFunction();
var chatResponse = await chatClient.CompleteAsync("Pick a song for me.", new ChatOptions
{
ToolMode = ChatToolMode.Auto,
Tools = [songSuggesterFunction, /* include other functions, native or otherwise, here */]
}); To err on the side of caution I have used the same OllamaChatClient for both the Kernel and the chatClient (as IChatClient). Just curious if I'm on the wrong track or if there is a cleaner/clearer intention of where this might go in the coming months. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The distinction between "semantic functions" and "native functions" is antiquated. In both cases, you're just running some code; it just happens in the former case that the code you're running is hardcoded to perform a particular action. As you've discovered, you can use any KernelFunction, regardless of how it was created or what operation it invokes, as an AIFunction via the AsAIFunction method. You could also create your own wrapper, e.g. AIFunctionFactory.Create(() => kernelFunction.InvokeAsync(...), ...). In all of these cases, there's just a delegate being invoked. |
Beta Was this translation helpful? Give feedback.
The distinction between "semantic functions" and "native functions" is antiquated. In both cases, you're just running some code; it just happens in the former case that the code you're running is hardcoded to perform a particular action.
As you've discovered, you can use any KernelFunction, regardless of how it was created or what operation it invokes, as an AIFunction via the AsAIFunction method. You could also create your own wrapper, e.g. AIFunctionFactory.Create(() => kernelFunction.InvokeAsync(...), ...). In all of these cases, there's just a delegate being invoked.