<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: divya bathoju</title>
    <description>The latest articles on DEV Community by divya bathoju (@divya_bathoju).</description>
    <link>https://dev.to/divya_bathoju</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3157995%2Fc22fafde-f56e-4bfc-9129-533984d18c59.jpg</url>
      <title>DEV Community: divya bathoju</title>
      <link>https://dev.to/divya_bathoju</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/divya_bathoju"/>
    <language>en</language>
    <item>
      <title>Add AI to your existing web APIs using Semantic Kernel</title>
      <dc:creator>divya bathoju</dc:creator>
      <pubDate>Tue, 13 May 2025 11:52:32 +0000</pubDate>
      <link>https://dev.to/divya_bathoju/add-ai-to-your-existing-web-apis-using-semantic-kernel-1ecm</link>
      <guid>https://dev.to/divya_bathoju/add-ai-to-your-existing-web-apis-using-semantic-kernel-1ecm</guid>
      <description>&lt;p&gt;Are you looking to integrate artificial intelligence into your .NET APIs without a complete overhaul? Consider Semantic Kernel, a tool that allows smooth AI prompt integration with your application logic, adding intelligence and automation easily.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/semantic-kernel/get-started/quick-start-guide?pivots=programming-language-csharp" rel="noopener noreferrer"&gt;Semantic Kernel&lt;/a&gt;  is an open-source AI orchestration library from Microsoft. It is model-agnostic, allowing you to choose your preferred models, whether self-hosted or third-party. It includes features like observability and strong security measures, such as filters to protect against prompt injection attacks, enabling you to build reliable, enterprise-grade applications effortlessly.&lt;br&gt;
In this post, you'll learn how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up Semantic Kernel in ASP.NET Core Web API&lt;/li&gt;
&lt;li&gt;Configure the model for the application&lt;/li&gt;
&lt;li&gt;Describe existing code to the AI Model&lt;/li&gt;
&lt;li&gt;Add a simple prompt endpoint that accepts user requests&lt;/li&gt;
&lt;li&gt;Leverage filters to implement logging for tracking the end-to-end request trail.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step-by-step instructions&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Step 1: Install Semantic Kernel&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Add nuget package in your existing web API project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add package Microsoft.SemanticKernel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Step 2: Add Semantic Kernel to your web application builder&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public static void Main(string[] args)
 {
     var builder = WebApplication.CreateBuilder(args);

     // ... 
     // Add Kernel
     builder.Services.AddKernel();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Step 3: Add model ( below code uses local model running using Ollama, if you are using 3rd party, set modelid and apikey, you don't need endpoint property)&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Add LLM 
builder.Services.AddOpenAIChatCompletion(
   modelId: "llama3.2:latest",
   endpoint: new Uri("http://localhost:11434/v1"),
   apiKey: "");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Step 4: Describe your existing code to AI model using function descriptors. Ensure the description matches the intent of the method and this is key for model to identify the right functions to call.&lt;br&gt;
&lt;code&gt;Because the LLMs are predominantly trained in python, use snake_case format for kernel function names and parameters.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frz0wlb1q0hp96retc2nq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frz0wlb1q0hp96retc2nq.png" alt="kernel descriptions for model" width="777" height="613"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdigls3yzym870zu1v68.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdigls3yzym870zu1v68.png" alt="kernel functions" width="800" height="734"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Step 5: Register the above class as Kernel Plugin. You can register even HttpClient (Let's say your API is calling another API to fulfill request) as plugins.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Add your existing services as Kernel plugins
builder.Services.AddTransient(serviceProvider =&amp;gt;
{
    Kernel k = new(serviceProvider);
    var productRepository = serviceProvider.GetRequiredService&amp;lt;IProductService&amp;gt;();
    k.Plugins.AddFromObject(productRepository);
    return k;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Step 6: Add a sample Prompt Endpoint and Invoke Kernel with the prompt&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public class PromptController : Controller
    {
        private readonly Kernel _kernel;
        public PromptController(Kernel kernel)
        {
            _kernel = kernel;
        }
        [HttpGet]
        public async Task&amp;lt;IActionResult&amp;gt; HandlePromptAsync(string prompt)
        {
            OpenAIPromptExecutionSettings settings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions};
            var result = await _kernel.InvokePromptAsync&amp;lt;string&amp;gt;(prompt, new(settings));
            return Ok(result);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Step 7: Add FunctionFilter to see the request trail&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class FunctionFilter : IAutoFunctionInvocationFilter
    {
        public async Task OnAutoFunctionInvocationAsync(AutoFunctionInvocationContext context, Func&amp;lt;AutoFunctionInvocationContext, Task&amp;gt; next)
        {
            Console.WriteLine($"{nameof(FunctionFilter)}.FunctionInvoking - {context.Function.PluginName}.{context.Function.Name}");
            await next(context);
            Console.WriteLine($"{nameof(FunctionFilter)}.FunctionInvoked - {context.Function.PluginName}.{context.Function.Name}");
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Step 8: Register the FunctionInvocationFilter&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; builder.Services.AddSingleton&amp;lt;IAutoFunctionInvocationFilter, FunctionFilter&amp;gt;();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Step 9: Let's test the endpoint (If you are using local model, make sure its up and running)&lt;br&gt;
Prompt provided (create a product tablecloth worth 100$) and response is 200, product is created with name tablecloth and price is set to 100.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fclshffl3bvga8nqle9ss.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fclshffl3bvga8nqle9ss.png" alt="POST product" width="800" height="452"&gt;&lt;/a&gt;&lt;br&gt;
Here is the request audit trail.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv3w3vlz0vx4gwhuwar3m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv3w3vlz0vx4gwhuwar3m.png" alt="POST product request trail" width="800" height="173"&gt;&lt;/a&gt;&lt;br&gt;
Let's try retrieving the product&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxjdviiamix2ecoq0bki7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxjdviiamix2ecoq0bki7.png" alt="GET product" width="800" height="587"&gt;&lt;/a&gt;&lt;br&gt;
and request trail&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcifaaligyvny5wiuppoj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcifaaligyvny5wiuppoj.png" alt="GET product request trail" width="800" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Final Thoughts
&lt;/h4&gt;

&lt;p&gt;That’s it! You’ve now extended your API with AI capabilities using Semantic Kernel. While AI can unlock powerful capabilities, it's important to remember that not every product needs AI. Use tools like Semantic Kernel when they genuinely help solve a customer problem or enhance the user experience. Start with the problem, not the technology—and let AI be a solution where it makes sense, not just a trend to follow.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>semantickernel</category>
      <category>aspdotnet</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
