Browse Source

Add ChatClientWithSystemMessage abstraction and extensions

Introduced the Volo.Abp.AI.Abstractions project with a new ChatClientWithSystemMessage delegate and related extension methods. Updated the solution and AbpAIModule to include and reference the new abstractions, supporting enhanced AI chat client extensibility.
pull/23533/head
enisn 1 year ago
parent
commit
d718c05309
No known key found for this signature in database GPG Key ID: A052619F04155D1C
  1. 1963
      framework/Volo.Abp.sln
  2. 52
      framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/Delegates/ChatClientWithSystemMessage.cs
  3. 3
      framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIModule.cs
  4. 16
      framework/src/Volo.Abp.AI/Volo/Abp/AI/Extensions/ChatClientWithSystemMessageExtensions.cs

1963
framework/Volo.Abp.sln

File diff suppressed because it is too large

52
framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/Delegates/ChatClientWithSystemMessage.cs

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
namespace Volo.Abp.AI.Delegates;
public class ChatClientWithSystemMessage : DelegatingChatClient
{
public string SystemMessage { get; private set; }
public ChatClientWithSystemMessage(IChatClient innerClient, string systemMessage) : base(innerClient)
{
SystemMessage = systemMessage;
}
public override Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
{
return base.GetResponseAsync(PrepareMessages(messages), options, cancellationToken);
}
public override IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
{
return base.GetStreamingResponseAsync(PrepareMessages(messages), options, cancellationToken);
}
protected virtual List<ChatMessage> PrepareMessages(IEnumerable<ChatMessage> messages)
{
var messagesList = messages.ToList();
if(messagesList.Any(x => x.Role == ChatRole.System))
{
// If there is a system message, skip it. It might be continued conversation.
// No need to add a new one to prevent duplication.
// If developer provided system message, then it's overridden, still skipping.
// Logger.LogWarning("System message is not supported in ChatClientWithSystemMessage. Skipping.");
return messagesList;
}
if(!SystemMessage.IsNullOrEmpty())
{
messagesList.Insert(0, new ChatMessage(ChatRole.System, SystemMessage));
}
return messagesList;
}
}

3
framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIModule.cs

@ -5,6 +5,9 @@ using Volo.Abp.Modularity;
namespace Volo.Abp.AI; namespace Volo.Abp.AI;
[DependsOn(
typeof(AbpAIAbstractionsModule)
)]
public class AbpAIModule : AbpModule public class AbpAIModule : AbpModule
{ {
public override void PostConfigureServices(ServiceConfigurationContext context) public override void PostConfigureServices(ServiceConfigurationContext context)

16
framework/src/Volo.Abp.AI/Volo/Abp/AI/Extensions/ChatClientWithSystemMessageExtensions.cs

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
using Volo.Abp.AI.Delegates;
namespace Volo.Abp.AI.Extensions;
public static class ChatClientWithSystemMessageExtensions
{
public static ChatClientBuilder UseSystemMessage(this ChatClientBuilder builder, string systemMessage)
{
return builder.Use(client => new ChatClientWithSystemMessage(client, systemMessage));
}
}
Loading…
Cancel
Save