Created
March 12, 2025 02:07
-
-
Save luisquintanilla/b498d67e76fea3184f2cd0aea7329666 to your computer and use it in GitHub Desktop.
Creating custom middleware using MEAI AnonymousDelegatingChatClient
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"polyglot_notebook": { | |
"kernelName": "csharp" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Microsoft.Extensions.AI, 9.3.0-preview.1.25114.11</span></li><li><span>Microsoft.Extensions.AI.AzureAIInference, 9.3.0-preview.1.25114.11</span></li></ul></div></div>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"#r \"nuget: Microsoft.Extensions.AI, 9.3.0-preview.1.25114.11\"\n", | |
"#r \"nuget: Microsoft.Extensions.AI.AzureAIInference, 9.3.0-preview.1.25114.11\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"polyglot_notebook": { | |
"kernelName": "csharp" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"using Azure;\n", | |
"using Azure.AI.Inference;\n", | |
"using Microsoft.Extensions.AI;\n", | |
"using ChatRole = Microsoft.Extensions.AI.ChatRole;" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"polyglot_notebook": { | |
"kernelName": "csharp" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"IChatClient chatClient = \n", | |
" new ChatCompletionsClient(\n", | |
" new Uri(\"https://models.inference.ai.azure.com\"), \n", | |
" new AzureKeyCredential(Environment.GetEnvironmentVariable(\"GH_TOKEN\")))\n", | |
" .AsChatClient(\"gpt-4o-mini\")\n", | |
" .AsBuilder()\n", | |
" .Use(async (messages, options, next, ctx) => \n", | |
" {\n", | |
" // Create a new object that contains the same data as the original messages\n", | |
" // but with the text \"Microsoft\" replaced with \"[REDACTED]\n", | |
" var redactedHistory = \n", | |
" messages\n", | |
" .Select(m => new ChatMessage(m.Role, m.Text.Replace(\"Microsoft\", \"[REDACTED]\")))\n", | |
" .ToList();\n", | |
" await next(redactedHistory, options, ctx);\n", | |
" })\n", | |
" .Build();" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"polyglot_notebook": { | |
"kernelName": "csharp" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"List<ChatMessage> chatHistory = [];" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"polyglot_notebook": { | |
"kernelName": "csharp" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"var response = await chatClient.GetResponseAsync(\"Can you tell me about Microsoft?\");\n", | |
"chatHistory.Add(response.Message);" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"polyglot_notebook": { | |
"kernelName": "csharp" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>It seems you've referenced something with "[REDACTED]." Could you please provide more context or specify what you'd like to know? That will help me assist you better!</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>AuthorName</td><td><div class=\"dni-plaintext\"><pre><null></pre></div></td></tr><tr><td>Role</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>assistant</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Value</td><td><div class=\"dni-plaintext\"><pre>assistant</pre></div></td></tr></tbody></table></div></details></td></tr><tr><td>Text</td><td><div class=\"dni-plaintext\"><pre>It seems you've referenced something with "[REDACTED]." Could you please provide more context or specify what you'd like to know? That will help me assist you better!</pre></div></td></tr><tr><td>Contents</td><td><table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>It seems you've referenced something with "[REDACTED]." Could you please provide more context or specify what you'd like to know? That will help me assist you better!</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Text</td><td><div class=\"dni-plaintext\"><pre>It seems you've referenced something with "[REDACTED]." Could you please provide more context or specify what you'd like to know? That will help me assist you better!</pre></div></td></tr><tr><td>RawRepresentation</td><td><div class=\"dni-plaintext\"><pre><null></pre></div></td></tr><tr><td>AdditionalProperties</td><td><div class=\"dni-plaintext\"><pre><null></pre></div></td></tr></tbody></table></div></details></td></tr></tbody></table></td></tr><tr><td>RawRepresentation</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>It seems you've referenced something with "[REDACTED]." Could you please provide more context or specify what you'd like to know? That will help me assist you better!</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>FinishReason</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>stop</code></span></summary><div><table><thead><tr></tr></thead><tbody></tbody></table></div></details></td></tr><tr><td>Role</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>assistant</code></span></summary><div><table><thead><tr></tr></thead><tbody></tbody></table></div></details></td></tr><tr><td>Content</td><td><div class=\"dni-plaintext\"><pre>It seems you've referenced something with "[REDACTED]." Could you please provide more context or specify what you'd like to know? That will help me assist you better!</pre></div></td></tr><tr><td>ToolCalls</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>[ ]</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>IsUndefined</td><td><div class=\"dni-plaintext\"><pre>True</pre></div></td></tr><tr><td>Count</td><td><div class=\"dni-plaintext\"><pre>0</pre></div></td></tr><tr><td>IsReadOnly</td><td><div class=\"dni-plaintext\"><pre>False</pre></div></td></tr><tr><td><i>(values)</i></td><td><i>(empty)</i></td></tr></tbody></table></div></details></td></tr><tr><td>Id</td><td><div class=\"dni-plaintext\"><pre>chatcmpl-BA5U8b8Zv4czsuXMmcEAWKtiDWasK</pre></div></td></tr><tr><td>Created</td><td><span>2025-03-12 01:47:44Z</span></td></tr><tr><td>Model</td><td><div class=\"dni-plaintext\"><pre>gpt-4o-mini-2024-07-18</pre></div></td></tr><tr><td>Usage</td><td><details class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>Azure.AI.Inference.CompletionsUsage</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>CompletionTokens</td><td><div class=\"dni-plaintext\"><pre>35</pre></div></td></tr><tr><td>PromptTokens</td><td><div class=\"dni-plaintext\"><pre>18</pre></div></td></tr><tr><td>TotalTokens</td><td><div class=\"dni-plaintext\"><pre>53</pre></div></td></tr></tbody></table></div></details></td></tr></tbody></table></div></details></td></tr><tr><td>AdditionalProperties</td><td><div class=\"dni-plaintext\"><pre><null></pre></div></td></tr></tbody></table></div></details></td></tr></tbody></table><style>\r\n", | |
".dni-code-hint {\r\n", | |
" font-style: italic;\r\n", | |
" overflow: hidden;\r\n", | |
" white-space: nowrap;\r\n", | |
"}\r\n", | |
".dni-treeview {\r\n", | |
" white-space: nowrap;\r\n", | |
"}\r\n", | |
".dni-treeview td {\r\n", | |
" vertical-align: top;\r\n", | |
" text-align: start;\r\n", | |
"}\r\n", | |
"details.dni-treeview {\r\n", | |
" padding-left: 1em;\r\n", | |
"}\r\n", | |
"table td {\r\n", | |
" text-align: start;\r\n", | |
"}\r\n", | |
"table tr { \r\n", | |
" vertical-align: top; \r\n", | |
" margin: 0em 0px;\r\n", | |
"}\r\n", | |
"table tr td pre \r\n", | |
"{ \r\n", | |
" vertical-align: top !important; \r\n", | |
" margin: 0em 0px !important;\r\n", | |
"} \r\n", | |
"table th {\r\n", | |
" text-align: start;\r\n", | |
"}\r\n", | |
"</style>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"chatHistory" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": ".NET (C#)", | |
"language": "C#", | |
"name": ".net-csharp" | |
}, | |
"polyglot_notebook": { | |
"kernelInfo": { | |
"defaultKernelName": "csharp", | |
"items": [ | |
{ | |
"aliases": [], | |
"languageName": "csharp", | |
"name": "csharp" | |
} | |
] | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment