Building Intelligent Workflows with Semantic Kernel Pipelines

When it comes to automating workflows, breaking down complex tasks into smaller, modular steps can make the process more efficient and maintainable. Semantic Kernel (SK) provides a powerful way to achieve this through pipelines. In this post, we’ll explore how to create and execute a pipeline that processes a number through parsing, arithmetic, truncation, and humanization. Pleae note that the SK pipeline described in this example is not the state-full machine designed for long running processes. If you want to leverage such scenarios, I recommend using Azure Durable Functions.

Why Use Semantic Kernel Pipelines?

Semantic Kernel pipelines allow you to:

Modularize functionality into reusable components.
Chain together functions to handle complex workflows.
Integrate with AI capabilities such as prompt-based generation.
Let’s dive into a practical example where we build a pipeline to take a string, process it numerically, and then convert it into a spelled-out English phrase.

The Workflow

In this example, I use the pipeline to solve the following problem:

  1. Parse a string representation of a number (e.g., "123.456") into a double.
  2. Multiply the double by another double (e.g., 78.90).
  3. Truncate the resulting value to an integer.
  4. Convert the integer into its English word representation (e.g., "nine thousand seven hundred forty").
    public async Task DemoPipelineAsync()
    {
        IKernelBuilder builder = Kernel.CreateBuilder();
        builder.AddOpenAIChatCompletion(
            TestConfiguration.OpenAI.ChatModelId,
            TestConfiguration.OpenAI.ApiKey);
        builder.Services.AddLogging(c => c.AddConsole().SetMinimumLevel(LogLevel.Trace));
        Kernel kernel = builder.Build();

            KernelFunction parseDouble = KernelFunctionFactory.CreateFromMethod((string s) => double.Parse(s, CultureInfo.InvariantCulture), "parseDouble");
            KernelFunction multiplyByN = KernelFunctionFactory.CreateFromMethod((double i, double n) => i * n, "multiplyByN");
            KernelFunction truncate = KernelFunctionFactory.CreateFromMethod((double d) => (int)d, "truncate");
            KernelFunction humanize = KernelFunctionFactory.CreateFromPrompt(new PromptTemplateConfig()
            {
                Template = "Spell out this number in English: {{$number}}",
                InputVariables = [new() { Name = "number" }],
            });
            KernelFunction pipeline = KernelFunctionCombinators.Pipe([parseDouble, multiplyByN, truncate, humanize], "pipeline");

            KernelArguments args = new()
            {
                ["s"] = "123.456",
                ["n"] = (double)78.90,
            };

            // - The parseInt32 function will be invoked, read "123.456" from the arguments, and parse it into (double)123.456.
            // - The multiplyByN function will be invoked, with i=123.456 and n=78.90, and return (double)9740.6784.
            // - The truncate function will be invoked, with d=9740.6784, and return (int)9740, which will be the final result.
            Console.WriteLine(await pipeline.InvokeAsync(kernel, args));
}

Creating the Pipeline

Step 1: Define the Functions
We start by defining individual functions for each step in the workflow. Using Semantic Kernel’s KernelFunctionFactory, we create these modular functions:

Parsing a String into a Double: Converts the string "123.456" into the numeric value 123.456.
Multiplication: Multiplies the parsed number by a given multiplier.
Truncation: Truncates the result to an integer.
Humanization: Converts the integer into a spelled-out English string using a prompt-based function.

Step 2: Combine Functions into a Pipeline
With the functions ready, we use KernelFunctionCombinators.Pipe to chain them together into a pipeline. The output of one function feeds directly into the next, ensuring a seamless data flow.

Step 3: Provide Input Arguments
The pipeline takes input in the form of KernelArguments. For our example, we provide:

"123.456" as the string to parse.
78.90 as the multiplier.

Step 4: Execute the Pipeline
Finally, the pipeline is invoked with the input arguments. Each function is executed sequentially, producing the final human-readable result.

Wrap-up

Semantic Kernel pipelines make it easy to build intelligent workflows that combine traditional logic with AI capabilities. Whether you’re processing numbers, analyzing text, or orchestrating complex tasks, pipelines offer a structured and efficient approach to solving problems.

If you’re looking to build smarter applications for the new software era, try Semantic Kernel! With a little creativity, the possibilities are endless.


comments powered by Disqus