Skip to content

Commit e979c9d

Browse files
authored
Merge pull request #70 from elsa-workflows/enh/ai
Enhances Agents module with AI Copilot and more
2 parents b3f610d + 81530e3 commit e979c9d

28 files changed

Lines changed: 2343 additions & 6 deletions
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
3+
<!-- Elsa.Studio.Agents -->
4+
<ItemGroup Condition="'$(UseProjectReferences)' == 'true' and '$(MSBuildProjectName)' == 'Elsa.Studio.Agents'">
5+
<ProjectReference Include="$(MSBuildThisFileDirectory)..\..\..\client\studio\src\modules\Elsa.Studio.Workflows\Elsa.Studio.Workflows.csproj"/>
6+
</ItemGroup>
7+
8+
</Project>

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@
4747
</PropertyGroup>
4848

4949
<Import Project=".build\ElsaCore.ProjectReferences.targets" />
50+
<Import Project=".build\ElsaStudio.ProjectReferences.targets" />
5051

5152
</Project>

Directory.Packages.props

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@
126126
<PackageVersion Include="Microsoft.Extensions.Resilience" Version="9.6.0" />
127127
<PackageVersion Include="Microsoft.Identity.Client" Version="4.72.1" />
128128
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.0" />
129-
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.53.1" />
129+
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.58.0" />
130+
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.58.0-alpha" />
131+
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.InMemory" Version="1.58.0-preview" />
130132
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
131133
<PackageVersion Include="MongoDB.Driver" Version="3.4.0" />
132134
<PackageVersion Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="2.1.0" />

src/modules/agents/Elsa.Agents.Activities/ActivityProviders/AgentActivityProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async ValueTask<IEnumerable<ActivityDescriptor>> GetDescriptorsAsync(Canc
3838
activityDescriptor.DisplayName = agentConfig.Name.Humanize().Transform(To.TitleCase);
3939
activityDescriptor.IsBrowsable = true;
4040
activityDescriptor.Category = "Agents";
41-
activityDescriptor.Kind = ActivityKind.Job;
41+
activityDescriptor.Kind = ActivityKind.Task;
4242
activityDescriptor.CustomProperties["RootType"] = nameof(AgentActivity);
4343

4444
activityDescriptor.Constructor = context =>

src/modules/agents/Elsa.Agents.Core/Elsa.Agents.Core.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>Provides an agentic framework using Semantic Kernel</Description>
@@ -12,6 +12,9 @@
1212
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
1313
<PackageReference Include="Microsoft.SemanticKernel" />
1414
<PackageReference Include="Microsoft.SemanticKernel.PromptTemplates.Handlebars" />
15+
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Memory" />
16+
<PackageReference Include="Microsoft.SemanticKernel.Connectors.InMemory" />
17+
<PackageReference Include="System.Linq.Async" />
1518
</ItemGroup>
1619

1720
<ItemGroup Label="Elsa" Condition="'$(UseProjectReferences)' != 'true'">

src/modules/agents/Elsa.Agents.Core/Features/AgentsFeature.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ public override void Apply()
3333
.AddScoped(_kernelConfigProviderFactory)
3434
.AddScoped<ConfigurationKernelConfigProvider>()
3535
.AddPluginProvider<ImageGeneratorPluginProvider>()
36+
.AddPluginProvider<DocumentQueryPluginProvider>()
3637
.AddAgentServiceProvider<OpenAIChatCompletionProvider>()
3738
.AddAgentServiceProvider<OpenAITextToImageProvider>()
39+
.AddAgentServiceProvider<OpenAIEmbeddingGenerator>()
3840
;
3941
}
4042
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.ComponentModel;
2+
using System.Diagnostics.CodeAnalysis;
3+
using Microsoft.Extensions.AI;
4+
using Microsoft.Extensions.VectorData;
5+
using Microsoft.SemanticKernel;
6+
using Microsoft.SemanticKernel.Connectors.InMemory;
7+
using Microsoft.SemanticKernel.Data;
8+
9+
namespace Elsa.Agents.Plugins;
10+
11+
public class DocumentQueryPlugin
12+
{
13+
[Experimental("SKEXP0001")]
14+
[KernelFunction("query_document")]
15+
[Description("Queries a document using vector search.")]
16+
public async Task<string[]> QueryDocumentAsync(
17+
Kernel kernel,
18+
[Description("The document to query.")] string documentText,
19+
[Description("The query to perform on the document.")] string query,
20+
CancellationToken cancellationToken = default)
21+
{
22+
var embeddingService = kernel.GetRequiredService<IEmbeddingGenerator<string, Embedding<float>>>();
23+
var vectorStore = new InMemoryVectorStore(new() { EmbeddingGenerator = embeddingService });
24+
var collection = vectorStore.GetCollection<string, Document>("documents");
25+
var document = new Document
26+
{
27+
Key = Guid.NewGuid().ToString(),
28+
Text = documentText,
29+
TextEmbedding = await embeddingService.GenerateAsync(documentText, cancellationToken: cancellationToken)
30+
};
31+
await collection.EnsureCollectionExistsAsync(cancellationToken);
32+
await collection.UpsertAsync(document, cancellationToken);
33+
34+
var vectorSearch = new VectorStoreTextSearch<Document>(collection);
35+
var results = await vectorSearch.SearchAsync(query, cancellationToken: cancellationToken);
36+
var results2 = await results.Results.ToArrayAsync(cancellationToken: cancellationToken);
37+
38+
return results2;
39+
}
40+
}
41+
42+
public class DocumentQueryPluginProvider : PluginProvider
43+
{
44+
public override IEnumerable<PluginDescriptor> GetPlugins()
45+
{
46+
yield return PluginDescriptor.From<DocumentQueryPlugin>();
47+
}
48+
}
49+
50+
public class Document
51+
{
52+
[VectorStoreKey] [TextSearchResultName] public string Key { get; set; } = Guid.NewGuid().ToString();
53+
[VectorStoreData] [TextSearchResultValue] public string Text { get; set; } = null!;
54+
[VectorStoreVector(1536)] public Embedding<float> TextEmbedding { get; set; } = null!;
55+
}

src/modules/agents/Elsa.Agents.Core/ServiceProviders/OpenAIChatCompletionProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Elsa.Agents;
55
public class OpenAIChatCompletionProvider : IAgentServiceProvider
66
{
77
public string Name => "OpenAIChatCompletion";
8+
89
public void ConfigureKernel(KernelBuilderContext context)
910
{
1011
var modelId = (string)context.ServiceConfig.Settings["ModelId"];
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Elsa.Extensions;
3+
using Microsoft.SemanticKernel;
4+
using Microsoft.SemanticKernel.Connectors.InMemory;
5+
using Microsoft.SemanticKernel.Memory;
6+
using Microsoft.SemanticKernel.Connectors.OpenAI;
7+
8+
namespace Elsa.Agents;
9+
10+
public class OpenAIEmbeddingGenerator : IAgentServiceProvider
11+
{
12+
public string Name => "OpenAIEmbeddingGenerator";
13+
14+
[Experimental("SKEXP0010")]
15+
public void ConfigureKernel(KernelBuilderContext context)
16+
{
17+
var modelId = (string)context.ServiceConfig.Settings["ModelId"];
18+
var apiKey = context.GetApiKey();
19+
20+
context.KernelBuilder.Services.AddInMemoryVectorStore();
21+
context.KernelBuilder.AddOpenAIEmbeddingGenerator(modelId, apiKey);
22+
}
23+
}

src/modules/agents/Elsa.Studio.Agents/AgentsIcons.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ public static class AgentIcons
1313
<path d="m50 10.938c-5.1406 0-9.375 4.2344-9.375 9.375 0 4.0469 2.625 7.5273 6.25 8.8281v6.7969h-6.2461c-5.1406 0-9.3789 4.2344-9.3789 9.375h-5.5547c-5.3555 0-10.07 4.0391-10.07 9.3672v0.007812c-5.1406 0-9.375 4.2383-9.375 9.3789v6.25c0 5.1406 4.2344 9.375 9.375 9.3789 0.003906 5.3242 4.7148 9.3711 10.07 9.3711h48.609c5.3555 0 10.066-4.0508 10.07-9.3711 5.1406 0 9.375-4.2383 9.375-9.3789v-6.2539c0-5.1406-4.2344-9.375-9.375-9.3789v-0.007813c0-5.3242-4.7148-9.3672-10.07-9.3672l-5.3516 0.003906c0-5.1406-4.2344-9.375-9.375-9.375h-6.4492v-6.7969c3.6211-1.3008 6.2461-4.7852 6.2461-8.8281 0-5.1406-4.2344-9.375-9.375-9.375zm0 6.25c1.7617 0 3.125 1.3633 3.125 3.125s-1.3633 3.125-3.125 3.125-3.125-1.3633-3.125-3.125 1.3633-3.125 3.125-3.125zm-9.3711 25h9.3164c0.019532 0.003906 0.035157 0.003906 0.054688 0.007812 0.050781-0.003906 0.10547-0.003906 0.15625-0.007812h9.4258c1.7617 0 3.125 1.3633 3.125 3.125h-25.203c0-1.7617 1.3633-3.125 3.125-3.125zm-14.934 9.3672h8.6211c0.019532 0 0.039063 0.003906 0.058594 0.007812 0.066406 0 0.12891-0.003906 0.19531-0.007812h31.199c0.019531 0 0.039063 0.003906 0.058594 0.007812 0.066406 0 0.13281-0.003906 0.19531-0.007812h8.2812c2.3398 0 3.8164 1.5273 3.8164 3.125v25.008c0 1.5977-1.4805 3.125-3.8164 3.125l-48.609-0.003906c-2.3398 0-3.8164-1.5273-3.8164-3.125v-25.004c0-1.5977 1.4805-3.125 3.8164-3.125zm11.805 3.1328c-5.1406 0-9.375 4.2344-9.375 9.375s4.2344 9.375 9.375 9.375 9.375-4.2344 9.375-9.375-4.2344-9.375-9.375-9.375zm25.008 0c-5.1406 0-9.3828 4.2344-9.3828 9.375s4.2383 9.375 9.3828 9.375c5.1406 0 9.3672-4.2344 9.3672-9.375s-4.2266-9.375-9.3672-9.375zm-46.883 6.2461v12.508c-1.7617 0-3.1211-1.3672-3.1211-3.1289v-6.25c0-1.7617 1.3594-3.125 3.1211-3.1289zm68.75 0c1.7617 0 3.1211 1.3672 3.1211 3.1289v6.25c0 1.7617-1.3594 3.125-3.1211 3.1289zm-46.875 0.003906c1.7617 0 3.125 1.3633 3.125 3.125s-1.3633 3.125-3.125 3.125-3.125-1.3633-3.125-3.125 1.3633-3.125 3.125-3.125zm25.008 0c1.7617 0 3.1289 1.3633 3.1289 3.125s-1.3633 3.125-3.1289 3.125c-1.7617 0-3.1289-1.3633-3.1289-3.125s1.3633-3.125 3.1289-3.125z"/>
1414
</svg>
1515
""";
16+
17+
public const string Copilot = """
18+
<svg viewBox="0 0 512 416" xmlns="http://www.w3.org/2000/svg" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2"><path d="M181.33 266.143c0-11.497 9.32-20.818 20.818-20.818 11.498 0 20.819 9.321 20.819 20.818v38.373c0 11.497-9.321 20.818-20.819 20.818-11.497 0-20.818-9.32-20.818-20.818v-38.373zM308.807 245.325c-11.477 0-20.798 9.321-20.798 20.818v38.373c0 11.497 9.32 20.818 20.798 20.818 11.497 0 20.818-9.32 20.818-20.818v-38.373c0-11.497-9.32-20.818-20.818-20.818z" fill-rule="nonzero"/><path d="M512.002 246.393v57.384c-.02 7.411-3.696 14.638-9.67 19.011C431.767 374.444 344.695 416 256 416c-98.138 0-196.379-56.542-246.33-93.21-5.975-4.374-9.65-11.6-9.671-19.012v-57.384a35.347 35.347 0 016.857-20.922l15.583-21.085c8.336-11.312 20.757-14.31 33.98-14.31 4.988-56.953 16.794-97.604 45.024-127.354C155.194 5.77 226.56 0 256 0c29.441 0 100.807 5.77 154.557 62.722 28.19 29.75 40.036 70.401 45.025 127.354 13.263 0 25.602 2.936 33.958 14.31l15.583 21.127c4.476 6.077 6.878 13.345 6.878 20.88zm-97.666-26.075c-.677-13.058-11.292-18.19-22.338-21.824-11.64 7.309-25.848 10.183-39.46 10.183-14.454 0-41.432-3.47-63.872-25.869-5.667-5.625-9.527-14.454-12.155-24.247a212.902 212.902 0 00-20.469-1.088c-6.098 0-13.099.349-20.551 1.088-2.628 9.793-6.509 18.622-12.155 24.247-22.4 22.4-49.418 25.87-63.872 25.87-13.612 0-27.86-2.855-39.501-10.184-11.005 3.613-21.558 8.828-22.277 21.824-1.17 24.555-1.272 49.11-1.375 73.645-.041 12.318-.082 24.658-.288 36.976.062 7.166 4.374 13.818 10.882 16.774 52.97 24.124 103.045 36.278 149.137 36.278 46.01 0 96.085-12.154 149.014-36.278 6.508-2.956 10.84-9.608 10.881-16.774.637-36.832.124-73.809-1.642-110.62h.041zM107.521 168.97c8.643 8.623 24.966 14.392 42.56 14.392 13.448 0 39.03-2.874 60.156-24.329 9.28-8.951 15.05-31.35 14.413-54.079-.657-18.231-5.769-33.28-13.448-39.665-8.315-7.371-27.203-10.574-48.33-8.644-22.399 2.238-41.267 9.588-50.875 19.833-20.798 22.728-16.323 80.317-4.476 92.492zm130.556-56.008c.637 3.51.965 7.35 1.273 11.517 0 2.875 0 5.77-.308 8.952 6.406-.636 11.847-.636 16.959-.636s10.553 0 16.959.636c-.329-3.182-.329-6.077-.329-8.952.329-4.167.657-8.007 1.294-11.517-6.735-.637-12.812-.965-17.924-.965s-11.21.328-17.924.965zm49.275-8.008c-.637 22.728 5.133 45.128 14.413 54.08 21.105 21.454 46.708 24.328 60.155 24.328 17.596 0 33.918-5.769 42.561-14.392 11.847-12.175 16.322-69.764-4.476-92.492-9.608-10.245-28.476-17.595-50.875-19.833-21.127-1.93-40.015 1.273-48.33 8.644-7.679 6.385-12.791 21.434-13.448 39.665z"/></svg>
19+
""";
1620
}

0 commit comments

Comments
 (0)