forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Updated planner notebook to use function calling (microsoft#9372)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Resolves: microsoft#9306 Since function calling is the new recommended way for planning capabilities, this PR contains a change to update the planner notebook to use function calling. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
- Loading branch information
1 parent
01c288a
commit f414fcd
Showing
4 changed files
with
221 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Introduction to the Function Calling\n", | ||
"\n", | ||
"The most powerful feature of chat completion is the ability to call functions from the model. This allows you to create a chat bot that can interact with your existing code, making it possible to automate business processes, create code snippets, and more.\n", | ||
"\n", | ||
"With Semantic Kernel, we simplify the process of using function calling by automatically describing your functions and their parameters to the model and then handling the back-and-forth communication between the model and your code.\n", | ||
"\n", | ||
"Read more about it [here](https://learn.microsoft.com/en-us/semantic-kernel/concepts/ai-services/chat-completion/function-calling)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"dotnet_interactive": { | ||
"language": "csharp" | ||
}, | ||
"polyglot_notebook": { | ||
"kernelName": "csharp" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"#r \"nuget: Microsoft.SemanticKernel, 1.23.0\"\n", | ||
"\n", | ||
"#!import config/Settings.cs\n", | ||
"#!import config/Utils.cs\n", | ||
"\n", | ||
"using Microsoft.SemanticKernel;\n", | ||
"using Microsoft.SemanticKernel.Connectors.OpenAI;\n", | ||
"using Kernel = Microsoft.SemanticKernel.Kernel;\n", | ||
"\n", | ||
"var builder = Kernel.CreateBuilder();\n", | ||
"\n", | ||
"// Configure AI backend used by the kernel\n", | ||
"var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = Settings.LoadFromFile();\n", | ||
"\n", | ||
"if (useAzureOpenAI)\n", | ||
" builder.AddAzureOpenAIChatCompletion(model, azureEndpoint, apiKey);\n", | ||
"else\n", | ||
" builder.AddOpenAIChatCompletion(model, apiKey, orgId);\n", | ||
"\n", | ||
"var kernel = builder.Build();" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Setting Up Execution Settings" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Using `FunctionChoiceBehavior.Auto()` will enable automatic function calling. There are also other options like `Required` or `None` which allow to control function calling behavior. More information about it can be found [here](https://learn.microsoft.com/en-gb/semantic-kernel/concepts/ai-services/chat-completion/function-calling/function-choice-behaviors?pivots=programming-language-csharp)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": { | ||
"dotnet_interactive": { | ||
"language": "csharp" | ||
}, | ||
"polyglot_notebook": { | ||
"kernelName": "csharp" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"#pragma warning disable SKEXP0001\n", | ||
"\n", | ||
"OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new() \n", | ||
"{\n", | ||
" FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()\n", | ||
"};" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Providing plugins to the Kernel\n", | ||
"Function calling needs an information about available plugins/functions. Here we'll import the `SummarizePlugin` and `WriterPlugin` we have defined on disk." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"metadata": { | ||
"dotnet_interactive": { | ||
"language": "csharp" | ||
}, | ||
"polyglot_notebook": { | ||
"kernelName": "csharp" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"var pluginsDirectory = Path.Combine(System.IO.Directory.GetCurrentDirectory(), \"..\", \"..\", \"prompt_template_samples\");\n", | ||
"\n", | ||
"kernel.ImportPluginFromPromptDirectory(Path.Combine(pluginsDirectory, \"SummarizePlugin\"));\n", | ||
"kernel.ImportPluginFromPromptDirectory(Path.Combine(pluginsDirectory, \"WriterPlugin\"));" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Define your ASK. What do you want the Kernel to do?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"metadata": { | ||
"dotnet_interactive": { | ||
"language": "csharp" | ||
}, | ||
"polyglot_notebook": { | ||
"kernelName": "csharp" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"var ask = \"Tomorrow is Valentine's day. I need to come up with a few date ideas. My significant other likes poems so write them in the form of a poem.\";" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Since we imported available plugins to Kernel and defined the ask, we can now invoke a prompt with all the provided information. \n", | ||
"\n", | ||
"We can run function calling with Kernel, if we are interested in result only." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"dotnet_interactive": { | ||
"language": "csharp" | ||
}, | ||
"polyglot_notebook": { | ||
"kernelName": "csharp" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"var result = await kernel.InvokePromptAsync(ask, new(openAIPromptExecutionSettings));\n", | ||
"\n", | ||
"Console.WriteLine(result);" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"But we can also run it with `IChatCompletionService` to have an access to `ChatHistory` object, which allows us to see which functions were called as part of a function calling process. Note that passing a Kernel as a parameter to `GetChatMessageContentAsync` method is required, since Kernel holds an information about available plugins." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"using Microsoft.SemanticKernel.ChatCompletion;\n", | ||
"\n", | ||
"var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();\n", | ||
"\n", | ||
"var chatHistory = new ChatHistory();\n", | ||
"\n", | ||
"chatHistory.AddUserMessage(ask);\n", | ||
"\n", | ||
"var chatCompletionResult = await chatCompletionService.GetChatMessageContentAsync(chatHistory, openAIPromptExecutionSettings, kernel);\n", | ||
"\n", | ||
"Console.WriteLine($\"Result: {chatCompletionResult}\\n\");\n", | ||
"Console.WriteLine($\"Chat history: {JsonSerializer.Serialize(chatHistory)}\\n\");" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": ".NET (C#)", | ||
"language": "C#", | ||
"name": ".net-csharp" | ||
}, | ||
"language_info": { | ||
"name": "polyglot-notebook" | ||
}, | ||
"polyglot_notebook": { | ||
"kernelInfo": { | ||
"defaultKernelName": "csharp", | ||
"items": [ | ||
{ | ||
"aliases": [], | ||
"name": "csharp" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.