11 changed files with 313 additions and 34 deletions
@ -1,13 +1,12 @@ |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Agents.AI; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace LINGYUN.Abp.AI.Agent; |
|||
public interface IChatClientAgentFactory |
|||
{ |
|||
[NotNull] |
|||
Task<ChatClientAgent> CreateAsync<TWorkspace>(); |
|||
Task<WorkspaceAIAgent> CreateAsync<TWorkspace>(); |
|||
|
|||
[NotNull] |
|||
Task<ChatClientAgent> CreateAsync(string workspace); |
|||
Task<WorkspaceAIAgent> CreateAsync(string workspace); |
|||
} |
|||
|
|||
@ -0,0 +1,59 @@ |
|||
using LINGYUN.Abp.AI.Workspaces; |
|||
using Microsoft.Agents.AI; |
|||
using Microsoft.Extensions.AI; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text.Json; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace LINGYUN.Abp.AI.Agent; |
|||
public class WorkspaceAIAgent : AIAgent |
|||
{ |
|||
protected AIAgent InnerAIAgent { get; } |
|||
public WorkspaceDefinition? Workspace { get; } |
|||
|
|||
public WorkspaceAIAgent( |
|||
AIAgent innerAIAgent, |
|||
WorkspaceDefinition? workspace = null) |
|||
{ |
|||
InnerAIAgent = innerAIAgent; |
|||
Workspace = workspace; |
|||
} |
|||
|
|||
public override AgentThread DeserializeThread(JsonElement serializedThread, JsonSerializerOptions? jsonSerializerOptions = null) |
|||
{ |
|||
return InnerAIAgent.DeserializeThread(serializedThread, jsonSerializerOptions); |
|||
} |
|||
|
|||
public override AgentThread GetNewThread() |
|||
{ |
|||
return InnerAIAgent.GetNewThread(); |
|||
} |
|||
|
|||
protected override Task<AgentRunResponse> RunCoreAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) |
|||
{ |
|||
return InnerAIAgent.RunAsync(GetChatMessages(messages), thread, options, cancellationToken); |
|||
} |
|||
|
|||
protected override IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) |
|||
{ |
|||
return InnerAIAgent.RunStreamingAsync(GetChatMessages(messages), thread, options, cancellationToken); |
|||
} |
|||
|
|||
protected virtual IEnumerable<ChatMessage> GetChatMessages(IEnumerable<ChatMessage> messages) |
|||
{ |
|||
if (Workspace?.SystemPrompt?.IsNullOrWhiteSpace() == false && |
|||
!messages.Any(msg => msg.Role == ChatRole.System)) |
|||
{ |
|||
// 加入系统提示词
|
|||
messages = new List<ChatMessage> |
|||
{ |
|||
new ChatMessage(ChatRole.System, Workspace.SystemPrompt) |
|||
}.Union(messages); |
|||
} |
|||
|
|||
return messages; |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using LINGYUN.Abp.AI.Workspaces; |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace LINGYUN.Abp.AI; |
|||
public abstract class ChatClientProvider : IChatClientProvider, ITransientDependency |
|||
{ |
|||
public abstract string Name { get; } |
|||
|
|||
protected IServiceProvider ServiceProvider { get; } |
|||
protected ChatClientProvider(IServiceProvider serviceProvider) |
|||
{ |
|||
ServiceProvider = serviceProvider; |
|||
} |
|||
|
|||
public abstract Task<IWorkspaceChatClient> CreateAsync(WorkspaceDefinition workspace); |
|||
} |
|||
@ -1,13 +1,12 @@ |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.AI; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace LINGYUN.Abp.AI; |
|||
public interface IChatClientFactory |
|||
{ |
|||
[NotNull] |
|||
Task<IChatClient> CreateAsync<TWorkspace>(); |
|||
Task<IWorkspaceChatClient> CreateAsync<TWorkspace>(); |
|||
|
|||
[NotNull] |
|||
Task<IChatClient> CreateAsync(string workspace); |
|||
Task<IWorkspaceChatClient> CreateAsync(string workspace); |
|||
} |
|||
|
|||
@ -0,0 +1,8 @@ |
|||
using LINGYUN.Abp.AI.Workspaces; |
|||
using Microsoft.Extensions.AI; |
|||
|
|||
namespace LINGYUN.Abp.AI; |
|||
public interface IWorkspaceChatClient : IChatClient |
|||
{ |
|||
WorkspaceDefinition? Workspace { get; } |
|||
} |
|||
@ -0,0 +1,69 @@ |
|||
using LINGYUN.Abp.AI.Tools; |
|||
using LINGYUN.Abp.AI.Workspaces; |
|||
using Microsoft.Extensions.AI; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace LINGYUN.Abp.AI; |
|||
public class WorkspaceChatClient : IWorkspaceChatClient |
|||
{ |
|||
public IChatClient Client { get; } |
|||
public WorkspaceDefinition? Workspace { get; } |
|||
public WorkspaceChatClient( |
|||
IChatClient chatClient, |
|||
WorkspaceDefinition? workspace = null) |
|||
{ |
|||
Workspace = workspace; |
|||
Client = chatClient; |
|||
} |
|||
|
|||
public virtual Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default) |
|||
{ |
|||
return Client.GetResponseAsync(GetChatMessages(messages), GetChatOptions(options), cancellationToken); |
|||
} |
|||
|
|||
public virtual IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default) |
|||
{ |
|||
return Client.GetStreamingResponseAsync(GetChatMessages(messages), GetChatOptions(options), cancellationToken); |
|||
} |
|||
|
|||
public virtual object? GetService(Type serviceType, object? serviceKey = null) |
|||
{ |
|||
return Client.GetService(serviceType, serviceKey); |
|||
} |
|||
|
|||
public virtual void Dispose() |
|||
{ |
|||
Client.Dispose(); |
|||
} |
|||
|
|||
protected virtual ChatOptions GetChatOptions(ChatOptions? options) |
|||
{ |
|||
options ??= new ChatOptions(); |
|||
options.Instructions = Workspace?.Instructions; |
|||
options.Temperature ??= Workspace?.Temperature; |
|||
options.MaxOutputTokens ??= Workspace?.MaxOutputTokens; |
|||
options.FrequencyPenalty ??= Workspace?.FrequencyPenalty; |
|||
options.PresencePenalty ??= Workspace?.PresencePenalty; |
|||
|
|||
return options; |
|||
} |
|||
|
|||
protected virtual IEnumerable<ChatMessage> GetChatMessages(IEnumerable<ChatMessage> messages) |
|||
{ |
|||
if (Workspace?.SystemPrompt?.IsNullOrWhiteSpace() == false && |
|||
!messages.Any(msg => msg.Role == ChatRole.System)) |
|||
{ |
|||
// 加入系统提示词
|
|||
messages = new List<ChatMessage> |
|||
{ |
|||
new ChatMessage(ChatRole.System, Workspace.SystemPrompt) |
|||
}.Union(messages); |
|||
} |
|||
|
|||
return messages; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue