Automate E-Commerce Product Descriptions with .NET, Microsoft Semantic Kernel & SLM (Phi-3)


In the highly competitive world of e-commerce, the quality and accuracy of product descriptions can make the difference between a successful sale and an abandoned cart. Detailed, attractive, and well-written descriptions not only inform customers but also enhance your site’s SEO, driving more organic traffic. In this post, I’ll demonstrate how you can automate the generation of product descriptions using .NET, Microsoft Semantic Kernel and Phi-3.

Introduction

Imagine managing an inventory of hundreds or even thousands of products. Writing unique descriptions for each one can be an overwhelming task. This is where artificial intelligence comes into play, providing an efficient and scalable solution for generating compelling and precise descriptions based on each product’s attributes. Below, I’ll share the necessary dependencies and the program code to achieve this.

Required Dependencies

First of all remember to download the Phi-3 model used for this sample from here: https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx.

Make sure to include the following NuGet packages in your project:

<PackageReference Include="feiyun0112.SemanticKernel.Connectors.OnnxRuntimeGenAI.CPU" Version="1.0.0" />
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.18.1" />
<PackageReference Include="Microsoft.ML.OnnxRuntimeGenAI" Version="0.3.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.15.1" />

Program Code

Here is the code that leverages these dependencies to automate product description generation:

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using System.Drawing;
using System.Text;

const string modelPath =
    @"C:\phi-3\models\Phi-3-mini-4k-instruct-onnx\cpu_and_mobile\cpu-int4-awq-block-128";

const string culture = "en-us";
const string currency = "eur";

var products = new List<Product>
{
    new Product("MacBook Pro", "Electronics", "MBP2023", Color.Silver, 2399.99),
    new Product("ErgoChair 2", "Furniture", "ERGO2", Color.Gray, 349.99),
    new Product("Nike Air Max", "Footwear", "AIRMAX2023", Color.White, 149.99),
    new Product("Samsung Galaxy S21", "Electronics", "S21", Color.Pink, 799.99),
    new Product("Sony WH-1000XM4", "Electronics", "WH1000XM4", Color.Black, 349.99),
    new Product("IKEA Malm Desk", "Furniture", "MALM001", Color.Brown, 199.99),
};

// Create kernel
var builder = Kernel.CreateBuilder();
builder.AddOnnxRuntimeGenAIChatCompletion(modelPath: modelPath);
var kernel = builder.Build();

// Create chat
var chat = kernel.GetRequiredService<IChatCompletionService>();

foreach (var p in products)
{
    Console.WriteLine($"Generating description for product: {p.Name} ...");

    var question = GetQuestion(p);

    var history = new ChatHistory();
    history.AddUserMessage(question);

    var response = new StringBuilder();
    var result = chat.GetStreamingChatMessageContentsAsync(history);
    await foreach (var message in result)
    {
        response.Append(message.Content);
    }

    history.AddAssistantMessage(response.ToString());

    p.AddDescription(response.ToString());

    Console.WriteLine(response.ToString());
    Console.WriteLine();
}

return;

string GetQuestion(Product p) =>
    $"Get a unique sentence to describe (in language based in the culture '{culture}' and price based in the currency '{currency}') in no more 500 characters. Make sure that in the maximum of these exactly 500 characters the description is completed and finalize with a dot. " +
    $"The product: Category: {p.Type}, Type: {p.Type}, Color: {p.Color}, Model: {p.Model}, Price: {p.Price}.";

public record Product(string Name, string Type, string Model, Color Color, double Price)
{
    public string? Description { get; private set; }
    public void AddDescription(string description) => this.Description = description;
};

Output

The program generates descriptions for each product based on its attributes. Below is an example of the output for the sample list of the products:

By implementing this code, you can streamline the process of creating product descriptions for your e-commerce platform, saving time and ensuring consistency and quality in your product listings.

Happy AI coding

References

Un comentario sobre “Automate E-Commerce Product Descriptions with .NET, Microsoft Semantic Kernel & SLM (Phi-3)

Deja un comentario

Este sitio utiliza Akismet para reducir el spam. Conoce cómo se procesan los datos de tus comentarios.