{"id":57663,"date":"2025-08-19T10:05:00","date_gmt":"2025-08-19T17:05:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=57663"},"modified":"2025-08-19T09:27:55","modified_gmt":"2025-08-19T16:27:55","slug":"gpt-oss-csharp-ollama","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/gpt-oss-csharp-ollama\/","title":{"rendered":"GPT-OSS &#8211; A C# Guide with Ollama"},"content":{"rendered":"<p>GPT-OSS is OpenAI&#8217;s first open-weight model since GPT-2, and it&#8217;s a game-changer for developers who want powerful AI without the cloud dependency. You get two flavors &#8211; gpt-oss-120b and gpt-oss-20b &#8211; both delivering solid performance on coding, math, and tool use while keeping your data completely private. The 20B model is especially interesting because it runs on just 16GB of memory, making it perfect for local development and experimentation. Check out the <a href=\"https:\/\/openai.com\/index\/introducing-gpt-oss\/\">official OpenAI announcement<\/a> to see how these models are putting serious AI power directly in developers&#8217; hands.<\/p>\n<p>Running GPT-OSS locally opens up new possibilities for experimentation, cost efficiency, and privacy. In this guide, you&#8217;ll learn how to use the open-source GPT-OSS model with Ollama to build fast, private, and offline-capable AI features using C#.<\/p>\n<h2>What you&#8217;ll need<\/h2>\n<ul>\n<li>A machine with at least 16 GB of RAM and a capable GPU (or an Apple Silicon Mac).<\/li>\n<li>The .NET 8 SDK or higher installed.<\/li>\n<li>Ollama installed and running.<\/li>\n<li>The GPT-OSS:20b model pulled with <code>ollama pull gpt-oss:20b<\/code>.<\/li>\n<\/ul>\n<h2>C# toolbox<\/h2>\n<p>Microsoft has made it easy to work with AI models using the <code>Microsoft.Extensions.AI<\/code> libraries. These libraries provide a unified set of abstractions, letting you write code that can work with different AI providers\u2014like Ollama, Azure AI, or OpenAI\u2014without changing your core logic.<\/p>\n<h2>Step 1: Create a new console app<\/h2>\n<p>First, create a new console application. Open your terminal and run:<\/p>\n<pre><code class=\"language-bash\">dotnet new console -n OllamaGPTOSS\r\ncd OllamaGPTOSS<\/code><\/pre>\n<h2>Step 2: Add the NuGet packages<\/h2>\n<p>To connect to Ollama using <code>Microsoft.Extensions.AI<\/code>, you&#8217;ll need two main packages. The <code>Microsoft.Extensions.AI<\/code> package provides the core abstractions, while the <code>OllamaSharp<\/code> package acts as the provider that implements these abstractions for Ollama.<\/p>\n<pre><code class=\"language-bash\">dotnet add package Microsoft.Extensions.AI\r\ndotnet add package OllamaSharp<\/code><\/pre>\n<p><strong>Note:<\/strong> The <code>Microsoft.Extensions.AI.Ollama<\/code> package is deprecated. Use <code>OllamaSharp<\/code> as the recommended alternative for connecting to Ollama.<\/p>\n<h2>Step 3: Write your chat code<\/h2>\n<p>Open <code>Program.cs<\/code> and replace its contents with the following code. This example keeps a rolling chat history and streams responses in real time.<\/p>\n<pre><code class=\"language-csharp\">using Microsoft.Extensions.AI;\r\nusing OllamaSharp;\r\n\r\n\/\/ Initialize OllamaApiClient targeting the \"gpt-oss:20b\" model\r\nIChatClient chatClient = new OllamaApiClient(new Uri(\"http:\/\/localhost:11434\/\"), \"gpt-oss:20b\");\r\n\r\n\/\/ Maintain conversation history\r\nList&lt;ChatMessage&gt; chatHistory = new();\r\n\r\nConsole.WriteLine(\"GPT-OSS Chat - Type 'exit' to quit\");\r\nConsole.WriteLine();\r\n\r\n\/\/ Prompt user for input in a loop\r\nwhile (true)\r\n{\r\n    Console.Write(\"You: \");\r\n    var userInput = Console.ReadLine();\r\n\r\n    if (userInput?.ToLower() == \"exit\")\r\n        break;\r\n\r\n    if (string.IsNullOrWhiteSpace(userInput))\r\n        continue;\r\n\r\n    \/\/ Add user message to chat history\r\n    chatHistory.Add(new ChatMessage(ChatRole.User, userInput));\r\n\r\n    \/\/ Stream the AI response and display in real time\r\n    Console.Write(\"Assistant: \");\r\n    var assistantResponse = \"\";\r\n\r\n    await foreach (var update in chatClient.GetStreamingResponseAsync(chatHistory))\r\n    {\r\n        Console.Write(update.Text);\r\n        assistantResponse += update.Text;\r\n    }\r\n\r\n    \/\/ Append assistant message to chat history\r\n    chatHistory.Add(new ChatMessage(ChatRole.Assistant, assistantResponse));\r\n    Console.WriteLine();\r\n    Console.WriteLine();\r\n}<\/code><\/pre>\n<h2>Step 4: Run your application<\/h2>\n<p>Make sure your Ollama service is running. Then run your .NET console app:<\/p>\n<pre><code class=\"language-bash\">dotnet run<\/code><\/pre>\n<p><div style=\"width: 1000px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-57663-1\" width=\"1000\" height=\"563\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/webm\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/08\/gpt-oss-ollama-demo.webm?_=1\" \/><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/08\/gpt-oss-ollama-demo.webm\">https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2025\/08\/gpt-oss-ollama-demo.webm<\/a><\/video><\/div><\/p>\n<p>Your application will connect to the local Ollama server, and you can start chatting with your own private GPT-OSS model.<\/p>\n<h2>Build agentic apps next<\/h2>\n<p>This is just the beginning. The <code>Microsoft.Extensions.AI<\/code> libraries also support function calling, allowing you to give your local LLM access to your C# methods, APIs, and data. This is where you can build truly powerful, &#8220;agentic&#8221; applications.<\/p>\n<h3>Your mission (should you choose to accept it) \ud83d\udd75\ufe0f<\/h3>\n<ol>\n<li>Get this sample running and see how easy local LLM development is.<\/li>\n<li>Explore the documentation for <code>Microsoft.Extensions.AI<\/code> and <code>OllamaSharp<\/code>.<\/li>\n<li>Integrate this into a project: document summarizer, code generator, or intelligent assistant that runs on your machine.<\/li>\n<\/ol>\n<p>The future of AI is decentralized, and as a C# developer, you have the tools to lead the charge. The power is on your machine\u2014now go build something incredible!<\/p>\n<h2>Up next \u2014 Foundry Local<\/h2>\n<p>In follow-up posts we\u2019ll show how to run the same GPT-OSS model using Foundry Local instead of Ollama. Foundry Local offers Windows-native GPU acceleration and a slightly different runtime, and we\u2019ll provide Foundry-specific configuration, tips for GPU setup, and an example C# wiring that mirrors this guide\u2019s chat + streaming pattern.<\/p>\n<p>Read the announcement for <a href=\"https:\/\/blogs.windows.com\/windowsdeveloper\/2025\/08\/05\/available-today-gpt-oss-20b-model-on-windows-with-gpu-acceleration-further-pushing-the-boundaries-on-the-edge\/\">Foundry Local support on the Windows Developer Blog<\/a>.<\/p>\n<h2>Summary<\/h2>\n<p>You learned how to: (1) set up a .NET console app, (2) add <code>Microsoft.Extensions.AI<\/code> plus <code>OllamaSharp<\/code>, (3) stream chat completions from a local GPT-OSS model, and (4) prepare for advanced scenarios like function calling. Try extending this sample with tool invocation or local RAG over your documents to unlock richer agent patterns\u2014all while keeping data local.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Run GPT-OSS locally with C# and Ollama to build fast, private, offline AI<\/p>\n","protected":false},"author":120281,"featured_media":57664,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685,7781,756],"tags":[8057,8058,7888],"class_list":["post-57663","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","category-ai","category-csharp","tag-gpt","tag-gpt-oss","tag-ollama"],"acf":[],"blog_post_summary":"<p>Run GPT-OSS locally with C# and Ollama to build fast, private, offline AI<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/57663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/120281"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=57663"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/57663\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/57664"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=57663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=57663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=57663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}