|
|
@ -2,6 +2,8 @@ |
|
|
using Microsoft.Agents.AI; |
|
|
using Microsoft.Agents.AI; |
|
|
using Microsoft.Extensions.AI; |
|
|
using Microsoft.Extensions.AI; |
|
|
using Microsoft.Extensions.Localization; |
|
|
using Microsoft.Extensions.Localization; |
|
|
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
|
|
using System; |
|
|
using System.Collections.Concurrent; |
|
|
using System.Collections.Concurrent; |
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
|
using Volo.Abp.AI; |
|
|
using Volo.Abp.AI; |
|
|
@ -12,25 +14,27 @@ using Volo.Abp.SimpleStateChecking; |
|
|
namespace LINGYUN.Abp.AI.Agent; |
|
|
namespace LINGYUN.Abp.AI.Agent; |
|
|
public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDependency |
|
|
public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDependency |
|
|
{ |
|
|
{ |
|
|
private readonly static ConcurrentDictionary<string, ChatClientAgent> _chatClientAgentCache = new(); |
|
|
private readonly static ConcurrentDictionary<string, WorkspaceAIAgent> _chatClientAgentCache = new(); |
|
|
|
|
|
protected IServiceProvider ServiceProvider { get; } |
|
|
protected IChatClientFactory ChatClientFactory { get; } |
|
|
protected IChatClientFactory ChatClientFactory { get; } |
|
|
protected IStringLocalizerFactory StringLocalizerFactory { get; } |
|
|
protected IStringLocalizerFactory StringLocalizerFactory { get; } |
|
|
protected IWorkspaceDefinitionManager WorkspaceDefinitionManager { get; } |
|
|
protected IWorkspaceDefinitionManager WorkspaceDefinitionManager { get; } |
|
|
protected ISimpleStateCheckerManager<WorkspaceDefinition> StateCheckerManager { get; } |
|
|
protected ISimpleStateCheckerManager<WorkspaceDefinition> StateCheckerManager { get; } |
|
|
public ChatClientAgentFactory( |
|
|
public ChatClientAgentFactory( |
|
|
|
|
|
IServiceProvider serviceProvider, |
|
|
IChatClientFactory chatClientFactory, |
|
|
IChatClientFactory chatClientFactory, |
|
|
IStringLocalizerFactory stringLocalizerFactory, |
|
|
IStringLocalizerFactory stringLocalizerFactory, |
|
|
IWorkspaceDefinitionManager workspaceDefinitionManager, |
|
|
IWorkspaceDefinitionManager workspaceDefinitionManager, |
|
|
ISimpleStateCheckerManager<WorkspaceDefinition> stateCheckerManager) |
|
|
ISimpleStateCheckerManager<WorkspaceDefinition> stateCheckerManager) |
|
|
{ |
|
|
{ |
|
|
|
|
|
ServiceProvider = serviceProvider; |
|
|
ChatClientFactory = chatClientFactory; |
|
|
ChatClientFactory = chatClientFactory; |
|
|
StringLocalizerFactory = stringLocalizerFactory; |
|
|
StringLocalizerFactory = stringLocalizerFactory; |
|
|
WorkspaceDefinitionManager = workspaceDefinitionManager; |
|
|
WorkspaceDefinitionManager = workspaceDefinitionManager; |
|
|
StateCheckerManager = stateCheckerManager; |
|
|
StateCheckerManager = stateCheckerManager; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async virtual Task<ChatClientAgent> CreateAsync<TWorkspace>() |
|
|
public async virtual Task<WorkspaceAIAgent> CreateAsync<TWorkspace>() |
|
|
{ |
|
|
{ |
|
|
var workspace = WorkspaceNameAttribute.GetWorkspaceName<TWorkspace>(); |
|
|
var workspace = WorkspaceNameAttribute.GetWorkspaceName<TWorkspace>(); |
|
|
if (_chatClientAgentCache.TryGetValue(workspace, out var chatClientAgent)) |
|
|
if (_chatClientAgentCache.TryGetValue(workspace, out var chatClientAgent)) |
|
|
@ -53,17 +57,33 @@ public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDepende |
|
|
description = workspaceDefine.Description.Localize(StringLocalizerFactory); |
|
|
description = workspaceDefine.Description.Localize(StringLocalizerFactory); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
chatClientAgent = chatClient.CreateAIAgent( |
|
|
var clientAgentOptions = new ChatClientAgentOptions |
|
|
instructions: workspaceDefine?.SystemPrompt, |
|
|
{ |
|
|
name: workspaceDefine?.Name, |
|
|
ChatOptions = new ChatOptions |
|
|
description: description); |
|
|
{ |
|
|
|
|
|
Instructions = workspaceDefine?.Instructions, |
|
|
|
|
|
Temperature = workspaceDefine?.Temperature, |
|
|
|
|
|
MaxOutputTokens = workspaceDefine?.MaxOutputTokens, |
|
|
|
|
|
PresencePenalty = workspaceDefine?.PresencePenalty, |
|
|
|
|
|
FrequencyPenalty = workspaceDefine?.FrequencyPenalty, |
|
|
|
|
|
}, |
|
|
|
|
|
Name = workspaceDefine?.Name, |
|
|
|
|
|
Description = description |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
chatClientAgent = new WorkspaceAIAgent( |
|
|
|
|
|
new AIAgentBuilder(chatClient.CreateAIAgent(clientAgentOptions)) |
|
|
|
|
|
.UseLogging() |
|
|
|
|
|
.UseOpenTelemetry() |
|
|
|
|
|
.Build(ServiceProvider), |
|
|
|
|
|
workspaceDefine); |
|
|
|
|
|
|
|
|
_chatClientAgentCache.TryAdd(workspace, chatClientAgent); |
|
|
_chatClientAgentCache.TryAdd(workspace, chatClientAgent); |
|
|
|
|
|
|
|
|
return chatClientAgent; |
|
|
return chatClientAgent; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async virtual Task<ChatClientAgent> CreateAsync(string workspace) |
|
|
public async virtual Task<WorkspaceAIAgent> CreateAsync(string workspace) |
|
|
{ |
|
|
{ |
|
|
if (_chatClientAgentCache.TryGetValue(workspace, out var chatClientAgent)) |
|
|
if (_chatClientAgentCache.TryGetValue(workspace, out var chatClientAgent)) |
|
|
{ |
|
|
{ |
|
|
@ -81,10 +101,26 @@ public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDepende |
|
|
description = workspaceDefine.Description.Localize(StringLocalizerFactory); |
|
|
description = workspaceDefine.Description.Localize(StringLocalizerFactory); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
chatClientAgent = chatClient.CreateAIAgent( |
|
|
var clientAgentOptions = new ChatClientAgentOptions |
|
|
instructions: workspaceDefine.SystemPrompt, |
|
|
{ |
|
|
name: workspaceDefine.Name, |
|
|
ChatOptions = new ChatOptions |
|
|
description: description); |
|
|
{ |
|
|
|
|
|
Instructions = workspaceDefine.Instructions, |
|
|
|
|
|
Temperature = workspaceDefine.Temperature, |
|
|
|
|
|
MaxOutputTokens = workspaceDefine.MaxOutputTokens, |
|
|
|
|
|
PresencePenalty = workspaceDefine.PresencePenalty, |
|
|
|
|
|
FrequencyPenalty = workspaceDefine.FrequencyPenalty, |
|
|
|
|
|
}, |
|
|
|
|
|
Name = workspaceDefine.Name, |
|
|
|
|
|
Description = description |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
chatClientAgent = new WorkspaceAIAgent( |
|
|
|
|
|
new AIAgentBuilder(chatClient.CreateAIAgent(clientAgentOptions)) |
|
|
|
|
|
.UseLogging() |
|
|
|
|
|
.UseOpenTelemetry() |
|
|
|
|
|
.Build(ServiceProvider), |
|
|
|
|
|
workspaceDefine); |
|
|
|
|
|
|
|
|
_chatClientAgentCache.TryAdd(workspace, chatClientAgent); |
|
|
_chatClientAgentCache.TryAdd(workspace, chatClientAgent); |
|
|
|
|
|
|
|
|
|