{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# How to run a semantic plugins from file\n", "Now that you're familiar with Kernel basics, let's see how the kernel allows you to run Semantic Plugins and Semantic Functions stored on disk. \n", "\n", "A Semantic Plugin is a collection of Semantic Functions, where each function is defined with natural language that can be provided with a text file. \n", "\n", "Refer to our [glossary](../../docs/GLOSSARY.md) for an in-depth guide to the terms.\n", "\n", "The repository includes some examples under the [samples](https://github.com/microsoft/semantic-kernel/tree/main/samples) folder.\n", "\n", "For instance, [this](../../samples/plugins/FunPlugin/Joke/skprompt.txt) is the **Joke function** part of the **FunPlugin plugin**:" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "WRITE EXACTLY ONE JOKE or HUMOROUS STORY ABOUT THE TOPIC BELOW.\n", "JOKE MUST BE:\n", "- G RATED\n", "- WORKPLACE/FAMILY SAFE\n", "NO SEXISM, RACISM OR OTHER BIAS/BIGOTRY.\n", "BE CREATIVE AND FUNNY. I WANT TO LAUGH.\n", "+++++\n", "{{$input}}\n", "+++++\n", "```" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Note the special **`{{$input}}`** token, which is a variable that is automatically passed when invoking the function, commonly referred to as a \"function parameter\". \n", "\n", "We'll explore later how functions can accept multiple variables, as well as invoke other functions." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "\n", "In the same folder you'll notice a second [config.json](../../samples/plugins/FunPlugin/Joke/config.json) file. The file is optional, and is used to set some parameters for large language models like Temperature, TopP, Stop Sequences, etc.\n", "\n", "```\n", "{\n", " \"schema\": 1,\n", " \"description\": \"Generate a funny joke\",\n", " \"models\": [\n", " {\n", " \"max_tokens\": 500,\n", " \"temperature\": 0.5,\n", " \"top_p\": 0.5\n", " }\n", " ]\n", "}\n", "```" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Given a semantic function defined by these files, this is how to load and use a file based semantic function.\n", "\n", "Configure and create the kernel, as usual, loading also the AI backend settings defined in the [Setup notebook](0-AI-settings.ipynb):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#r \"nuget: Microsoft.SemanticKernel, 1.0.0-beta1\"\n", "\n", "#!import config/Settings.cs\n", "\n", "using Microsoft.SemanticKernel;\n", "\n", "var builder = new KernelBuilder();\n", "\n", "// Configure AI backend used by the kernel\n", "var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = Settings.LoadFromFile();\n", "if (useAzureOpenAI)\n", " builder.WithAzureChatCompletionService(model, azureEndpoint, apiKey);\n", "else\n", " builder.WithOpenAIChatCompletionService(model, apiKey, orgId);\n", "\n", "IKernel kernel = builder.Build();" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Import the plugin and all its functions:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "// note: using plugins from the repo\n", "var pluginsDirectory = Path.Combine(System.IO.Directory.GetCurrentDirectory(), \"..\", \"..\", \"samples\", \"plugins\");\n", "\n", "var funPluginFunctions = kernel.ImportSemanticFunctionsFromDirectory(pluginsDirectory, \"FunPlugin\");" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "How to use the plugin functions, e.g. generate a joke about \"*time travel to dinosaur age*\":" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "var result = await kernel.RunAsync(\"time travel to dinosaur age\", funPluginFunctions[\"Joke\"]);\n", "var resultString = result.GetValue<string>();\n", "\n", "Console.WriteLine(resultString);" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Great, now that you know how to load a plugin from disk, let's show how you can [create and run a semantic function inline.](./03-semantic-function-inline.ipynb)" ] } ], "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 }