Updated the artificial intelligence documentation to clarify ABP Framework's AI integration approach, focusing on abstractions and workspaces using Microsoft's AI stacks. Simplified installation and usage instructions, provided clearer examples for Microsoft.Extensions.AI and Semantic Kernel, and improved explanations of workspaces and configuration. Removed redundant and verbose sections for better readability and maintainability.
ABP Framework provides integration for AI capabilities to your application by using Microsoft's AI stacks by using abstractions and workspaces. The main purpose of this integration is to provide a consistent way to use AI capabilities and managing different AI providers, models and configurations by using workspaces.
ABP provides a simple way to integrate AI capabilities into your applications by unifying two popular .NET AI stacks under a common concept called a "workspace":
ABP Framework doesn't implement any AI providers or models, it only provides the abstractions by using Microsoft's packages such as [Microsoft.Extensions.AI](https://learn.microsoft.com/en-us/dotnet/ai/microsoft-extensions-ai) and [Microsoft.SemanticKernel](https://learn.microsoft.com/en-us/semantic-kernel/overview/).
- Microsoft.Extensions.AI `IChatClient`
- Microsoft.SemanticKernel `Kernel`
A workspace is just a named scope. You configure providers per workspace and then resolve either default services (for the "Default" workspace) or workspace-scoped services.
ABP allows you to define a default configuration for across the application and also allows you to define isolated configurations for different different purposes by using workspaces. A workspace allows you to configure isolated AI configurations for a named scope. You can resolve AI services for a specific workspace when you need to use them.
## Installation
@ -17,152 +15,59 @@ It is suggested to use the ABP CLI to install the package. Open a command line w
abp add-package Volo.Abp.AI
```
### Manual Installation
Add nuget package to your project:
```bash
dotnet add package Volo.Abp.AI
```
Then add the module dependency to your module class:
```csharp
using Volo.Abp.AI;
using Volo.Abp.Modularity;
[DependsOn(typeof(AbpAIModule))]
public class MyProjectModule : AbpModule
{
}
```
## Usage
### Chat Client
Since ABP supports both `Microsoft.Extensions.AI` and `Microsoft.SemanticKernel`, you can use both of them in your application by resolving `IChatClient` or `IKernelAccessor` services from the [service provider](../fundamentals/dependency-injection.md).
#### Default configuration (quick start)
### Microsoft.Extensions.AI
Configure the default workspace to inject `IChatClient` directly.
You can resolve both `IChatClient` to access configured chat client from your service and use it directly.
```csharp
using Microsoft.Extensions.AI;
using Microsoft.SemanticKernel;
using Volo.Abp.AI;
using Volo.Abp.Modularity;
public class MyProjectModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
Workspaces allow multiple, isolated AI configurations. Define workspace types (optionally decorated with `WorkspaceNameAttribute`). If omitted, the type’s full name is used.
```csharp
using Volo.Abp.AI;
[WorkspaceName("GreetingAssistant")]
public class GreetingAssistant // ChatClient-only workspace
{
}
```
Configure a ChatClient workspace:
```csharp
public class MyProjectModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
public async Task<string> GetResponseAsync(string prompt)
You can also resolve `IChatClientAccessor` to access the `IChatClient` optionally configured scenarios such as developing a module or a service that may use AI capabilities **optionally**.
```csharp
public class MyProjectModule : AbpModule
public class MyService
{
public override void ConfigureServices(ServiceConfigurationContext context)
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.
```csharp
public class MyService
{
private readonly IKernelAccessor _kernelAccessor;
@ -171,137 +76,59 @@ public class MyService
_kernelAccessor = kernelAccessor;
}
public async Task DoSomethingAsync()
{
var kernel = _kernelAccessor.Kernel; // Kernel might be null if no workspace is configured.
var result = await kernel.InvokeAsync(/*... */);
}
}
```
#### Workspace configuration
```csharp
public class MyProjectModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
public async Task<string> GetResponseAsync(string prompt)
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)_.
private readonly IChatClient<ContentPlanner> _chatClient; // available even if only Kernel is configured
public PlanningService(
IKernelAccessor<ContentPlanner> kernelAccessor,
IChatClient<ContentPlanner> chatClient)
{
_kernelAccessor = kernelAccessor;
_chatClient = chatClient;
}
public async Task<string> PlanAsync(string topic)
{
var kernel = _kernelAccessor.Kernel; // Microsoft.SemanticKernel.Kernel
// Use Semantic Kernel APIs if needed...
var response = await _chatClient.GetResponseAsync(
[new ChatMessage(ChatRole.User, $"Create a content plan for: {topic}")]
);
return response?.Message?.Text ?? string.Empty;
}
}
```
## Options
> [!NOTE]
> If you don't specify the workspace name, the full name of the class will be used as the workspace name.
`AbpAIOptions` configuration pattern offers `ConfigureChatClient(...)` and `ConfigureKernel(...)` methods for configuration. These methods are defined in the `WorkspaceConfiguration` class. They are used to configure the `ChatClient` and `Kernel` respectively.
`Builder` is set once and is used to build the `ChatClient` or `Kernel` instance. `BuilderConfigurers` is a list of actions that are applied to the `Builder` instance for incremental changes. These actions are executed in the order they are added.
If a workspace configures only the Kernel, a chat client may still be exposed for that workspace through the Kernel’s service provider (when available).
## Advanced Usage and Customizations
### Addding Your Own DelegatingChatClient
If you want to build your own decorator, implement a `DelegatingChatClient` derivative and provide an extension method that adds it to the `ChatClientBuilder` using `builder.Use(...)`.
Example sketch:
You can resolve generic versions of `IChatClient`, `IChatClientAccessor` or `IKernelAccessor` services for a specific workspace as generic arguments. If Chat Client or Kernel is not configured for a workspace, you will get `null` from the accessor services. You should check the accessor before using it. This applies only for specified workspaces. Another workspace may have a configured Chat Client or Kernel.
```csharp
using Microsoft.Extensions.AI;
public class SystemMessageChatClient : DelegatingChatClient
public class MyService
{
public SystemMessageChatClient(IChatClient inner, string systemMessage) : base(inner)