Skip to main content

Posts

Showing posts with the label Semantic Kernel

Microsoft.Extensions.AI – Part IX–Semantic kernel integration

Semantic Kernel was the first AI library specifically created to build AI agent and chat experiences in .NET. Later the .NET team started working on Microsoft.Extensions.AI as a common abstraction layer for integrating AI capabilities in your .NET applications. As a consequence, these 2 libraries have some overlap and similar abstractions exist in both libraries. This post is part of a blog series. Other posts so far: Part I – An introduction to Microsoft.Extensions.AI Part II – ASP.NET Core integration Part III –Tool calling Part IV – Telemetry integration Part V – Chat history Part VI – Structured output Part VII -  MCP integration Part VIII – Evaluations Part VIII – Evaluations (continued) Part IX (this post) -  Semantic Kernel Integration What now? The good news is that Microsoft is actively working on aligning both libraries and (re)building Semantic Kernel on top of the same Microsoft.Extensions.AI abstractions. This mea...

Supercharging On-Device AI: Foundry Local + Semantic Kernel

So far my ‘go to’ approach for using language models locally was through Ollama and Semantic Kernel. With the announcement of Foundry Local at Build, I decided to try to combine AI Foundry Local with Semantic Kernel. Time to dive in… What Is Foundry Local? Foundry Local is Microsoft’s local execution runtime for large language models. Unlike cloud-hosted models, Foundry Local runs entirely on your device, giving you privacy, customization, and cost-efficiency. Thanks to its simple CLI and REST API, it integrates smoothly into existing workflows and can support a variety of models and use cases. The easiest way to get started with Foundry Local is through winget: winget install Microsoft.FoundryLocal Once Local Foundry is installed, you can request the list of available models: foundry model list Download the model you want to use: foundry model download phi-3.5-mini Now you can run foundry using the downloaded model: foundry model run phi-3.5-mini If you forgot, ...

Meet the Phi family

The Phi family keeps growing with the recent introduction of the Phi-4-Reasoning, Phi-4-mini and Phi-4-multimodal models. These new models introduce some great enhancements we'll discuss later in this post. But maybe let me start by introducing you to the Phi family. The Phi family The Phi family is a set of small language models created and maintained by Microsoft Research. They are designed to achieve strong performance while being much smaller and more efficient than their larger counterparts like GPT-4 or Claude. Phi’s evolution over the last year has continually pushed this envelope of quality vs. size, expanding the family with new features to address diverse needs. Where the original Phi models focussed mostly on chat and coding activitities, capabilities have now extended to multimodel (vision, speech), function calling and advanced reasoning. Function calling support A feature I’m especially happy with that it finally got introduced into the Phi models is funct...

Semantic Kernel with Ollama returns 404 - The final solution

Today I had to do some small changes in an existing project where we were using Semantic Kernel together with Ollama during debugging and testing. As Ollama exposes an Open AI compatible API, this is possible without any code changes. At least it was... Here is the code I was using: When I opened the project I noticed that some of the packages references where outdated so I started by upgrading to the latest version of the Semantic Kernel NuGet packages. However after doing that the requests started to fail with a 404 error message. Mmm? Didn’t I got the same error before? And indeed I talked about this issue in an earlier post . The issue is still there as I could confirm by looking at the Ollama log messages: Here is the successful request using the old Semantic Kernel version: [GIN] 2025/04/24 - 10:05:40 | 200 |    48.466781s |       127.0.0.1 | POST     "/v1/chat/completions" Here is the failing ...

Getting started with the Azure AI services

Azure has a lot to offer if you want start building AI enabled solutions. But where do you start? There is just so much to learn and explore that it is easy to get lost in all the services, documentation and information out there. That's where the Book of AI comes in. This comprehensive guide is designed to help you navigate the world of AI services in Azure, providing you with the resources, tutorials, and sample code you need to build innovative solutions using Azure AI services. Why use the Book of AI? The website itself gives the following arguments: I can only agree with that. Use the book of AI to try out not only the different Azure AI services but also the different SDK’s and client libraries. Not a C# developer?  No problem! All examples are available in multiple programming languages.   Eat your own AI But for me the greatest feature of the Book of AI is not the content itself but the way it has been created. Instead of manually writing the complete webs...

Semantic Kernel – Auto function calling

A  few weeks ago I got contacted by someone(Hi Chris!) who was trying to get my Semantic Kernel demo's up and running on his machine. Chris tried to get the application up and running but got some error messages. I used his input to improve the readme file and updated the main branch to simplify the getting started experience. However there was one specific error he shared that I want to talk about a little more. Here is the screenshot he shared with me:   The reason that he got this error is because ‘auto function calling’ was enabled in the code and the model he was using didn’t support this feature. Remark: I updated the code to disable auto function calling after I got his email. A good excuse to talk a little more about this feature… What is (auto) function calling in Semantic Kernel? With function calling, you give the LLM the option to interact with your existing code. You can do this quite explicit as I explained in my OllamaSharp post but with Semanti...

Qdrant Vector Database–What you need to know to get started in .NET

A key element in building an RAG(Retrieval Augmented Generation) is the usage of a vector database. The list of vector databases is growing every day and even SQL Server now supports vectors. In this post I focus on Qdrant (pronounced as "quadrant") a vector similarity search engine and database written in Rust. It's specifically designed to handle vector embeddings and payload, making it perfect for modern machine learning applications. The goal of this post is not to make you a Qdrant expert(neither am I) but to provide you with enough information to start using it correctly. What is Qdrant? Qdrant is an AI-native vector database and semantic search engine designed to handle high-dimensional vectors. It provides a production-ready service with a convenient API to store, search, and manage vectors with additional payloads.  A vector database is designed to store and query high-dimensional vectors efficiently. Unlike traditional databases that organize data in rows a...

Use text embeddings within Semantic Kernel and Ollama

In a previous post I showed you how to use Semantic Kernel, Ollama and Qdrant to generate and store vector embeddings. Doing this doesn't make much sense if we don't look at a way to expose and use this data. In this post I'll show you how to query the vector store to search for results. Remark: If you missed the previous post, go check it out first. Let’s dive in… We start by having a look again at the model we were using: First important thing to notice is that we can decided which properties should be used when querying the vector store. This is useful when we have multiple vectors stored in the data model. If we don’t specify the vector property, the first vector found will be used when querying. (In our example we only have one vector property so in theory we could ignore this). Let’s do a first search: Remark: Notice that we need to use the Property name not the name configured in the store or attribute. Most vector stores also support the concept o...

Generate text embeddings with Semantic Kernel and Ollama

Retrieval-augmented regeneration, also known as RAG, is an NLP technique that can help improve the quality of large language models (LLMs). It allows your AI agent to retrieve data from external sources to generate grounded responses. This helps avoid that your agent hallucinates and returns incorrect information. There are multiple ways to retrieve this external data. In this post I want to show you how to generate vector embeddings that can be stored and retrieved from a vector database. This means we first need to decide which database to use. The list of options keeps growing (even the new SQL server version will support vector embeddings out of the box). As we want to demonstrate this feature using Semantic Kernel, we need to take a look at one of the available connectors . Qdrant Vector Database I decided to use Qdrant for this blog post. Qdrant is an AI-native vector database and a semantic search engine. You can use it to extract meaningful information from unstructure...

Structured output with Ollama

I talked about Structured Output before in the context of Semantic Kernel. Structured Outputs is a feature that ensures the model will always generate responses that follow a supplied JSON Schema, so you can process the responses in automated fashion without worrying about getting invalid JSON back. Recently support for Structured Output was announced by Ollama . In this post I want to show you how you can use this in combination with OllamaSharp , the C# client for Ollama. Using Structured Ouput in OllamaSharp Remark: Make sure you have latest Ollama version running on your local machine before you continue. Add the OllamaSharp client to your project: dotnet add package OllamaSharp Now let’s first define our response model: Afterwards we need to initiate a new OllamaSharp client instance: We create a new request object. Notice that we specify a JSON schema object based on the Recipe model we created earlier If we now invoke the appl...

Semantic Kernel - Structured output

One of the challenges when integrating a large language model into your backend processes is that the response you get back is non-deterministic. This is not a big problem if you only want to output the response as text, but it can be a challenge to process the response in an automated fashion. Prompting for JSON Of course you can use prompt engineering to ask the LLM to return the response as JSON and even provide an example to steer the LLM, but still it can happen that the JSON you get back is not formatted correctly. Here is a possible prompt: A trick that also can help as mentioned in the Anthropic documentation is to prefill the response with a part of the JSON message. JSON mode Although the techniques above will certainly help, they are not fool proof. A first improvement on this approach was the introduction of JSON mode in the OpenAI API. When JSON mode is turned on, the model's output is ensured to be valid JSON, except for some edge cases that are descri...

Semantic Kernel–Plugin KeyNotFoundException

Plugins are a key element when building agents in Semantic Kernel. They allow you to extend the capabilities of your Large Language Model with extra functions. This post is not about writing your own plugin but Just as a reminder, registering a plugin can be done like this: Here is the code I used to try to invoke the TimePlugin in a prompt: However when I did this the following error was returned: System.Collections.Generic.KeyNotFoundException: 'The plugin collection does not contain a plugin and/or function with the specified names. Plugin name – 'time', function name - 'Date'.' The problem is caused because I had configured an alias name for the plugin but I was using the plugin name in the prompt. I have 2 ways to fix the issue above. Either I update the prompt to point to the configured alias: Or I remove the alias from my configuration: Hope that helps… More information Plugins in Semantic Kernel | Microsoft Learn

Semantic Kernel–Giving the new Ollama connector a try

As Semantic Kernel could work with any OpenAI compatible endpoint, and Ollama exposes it language models through an OpenAI compatible API, combining the 2 was always possible. However not all features of Ollama were accessible through Semantic Kernel. With the recent release of a dedicated Ollama connector for Semantic Kernel, we can start using some of the more advanced Semantic Kernel features directly targetting Ollama deployed models. The new connector is using Ollama Sharp(I talked about it in this post ) so you can directly access the library if needed. Giving the new connector a try… Create a new Console application and add the Microsoft.SemanticKernel.Connectors.Ollama NuGet package: dotnet add package Microsoft.SemanticKernel.Connectors.Ollama --version 1.21.1-alpha Now instead of creating a Semantic Kernel instance, we can directly create an OllamaChatCompletionService instance: The remaining part of the code remains the same as with the default ...

Semantic Kernel - 404 error when using the v1.20.0 version

While preparing a demo for my team, I encountered the following error after upgrading to Semantic Kernel 1.20.0(alpha). Microsoft.SemanticKernel.HttpOperationException: Service request failed. Status: 404 (Not Found) ---> System.ClientModel.ClientResultException: Service request failed. Status: 404 When I took a look at the request URI, I noticed that the following URI was used:   I switched back to the original version I was using (v1.17.2) and now I could see that a different URI was used:   Do you notice the difference? Somehow the 'v1' part in the URI disappeared... A look at the Semantic Kernel Github repo brought me to the following issue: .Net: Bug: HTTP 404 - POST /chat/completions · Issue #8525 · microsoft/semantic-kernel (github.com) It seems that it is related to the OpenAI version in use. A fix is to stay a little bit longer on the v1.17.2 version until a new release with the following fix is available: .Net: OpenAI + AzureOpenAI Connector SDK ...

Semantic Kernel - Multi agent systems

Yesterday I talked about the new agent abstraction in Semantic Kernel and how it can simplify the steps required to build your own AI agent.  But what could be better than having one agent? Multiple agents of course! And that is exactly what was recently introduced as a preview in Semantic Kernel. As explained in this blog post , there are multiple ways that multiple agents can work together. The simplest way is as a group chat where multiple agents can talk back-and-forth with each other. To avoid that these agents get stuck in a loop this is combined with a custom termination strategy that specifies when the conversation is over. Here is a small example. I start with the default Semantic Kernel configuration to create a kernel instance: Now I define the instructions for the different agents and create them: Remark: Notice that I can use different kernels with different models if I want to. To make sure that the conversation is ended I need to specify a Terminatio...

Semantic Kernel–Agent Framework

In this post I show you the recently introduced Semantic Kernel agents feature and how it simplifies building your own AI agents. But maybe I should start with a short recap about Semantic Kernel. On the documentation pages , Semantic Kernel is described like this: Semantic Kernel is a lightweight, open-source development kit that lets you easily build AI agents and integrate the latest AI models into your C#, Python, or Java codebase. It serves as an efficient middleware that enables rapid delivery of enterprise-grade solutions. It gives you all the building blocks required to build your own agent; a chat completion model, a plugin system, a planner and more. However until recently you had to bring all this building blocks together yourself. Here is a small code snippet I copied from an existing project: There are a lot of things going on in the code above and if you have hard time to understand all of this I have some good news for you. Starting with the Python (1.6.0) and...

Combining Semantic Kernel with Podman AI Labs

Yesterday I talked about Podman AI Labs as an alternative to Ollama to run your Large Language Models locally. Among the list of features I noticed the following one: Mmh, an OpenAI compatible API… That made me wonder if I could use Semantic Kernel to talk to the local service. Let’s give it a try… I first add the minimal amount of code to use Semantic Kernel. Compared to the same code using Ollama there are only 2 important things to notice: I adapted the URI to match the service URI running inside Podman I could set the ModelId to any value I want as the endpoint only hosts one specific model(granite in this example) And just to proof that it really works, here are the results I got back: This is again a great example how the abstraction that Semantic Kernel has to offer simplifies interacting with multiple LLM’s. Nice! IMPORTANT: I first tried to get it working with the latest prerelease of Semantic Kernel(1.18.0-rc). However when I used that version ...

Semantic Kernel–OpenTelemetry integration in C#

I already showed in a previous post how you could integrate Semantic Kernel with the .NET Core LoggerFactory to see what is going on while interacting with your OpenAI backend. Here is the link in case you missed it: Debugging Semantic Kernel in C# (bartwullems.blogspot.com) . An even better solution is to use the OpenTelemetry integration. Therefore we need to create a LoggerFactory instance that uses OpenTelemetry as a logging provider: Now we need to register this LoggerFactory as a service of the Semantic Kernel builder: If we now take a look at our Aspire Dashboard , we could see the logged messages appear: It is also possible to collect any related metrics and traces. Therefore add the following code to your Program.cs : If we now take a look at the Aspire Dashboard, we can see both the metrics and the end-2-end trace:

Semantic Kernel–Change timeout value in C#

If you are new to Semantic Kernel , I would point you to one of my earlier posts. In this post I want to show how you can change the timeout values when using Semantic Kernel. The power of Semantic Kernel is that it gives you the ability to interact with multiple (large language) models in an uniform way. You interact using C#, Java or Python with the Semantic Kernel SDK and behind the scenes it will do the necessary API calls to OpenAI, Azure OpenAI, Hugging Face or  a local OpenAI compatible tool like Ollama . Of course as we are interacting with an API behind the scenes, it can happen that the API doesn’t return any results in time and that we get a timeout exception. The operation was cancelled because it exceeded the configured timeout. Let me share how I fixed it… Use a custom HttpClient One option you have is to explicitly pass an HttpClient instance when creating the Semantic Kernel instance: Retry when a timeout happens If the timeout typically happen...

Code & Comedy 2024–This session will give you 2.6 hours of your time back

Yesterday I had the pleasure to present at Code & Comedy 2024 . Again it was a great combination of inspiring sessions, great food and a lot of nice people to meet. All of this followed by a Comedy Act by Jan Jaap van der Wal. I never thought that AI could be that much fun! Note to myself: Next time make sure that I’m not one of the 2 Flemish guys in the room. I did a presentation titled " This session will give you 2,6h a day of your time back!"; which is a hard promise to make, so I hope that the participants could confirm if I succeeded or not. In the session I shared some surprising insights in behavioral science and explained how we can apply this knowledge in building AI assistants. In case you missed my session or you want to a look at the code in more detail, here are the relevant links: Presentation: wullemsb/presentations: Repo with all my (public) presentations (github.com) Source code: wullemsb/SemanticKernel: Demo code for my AI session (githu...