Browse Source

Add AI chat client delegates and extension methods

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
Enis Necipoglu 1 year ago
parent
commit
451b973b09
No known key found for this signature in database GPG Key ID: 1EC55E13241E1680
  1. 1963
      framework/Volo.Abp.sln
  2. 54
      framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/Delegates/ChatClientWithSystemMessage.cs
  3. 56
      framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/Delegates/ChatClientWithTemperature.cs
  4. 73
      framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/Delegates/ChatClientWithTools.cs
  5. 16
      framework/src/Volo.Abp.AI/Volo/Abp/AI/Extensions/ChatClientWithSystemMessageExtensions.cs
  6. 16
      framework/src/Volo.Abp.AI/Volo/Abp/AI/Extensions/ChatClientWithTemperatureExtensions.cs
  7. 21
      framework/src/Volo.Abp.AI/Volo/Abp/AI/Extensions/ChatClientWithToolsExtensions.cs

1963
framework/Volo.Abp.sln

File diff suppressed because it is too large

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

@ -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;
}
}

56
framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/Delegates/ChatClientWithTemperature.cs

@ -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;
}
}

73
framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/Delegates/ChatClientWithTools.cs

@ -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;
}
}

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(chatClient => new ChatClientWithSystemMessage(chatClient, systemMessage));
}
}

16
framework/src/Volo.Abp.AI/Volo/Abp/AI/Extensions/ChatClientWithTemperatureExtensions.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 ChatClientWithTemperatureExtensions
{
public static ChatClientBuilder UseTemperature(this ChatClientBuilder builder, float temperature)
{
return builder.Use(chatClient => new ChatClientWithTemperature(chatClient, temperature));
}
}

21
framework/src/Volo.Abp.AI/Volo/Abp/AI/Extensions/ChatClientWithToolsExtensions.cs

@ -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…
Cancel
Save