DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Alexa Skill With .NET Core
  • Serverless NLP: Implementing Sentiment Analysis Using Serverless Technologies
  • How to Merge Excel XLSX Files in Java
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel

Trending

  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • Agentic AI for Automated Application Security and Vulnerability Management
  • Ethical AI in Agile
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Implementing CRUD Operations With NLP Using Microsoft.Extensions.AI

Implementing CRUD Operations With NLP Using Microsoft.Extensions.AI

Learn how to implement CRUD operations using NLP in a .NET Web API application with the Microsoft.Extensions.AI library for a light management system.

By 
Baraneetharan Ramasamy user avatar
Baraneetharan Ramasamy
·
Dec. 30, 24 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
3.0K Views

Join the DZone community and get the full member experience.

Join For Free

In this blog post, we will explore how to implement CRUD (Create, Read, Update, Delete) operations using Natural Language Processing (NLP) with the Microsoft.Extensions.AI library in a .NET Web API application. We will utilize the power of NLP to interact with our application through natural language queries and perform CRUD operations on a light management system.

Step-by-Step Guide

1. Create a .NET Web API Application

First, let's create a new Web API project using the dotnet CLI:

Plain Text
 
dotnet new webapi -o lightsmeai


This command generates a basic Web API project named "lightsmeai."

2. Add Required Packages

Next, we need to add the necessary packages to our project. These packages include Azure.AI.OpenAI, Azure.Identity, DotNetEnv, Microsoft.AspNetCore.OpenApi, Microsoft.Extensions.AI, and more. Run the following commands to install the required packages:

Plain Text
 
dotnet add package Azure.AI.OpenAI --version 2.1.0-beta.2
 dotnet add package Azure.Identity --version 1.13.1
 dotnet add package DotNetEnv --version 3.1.1
 dotnet add package Microsoft.AspNetCore.OpenApi --version 8.0.1
 dotnet add package Microsoft.Extensions.AI --version 9.0.0-preview.9.24556.5
 dotnet add package Microsoft.Extensions.AI.AzureAIInference --version 9.0.0-preview.9.24556.5
 dotnet add package Microsoft.Extensions.AI.OpenAI --version 9.0.0-preview.9.24556.5
 dotnet add package Swashbuckle.AspNetCore --version 6.4.0


3. Configure Program.cs

In the Program.cs file, we set up the necessary configurations and services for our application. Here's the code snippet:

Plain Text
 
using Azure;
 using Azure.AI.Inference;
 using Azure.AI.OpenAI;
 using DotNetEnv;
 using Microsoft.Extensions.AI;
 
// Get keys from configuration
 Env.Load(".env");
 string githubKey = Env.GetString("GITHUB_KEY");
 
var builder = WebApplication.CreateBuilder(args);
 
// Add services to the container.
 builder.Services.AddControllers();
 builder.Services.AddEndpointsApiExplorer();
 builder.Services.AddSwaggerGen();
 
// Add the chat client
 IChatClient innerChatClient = new ChatCompletionsClient(
    endpoint: new Uri("<https://models.inference.ai.azure.com>"),
    new AzureKeyCredential(githubKey))
    .AsChatClient("gpt-4o-mini");
 
builder.Services.AddChatClient(chatClientBuilder => chatClientBuilder
    .UseFunctionInvocation()
    // .UseLogging()
    .Use(innerChatClient));
 
// Register embedding generator
 builder.Services.AddSingleton<IEmbeddingGenerator<string, Embedding<float>>>(sp =>
    new AzureOpenAIClient(new Uri("<https://models.inference.ai.azure.com>"),
        new AzureKeyCredential(githubKey))
        .AsEmbeddingGenerator(modelId: "text-embedding-3-large"));
 
builder.Services.AddLogging(loggingBuilder =>
    loggingBuilder.AddConsole().SetMinimumLevel(LogLevel.Trace));
 
var app = builder.Build();
 
// Configure the HTTP request pipeline.
 if (app.Environment.IsDevelopment())
 {
    app.UseSwagger();
    app.UseSwaggerUI();
 }
 
app.UseStaticFiles(); // Enable serving static files
 app.UseRouting(); // Must come before UseEndpoints
 app.UseAuthorization();
 app.MapControllers();
 // Serve index.html as the default page
 app.MapFallbackToFile("index.html");
 app.Run();


4. Add ChatController

Let's create a ChatController to handle natural language queries and perform CRUD operations. Here's the code for the ChatController:

Plain Text
 
using System.Collections.Generic;
 using System.Threading.Tasks;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.AI;
 
namespace lightsmeai.Controllers
 {
    [ApiController]
    [Route("[controller]")]
    public class ChatController : ControllerBase
    {
        private readonly IChatClient _chatClient;
        private readonly IEmbeddingGenerator<string, Embedding<float>> _embeddingGenerator;
        private readonly ChatOptions _chatOptions;
 
        public ChatController(
            IChatClient chatClient,
            IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator,
            ChatOptions chatOptions
            )
        {
            _chatClient = chatClient;
            _embeddingGenerator = embeddingGenerator;
            _chatOptions = chatOptions;
        }
 
        [HttpPost("chat")]
        public async Task<ActionResult<IEnumerable<string>>> Chat(string userMessage)
        {
            var messages = new List<ChatMessage>
            {
                new(Microsoft.Extensions.AI.ChatRole.System, """
                You answer any question, Hey there, I'm Lumina, your friendly lighting assistant!
                I can help you with all your lighting needs.
                You can ask me to turn on the light, get the status of the light,
                turn off all the lights, add a new light, or delete the light.
                For update you should create an object like below.
                some time the user will pass all key values or one or two key value.
                { "id": 6, "name": "Chandelier", "Switched": false }
                Just let me know what you need and I'll do my best to help!
                """),
                new(Microsoft.Extensions.AI.ChatRole.User, userMessage)
            };
 
            var response = await _chatClient.CompleteAsync(messages,_chatOptions);
            return Ok(response.Message.Text);
        }
    }
 }


5. Remove WeatherForecast Related Code

We will remove the WeatherForecast-related code from the Program.cs file as it is not relevant to our CRUD operations.

6. Add Microsoft.EntityFrameworkCore.InMemory

To manage our light data, we will use an in-memory database provided by Microsoft.EntityFrameworkCore.InMemory. Install the package using the following command:

dotnet add package Microsoft.EntityFrameworkCore.InMemory
 


7. Add Model and Its DBContext

Let's define a Light model to represent our light entities and create a LightContext to manage the in-memory database. Here's the code:

Plain Text
 
using System.Text.Json.Serialization;
 
namespace lightsmeai.Models
 {
    public class Light
    {
        [JsonPropertyName("id")]
        public int Id { get; set; }
 
        [JsonPropertyName("name")]
        public string? Name { get; set; }
 
        [JsonPropertyName("Switched")]
        public bool? Switched { get; set; }
    }
 }
Plain Text
 
using lightsmeai.Models;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.EntityFrameworkCore;
 
namespace lightsmeai.Controllers
 {
    [Route("api/[controller]")]
    [ApiController]
    public class LightController : ControllerBase
    {
        // Initialize the context within the constructor
        private readonly LightContext _context;
 
        public LightController()
        {
            var options = new DbContextOptionsBuilder<LightContext>()
                .UseInMemoryDatabase("LightList")
                .Options;
            _context = new LightContext(options);
        }
 
        // CRUD operations implementation...
    }
 }


8. Check the LightController REST API Endpoints in Swagger

After setting up the LightController, we can check the REST API endpoints in Swagger to interact with our light management system.

9. Add Function in Program.cs

In the Program.cs file, we will add functions to expose the CRUD operations as tools for the chat client. Here's the updated code:

Plain Text
 
using Azure;
 using Azure.AI.Inference;
 using Azure.AI.OpenAI;
 using DotNetEnv;
 using lightsmeai.Controllers;
 using Microsoft.Extensions.AI;
 
// Get keys from configuration
 Env.Load(".env");
 string githubKey = Env.GetString("GITHUB_KEY");
 
var builder = WebApplication.CreateBuilder(args);
 
// Add services to the container.
 builder.Services.AddControllers();
 builder.Services.AddEndpointsApiExplorer();
 builder.Services.AddSwaggerGen();
 
// Add the chat client
 IChatClient innerChatClient = new ChatCompletionsClient(
    endpoint: new Uri("<https://models.inference.ai.azure.com>"),
    new AzureKeyCredential(githubKey))
    .AsChatClient("gpt-4o-mini");
 
builder.Services.AddChatClient(chatClientBuilder => chatClientBuilder
    .UseFunctionInvocation()
    .UseLogging()
    .Use(innerChatClient));
 
// Register embedding generator
 builder.Services.AddSingleton<IEmbeddingGenerator<string, Embedding<float>>>(sp =>
    new AzureOpenAIClient(new Uri("<https://models.inference.ai.azure.com>"),
        new AzureKeyCredential(githubKey))
        .AsEmbeddingGenerator(modelId: "text-embedding-3-large"));
 
builder.Services.AddLogging(loggingBuilder =>
    loggingBuilder.AddConsole().SetMinimumLevel(LogLevel.Trace));
 
var light = new LightController();
 var getAllLightsTool = AIFunctionFactory.Create(light.GetLights);
 var getLightTool = AIFunctionFactory.Create(light.GetLight);
 var createLightTool = AIFunctionFactory.Create(light.AddLight);
 var updateLightTool = AIFunctionFactory.Create(light.UpdateLight);
 var deleteLightTool = AIFunctionFactory.Create(light.DeleteLight);
 
var chatOptions = new ChatOptions
 {
    Tools = new[]
    {
        getAllLightsTool,
        getLightTool,
        createLightTool,
        updateLightTool,
        deleteLightTool
        }
 };
 
builder.Services.AddSingleton(light);
 builder.Services.AddSingleton(chatOptions);
 
var app = builder.Build();
 
// Configure the HTTP request pipeline.
 if (app.Environment.IsDevelopment())
 {
    app.UseSwagger();
    app.UseSwaggerUI();
 }
 
app.UseStaticFiles(); // Enable serving static files
 app.UseRouting(); // Must come before UseEndpoints
 app.UseAuthorization();
 app.MapControllers();
 // Serve index.html as the default page
 app.MapFallbackToFile("index.html");
 app.Run();


10. Make a Few Changes in ChatController

We will make a few adjustments to the ChatController to utilize the tools we exposed in the previous step. Here's the updated code:

Plain Text
 
using System.Collections.Generic;
 using System.Threading.Tasks;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.AI;
 
namespace lightsmeai.Controllers
 {
    [ApiController]
    [Route("[controller]")]
    public class ChatController : ControllerBase
    {
        private readonly IChatClient _chatClient;
        private readonly IEmbeddingGenerator<string, Embedding<float>> _embeddingGenerator;
        private readonly ChatOptions _chatOptions;
 
        public ChatController(
            IChatClient chatClient,
            IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator,
            ChatOptions chatOptions
            )
        {
            _chatClient = chatClient;
            _embeddingGenerator = embeddingGenerator;
            _chatOptions = chatOptions;
        }
 
        [HttpPost("chat")]
        public async Task<ActionResult<IEnumerable<string>>> Chat(string userMessage)
        {
            var messages = new List<ChatMessage>
            {
                new(Microsoft.Extensions.AI.ChatRole.System, """
                You answer any question, Hey there, I'm Lumina, your friendly lighting assistant!
                I can help you with all your lighting needs.
                You can ask me to turn on the light, get the status of the light,
                turn off all the lights, add a new light, or delete the light.
                For update you should create an object like below.
                some time the user will pass all key values or one or two key value.
                { "id": 6, "name": "Chandelier", "Switched": false }
                Just let me know what you need and I'll do my best to help!
                """),
                new(Microsoft.Extensions.AI.ChatRole.User, userMessage)
            };
 
            var response = await _chatClient.CompleteAsync(messages,_chatOptions);
            return Ok(response.Message.Text);
        }
    }
 }


11. Check the ChatController REST API Endpoints in Swagger

Finally, we can check the REST API endpoints in Swagger to interact with our chat controller and perform CRUD operations using natural language queries.

Wrapping Up

With this setup, users can interact with our light management system through natural language queries, and the application will respond with appropriate actions based on the user's input. The Microsoft.Extensions.AI library and the power of NLP enable us to create a more intuitive and user-friendly interface for managing lights.

API NLP Web API .NET

Opinions expressed by DZone contributors are their own.

Related

  • Alexa Skill With .NET Core
  • Serverless NLP: Implementing Sentiment Analysis Using Serverless Technologies
  • How to Merge Excel XLSX Files in Java
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!