Created
April 22, 2025 18:55
-
-
Save luisquintanilla/bed824062b37c6696e2fed9d23bad368 to your computer and use it in GitHub Desktop.
Custom Middleware MEAI
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": null, | |
"metadata": { | |
"polyglot_notebook": { | |
"kernelName": "csharp" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"#r \"nuget: Microsoft.Extensions.AI, 9.4.0-preview.1.25207.5\"\n", | |
"#r \"nuget: Microsoft.Extensions.AI.AzureAIInference, 9.4.0-preview.1.25207.5\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"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": null, | |
"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", | |
" .AsIChatClient(\"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", | |
" await next(messages, options, ctx);\n", | |
"\n", | |
" var redactedHistory = \n", | |
" messages\n", | |
" .Select(m => new ChatMessage(m.Role, m.Text.Replace(\"Microsoft\", \"[REDACTED]\")))\n", | |
" .ToList();\n", | |
"\n", | |
" await next(redactedHistory, options, ctx);\n", | |
" })\n", | |
" .Build();" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"polyglot_notebook": { | |
"kernelName": "csharp" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"List<ChatMessage> chatHistory = [];" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"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": null, | |
"metadata": { | |
"polyglot_notebook": { | |
"kernelName": "csharp" | |
} | |
}, | |
"outputs": [], | |
"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