mirror of https://github.com/abpframework/abp.git
Browse Source
Introduced new delegates for ChatClient with system message, temperature, and tools in the Volo.Abp.AI.Abstractions project. Added corresponding extension methods in Volo.Abp.AI. Updated the solution file to include the new project and configuration platforms.pull/23533/head
7 changed files with 2199 additions and 0 deletions
File diff suppressed because it is too large
@ -0,0 +1,54 @@ |
|||
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 |
|||
{ |
|||
private readonly string _systemMessage; |
|||
|
|||
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( |
|||
BuildMessageList(messages), |
|||
options, |
|||
cancellationToken |
|||
); |
|||
} |
|||
|
|||
public override IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync( |
|||
IEnumerable<ChatMessage> messages, |
|||
ChatOptions? options = null, |
|||
CancellationToken cancellationToken = default) |
|||
{ |
|||
return base.GetStreamingResponseAsync( |
|||
BuildMessageList(messages), |
|||
options, |
|||
cancellationToken |
|||
); |
|||
} |
|||
|
|||
private List<ChatMessage> BuildMessageList(IEnumerable<ChatMessage> messages) |
|||
{ |
|||
var messagesList = messages.ToList(); |
|||
|
|||
if (messagesList.Count <= 0 || messagesList[0].Role != ChatRole.System) |
|||
{ |
|||
messagesList.AddFirst(new ChatMessage(ChatRole.System, _systemMessage)); |
|||
} |
|||
|
|||
return messagesList; |
|||
} |
|||
} |
|||
@ -0,0 +1,56 @@ |
|||
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 ChatClientWithTemperature : DelegatingChatClient |
|||
{ |
|||
private readonly float _temperature; |
|||
|
|||
public ChatClientWithTemperature(IChatClient innerClient, float temperature) |
|||
: base(innerClient) |
|||
{ |
|||
_temperature = temperature; |
|||
} |
|||
|
|||
public override Task<ChatResponse> GetResponseAsync( |
|||
IEnumerable<ChatMessage> messages, |
|||
ChatOptions? options = null, |
|||
CancellationToken cancellationToken = default) |
|||
{ |
|||
return base.GetResponseAsync( |
|||
messages, |
|||
BuildChatOptions(options), |
|||
cancellationToken |
|||
); |
|||
} |
|||
|
|||
public override IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync( |
|||
IEnumerable<ChatMessage> messages, |
|||
ChatOptions? options = null, |
|||
CancellationToken cancellationToken = default) |
|||
{ |
|||
return base.GetStreamingResponseAsync( |
|||
messages, |
|||
BuildChatOptions(options), |
|||
cancellationToken |
|||
); |
|||
} |
|||
|
|||
private ChatOptions BuildChatOptions(ChatOptions? options) |
|||
{ |
|||
if (options?.Temperature != null) |
|||
{ |
|||
return options; |
|||
} |
|||
|
|||
options ??= new ChatOptions(); |
|||
|
|||
options.Temperature ??= _temperature; |
|||
|
|||
return options; |
|||
} |
|||
} |
|||
@ -0,0 +1,73 @@ |
|||
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 ChatClientWithTools : DelegatingChatClient |
|||
{ |
|||
public IList<AIFunction> Tools { get; private set; } |
|||
|
|||
public ChatToolMode? ToolMode { get; private set; } |
|||
|
|||
public bool? AllowMultipleToolCalls { get; private set; } |
|||
|
|||
public ChatClientWithTools(IChatClient innerClient, IEnumerable<AIFunction> tools, ChatToolMode? toolMode = null, bool? allowMultipleToolCalls = null) |
|||
: base(innerClient) |
|||
{ |
|||
Tools = tools.ToList(); |
|||
ToolMode = toolMode; |
|||
AllowMultipleToolCalls = allowMultipleToolCalls; |
|||
} |
|||
|
|||
public override Task<ChatResponse> GetResponseAsync( |
|||
IEnumerable<ChatMessage> messages, |
|||
ChatOptions? options = null, |
|||
CancellationToken cancellationToken = default) |
|||
{ |
|||
return base.GetResponseAsync( |
|||
messages, |
|||
BuildChatOptions(options), |
|||
cancellationToken |
|||
); |
|||
} |
|||
|
|||
public override IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync( |
|||
IEnumerable<ChatMessage> messages, |
|||
ChatOptions? options = null, |
|||
CancellationToken cancellationToken = default) |
|||
{ |
|||
return base.GetStreamingResponseAsync( |
|||
messages, |
|||
BuildChatOptions(options), |
|||
cancellationToken |
|||
); |
|||
} |
|||
|
|||
private ChatOptions BuildChatOptions(ChatOptions? options) |
|||
{ |
|||
options ??= new ChatOptions(); |
|||
|
|||
options.Tools ??= new List<AITool>(); |
|||
|
|||
foreach (var tool in Tools) |
|||
{ |
|||
options.Tools.Add(tool); |
|||
} |
|||
|
|||
if (ToolMode is not null) |
|||
{ |
|||
options.ToolMode = ToolMode; |
|||
} |
|||
|
|||
if (AllowMultipleToolCalls is not null) |
|||
{ |
|||
options.AllowMultipleToolCalls = AllowMultipleToolCalls.Value; |
|||
} |
|||
|
|||
return options; |
|||
} |
|||
} |
|||
@ -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(chatClient => new ChatClientWithSystemMessage(chatClient, systemMessage)); |
|||
} |
|||
} |
|||
@ -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 ChatClientWithTemperatureExtensions |
|||
{ |
|||
public static ChatClientBuilder UseTemperature(this ChatClientBuilder builder, float temperature) |
|||
{ |
|||
return builder.Use(chatClient => new ChatClientWithTemperature(chatClient, temperature)); |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
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 ChatClientWithToolsExtensions |
|||
{ |
|||
public static ChatClientBuilder UseTools( |
|||
this ChatClientBuilder builder, |
|||
IEnumerable<AIFunction> tools, |
|||
ChatToolMode? toolMode = null, |
|||
bool? allowMultipleToolCalls = null) |
|||
{ |
|||
return builder.Use(chatClient |
|||
=> new ChatClientWithTools(chatClient, tools, toolMode, allowMultipleToolCalls)); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue