How Can Microsoft’s Semantic Kernel Revolutionize AI Orchestration?

July 23, 2024

Microsoft’s Semantic Kernel represents a significant breakthrough in AI orchestration by providing an open-source SDK that integrates large language models (LLMs) with external data and functionalities. This toolkit goes beyond the conventional function calling to autonomously generate plans, utilizing LLMs and templates to fulfill user requests. While LLMs alone might be seen as “stochastic parrots,” Semantic Kernel leverages them to generate actionable, coherent plans. This feature can significantly curtail the risks of “hallucinations” often associated with LLMs and create practical, reliable AI agents. Offering support for multiple programming languages like C#, Python, and Java, this SDK is engineered to be user-friendly and versatile.

Semantic Kernel fills a critical role in Microsoft’s AI ecosystem by serving as the orchestration layer in the company’s open-source LLM application stack. Just as Copilot functions as a collaborative AI agent, Semantic Kernel sits at the core, tying together various components seamlessly. Being a versatile SDK, it’s capable of executing sophisticated tasks such as email automation and data retrieval, making it an essential tool for developers aiming to build robust AI applications. Microsoft’s intention is clear: they want Semantic Kernel to be the go-to tool for developers looking to integrate AI effortlessly into their software projects.

1. Initialize a Handlebars Planner Object

To begin your journey with Semantic Kernel, you’ll first need to create a Handlebars planner object. This step involves constructing a Handlebars planning instance. It’s a straightforward process, and the following code snippet illustrates how you can initialize this object in C#:

“`csharp
var planner = new HandlebarsPlanner(new HandlebarsPlannerOptions() { AllowLoops = true });
“`

The significance of initializing this planner object lies in preparing the framework to understand and execute complex queries. Essentially, the planner serves as an intermediary layer that intelligently routes the “asks” or requests, plans their fulfillment by invoking appropriate plugins, and maps out a logical sequence of actions required to achieve the desired outcome. By setting the option `AllowLoops` to true, you’re permitting the planner to consider iterative processes, thus adding a layer of flexibility and robustness to your AI applications.

2. Formulate and Execute a Plan

Once the Handlebars Planner Object is initialized, the next step is to formulate and execute a plan. This crucial segment of AI orchestration involves devising a plan utilizing the planning instance. Here’s how you can do this in C#:

“`csharp
var plan = await planner.CreatePlanAsync(kernelWithMath, problem);
this._logger.LogInformation(“Plan: {Plan}”, plan);
“`

In this code snippet, the `CreatePlanAsync` method instructs the planner to develop a detailed plan based on the problem statement and the available plugins. Logging this plan serves as an excellent debugging aid. Following plan formulation, the next phase is to implement or execute the plan. The following code helps achieve this:

“`csharp
var result = (await plan.InvokeAsync(kernelWithMath)).Trim();
this._logger.LogInformation(“Results: {Result}”, result);
“`

Executing the plan involves invoking the AI services and using the plugin functions to find a solution. The final result is logged, allowing developers to inspect the outcomes and ensure that the plan fulfills its intended purpose. This ability to dynamically create and implement plans elevates Semantic Kernel beyond mere function calling, making it a robust orchestration tool.

3. Verify and Log Initial Values

The third step in utilizing Microsoft’s Semantic Kernel involves verifying and logging the initial values crucial to the execution of the plan. This step ensures that all essential variables within the prompt are accurately identified and appropriately managed. The following markdown serves as a set of instructions to make this process as effective as possible:

“`markdown

Start

Now take a deep breath and accomplish the task:
1. Keep the template concise. Be as efficient as possible.
2. Do not make up helpers or functions that were not provided to you, and be especially careful to NOT assume or use any helpers or operations that were not explicitly defined already.
3. If you can’t fully accomplish the goal with the available helpers, just print “{{insufficientFunctionsErrorMessage}}”.
4. Always start by identifying any important values in the goal. Then, use the `{{set}}` helper to create variables for each of these values.
5. The template should use the `{{json}}` helper at least once to output the result of the final step.
6. Don’t forget to use the tips and tricks otherwise the template will not work.
7. Don’t close the `{{` handlebars block until you’re done with all the steps.
“`

These instructions ensure that the planner operates within a defined framework, adhering to specific rules and guidelines to avoid erroneous results. The use of the `{{set}}` helper to create variables from the identified values adds to the clarity and accuracy of the generated plans. Here, we also stress the importance of using the `{{json}}` helper at least once to provide a tangible output from the final step. This meticulous planning and verification prevent unexpected outcomes and help maintain the operational integrity of the AI applications.

4. Render the Prompt Including Function Definitions

The next step is to render the prompt while including all the available function definitions. This is important for transparency and accountability within the AI orchestration process. A detailed list of all available functions ensures that planners can select the appropriate functions for specific tasks. Here’s how you can list and define these functions:

“`markdown
[AVAILABLE FUNCTIONS]

`{{MathPlugin-Add}}`

Description: Add two numbers
Inputs:
– number1 double – The first number to add (required)
– number2 double – The second number to add (required)
Output: double

`{{MathPlugin-Divide}}`

Description: Divide two numbers
Inputs:
– number1: double – The first number to divide from (required)
– number2: double – The second number to divide by (required)
Output: double
“`

Rendering these function definitions serves multiple purposes. Firstly, it ensures that the planner and the subsequent AI services have access to a complete set of operations that can be used to achieve the end goal. Secondly, it provides human-readable documentation that can be useful for debugging and auditing purposes. By ensuring that all functions are comprehensively defined, the framework minimizes the risk of running into unexpected errors or inadequacies during execution.

5. Review Generated Plan

Once the prompt has been rendered with all function definitions, the next step is to review the generated plan for correctness. This involves a meticulous check to ensure that the plan aligns with the initial problem statement and uses the defined functions appropriately. Here’s an example of how a typical generated plan might look:

“`markdown
Plugins.MathSolver: Information: Plan:
{{!– Step 1: Set the initial investment amount –}}
{{set “initialInvestment” 2130.23}}
{{!– Step 2: Calculate the increase percentage –}}
{{set “increasePercentage” 0.23}}
{{!– Step 3: Calculate the final amount after the increase –}}
{{set “finalAmount” (MathPlugin-Multiply (get “initialInvestment”) (MathPlugin-Add 1 (get “increasePercentage”)))}}
{{!– Step 4: Output the final amount –}}
{{json (get “finalAmount”)}}
“`

Desk-checking the logic within these steps helps ensure the plan is sound and capable of producing accurate results. It’s crucial to confirm that all expected operations are included in the steps. For example, in financial calculations, a step for subtracting fees or additional costs might be necessary but could be missing. Identifying such omissions during the review phase allows for adjustments before the plan is executed, thereby enhancing the overall reliability and efficiency of the AI application.

6. Update Documentation and Examples

The sixth step involves updating the documentation and ensuring that examples align with the current reality of the project. Given the evolving nature of software and AI frameworks, keeping documentation up-to-date is crucial for both new and seasoned developers. Proper documentation ensures that all functionalities and features are correctly represented, making it easier for developers to implement and utilize the Semantic Kernel effectively.

For instance, a review might identify missing steps or inaccuracies in the plan logic that need to be addressed. This requirement becomes particularly important if the project’s scope has expanded or if new features have been added recently. Additionally, ensuring that examples are updated aids in reducing the learning curve for new users and helps in maintaining consistency throughout the development process. Keeping everything documented accurately can also help highlight improvements or changes needed in the future.

7. Recommendations for Learning and Installation

Getting started with Semantic Kernel requires proper installation steps that vary depending on the programming language. The process is straightforward for each supported language, but it’s crucial to follow the recommended guidelines to ensure proper setup and functionality. For C#, Semantic Kernel can be installed from NuGet using the following command:

“`csharp
dotnet add package Microsoft.SemanticKernel
“`

For Python, installation is done through PyPI with the following command:

“`markdown
pip install semantic-kernel
“`

If there are issues with `pip`, you might need to use `pip3` instead. For Java, building the project using Maven from the repository will pull in all required dependencies. No matter which language you choose, having a well-documented installation process ensures a seamless experience and helps avoid common pitfalls. Proper installation is the first step toward exploring the full potential of Semantic Kernel and integrating it into your AI projects.

8. Secure API Key

An essential step in setting up Semantic Kernel involves obtaining and securely storing an API key. This key can be acquired from either OpenAI or Azure OpenAI. Secure storage of this key is crucial to prevent unauthorized access and ensure the smooth functioning of your AI applications. The API key acts as a gateway, enabling the Semantic Kernel to make calls to LLMs and other services. By ensuring the key is stored in a secure location, you can protect your application from potential security breaches. Additionally, properly integrating the API key with your code is vital, as it allows the Semantic Kernel to operate without interruptions.

9. Execute Sample Projects

“`csharp
var planner = new HandlebarsPlanner(new HandlebarsPlannerOptions() { AllowLoops = true });
“`

The value of initializing this planner object lies in setting up the framework to interpret and execute intricate queries effectively. The planner operates as a crucial intermediary layer, which skillfully directs the “asks” or requests and plans their execution by calling upon suitable plugins. It meticulously maps out a logical sequence of steps required to achieve the desired outcome, ensuring all actions are precisely coordinated. The inclusion of the `AllowLoops` option set to true is particularly significant. This setting enables the planner to consider iterative processes, which adds an extra dimension of flexibility and resilience to your AI applications. Iterative processes are essential for scenarios where repeated adjustments or refinements are necessary to arrive at an optimal solution. By accommodating loops, the system can handle more sophisticated tasks that might require multiple iterations to resolve. This capability is indispensable in dynamic environments where conditions can change unpredictably. Consequently, setting `AllowLoops` to true ensures that your application is better equipped to adapt, allowing it to deliver more robust and reliable performance. Overall, this thoughtful initialization of the planner fosters a more intelligent, versatile, and efficient AI-driven system, capable of addressing complex challenges with precision and agility.

Subscribe to our weekly news digest!

Join now and become a part of our fast-growing community.

Invalid Email Address
Thanks for subscribing.
We'll be sending you our best soon.
Something went wrong, please try again later