Browse Source

feat(ai): Optimize the configuration of AI tools

pull/1464/head
colin 6 months ago
parent
commit
72e9b2005a
  1. 8
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/AbpAIToolsModule.cs
  2. 5
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/IWorkspaceAIToolFinder.cs
  3. 20
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/WorkspaceAIToolFinder.cs
  4. 70
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ConversationChangeNameHandler.cs

8
aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/AbpAIToolsModule.cs

@ -35,12 +35,8 @@ public class AbpAIToolsModule : AbpModule
{
options.ChatClientBuildActions.Add(async (workspace, sp, builder) =>
{
IList<AITool>? workspaceAITools = default!;
var workspaceAIToolFinder = sp.GetService<IWorkspaceAIToolFinder>();
if (workspaceAIToolFinder != null && workspaceAIToolFinder.IsAIToolEnabled())
{
workspaceAITools = await workspaceAIToolFinder.GetToolsAsync(workspace);
}
var workspaceAIToolFinder = sp.GetRequiredService<IWorkspaceAIToolFinder>();
var workspaceAITools = await workspaceAIToolFinder.GetToolsAsync(workspace);
return builder
.ConfigureOptions(config =>

5
aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/IWorkspaceAIToolFinder.cs

@ -1,14 +1,9 @@
using LINGYUN.Abp.AI.Workspaces;
using Microsoft.Extensions.AI;
using System;
using System.Threading.Tasks;
namespace LINGYUN.Abp.AI.Tools;
public interface IWorkspaceAIToolFinder
{
IDisposable DisableAITool();
Task<AITool[]?> GetToolsAsync(WorkspaceDefinition workspace);
bool IsAIToolEnabled();
}

20
aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/WorkspaceAIToolFinder.cs

@ -1,34 +1,22 @@
using LINGYUN.Abp.AI.Workspaces;
using Microsoft.Extensions.AI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
namespace LINGYUN.Abp.AI.Tools;
public class WorkspaceAIToolFinder : IWorkspaceAIToolFinder, ITransientDependency
{
private const string AIToolDisabledScopeKey = "Abp.AI.Tools.DisabledAITool";
private readonly IAIToolFactory _aiToolFactory;
private readonly IAIToolDefinitionManager _aiToolDefinitionManager;
private readonly IAmbientScopeProvider<AIToolDisabledState> _aiToolDisabledState;
public WorkspaceAIToolFinder(
IAIToolFactory aiToolFactory,
IAIToolDefinitionManager aiToolDefinitionManager,
IAmbientScopeProvider<AIToolDisabledState> aiToolDisabledState)
IAIToolDefinitionManager aiToolDefinitionManager)
{
_aiToolFactory = aiToolFactory;
_aiToolDefinitionManager = aiToolDefinitionManager;
_aiToolDisabledState = aiToolDisabledState;
}
public virtual IDisposable DisableAITool()
{
return _aiToolDisabledState.BeginScope(AIToolDisabledScopeKey, new AIToolDisabledState(true));
}
public async virtual Task<AITool[]?> GetToolsAsync(WorkspaceDefinition workspace)
@ -61,10 +49,4 @@ public class WorkspaceAIToolFinder : IWorkspaceAIToolFinder, ITransientDependenc
return useAITools.ToArray();
}
public virtual bool IsAIToolEnabled()
{
var state = _aiToolDisabledState.GetValue(AIToolDisabledScopeKey);
return state == null || !state.IsDisabled;
}
}

70
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ConversationChangeNameHandler.cs

@ -1,5 +1,4 @@
using LINGYUN.Abp.AI;
using LINGYUN.Abp.AI.Tools;
using LINGYUN.Abp.AIManagement.Localization;
using LINGYUN.Abp.AIManagement.Tokens;
using Microsoft.Extensions.AI;
@ -26,7 +25,6 @@ public class ConversationChangeNameHandler :
private readonly IGuidGenerator _guidGenerator;
private readonly IAbpDistributedLock _distributedLock;
private readonly IChatClientFactory _chatClientFactory;
private readonly IWorkspaceAIToolFinder _workspaceAIToolFinder;
private readonly IStringLocalizer<AIManagementResource> _stringLocalizer;
private readonly ITokenUsageRecordRepository _tokenUsageRecordRepository;
private readonly IConversationRecordRepository _conversationRecordRepository;
@ -37,7 +35,6 @@ public class ConversationChangeNameHandler :
IGuidGenerator guidGenerator,
IAbpDistributedLock distributedLock,
IChatClientFactory chatClientFactory,
IWorkspaceAIToolFinder workspaceAIToolFinder,
IStringLocalizer<AIManagementResource> stringLocalizer,
ITokenUsageRecordRepository tokenUsageRecordRepository,
IConversationRecordRepository conversationRecordRepository,
@ -47,7 +44,6 @@ public class ConversationChangeNameHandler :
_guidGenerator = guidGenerator;
_distributedLock = distributedLock;
_chatClientFactory = chatClientFactory;
_workspaceAIToolFinder = workspaceAIToolFinder;
_stringLocalizer = stringLocalizer;
_tokenUsageRecordRepository = tokenUsageRecordRepository;
_conversationRecordRepository = conversationRecordRepository;
@ -101,40 +97,44 @@ public class ConversationChangeNameHandler :
var chatClient = await _chatClientFactory.CreateAsync(chatMessage.Workspace);
var instructions = _stringLocalizer["DesignConversationNamePrompt", ConversationRecordConsts.MaxNameLength].Value;
// 禁用AI工具
using (_workspaceAIToolFinder.DisableAITool())
{
var aiAgent = chatClient
.CreateAIAgent(
instructions: instructions,
services: _serviceProvider);
var agentRunRes = await aiAgent.RunAsync([
new ChatMessage(ChatRole.System, instructions),
new ChatMessage(ChatRole.User, chatMessage.Content)]);
conversation.SetName(
agentRunRes.Text.Length > ConversationRecordConsts.MaxNameLength
var aiAgent = chatClient
.AsBuilder()
.ConfigureOptions(options =>
{
// 禁用工具
options.Tools = [];
})
.BuildAIAgent(
instructions: instructions,
services: _serviceProvider);
var agentRunRes = await aiAgent.RunAsync([
new ChatMessage(ChatRole.System, instructions),
new ChatMessage(ChatRole.User, chatMessage.Content)]);
conversation.SetName(
agentRunRes.Text.Length > ConversationRecordConsts.MaxNameLength
? chatMessage.Content.Length > ConversationRecordConsts.MaxNameLength
? chatMessage.Content[..ConversationRecordConsts.MaxNameLength]
: agentRunRes.Text);
: chatMessage.Content
: agentRunRes.Text);
await _conversationRecordRepository.UpdateAsync(conversation);
await _conversationRecordRepository.UpdateAsync(conversation);
if (agentRunRes.Usage != null)
{
var tokenUsageRecord = new TokenUsageRecord(
_guidGenerator.Create(),
chatMessage.Id,
conversation.Id,
agentRunRes.Usage.InputTokenCount,
agentRunRes.Usage.OutputTokenCount,
agentRunRes.Usage.TotalTokenCount,
agentRunRes.Usage.CachedInputTokenCount,
agentRunRes.Usage.ReasoningTokenCount,
chatMessage.TenantId);
await _tokenUsageRecordRepository.InsertAsync(tokenUsageRecord);
}
if (agentRunRes.Usage != null)
{
var tokenUsageRecord = new TokenUsageRecord(
_guidGenerator.Create(),
chatMessage.Id,
conversation.Id,
agentRunRes.Usage.InputTokenCount,
agentRunRes.Usage.OutputTokenCount,
agentRunRes.Usage.TotalTokenCount,
agentRunRes.Usage.CachedInputTokenCount,
agentRunRes.Usage.ReasoningTokenCount,
chatMessage.TenantId);
await _tokenUsageRecordRepository.InsertAsync(tokenUsageRecord);
}
}
}

Loading…
Cancel
Save