Skip to content

Instantly share code, notes, and snippets.

@luisquintanilla
Created March 7, 2025 20:15
Show Gist options
  • Save luisquintanilla/48a3dae23ca160a505b29ac8b87d38a4 to your computer and use it in GitHub Desktop.
Save luisquintanilla/48a3dae23ca160a505b29ac8b87d38a4 to your computer and use it in GitHub Desktop.
M.E.AI DataContent
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"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": 2,
"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": 3,
"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\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Text/Plain"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"var chatMessage = new ChatMessage\n",
"{\n",
" Role = ChatRole.User,\n",
" Contents = [\n",
" new DataContent(uri: \"data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==\", mediaType:\"text/plain\")\n",
" ]\n",
"};"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"ename": "Error",
"evalue": "System.ArgumentException: Value cannot be an empty collection. (Parameter 'content')\r\n at Azure.AI.Inference.Argument.AssertNotNullOrEmpty[T](IEnumerable`1 value, String name)\r\n at Azure.AI.Inference.ChatRequestUserMessage..ctor(IEnumerable`1 content)\r\n at Microsoft.Extensions.AI.AzureAIInferenceChatClient.ToAzureAIInferenceChatMessages(IList`1 inputs)+MoveNext()\r\n at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n at Azure.AI.Inference.ChatCompletionsOptions..ctor(IEnumerable`1 messages)\r\n at Microsoft.Extensions.AI.AzureAIInferenceChatClient.ToAzureAIOptions(IList`1 chatContents, ChatOptions options)\r\n at Microsoft.Extensions.AI.AzureAIInferenceChatClient.GetResponseAsync(IList`1 chatMessages, ChatOptions options, CancellationToken cancellationToken)\r\n at Submission#8.<<Initialize>>d__0.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)",
"output_type": "error",
"traceback": [
"System.ArgumentException: Value cannot be an empty collection. (Parameter 'content')\r\n",
" at Azure.AI.Inference.Argument.AssertNotNullOrEmpty[T](IEnumerable`1 value, String name)\r\n",
" at Azure.AI.Inference.ChatRequestUserMessage..ctor(IEnumerable`1 content)\r\n",
" at Microsoft.Extensions.AI.AzureAIInferenceChatClient.ToAzureAIInferenceChatMessages(IList`1 inputs)+MoveNext()\r\n",
" at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n",
" at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n",
" at Azure.AI.Inference.ChatCompletionsOptions..ctor(IEnumerable`1 messages)\r\n",
" at Microsoft.Extensions.AI.AzureAIInferenceChatClient.ToAzureAIOptions(IList`1 chatContents, ChatOptions options)\r\n",
" at Microsoft.Extensions.AI.AzureAIInferenceChatClient.GetResponseAsync(IList`1 chatMessages, ChatOptions options, CancellationToken cancellationToken)\r\n",
" at Submission#8.<<Initialize>>d__0.MoveNext()\r\n",
"--- End of stack trace from previous location ---\r\n",
" at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)"
]
}
],
"source": [
"var chatResponse = await chatClient.GetResponseAsync([chatMessage]);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Custom DataContent"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"var customDataContent = new DataContent(\"data:custom/mime-type;base64,SGVsbG8sIFdvcmxkIQ==\", \"custom/mime-type\");"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"var customChatMessage = new ChatMessage\n",
"{\n",
" Role = ChatRole.User,\n",
" Contents = [\n",
" customDataContent\n",
" ]\n",
"};"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"ename": "Error",
"evalue": "System.ArgumentException: Value cannot be an empty collection. (Parameter 'content')\r\n at Azure.AI.Inference.Argument.AssertNotNullOrEmpty[T](IEnumerable`1 value, String name)\r\n at Azure.AI.Inference.ChatRequestUserMessage..ctor(IEnumerable`1 content)\r\n at Microsoft.Extensions.AI.AzureAIInferenceChatClient.ToAzureAIInferenceChatMessages(IList`1 inputs)+MoveNext()\r\n at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n at Azure.AI.Inference.ChatCompletionsOptions..ctor(IEnumerable`1 messages)\r\n at Microsoft.Extensions.AI.AzureAIInferenceChatClient.ToAzureAIOptions(IList`1 chatContents, ChatOptions options)\r\n at Microsoft.Extensions.AI.AzureAIInferenceChatClient.GetResponseAsync(IList`1 chatMessages, ChatOptions options, CancellationToken cancellationToken)\r\n at Submission#14.<<Initialize>>d__0.MoveNext()\r\n--- End of stack trace from previous location ---\r\n at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)",
"output_type": "error",
"traceback": [
"System.ArgumentException: Value cannot be an empty collection. (Parameter 'content')\r\n",
" at Azure.AI.Inference.Argument.AssertNotNullOrEmpty[T](IEnumerable`1 value, String name)\r\n",
" at Azure.AI.Inference.ChatRequestUserMessage..ctor(IEnumerable`1 content)\r\n",
" at Microsoft.Extensions.AI.AzureAIInferenceChatClient.ToAzureAIInferenceChatMessages(IList`1 inputs)+MoveNext()\r\n",
" at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n",
" at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n",
" at Azure.AI.Inference.ChatCompletionsOptions..ctor(IEnumerable`1 messages)\r\n",
" at Microsoft.Extensions.AI.AzureAIInferenceChatClient.ToAzureAIOptions(IList`1 chatContents, ChatOptions options)\r\n",
" at Microsoft.Extensions.AI.AzureAIInferenceChatClient.GetResponseAsync(IList`1 chatMessages, ChatOptions options, CancellationToken cancellationToken)\r\n",
" at Submission#14.<<Initialize>>d__0.MoveNext()\r\n",
"--- End of stack trace from previous location ---\r\n",
" at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)"
]
}
],
"source": [
"var chatResponse = await chatClient.GetResponseAsync([customChatMessage]);"
]
}
],
"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