Browse Source

feat(ai): Add Use AI tool to the workspace

pull/1457/head
colin 6 months ago
parent
commit
4896a7c4a9
  1. 10
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Workspaces/WorkspaceDefinition.cs
  2. 24
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/AbpAIToolsModule.cs
  3. 2
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Workspaces/Dtos/WorkspaceDefinitionRecordCreateOrUpdateDto.cs
  4. 2
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Workspaces/Dtos/WorkspaceDefinitionRecordDto.cs
  5. 9
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application/LINGYUN/Abp/AIManagement/AbpAIManagementApplicationMappers.cs
  6. 7
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application/LINGYUN/Abp/AIManagement/Workspaces/WorkspaceDefinitionAppService.cs
  7. 1
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain.Shared/LINGYUN/Abp/AIManagement/Workspaces/WorkspaceDefinitionRecordConsts.cs
  8. 4
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Workspaces/DynamicWorkspaceDefinitionStoreInMemoryCache.cs
  9. 14
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Workspaces/WorkspaceDefinitionRecord.cs
  10. 1
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Workspaces/WorkspaceDefinitionSerializer.cs
  11. 2
      aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.EntityFrameworkCore/LINGYUN/Abp/AIManagement/EntityFrameworkCore/AIManagementDbContextModelBuilderExtensions.cs

10
aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Workspaces/WorkspaceDefinition.cs

@ -126,6 +126,9 @@ public class WorkspaceDefinition : IHasSimpleStateCheckers<WorkspaceDefinition>
[NotNull]
public Dictionary<string, object> Properties { get; }
[NotNull]
public List<string> Tools { get; }
public List<ISimpleStateChecker<WorkspaceDefinition>> StateCheckers { get; }
public WorkspaceDefinition(
@ -155,6 +158,7 @@ public class WorkspaceDefinition : IHasSimpleStateCheckers<WorkspaceDefinition>
PresencePenalty = presencePenalty;
IsEnabled = true;
Tools = new List<string>();
Properties = new Dictionary<string, object>();
StateCheckers = new List<ISimpleStateChecker<WorkspaceDefinition>>();
}
@ -171,6 +175,12 @@ public class WorkspaceDefinition : IHasSimpleStateCheckers<WorkspaceDefinition>
return this;
}
public virtual WorkspaceDefinition WithTools(params string[] tools)
{
Tools.AddIfNotContains(tools);
return this;
}
public virtual WorkspaceDefinition WithProperty(string key, object value)
{
Properties[key] = value;

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

@ -3,6 +3,7 @@ using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;
@ -32,10 +33,27 @@ public class AbpAIToolsModule : AbpModule
Configure<AbpAICoreOptions>(options =>
{
options.ChatClientBuildActions.Add(async (_, sp, builder) =>
options.ChatClientBuildActions.Add(async (workspace, sp, builder) =>
{
var useAITools = new List<AITool>();
var aiToolFactory = sp.GetRequiredService<IAIToolFactory>();
var aiTools = await aiToolFactory.CreateAllTools();
if (workspace.Tools.Count > 0)
{
var aiToolDefinitionManager = sp.GetRequiredService<IAIToolDefinitionManager>();
var aiToolDefinitions = await aiToolDefinitionManager.GetAllAsync();
var useAIToolDefinitions = aiToolDefinitions.Where(aiTool => workspace.Tools.Contains(aiTool.Name));
foreach (var aiToolDefinition in aiToolDefinitions)
{
var aiTools = await aiToolFactory.CreateTool(aiToolDefinition);
useAITools.AddRange(aiTools);
}
}
else
{
useAITools.AddRange(await aiToolFactory.CreateAllTools());
}
builder.ConfigureOptions(ai =>
{
@ -44,7 +62,7 @@ public class AbpAIToolsModule : AbpModule
ai.Tools ??= [];
foreach (var aiTool in aiTools)
foreach (var aiTool in useAITools)
{
ai.Tools.Add(aiTool);
}

2
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Workspaces/Dtos/WorkspaceDefinitionRecordCreateOrUpdateDto.cs

@ -42,6 +42,8 @@ public abstract class WorkspaceDefinitionRecordCreateOrUpdateDto : ExtensibleObj
public bool IsEnabled { get; set; }
public string[]? Tools { get; set; }
[DynamicStringLength(typeof(WorkspaceDefinitionRecordConsts), nameof(WorkspaceDefinitionRecordConsts.MaxStateCheckersLength))]
public string? StateCheckers { get; set; }
}

2
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application.Contracts/LINGYUN/Abp/AIManagement/Workspaces/Dtos/WorkspaceDefinitionRecordDto.cs

@ -35,6 +35,8 @@ public class WorkspaceDefinitionRecordDto : ExtensibleAuditedEntityDto<Guid>, IH
public bool IsSystem { get; set; }
public string[]? Tools { get; set; }
public string? StateCheckers { get; set; }
public string ConcurrencyStamp { get; set; }

9
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application/LINGYUN/Abp/AIManagement/AbpAIManagementApplicationMappers.cs

@ -14,8 +14,17 @@ namespace LINGYUN.Abp.AIManagement;
[MapExtraProperties(DefinitionChecks = MappingPropertyDefinitionChecks.None)]
public partial class WorkspaceDefinitionRecordToWorkspaceDefinitionRecordDtoMapper : MapperBase<WorkspaceDefinitionRecord, WorkspaceDefinitionRecordDto>
{
[MapPropertyFromSource(nameof(WorkspaceDefinitionRecordDto.Tools), Use = nameof(ConvertTools))]
public override partial WorkspaceDefinitionRecordDto Map(WorkspaceDefinitionRecord source);
[MapPropertyFromSource(nameof(WorkspaceDefinitionRecordDto.Tools), Use = nameof(ConvertTools))]
public override partial void Map(WorkspaceDefinitionRecord source, WorkspaceDefinitionRecordDto destination);
[UserMapping(Default = false)]
private static string[]? ConvertTools(WorkspaceDefinitionRecord record)
{
return record.Tools?.Split(",");
}
}
[Mapper(RequiredMappingStrategy = RequiredMappingStrategy.Target)]

7
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Application/LINGYUN/Abp/AIManagement/Workspaces/WorkspaceDefinitionAppService.cs

@ -5,6 +5,7 @@ using LINGYUN.Abp.AIManagement.Workspaces.Dtos;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
@ -113,6 +114,7 @@ public class WorkspaceDefinitionAppService :
createInput.MaxOutputTokens,
createInput.FrequencyPenalty,
createInput.PresencePenalty,
createInput.Tools?.JoinAsString(","),
createInput.StateCheckers)
{
IsEnabled = createInput.IsEnabled,
@ -184,6 +186,11 @@ public class WorkspaceDefinitionAppService :
entity.StateCheckers = updateInput.StateCheckers;
}
if (updateInput.Tools != null)
{
entity.Tools = updateInput.Tools.JoinAsString(",");
}
if (!updateInput.ApiKey.IsNullOrWhiteSpace())
{
var encryptApiKey = StringEncryptionService.Encrypt(updateInput.ApiKey);

1
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain.Shared/LINGYUN/Abp/AIManagement/Workspaces/WorkspaceDefinitionRecordConsts.cs

@ -10,5 +10,6 @@ public static class WorkspaceDefinitionRecordConsts
public static int MaxApiBaseUrlLength { get; set; } = 128;
public static int MaxSystemPromptLength { get; set; } = 512;
public static int MaxInstructionsLength { get; set; } = 512;
public static int MaxToolsLength { get; set; } = 128;
public static int MaxStateCheckersLength { get; set; } = 256;
}

4
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Workspaces/DynamicWorkspaceDefinitionStoreInMemoryCache.cs

@ -62,6 +62,10 @@ public class DynamicWorkspaceDefinitionStoreInMemoryCache : IDynamicWorkspaceDef
{
workspaceDef.WithApiBaseUrl(workspace.ApiBaseUrl);
}
if (!workspace.Tools.IsNullOrWhiteSpace())
{
workspaceDef.WithTools(workspace.Tools.Split(","));
}
workspaceDef.IsEnabled = workspace.IsEnabled;
if (!workspace.StateCheckers.IsNullOrWhiteSpace())

14
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Workspaces/WorkspaceDefinitionRecord.cs

@ -36,6 +36,8 @@ public class WorkspaceDefinitionRecord : AuditedAggregateRoot<Guid>
public bool IsSystem { get; set; }
public string? Tools { get; set; }
public string? StateCheckers { get; set; }
protected WorkspaceDefinitionRecord()
@ -57,6 +59,7 @@ public class WorkspaceDefinitionRecord : AuditedAggregateRoot<Guid>
int? maxOutputTokens = null,
float? frequencyPenalty = null,
float? presencePenalty = null,
string? tools = null,
string? stateCheckers = null)
: base(id)
{
@ -67,6 +70,7 @@ public class WorkspaceDefinitionRecord : AuditedAggregateRoot<Guid>
Description = Check.Length(description, nameof(description), WorkspaceDefinitionRecordConsts.MaxDescriptionLength);
SystemPrompt = Check.Length(systemPrompt, nameof(systemPrompt), WorkspaceDefinitionRecordConsts.MaxSystemPromptLength);
Instructions = Check.Length(instructions, nameof(instructions), WorkspaceDefinitionRecordConsts.MaxInstructionsLength);
Tools = Check.Length(tools, nameof(tools), WorkspaceDefinitionRecordConsts.MaxToolsLength);
StateCheckers = Check.Length(stateCheckers, nameof(stateCheckers), WorkspaceDefinitionRecordConsts.MaxStateCheckersLength);
Temperature = temperature;
MaxOutputTokens = maxOutputTokens;
@ -147,6 +151,11 @@ public class WorkspaceDefinitionRecord : AuditedAggregateRoot<Guid>
return false;
}
if (IsSystem != otherWorkspace.IsSystem)
{
return false;
}
if (Temperature != otherWorkspace.Temperature)
{
return false;
@ -232,6 +241,11 @@ public class WorkspaceDefinitionRecord : AuditedAggregateRoot<Guid>
IsEnabled = otherWorkspace.IsEnabled;
}
if (IsSystem != otherWorkspace.IsSystem)
{
IsSystem = otherWorkspace.IsSystem;
}
if (Temperature != otherWorkspace.Temperature)
{
Temperature = otherWorkspace.Temperature;

1
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Workspaces/WorkspaceDefinitionSerializer.cs

@ -58,6 +58,7 @@ public class WorkspaceDefinitionSerializer : IWorkspaceDefinitionSerializer, ITr
definition.MaxOutputTokens,
definition.FrequencyPenalty,
definition.PresencePenalty,
definition.Tools.JoinAsString(","),
SerializeStateCheckers(definition.StateCheckers));
if (!definition.ApiKey.IsNullOrWhiteSpace())

2
aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.EntityFrameworkCore/LINGYUN/Abp/AIManagement/EntityFrameworkCore/AIManagementDbContextModelBuilderExtensions.cs

@ -91,6 +91,8 @@ public static class AIManagementDbContextModelBuilderExtensions
.HasMaxLength(WorkspaceDefinitionRecordConsts.MaxInstructionsLength);
b.Property(x => x.StateCheckers)
.HasMaxLength(WorkspaceDefinitionRecordConsts.MaxStateCheckersLength);
b.Property(x => x.Tools)
.HasMaxLength(WorkspaceDefinitionRecordConsts.MaxToolsLength);
b.HasIndex(x => new { x.Name }).IsUnique();

Loading…
Cancel
Save