Skip to content

Commit

Permalink
Merge branch 'main' into chore/zengin-manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Oct 22, 2024
2 parents 99ac5cc + f414fcd commit 0a4fedd
Show file tree
Hide file tree
Showing 33 changed files with 3,455 additions and 361 deletions.
2 changes: 1 addition & 1 deletion dotnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ requirements and setup instructions.
3. [Running AI prompts from file](./notebooks/02-running-prompts-from-file.ipynb)
4. [Creating Semantic Functions at runtime (i.e. inline functions)](./notebooks/03-semantic-function-inline.ipynb)
5. [Using Kernel Arguments to Build a Chat Experience](./notebooks/04-kernel-arguments-chat.ipynb)
6. [Creating and Executing Plans](./notebooks/05-using-the-planner.ipynb)
6. [Introduction to the Planning/Function Calling](./notebooks/05-using-function-calling.ipynb)
7. [Building Memory with Embeddings](./notebooks/06-memory-and-embeddings.ipynb)
8. [Creating images with DALL-E 3](./notebooks/07-DALL-E-3.ipynb)
9. [Chatting with ChatGPT and Images](./notebooks/08-chatGPT-with-DALL-E-3.ipynb)
Expand Down
24 changes: 22 additions & 2 deletions dotnet/SK-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,22 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.InMemory.UnitTes
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GettingStartedWithTextSearch", "samples\GettingStartedWithTextSearch\GettingStartedWithTextSearch.csproj", "{16AFA226-E417-490D-9311-9F2099A1EEC8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VectorStoreRAG", "samples\Demos\VectorStoreRAG\VectorStoreRAG.csproj", "{28DFAF27-8FF3-4373-AAA4-2A6969C86246}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VectorStoreRAG", "samples\Demos\VectorStoreRAG\VectorStoreRAG.csproj", "{28DFAF27-8FF3-4373-AAA4-2A6969C86246}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Process.Runtime.Dapr", "src\Experimental\Process.Runtime.Dapr\Process.Runtime.Dapr.csproj", "{9D5B4B53-0E97-42D9-B37E-CD263B6A1892}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProcessWithDapr", "samples\Demos\ProcessWithDapr\ProcessWithDapr.csproj", "{95163AA2-1ED5-412A-990B-C40B81934BFD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProcessWithDapr", "samples\Demos\ProcessWithDapr\ProcessWithDapr.csproj", "{95163AA2-1ED5-412A-990B-C40B81934BFD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.MongoDB.UnitTests", "src\Connectors\Connectors.MongoDB.UnitTests\Connectors.MongoDB.UnitTests.csproj", "{6F591D05-5F7F-4211-9042-42D8BCE60415}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Memory", "Memory", "{077928EA-2C61-4667-82FC-6A5120B7AC45}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MongoDB", "MongoDB", "{AF7F68FD-ADB0-4941-90AE-88EAAB53BEEB}"
ProjectSection(SolutionItems) = preProject
src\InternalUtilities\connectors\Memory\MongoDB\MongoDBConstants.cs = src\InternalUtilities\connectors\Memory\MongoDB\MongoDBConstants.cs
src\InternalUtilities\connectors\Memory\MongoDB\MongoDBGenericDataModelMapper.cs = src\InternalUtilities\connectors\Memory\MongoDB\MongoDBGenericDataModelMapper.cs
src\InternalUtilities\connectors\Memory\MongoDB\MongoDBVectorStoreRecordMapper.cs = src\InternalUtilities\connectors\Memory\MongoDB\MongoDBVectorStoreRecordMapper.cs
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -992,6 +1003,12 @@ Global
{95163AA2-1ED5-412A-990B-C40B81934BFD}.Publish|Any CPU.Build.0 = Debug|Any CPU
{95163AA2-1ED5-412A-990B-C40B81934BFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95163AA2-1ED5-412A-990B-C40B81934BFD}.Release|Any CPU.Build.0 = Release|Any CPU
{6F591D05-5F7F-4211-9042-42D8BCE60415}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F591D05-5F7F-4211-9042-42D8BCE60415}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F591D05-5F7F-4211-9042-42D8BCE60415}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
{6F591D05-5F7F-4211-9042-42D8BCE60415}.Publish|Any CPU.Build.0 = Debug|Any CPU
{6F591D05-5F7F-4211-9042-42D8BCE60415}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F591D05-5F7F-4211-9042-42D8BCE60415}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1129,6 +1146,9 @@ Global
{28DFAF27-8FF3-4373-AAA4-2A6969C86246} = {5D4C0700-BBB5-418F-A7B2-F392B9A18263}
{9D5B4B53-0E97-42D9-B37E-CD263B6A1892} = {0D8C6358-5DAA-4EA6-A924-C268A9A21BC9}
{95163AA2-1ED5-412A-990B-C40B81934BFD} = {5D4C0700-BBB5-418F-A7B2-F392B9A18263}
{6F591D05-5F7F-4211-9042-42D8BCE60415} = {5A7028A7-4DDF-4E4F-84A9-37CE8F8D7E89}
{077928EA-2C61-4667-82FC-6A5120B7AC45} = {314A2705-0F70-44B6-8988-C6DF77BDFD42}
{AF7F68FD-ADB0-4941-90AE-88EAAB53BEEB} = {077928EA-2C61-4667-82FC-6A5120B7AC45}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBDC56A3-86AD-4323-AA0F-201E59123B83}
Expand Down
219 changes: 219 additions & 0 deletions dotnet/notebooks/05-using-function-calling.ipynb
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
}
Loading

0 comments on commit 0a4fedd

Please sign in to comment.