{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "107377b0", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#r \"nuget: Microsoft.DotNet.Interactive.AIUtilities, 1.0.0-beta.23562.1\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#r \"nuget:Microsoft.DotNet.Interactive.AI, 1.0.0-beta.23604.2\"\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#!value --name key\n", "YOUR AZURE OPEN AI KEY" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#!value --name endpoint\n", "https://your-enpoint.openai.azure.com/" ] }, { "attachments": {}, "cell_type": "markdown", "id": "2d15adc1", "metadata": {}, "source": [ "# Use extension to connect to models" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: If your deployment names are different, you will need to change `--deployment` from `text-embedding-ada-002` and `gpt-35-turbo-16k` in the lines below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#!connect azure-openai --model-type TextEmbeddingGenerator --kernel-name knowledge --api-key @value:key --endpoint @value:endpoint --deployment text-embedding-ada-002" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#!connect azure-openai --model-type ImageGenerator --kernel-name image --api-key @value:key --endpoint @value:endpoint --deployment DALL-E" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#!connect azure-openai --model-type ChatCompletion --kernel-name chat --api-key @value:key --endpoint @value:endpoint --deployment gpt-35-turbo-16k --use-knowledge knowledge" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "System.Diagnostics.Debugger.Launch();" ] }, { "attachments": {}, "cell_type": "markdown", "id": "59c61aca", "metadata": {}, "source": [ "# 1. Hello..." ] }, { "attachments": {}, "cell_type": "markdown", "id": "c3a6e9e6", "metadata": {}, "source": [ "### ... DALL-E" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "image" }, "polyglot_notebook": { "kernelName": "image" } }, "outputs": [], "source": [ "A cat flying a red biplane, in the style of Hiyao Miyazaki." ] }, { "attachments": {}, "cell_type": "markdown", "id": "fbd06e06", "metadata": {}, "source": [ "#### *Points of interest* \n", "\n", "Extensions are able to provide custom visualizations. \n", "\n", "[Our code](https://github.com/jonsequitur/interactive/blob/semantic-kernel-kernel/src/Microsoft.DotNet.Interactive.OpenAI/SkiaUtils.cs)\n", "\n", "[SkiaSharp's code](https://github.com/mono/SkiaSharp/blob/main/source/SkiaSharp.DotNet.Interactive/SkiaSharpKernelExtension.cs)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "26618651", "metadata": {}, "source": [ "# 2. Skills" ] }, { "attachments": {}, "cell_type": "markdown", "id": "67f5129d", "metadata": {}, "source": [ "### Defining semantic functions for `Microsoft.SemanticKernel`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(skill)" }, "polyglot_notebook": { "kernelName": "chat(skill)" } }, "outputs": [], "source": [ "#!function -h" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(skill)" }, "polyglot_notebook": { "kernelName": "chat(skill)" } }, "outputs": [], "source": [ "#!function summarize --skill writer\n", "Summarize the following text. \n", "[BEGIN TEXT]\n", "{{$input}}\n", "[END TEXT]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(skill)" }, "polyglot_notebook": { "kernelName": "chat(skill)" } }, "outputs": [], "source": [ "#!function stylist --skill writer\n", "Rewrite the following text in the style of {{$person}}:\n", "[BEGIN TEXT]\n", "{{$input}}\n", "[END TEXT]\n", "Please write it in verse." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(text)" }, "polyglot_notebook": { "kernelName": "chat(text)" } }, "outputs": [], "source": [ "#!set --name person --value @input:\"Whose style would you like to summarize this in?\"\n", "#!use-skills function.writer.summarize function.writer.stylist\n", ".NET Interactive engine can be used many ways.\n", "* It takes messages, routes them to subkernels, and can format returned data.\n", "* Messages are initiated via a host (like Polyglot Notebooks).\n", "* Subkernels can be a bunch of different languages (including Mermaid).\n", "* Subkernels can share data.\n", "* Subkernels can be running in different processes and on different machines.\n", "* We'd love your ideas on how we can leverage this for developers.\n" ] }, { "attachments": {}, "cell_type": "markdown", "id": "b5354e0e", "metadata": {}, "source": [ "# 3. Kernels" ] }, { "attachments": {}, "cell_type": "markdown", "id": "565fe9f0", "metadata": {}, "source": [ "### Using multiple AI models together" ] }, { "cell_type": "code", "execution_count": null, "id": "fb3f1135", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "using Microsoft.DotNet.Interactive;\n", "using Microsoft.DotNet.Interactive.Commands;\n", "using Microsoft.DotNet.Interactive.Events;\n", "\n", "var prompts = new List();\n", "\n", "Kernel.Root.KernelEvents.Subscribe(e =>\n", "{\n", " if (e is ReturnValueProduced rvp &&\n", " e.Command.TargetKernelName.Contains(\"chat(text)\") &&\n", " rvp.FormattedValues.SingleOrDefault(v => v.MimeType == \"text/plain\") is { } plainTextValue)\n", " {\n", " prompts.Add(plainTextValue.Value);\n", " }\n", "});" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(skill)" }, "polyglot_notebook": { "kernelName": "chat(skill)" } }, "outputs": [], "source": [ "#!function make_image_prompt --skill image\n", "{{$input}}\n", "Describe an image of the text above in {{$linecount}} lines." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(skill)" }, "polyglot_notebook": { "kernelName": "chat(skill)" } }, "outputs": [], "source": [ "#!function safe --skill image\n", "{{$input}}\n", "Rewrite the text above to be ok to read with a 6 years old child." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(skill)" }, "polyglot_notebook": { "kernelName": "chat(skill)" } }, "outputs": [], "source": [ "#!function improve --skill image\n", "{{$input}}\n", "Turn the text above into a better and more detailed prompt for DALL-E." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(text)" }, "polyglot_notebook": { "kernelName": "chat(text)" } }, "outputs": [], "source": [ "#!set --name linecount --value 10\n", "#!use-skills function.image.make_image_prompt function.image.safe function.image.improve \n", "\n", "A cat flying a red biplane, in the style of Hiyao Miyazaki." ] }, { "cell_type": "code", "execution_count": null, "id": "52b8de58", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "using Microsoft.DotNet.Interactive;\n", "using Microsoft.DotNet.Interactive.AI;\n", "\n", "var dallEKernel = Kernel.Root.FindKernels(k => k is ImageGenerationKernel).Single(k => k.Name == \"image\");\n", "\n", "var lastPrompt = prompts.Last().Trim();\n", "$\"Sending this prompt to DALL-E: *{lastPrompt}*\".DisplayAs(\"text/markdown\");\n", "\n", "var result = await dallEKernel.SendAsync(new SubmitCode(lastPrompt));" ] }, { "attachments": {}, "cell_type": "markdown", "id": "8e27b42e", "metadata": {}, "source": [ "# 4. Bonus round" ] }, { "attachments": {}, "cell_type": "markdown", "id": "28f3e8ea", "metadata": {}, "source": [ "## `#!explain` redux" ] }, { "cell_type": "code", "execution_count": null, "id": "408bd3bb", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "using Microsoft.DotNet.Interactive;\n", "using Microsoft.DotNet.Interactive.Events;\n", "using Microsoft.DotNet.Interactive.Commands;\n", "using Microsoft.DotNet.Interactive.AI;\n", "using System.CommandLine;\n", "\n", "var explainMagicCommand = new Command(\"#!explain\");\n", "\n", "Kernel.Root.AddDirective(explainMagicCommand);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "explainMagicCommand.SetHandler(async _ =>\n", "{\n", " var context = KernelInvocationContext.Current;\n", " if (context.Command is SubmitCode submitCode)\n", " {\n", " var prompt = $\"\"\"\n", " {submitCode.Code.Replace(\"#!explain\", \"\")}\n", " Explain what the code above does, using dad jokes.\n", " \"\"\";\n", " \n", " await Kernel.Root.FindKernelByName(\"chat(text)\")\n", " .SendAsync(new SubmitCode(prompt));\n", "\n", " context.Complete(submitCode);\n", " }\n", "});\n" ] }, { "cell_type": "code", "execution_count": null, "id": "eef481d1", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#!explain\n", "foreach (var i in Enumerable.Range(1, 10))\n", "{\n", " if (i % 2 == 0)\n", " {\n", " Console.WriteLine(i);\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "id": "9f130b91", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "using Microsoft.DotNet.Interactive;\n", "using Microsoft.DotNet.Interactive.Events;\n", "using Microsoft.DotNet.Interactive.Commands;\n", "using Microsoft.DotNet.Interactive.AI;\n", "using System.CommandLine;\n", "\n", "var rewriteMagicCommand = new Command(\"#!rewrite\");\n", "\n", "Kernel.Root.AddDirective(rewriteMagicCommand);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "rewriteMagicCommand.SetHandler(async _ =>\n", "{\n", " var context = KernelInvocationContext.Current;\n", " if (context.Command is SubmitCode submitCode)\n", " {\n", " var prompt = $\"\"\"\n", " {submitCode.Code.Replace(\"#!rewrite\", \"\")}\n", " Rewrite the code above in python. just output the code, no markdown.\n", " \"\"\";\n", " \n", " await Kernel.Root.FindKernelByName(\"chat(text)\")\n", " .SendAsync(new SubmitCode(prompt));\n", "\n", " context.Complete(submitCode);\n", " }\n", "});\n" ] }, { "cell_type": "code", "execution_count": null, "id": "eb312acb", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#!rewrite\n", "foreach (var i in Enumerable.Range(1, 10))\n", "{\n", " if (i % 2 == 0)\n", " {\n", " Console.WriteLine(i);\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "id": "26fa1f36", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "using Microsoft.DotNet.Interactive;\n", "using Microsoft.DotNet.Interactive.Events;\n", "using Microsoft.DotNet.Interactive.Commands;\n", "using Microsoft.DotNet.Interactive.AI;\n", "using System.CommandLine;\n", "\n", "var unwindMagicCommand = new Command(\"#!unwind\");\n", "\n", "Kernel.Root.AddDirective(unwindMagicCommand);" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "unwindMagicCommand.SetHandler(async _ =>\n", "{\n", " var context = KernelInvocationContext.Current;\n", " if (context.Command is SubmitCode submitCode)\n", " {\n", " var prompt = $\"\"\"\n", " {submitCode.Code.Replace(\"#!unwind\", \"\")}\n", " Rewrite the code above without recursion. Use iterations instead of recursion. just output the code, no markdown.\n", " \"\"\";\n", " \n", " await Kernel.Root.FindKernelByName(\"chat(text)\")\n", " .SendAsync(new SubmitCode(prompt));\n", "\n", " context.Complete(submitCode);\n", " } \n", "});" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#!unwind\n", "int fibonacci(int x){\n", " switch (x){\n", " case 0: \n", " return 1;\n", " case 1: \n", " return 1;\n", " default:\n", " return fibonacci(x-1) + fibonacci(x-2);\n", "\n", " }\n", "}" ] }, { "attachments": {}, "cell_type": "markdown", "id": "41148c9f", "metadata": {}, "source": [ "## Misc." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(text)" }, "polyglot_notebook": { "kernelName": "chat(text)" } }, "outputs": [], "source": [ "#!set --name person --value @input:\"Enter the name of the person you'd like to summarize this text.\"\n", "#!use-skills function.writer.stylist\n", "\n", "What is .NET Interactive?\n", "\n", "It takes messages, routes them to subkernels, and can format returned data\n", "Messages are initiated via a host\n", "Subkernels can be a bunch of different languages (including Mermaid)\n", "Subkernels can share data\n", "Subkernels can be running in different processes or on different machines\n", "We’d love your ideas on how we can leverage this for developers\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(text)" }, "polyglot_notebook": { "kernelName": "chat(text)" } }, "outputs": [], "source": [ "#!function explainer --skill writer\n", "{{$input}} \n", "\n", "Summarize the text above in the style of Randall Munroe's Thing Explainer, using only words that are amongst the most common 500 english words\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(skill)" }, "polyglot_notebook": { "kernelName": "chat(skill)" } }, "outputs": [], "source": [ "#!function detect_intent \n", "Intent detection examples start here:\n", "Intent: Unknown:\n", " * save\n", " * send\n", "Intent: Help:\n", " * How can you help me?\n", " * What can you do?\n", "Intent: TellAJoke:\n", " * What's the best joke you know?\n", " * Do you know any joke?\n", " * Tell me a joke\n", " * Tell me something funny\n", "Intent: WhatTimeIsIt\n", " * tell me the time please\n", " * time and date please\n", " * what time is it?\n", " * what's the current time?\n", " * is it lunch time yet?\n", " * is it dinner time?\n", "== end of examples ==\n", "Input: {{$input}}\n", "Intent:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(text)" }, "polyglot_notebook": { "kernelName": "chat(text)" } }, "outputs": [], "source": [ "#!use-skills function._GLOBAL_FUNCTIONS_.detect_intent\n", "\n", "I could really use some help understanding intent detection." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "chat(text)" }, "polyglot_notebook": { "kernelName": "chat(text)" } }, "outputs": [], "source": [ "#!use-skills function.writer.explainer\n", "\n", "Since .NET Interactive is capable of running as a kernel for notebooks, it enables a polyglot (multi-language) notebook experience. When using the .NET Interactive kernel, you can use different languages from one cell to the next, share variables between languages, and dynamically connect new languages and remote kernels within a notebook. There's no need to install different Jupyter kernels, use wrapper libraries, or install different tools to get the best experience for the language of your choice. You can always use the best language for the job and seamlessly transition between different stages of your workflow, all within one notebook.\n", "\n", "For the best experience when working with multi-language notebooks, we recommend installing the Polyglot Notebooks extension for Visual Studio Code. While the full .NET Interactive feature set is available in Jupyter, many features are only usable via code, whereas the Polyglot Notebooks extension provides additional features including a language/kernel picker for each cell, enhanced language services, a multi-kernel variable viewer, and more." ] } ], "metadata": { "kernelInfo": { "defaultKernelName": null, "items": [ { "aliases": [ "c#", "cs" ], "languageName": "C#", "name": "csharp" }, { "aliases": [ "f#", "fs" ], "languageName": "F#", "name": "fsharp" }, { "languageName": "HTML", "name": "html" }, { "languageName": "HTTP", "name": "http" }, { "aliases": [ "js" ], "languageName": "JavaScript", "name": "javascript" }, { "languageName": "KQL", "name": "kql" }, { "languageName": "Mermaid", "name": "mermaid" }, { "aliases": [ "powershell" ], "languageName": "PowerShell", "name": "pwsh" }, { "languageName": "SQL", "name": "sql" }, { "name": "value" } ] }, "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "name": "polyglot-notebook" }, "polyglot_notebook": { "kernelInfo": { "defaultKernelName": "csharp", "items": [ { "aliases": [], "languageName": "text", "name": "chat" }, { "aliases": [], "languageName": "semantic-kernel.skprompt", "name": "chat(skill)" }, { "aliases": [], "languageName": "text", "name": "chat(text)" }, { "aliases": [], "name": "csharp" }, { "aliases": [], "languageName": "text", "name": "image" }, { "aliases": [], "languageName": "text", "name": "knowledge" }, { "aliases": [], "languageName": "SQL", "name": "knowledge(duckDbVectorRAW)" }, { "aliases": [], "languageName": "text", "name": "openai-old(image)" }, { "aliases": [], "languageName": "SQL", "name": "openai(duckDb)" }, { "aliases": [], "languageName": "text", "name": "openai(image)" }, { "aliases": [], "languageName": "text", "name": "openai(text-gpt)" }, { "aliases": [], "languageName": "semantic-kernel.skprompt", "name": "openaicode(skill)" }, { "aliases": [], "languageName": "text", "name": "openaicode(text)" }, { "aliases": [], "languageName": "text", "name": "textCompletion" }, { "aliases": [], "languageName": "semantic-kernel.skprompt", "name": "textCompletion(skill)" } ] } } }, "nbformat": 4, "nbformat_minor": 5 }