|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Comparing text using embeddings" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "## Installation\n", |
| 15 | + "Install the Azure Open AI SDK using the below command." |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": null, |
| 21 | + "metadata": { |
| 22 | + "dotnet_interactive": { |
| 23 | + "language": "csharp" |
| 24 | + }, |
| 25 | + "polyglot_notebook": { |
| 26 | + "kernelName": "csharp" |
| 27 | + }, |
| 28 | + "vscode": { |
| 29 | + "languageId": "polyglot-notebook" |
| 30 | + } |
| 31 | + }, |
| 32 | + "outputs": [], |
| 33 | + "source": [ |
| 34 | + "#r \"nuget: Azure.AI.OpenAI, 1.0.0-beta.14\"\n", |
| 35 | + "#r \"nuget:Microsoft.DotNet.Interactive.AIUtilities, 1.0.0-beta.24129.1\"" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 4, |
| 41 | + "metadata": { |
| 42 | + "dotnet_interactive": { |
| 43 | + "language": "csharp" |
| 44 | + }, |
| 45 | + "polyglot_notebook": { |
| 46 | + "kernelName": "csharp" |
| 47 | + }, |
| 48 | + "vscode": { |
| 49 | + "languageId": "polyglot-notebook" |
| 50 | + } |
| 51 | + }, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "using Microsoft.DotNet.Interactive;" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "markdown", |
| 59 | + "metadata": {}, |
| 60 | + "source": [ |
| 61 | + "## Run this cell, it will prompt you for the apiKey, endPoint, and embedding deployment" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": 8, |
| 67 | + "metadata": { |
| 68 | + "dotnet_interactive": { |
| 69 | + "language": "csharp" |
| 70 | + }, |
| 71 | + "polyglot_notebook": { |
| 72 | + "kernelName": "csharp" |
| 73 | + }, |
| 74 | + "vscode": { |
| 75 | + "languageId": "polyglot-notebook" |
| 76 | + } |
| 77 | + }, |
| 78 | + "outputs": [], |
| 79 | + "source": [ |
| 80 | + "var azureOpenAIKey = await Kernel.GetPasswordAsync(\"Provide your OPEN_AI_KEY\");\n", |
| 81 | + "\n", |
| 82 | + "// Your endpoint should look like the following https://YOUR_OPEN_AI_RESOURCE_NAME.openai.azure.com/\n", |
| 83 | + "var azureOpenAIEndpoint = await Kernel.GetInputAsync(\"Provide the OPEN_AI_ENDPOINT\");\n", |
| 84 | + "\n", |
| 85 | + "// Enter the deployment name you chose when you deployed the model.\n", |
| 86 | + "var deployment = await Kernel.GetInputAsync(\"Provide embedding deployment name\");" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "metadata": {}, |
| 92 | + "source": [ |
| 93 | + "### Import namesapaces and create an instance of `OpenAiClient` using the `azureOpenAIEndpoint` and the `azureOpenAIKey`" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "code", |
| 98 | + "execution_count": 7, |
| 99 | + "metadata": { |
| 100 | + "dotnet_interactive": { |
| 101 | + "language": "csharp" |
| 102 | + }, |
| 103 | + "polyglot_notebook": { |
| 104 | + "kernelName": "csharp" |
| 105 | + }, |
| 106 | + "vscode": { |
| 107 | + "languageId": "polyglot-notebook" |
| 108 | + } |
| 109 | + }, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "using Azure;\n", |
| 113 | + "using Azure.AI.OpenAI;" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": 10, |
| 119 | + "metadata": { |
| 120 | + "dotnet_interactive": { |
| 121 | + "language": "csharp" |
| 122 | + }, |
| 123 | + "polyglot_notebook": { |
| 124 | + "kernelName": "csharp" |
| 125 | + }, |
| 126 | + "vscode": { |
| 127 | + "languageId": "polyglot-notebook" |
| 128 | + } |
| 129 | + }, |
| 130 | + "outputs": [], |
| 131 | + "source": [ |
| 132 | + "OpenAIClient client = new (new Uri(azureOpenAIEndpoint), new AzureKeyCredential(azureOpenAIKey.GetClearTextPassword()));" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "### Create some text" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": 11, |
| 145 | + "metadata": { |
| 146 | + "dotnet_interactive": { |
| 147 | + "language": "csharp" |
| 148 | + }, |
| 149 | + "polyglot_notebook": { |
| 150 | + "kernelName": "csharp" |
| 151 | + }, |
| 152 | + "vscode": { |
| 153 | + "languageId": "polyglot-notebook" |
| 154 | + } |
| 155 | + }, |
| 156 | + "outputs": [], |
| 157 | + "source": [ |
| 158 | + "var firstSentence = \"The quick brown fox jumps over the lazy dog\";\n", |
| 159 | + "var secondSentence = \"The quick fox jumps over the black dog\";" |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "markdown", |
| 164 | + "metadata": {}, |
| 165 | + "source": [ |
| 166 | + "### Create text embeddings using the `deployment`" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": 14, |
| 172 | + "metadata": { |
| 173 | + "dotnet_interactive": { |
| 174 | + "language": "csharp" |
| 175 | + }, |
| 176 | + "polyglot_notebook": { |
| 177 | + "kernelName": "csharp" |
| 178 | + }, |
| 179 | + "vscode": { |
| 180 | + "languageId": "polyglot-notebook" |
| 181 | + } |
| 182 | + }, |
| 183 | + "outputs": [], |
| 184 | + "source": [ |
| 185 | + "var firstEmbeddings = (await client.GetEmbeddingsAsync(new EmbeddingsOptions(deployment, new []{ firstSentence }))).Value.Data[0].Embedding.ToArray();\n", |
| 186 | + "var secondEmbeddings = (await client.GetEmbeddingsAsync(new EmbeddingsOptions(deployment, new []{ secondSentence }))).Value.Data[0].Embedding.ToArray();" |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "markdown", |
| 191 | + "metadata": {}, |
| 192 | + "source": [ |
| 193 | + "# calculate Cosine Similarity" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "execution_count": 16, |
| 199 | + "metadata": { |
| 200 | + "dotnet_interactive": { |
| 201 | + "language": "csharp" |
| 202 | + }, |
| 203 | + "polyglot_notebook": { |
| 204 | + "kernelName": "csharp" |
| 205 | + }, |
| 206 | + "vscode": { |
| 207 | + "languageId": "polyglot-notebook" |
| 208 | + } |
| 209 | + }, |
| 210 | + "outputs": [ |
| 211 | + { |
| 212 | + "data": { |
| 213 | + "text/html": [ |
| 214 | + "<div class=\"dni-plaintext\"><pre>0.948887</pre></div><style>\r\n", |
| 215 | + ".dni-code-hint {\r\n", |
| 216 | + " font-style: italic;\r\n", |
| 217 | + " overflow: hidden;\r\n", |
| 218 | + " white-space: nowrap;\r\n", |
| 219 | + "}\r\n", |
| 220 | + ".dni-treeview {\r\n", |
| 221 | + " white-space: nowrap;\r\n", |
| 222 | + "}\r\n", |
| 223 | + ".dni-treeview td {\r\n", |
| 224 | + " vertical-align: top;\r\n", |
| 225 | + " text-align: start;\r\n", |
| 226 | + "}\r\n", |
| 227 | + "details.dni-treeview {\r\n", |
| 228 | + " padding-left: 1em;\r\n", |
| 229 | + "}\r\n", |
| 230 | + "table td {\r\n", |
| 231 | + " text-align: start;\r\n", |
| 232 | + "}\r\n", |
| 233 | + "table tr { \r\n", |
| 234 | + " vertical-align: top; \r\n", |
| 235 | + " margin: 0em 0px;\r\n", |
| 236 | + "}\r\n", |
| 237 | + "table tr td pre \r\n", |
| 238 | + "{ \r\n", |
| 239 | + " vertical-align: top !important; \r\n", |
| 240 | + " margin: 0em 0px !important;\r\n", |
| 241 | + "} \r\n", |
| 242 | + "table th {\r\n", |
| 243 | + " text-align: start;\r\n", |
| 244 | + "}\r\n", |
| 245 | + "</style>" |
| 246 | + ] |
| 247 | + }, |
| 248 | + "metadata": {}, |
| 249 | + "output_type": "display_data" |
| 250 | + } |
| 251 | + ], |
| 252 | + "source": [ |
| 253 | + "using Microsoft.DotNet.Interactive.AIUtilities;\n", |
| 254 | + "\n", |
| 255 | + "var similarityComparer = new CosineSimilarityComparer<float[]>(f => f);\n", |
| 256 | + "var similarity = similarityComparer.Score(firstEmbeddings, secondEmbeddings);\n", |
| 257 | + "similarity.Display();" |
| 258 | + ] |
| 259 | + } |
| 260 | + ], |
| 261 | + "metadata": { |
| 262 | + "language_info": { |
| 263 | + "name": "csharp" |
| 264 | + }, |
| 265 | + "orig_nbformat": 4 |
| 266 | + }, |
| 267 | + "nbformat": 4, |
| 268 | + "nbformat_minor": 2 |
| 269 | +} |
0 commit comments