Today the Semantic Kernel team is happy to welcome back our guest author, Akshay Kokane. We will turn it over to him to dive into his recent Medium article on Semantic Kernel.
As we advance towards an Agentic Approach in the AI world, I would like to share my insights on how Semantic Kernel can assist in building AI agents and leveraging existing APIs to create intelligent agents.
In a previous article, I discussed how Semantic Kernel can develop a multi-agent system that fosters a collaborative environment for these agents to interact and deliver results. In this new blog post, I am pleased to share that by using the OpenAPI specification, we can utilize existing APIs to integrate with agents. These are referred to as tools or plugins, which AI agents can call upon to obtain contextual data.
Using Semantic Kernel, adding plugins via the OpenAPI specification is simplified. Furthermore, these plugins are automatically triggered as and when required using the OpenAI Tool Calling. Semantic Kernel eliminates the complexities associated with tool invocation, streamlining the development of AI agents by removing the need to focus on these intricacies.
In this blog post, I will showcase how to create a chat-based application for an e-commerce platform that manages customer payments through the integration of their existing Payment Service APIs. Here are high level steps to import plugin through OpenApi:
To add a plugin to your kernel:
1. Add the following NuGet Reference:
<PackageReference Include="Microsoft.SemanticKernel.Plugins.OpenApi" Version="1.32.0-preview" />
2. Import the Plugin to the Kernel:
await kernel.ImportPluginFromOpenApiAsync(
pluginName: "paymentProcessor",
uri: new Uri("localhost:8080/swagger/v1/swagger.json"),
executionParameters: new OpenApiFunctionExecutionParameters() {
EnablePayloadNamespacing = true
}
);
This approach is transformative for enterprises aiming to leverage their existing APIs without reinventing the wheel. Additionally, the OpenAPI spec enables microservice architecture for your AI agents, facilitating easy scalability and resource management at the plugin level.
Semantic Kernel offers several methods for calling APIs. The blog includes a simple example of non-Auth APIs, but it is also possible to call Auth-based APIs. For more information, refer to the documentation on Give agents access to OpenAPI APIs | Microsoft Learn For more details, please read the full article below.
Empowering AI Agents with Tools via OpenAPI: A Hands-On Guide with Microsoft Semantic Kernel Agents
I recently explored how the OpenAPI Specification can be utilized to give actionable capabilities to AI agents. In this blog, I aim to share my insights and demonstrate the process.
Understanding the OpenAPI Specification
TThe OpenAPI Specification (formerly known as Swagger) is a standardized, language-agnostic framework for defining HTTP APIs. It allows both humans and machines t discover a service’s capabilities without needing access to its source code or documentation. Properly defined OpenAPI specs enable consumers to interact with services using minimal implementation logic.. Learn more here.
Why Use OpenAPI with AI Agents?
Many enterprises already have robust APIs in place. With the growing demand for AI-enabled applications, using OpenAPI-based plugins simplifies the process of empowering AI agents with existing APIs. Semantic Kernel, when integrated with OpenAPI, provides AI agents with detailed API semantics, including endpoint descriptions, data types, and expected responses.
For instance, imagine an e-commerce platform launching a new feature called ShopChat.AI, where customers can interact with an AI agent to find and purchase products. The AI agent can handle payments and check order statuses by leveraging the existing Payment Service APIs through OpenAPI specs.
Key Benefits of Combining OpenAPI with Semantic Kernel
Integrating OpenAPI specs with Semantic Kernel and Azure OpenAI provides numerous advantages:
1. Simplified AI Integration
OpenAPI specs offer a standardized method for AI agents to understand and interact with existing APIs, eliminating the need for complex custom integrations.
2. Enhanced Agent Functionality
By leveraging existing APIs, AI agents can handle a wide range of tasks, such as inventory management and payment processing, resulting in more versatile applications.
3. Improved Scalability
As your application grows, integrating new APIs becomes easier with OpenAPI. This ensures that your AI agents can evolve alongside your platform.
Building an AI-Powered Application with OpenAPI and Semantic Kernel
Let’s consider an example: Suppose you have a payment service for your e-commerce platform already in use. The service exposes two APIs:
payment/accept
: This API accepts credit card information, processes the payment, and returns atransactionId
.payment/status
: This API uses thetransactionId
to retrieve the payment status.
Now the e-commerce website is planning to launch new feature “ShopeChat.AI” where you can interact with AI agent like you interact with actual shopkeeper to find best product and buy that product.
Step 1: Exposing OpenAPI Specs for Your Service
Make sure your Payment Service is exposing the OpenAPI Spec. I am doing it in .net, so in .net you can expose the OpenAPI Spech by using Swagger. Complete guide can be found here https://learn.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetcore-8.0
This is using SwagsBuckler package, I will add this service
builder.Services.AddSwaggerGen(options =>
{
var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
});
Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
This should create output http://localhost:5272/swagger/v1/swagger.json.
{
"openapi": "3.0.1",
"info": {
"title": "PaymentProcessor",
"version": "1.0"
},
"paths": {
"/api/Payments/status": {
"get": {
"tags": [
"Payments"
],
"summary": "Retrieves the status of a specific payment.",
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/api/Payments/process": {
"post": {
"tags": [
"Payments"
],
"summary": "Processes a payment request.",
"requestBody": {
"description": "The payment request details.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/PaymentRequest"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/PaymentRequest"
}
},
"application/*+json": {
"schema": {
"$ref": "#/components/schemas/PaymentRequest"
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
}
},
"components": {
"schemas": {
"PaymentRequest": {
"required": [
"transactionId"
],
"type": "object",
"properties": {
"transactionId": {
"minLength": 1,
"type": "string"
},
"amount": {
"minimum": 0.01,
"type": "number",
"format": "double"
}
},
"additionalProperties": false
}
}
}
}
To ensure the Payment Service APIs are invoked correctly, I’ve added a constant static transaction ID for verification purposes:
TransactionId = “TestService123”
Step 2: Optional Deployment to Azure
While optional, deploying your service to Azure ensures accessibility and scalability. You can follow Microsoft’s official guide on deploying ASP.NET Core apps to Azure App Services for a smooth cloud integration.If you plan to host your application in the cloud, deploy it to Azure for accessibility and scalability. While optional, this step ensures seamless integration and availability for testing and deployment.
Step 3: Configuring a Semantic Kernel Agent. We will call this agent as “SalesAgent”.
We’ll set up a Semantic Kernel agent, which we’ll refer to as “SalesAgent” Here’s how to configure it:
- Add the required package reference in your project
- Import the paymentProcessor plugin from your OpenAPI specification:
<PackageReference Include="Microsoft.SemanticKernel.Plugins.OpenApi" Version="1.32.0-preview" />
await kernel.ImportPluginFromOpenApiAsync(
pluginName: "paymentProcessor",
uri: new Uri("http://payment-backend-f4bjgseugsdhddb4.eastus-01.azurewebsites.net/swagger/v1/swagger.json."),
executionParameters: new OpenApiFunctionExecutionParameters()
{
EnablePayloadNamespacing = true
}
);
Once your application is launched, the Kernel
object should display the paymentProcessor plugin. This confirms the successful integration of the APIs.
If you want to learn how to define kernel, checkout my previous blogs
Step 4: Run the app
Now that everything is set up, deploy your application and begin interacting with the Semantic Kernel agent. The system is designed to handle various tasks seamlessly.
Sample Application Overview
Here’s a quick summary of the sample application I’ve created. It includes two key plugins:
- Inventory Plugin Retrieves the latest inventory details. This plugin resides inside SalesAgent service
- PaymentProcessor Plugin This plugin is hosted on another service. Makes API calls hosted on the PaymentProcessor service for handling transactions.
Transaction id confirms that InventoryService’s both APIs were called successfully by our AI Agent!!
Conclusion: A Powerful Combination for Streamlined AI Integration
The OpenAPI Specification, along with Semantic Kernel and Azure OpenAI, presents a compelling solution for empowering AI agents with real-world capabilities. This combination offers several key benefits:
- Simplified AI Integration: OpenAPI specs provide a standardized way for AI agents to understand and interact with existing APIs, eliminating the need for complex custom integrations.
- Enhanced Agent Functionality: By leveraging existing APIs, AI agents can perform a wider range of tasks, such as processing payments or managing inventory. This leads to more versatile and helpful AI experiences.
- Improved Scalability: As your application grows, you can easily integrate new APIs using the OpenAPI, allowing your AI agents to keep pace with evolving functionalities.
This blog has provided a step-by-step guide on utilizing OpenAPI specs with Semantic Kernel Agents and Azure OpenAI. By following these steps and embracing this powerful combination, you can empower your AI agents to deliver a more comprehensive and valuable user experience.
0 comments
Be the first to start the discussion.