mirror of https://github.com/abpframework/abp.git
Browse Source
Removed ChatClientWithSystemMessage and its extension, consolidating system message logic into user-extensible patterns. Renamed ChatClientNameAttribute to WorkspaceNameAttribute and restricted its usage to classes. Added ConfigureDefault to WorkspaceConfigurationDictionary for easier default workspace setup. Updated documentation to reflect these changes and clarify configuration patterns.pull/23533/head
8 changed files with 84 additions and 126 deletions
@ -1,52 +0,0 @@ |
|||
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; |
|||
} |
|||
} |
|||
@ -1,9 +1,14 @@ |
|||
|
|||
using Microsoft.SemanticKernel; |
|||
|
|||
namespace Volo.Abp.AI; |
|||
|
|||
public interface IKernelAccessor<TWorkSpace> |
|||
where TWorkSpace : class |
|||
public interface IKernelAccessor |
|||
{ |
|||
Kernel? Kernel { get; } |
|||
} |
|||
|
|||
public interface IKernelAccessor<TWorkSpace> : IKernelAccessor |
|||
where TWorkSpace : class |
|||
{ |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using System; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.SemanticKernel; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AI; |
|||
|
|||
[ExposeServices(typeof(IKernelAccessor))] |
|||
public class DefaultKernelAccessor : IKernelAccessor, ITransientDependency |
|||
{ |
|||
public Kernel? Kernel { get; } |
|||
|
|||
public DefaultKernelAccessor(IServiceProvider serviceProvider) |
|||
{ |
|||
Kernel = serviceProvider.GetKeyedService<Kernel>( |
|||
AbpAIModule.DefaultWorkspaceName); |
|||
} |
|||
} |
|||
@ -1,12 +0,0 @@ |
|||
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…
Reference in new issue