{"id":2393,"date":"2025-08-19T17:12:15","date_gmt":"2025-08-19T17:12:15","guid":{"rendered":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/2025\/08\/19\/gpt-oss-a-c-guide-with-ollama\/"},"modified":"2025-08-19T17:12:15","modified_gmt":"2025-08-19T17:12:15","slug":"gpt-oss-a-c-guide-with-ollama","status":"publish","type":"post","link":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/2025\/08\/19\/gpt-oss-a-c-guide-with-ollama\/","title":{"rendered":"GPT-OSS \u2013 A C# Guide with Ollama"},"content":{"rendered":"<p>GPT-OSS is OpenAI\u2019s first open-weight model since GPT-2, and it\u2019s a game-changer for developers who want powerful AI without the cloud dependency. You get two flavors \u2013 gpt-oss-120b and gpt-oss-20b \u2013 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\u2019 hands.<\/p>\n<p>Running GPT-OSS locally opens up new possibilities for experimentation, cost efficiency, and privacy. In this guide, you\u2019ll 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\u2019ll need<\/h2>\n<p>A machine with at least 16 GB of RAM and a capable GPU (or an Apple Silicon Mac).<br \/>\nThe .NET 8 SDK or higher installed.<br \/>\nOllama installed and running.<br \/>\nThe GPT-OSS:20b model pulled with ollama pull gpt-oss:20b.<\/p>\n<h2>C# toolbox<\/h2>\n<p>Microsoft has made it easy to work with AI models using the Microsoft.Extensions.AI 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<p>dotnet new console -n OllamaGPTOSS<br \/>\ncd OllamaGPTOSS<\/p>\n<h2>Step 2: Add the NuGet packages<\/h2>\n<p>To connect to Ollama using Microsoft.Extensions.AI, you\u2019ll need two main packages. The Microsoft.Extensions.AI package provides the core abstractions, while the OllamaSharp package acts as the provider that implements these abstractions for Ollama.<\/p>\n<p>dotnet add package Microsoft.Extensions.AI<br \/>\ndotnet add package OllamaSharp<\/p>\n<p><strong>Note:<\/strong> The Microsoft.Extensions.AI.Ollama package is deprecated. Use OllamaSharp as the recommended alternative for connecting to Ollama.<\/p>\n<h2>Step 3: Write your chat code<\/h2>\n<p>Open Program.cs and replace its contents with the following code. This example keeps a rolling chat history and streams responses in real time.<\/p>\n<p>using Microsoft.Extensions.AI;<br \/>\nusing OllamaSharp;<\/p>\n<p>\/\/ Initialize OllamaApiClient targeting the &#8220;gpt-oss:20b&#8221; model<br \/>\nIChatClient chatClient = new OllamaApiClient(new Uri(&#8220;http:\/\/localhost:11434\/&#8221;), &#8220;gpt-oss:20b&#8221;);<\/p>\n<p>\/\/ Maintain conversation history<br \/>\nList&lt;ChatMessage&gt; chatHistory = new();<\/p>\n<p>Console.WriteLine(&#8220;GPT-OSS Chat &#8211; Type &#8216;exit&#8217; to quit&#8221;);<br \/>\nConsole.WriteLine();<\/p>\n<p>\/\/ Prompt user for input in a loop<br \/>\nwhile (true)<br \/>\n{<br \/>\n    Console.Write(&#8220;You: &#8220;);<br \/>\n    var userInput = Console.ReadLine();<\/p>\n<p>    if (userInput?.ToLower() == &#8220;exit&#8221;)<br \/>\n        break;<\/p>\n<p>    if (string.IsNullOrWhiteSpace(userInput))<br \/>\n        continue;<\/p>\n<p>    \/\/ Add user message to chat history<br \/>\n    chatHistory.Add(new ChatMessage(ChatRole.User, userInput));<\/p>\n<p>    \/\/ Stream the AI response and display in real time<br \/>\n    Console.Write(&#8220;Assistant: &#8220;);<br \/>\n    var assistantResponse = &#8220;&#8221;;<\/p>\n<p>    await foreach (var update in chatClient.GetStreamingResponseAsync(chatHistory))<br \/>\n    {<br \/>\n        Console.Write(update.Text);<br \/>\n        assistantResponse += update.Text;<br \/>\n    }<\/p>\n<p>    \/\/ Append assistant message to chat history<br \/>\n    chatHistory.Add(new ChatMessage(ChatRole.Assistant, assistantResponse));<br \/>\n    Console.WriteLine();<br \/>\n    Console.WriteLine();<br \/>\n}<\/p>\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<p>dotnet run<\/p>\n<div class=\"wp-video\"><!--[if lt IE 9]&gt;document.createElement('video');&lt;![endif]--><br \/>\n<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><\/div>\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 Microsoft.Extensions.AI 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, \u201cagentic\u201d applications.<\/p>\n<h3>Your mission (should you choose to accept it) <\/h3>\n<p>Get this sample running and see how easy local LLM development is.<br \/>\nExplore the documentation for Microsoft.Extensions.AI and OllamaSharp.<br \/>\nIntegrate this into a project: document summarizer, code generator, or intelligent assistant that runs on your machine.<\/p>\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 Microsoft.Extensions.AI plus OllamaSharp, (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<p>The post <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/gpt-oss-csharp-ollama\/\">GPT-OSS \u2013 A C# Guide with Ollama<\/a> appeared first on <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\">.NET Blog<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>GPT-OSS is OpenAI\u2019s first open-weight model since GPT-2, and it\u2019s a game-changer for developers who want powerful AI without the [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[7],"tags":[],"class_list":["post-2393","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts\/2393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/comments?post=2393"}],"version-history":[{"count":0,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/posts\/2393\/revisions"}],"wp:attachment":[{"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/media?parent=2393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/categories?post=2393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rssfeedtelegrambot.bnaya.co.il\/index.php\/wp-json\/wp\/v2\/tags?post=2393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}