mirror of https://github.com/abpframework/abp.git
Browse Source
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
4 changed files with 2034 additions and 0 deletions
File diff suppressed because it is too large
@ -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; |
|||
} |
|||
} |
|||
@ -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…
Reference in new issue