Microsoft.SemanticKernel
Microsoft.SemanticKernel is a library that provides a unified SDK for integrating AI services. This documentation is about the usage of this library with ABP Framework. Make sure you have read the Artificial Intelligence documentation before reading this documentation.
Usage
Semantic Kernel can be used by resolving IKernelAccessor service that carries the Kernel instance. Kernel might be null if no workspace is configured. You should check the kernel before using it.
public class MyService
{
private readonly IKernelAccessor _kernelAccessor;
public MyService(IKernelAccessor kernelAccessor)
{
_kernelAccessor = kernelAccessor;
}
public async Task<string> GetResponseAsync(string prompt)
{
var kernel = _kernelAccessor.Kernel;
if (kernel is null)
{
return "No kernel configured";
}
return await kernel.InvokeAsync(prompt);
}
}
Workspaces
Workspaces are a way to configure isolated AI configurations for a named scope. You can define a workspace by decorating a class with the WorkspaceNameAttribute attribute that carries the workspace name.
- Workspace names must be unique.
- Workspace names cannot contain spaces (use underscores or camelCase).
- Workspace names are case-sensitive.
using Volo.Abp.AI;
[WorkspaceName("CommentSummarization")]
public class CommentSummarization
{
}
[!NOTE] If you don't specify the workspace name, the full name of the class will be used as the workspace name.
You can resolve generic versions of IKernelAccessor service for a specific workspace as generic arguments. If Kernel is not configured for a workspace, you will get null from the accessor service. You should check the accessor before using it. This applies only for specified workspaces. Another workspace may have a configured Kernel.
IKernelAccessor<TWorkSpace> can be resolved to access a specific workspace's kernel. This is a typed kernel accessor and each workspace can have its own kernel configuration.
Example of resolving a typed kernel accessor:
public class MyService
{
private readonly IKernelAccessor<CommentSummarization> _kernelAccessor;
}
public async Task<string> GetResponseAsync(string prompt)
{
var kernel = _kernelAccessor.Kernel;
if (kernel is null)
{
return "No kernel configured";
}
return await kernel.InvokeAsync(prompt);
}
}
Configuration
AbpAIWorkspaceOptions configuration is used to configure AI workspaces and their configurations. You can configure the default workspace and also configure isolated workspaces by using the this options class.It has to be configured before the services are configured in the PreConfigure method of your module class. It is important since the services are registered after the configuration is applied.
AbpAIWorkspaceOptionshas aWorkspacesproperty that is type ofWorkspaceConfigurationDictionarywhich is a dictionary of workspace names and their configurations. It providesConfigure<T>andConfigureDefaultmethods to configure the default workspace and also configure isolated workspaces by using the workspace type.Configure method passes
WorkspaceConfigurationobject to the configure action. You can configure theKernelby using theConfigureKernelmethod.ConfigureKernel()method passesKernelConfigurationparameter to the configure action. You can configure theBuilderandBuilderConfigurersby using theConfigureBuildermethod.Builderis set once and is used to build theKernelinstance.BuilderConfigurersis a list of actions that are applied to theBuilderinstance for incremental changes.These actions are executed in the order they are added.
To configure a kernel, you'll need a kernel connector package such as Microsoft.SemanticKernel.Connectors.OpenAI to configure a kernel to use a specific LLM provider.
The following example requires Microsoft.SemanticKernel.Connectors.AzureOpenAI package to be installed.
Demonstration of the default workspace configuration:
[DependsOn(typeof(AbpAIModule))]
public class MyProjectModule : AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
PreConfigure<AbpAIOptions>(options =>
{
options.Workspaces.ConfigureDefault(configuration =>
{
configuration.ConfigureKernel(kernelConfiguration =>
{
kernelConfiguration.Builder = Kernel.CreateBuilder()
.AddAzureOpenAIChatClient("...", "...");
});
// Note: Chat client is not configured here
});
});
}
}
Demonstration of the isolated workspace configuration:
[DependsOn(typeof(AbpAIModule))]
public class MyProjectModule : AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
PreConfigure<AbpAIOptions>(options =>
{
options.Workspaces.Configure<CommentSummarization>(configuration =>
{
configuration.ConfigureKernel(kernelConfiguration =>
{
kernelConfiguration.Builder = Kernel.CreateBuilder()
.AddAzureOpenAIChatClient("...", "...");
});
});
});
}
}