A programming framework for agentic AI
iterating toward dynmamic activities from skills
Ryan Sweet23d2e7ab91c1
diff --git a/.gitignore b/.gitignoreindex eef1881b8..85fa5e4e2 100644--- a/.gitignore+++ b/.gitignore@@ -487,4 +487,7 @@ $RECYCLE.BIN/elsa.sqlite.*# env files-.env\ No newline at end of file+.env++# ignore local elsa-core src+elsa-core/\ No newline at end of filediff --git a/Elsa.SemanticKernel/Activities/ActivityProviders/SemanticKernelSkillActivityProvider.cs b/Elsa.SemanticKernel/Activities/ActivityProviders/SemanticKernelSkillActivityProvider.csnew file mode 100644index 000000000..b84bbd49c--- /dev/null+++ b/Elsa.SemanticKernel/Activities/ActivityProviders/SemanticKernelSkillActivityProvider.cs@@ -0,0 +1,90 @@+using System;+using System.Collections;+using System.Collections.Generic;+using System.Diagnostics.CodeAnalysis;+using System.Linq;+using System.Reflection;+using System.Threading;+using System.Threading.Tasks;+using Elsa;+using Elsa.Expressions.Models;+using Elsa.Extensions;+using Elsa.Workflows.Core;+using Elsa.Workflows.Core.Contracts;+using Elsa.Workflows.Core.Models;+using Elsa.Workflows.Management.Extensions;+using Elsa.Workflows.Core.Attributes;+using Elsa.Workflows.Core.Models;+using Elsa.Expressions.Models;+using Elsa.Extensions;+using Elsa.Http;+using Microsoft.Extensions.Logging;+using Microsoft.SemanticKernel;+using Microsoft.SemanticKernel.Connectors.Memory.Qdrant;+using Microsoft.SemanticKernel.Connectors.AI.OpenAI.TextEmbedding;+using Microsoft.SemanticKernel.Memory;+using Microsoft.SemanticKernel.Orchestration;+using Microsoft.SemanticKernel.Reliability;+using Microsoft.SKDevTeam;++namespace Elsa.SemanticKernel;++//<summary>+// Loads the Semantic Kernel skills and then generates activites for each skill+//</summary>+public class SemanticKernelActivityProvider : IActivityProvider+{+ private readonly IActivityFactory _activityFactory;+ private readonly IActivityDescriber _activityDescriber;++ public SemanticKernelActivityProvider(IActivityFactory activityFactory, IActivityDescriber activityDescriber)+ {+ _activityFactory = activityFactory;+ _activityDescriber = activityDescriber;+ }+ public async ValueTask<IEnumerable<ActivityDescriptor>> GetDescriptorsAsync(CancellationToken cancellationToken = default)+ {+ //get a list of skills in the assembly+ var skills = await LoadSkillsFromAssemblyAsync("skills");+ var descriptors = new List<ActivityDescriptor>();+ foreach (var skill in skills)+ {+ //var descriptor = await CreateActivityDescriptors(skill, cancellationToken);+ // descriptors.Add(descriptor);+ }+ return descriptors;+ }++ ///<summary>+ /// Gets a list of the skills in the assembly+ ///</summary>+ private async Task<IEnumerable<string>> LoadSkillsFromAssemblyAsync(string assemblyName)+ {+ var skills = new List<string>();+ var assembly = Assembly.Load(assemblyName);+ //IEnumerable<Type> skillTypes = GetTypesInNamespace(assembly, "skills");+ Type[] skillTypes = assembly.GetTypes().ToArray();+ foreach(Type skillType in skillTypes)+ {+ Console.WriteLine($"Found type: {assembly.FullName}.{skillType.Namespace}.{skillType.Name}");+ if(skillType.Namespace.Equals("Microsoft.SKDevTeam"))+ {+ skills.Add(skillType.Name);++ Console.WriteLine($"Added skill: {skillType.Name}");+ }++ }+ return skills;+ }++++ private IEnumerable<Type> GetTypesInNamespace(Assembly assembly, string nameSpace)+ {+ return+ assembly.GetTypes()+ .Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal));+ }+}+diff --git a/Elsa.SemanticKernel/Activities/SemanticKernel.cs b/Elsa.SemanticKernel/Activities/SemanticKernel.csindex 72f08c15a..3883ffe90 100644--- a/Elsa.SemanticKernel/Activities/SemanticKernel.cs+++ b/Elsa.SemanticKernel/Activities/SemanticKernel.cs@@ -10,11 +10,21 @@ using Microsoft.SemanticKernel.Connectors.AI.OpenAI.TextEmbedding;using Microsoft.SemanticKernel.Memory;using Microsoft.SemanticKernel.Orchestration;using Microsoft.SemanticKernel.Reliability;-namespace Elsa.SemanticKernel;+using Microsoft.SemanticKernel.SkillDefinition;+using Microsoft.SKDevTeam;+using System;-using System.Text.Json;+using System.Text;+using System.Collections;+using System.Collections.Generic;+using System.Diagnostics.CodeAnalysis;+using System.Linq;+using System.Reflection;+using System.Threading;using System.Threading.Tasks;-using skills;+++namespace Elsa.SemanticKernel;/// <summary>/// Invoke a Semantic Kernel skill.@@ -53,11 +63,11 @@ public class SemanticKernelSkill : CodeActivity<string>DefaultValue = "ChatCompletion")]public Input<string> FunctionName { get; set; }-/* [Input(- Description = "Mockup - don't actually call the AI, just output the prompts",- UIHint = InputUIHints.Checkbox,- DefaultValue = false)]- public Input<bool> Mockup { get; set; } */+ /* [Input(+ Description = "Mockup - don't actually call the AI, just output the prompts",+ UIHint = InputUIHints.Checkbox,+ DefaultValue = false)]+ public Input<bool> Mockup { get; set; } *//// <inheritdoc />protected override async ValueTask ExecuteAsync(ActivityExecutionContext workflowContext)@@ -116,20 +126,95 @@ public class SemanticKernelSkill : CodeActivity<string>} */var skillConfig = SemanticFunctionConfig.ForSkillAndFunction(skillName, functionName);- var function = kernel.CreateSemanticFunction(skillConfig.PromptTemplate, skillConfig.Name, skillConfig.SkillName,- skillConfig.Description, skillConfig.MaxTokens, skillConfig.Temperature,- skillConfig.TopP, skillConfig.PPenalty, skillConfig.FPenalty);- var context = new ContextVariables();- context.Set("input", prompt);- //context.Set("wafContext", wafContext);- SKContext answer = await kernel.RunAsync(context, function).ConfigureAwait(false);- string result = answer.Result;+ /* var function = kernel.CreateSemanticFunction(skillConfig.PromptTemplate, skillConfig.Name, skillConfig.SkillName,+ skillConfig.Description, skillConfig.MaxTokens, skillConfig.Temperature,+ skillConfig.TopP, skillConfig.PPenalty, skillConfig.FPenalty); */++ var contextVars = new ContextVariables();+ contextVars.Set("input", prompt);+ SKContext context = kernel.CreateNewContext();++ var theSkills = LoadSkillsFromAssemblyAsync("skills", kernel);+ var functionsAvailable = context.Skills.GetFunctionsView();++ var list = new StringBuilder();+ foreach (KeyValuePair<string, List<FunctionView>> skill in functionsAvailable.SemanticFunctions)+ {+ Console.WriteLine($"Skill: {skill.Key}");+ foreach (FunctionView func in skill.Value)+ {+ // Function description+ if (func.Description != null)+ {+ list.AppendLine($"// {func.Description}");+ }+ else+ {+ Console.WriteLine("{0}.{1} is missing a description", func.SkillName, func.Name);+ list.AppendLine($"// Function {func.SkillName}.{func.Name}.");+ }- Console.WriteLine(info);+ // Function name+ list.AppendLine($"{func.SkillName}.{func.Name}");+ // Function parameters+ foreach (var p in func.Parameters)+ {+ var description = string.IsNullOrEmpty(p.Description) ? p.Name : p.Description;+ var defaultValueString = string.IsNullOrEmpty(p.DefaultValue) ? string.Empty : $" (default value: {p.DefaultValue})";+ list.AppendLine($"Parameter \"{p.Name}\": {description} {defaultValueString}");+ }+ }+ }++ Console.WriteLine($"List of all skills ----- {list.ToString()}");++ //context.Set("wafContext", wafContext);++ SKContext answer = await kernel.RunAsync(contextVars, function).ConfigureAwait(false);+ string result = answer.Result;+workflowContext.SetResult(result);}}+ ///<summary>+ /// Gets a list of the skills in the assembly+ ///</summary>+ private IEnumerable<string> LoadSkillsFromAssemblyAsync(string assemblyName, IKernel kernel)+ {+ var skills = new List<string>();+ var assembly = Assembly.Load(assemblyName);+ Type[] skillTypes = assembly.GetTypes().ToArray();+ foreach (Type skillType in skillTypes)+ {+ if (skillType.Namespace.Equals("Microsoft.SKDevTeam"))+ {+ skills.Add(skillType.Name);+ var functions = skillType.GetFields();+ foreach (var function in functions)+ {+ string field = function.FieldType.ToString();+ if (field.Equals("Microsoft.SKDevTeam.SemanticFunctionConfig"))+ {+ var skillConfig = SemanticFunctionConfig.ForSkillAndFunction(skillType.Name, function.Name);+ var skfunc = kernel.CreateSemanticFunction(+ skillConfig.PromptTemplate,+ skillConfig.Name,+ skillConfig.SkillName,+ skillConfig.Description,+ skillConfig.MaxTokens,+ skillConfig.Temperature,+ skillConfig.TopP,+ skillConfig.PPenalty,+ skillConfig.FPenalty);++ Console.WriteLine($"SK Added function: {skfunc.SkillName}.{skfunc.Name}");+ }+ }+ }+ }+ return skills;+ }}\ No newline at end of filediff --git a/Elsa.SemanticKernel/Elsa.SemanticKernel.csproj b/Elsa.SemanticKernel/Elsa.SemanticKernel.csprojindex c6f15a6e7..9f6e6c2ee 100644--- a/Elsa.SemanticKernel/Elsa.SemanticKernel.csproj+++ b/Elsa.SemanticKernel/Elsa.SemanticKernel.csproj@@ -5,11 +5,15 @@<Description>Activities for calling Semantic Kernel SDK</Description>- <PackageTags>elsa module semantic kerne activities</PackageTags>+ <PackageTags>elsa module semantic kernel activities</PackageTags></PropertyGroup><ItemGroup>- <PackageReference Include="Elsa" Version="3.0.0-preview.541" />+ <PackageReference Include="Elsa" Version="3.0.0-preview.552" />+ <PackageReference Include="Elsa.Http" Version="3.0.0-preview.552" />+ <PackageReference Include="Elsa.Workflows.Api" Version="3.0.0-preview.552" />+ <PackageReference Include="Elsa.Workflows.Core" Version="3.0.0-preview.552" />+ <PackageReference Include="Elsa.Workflows.Management" Version="3.0.0-preview.552" /><PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" /><PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" /><PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />diff --git a/WorkflowsApp/WorkflowsApp.csproj b/WorkflowsApp/WorkflowsApp.csprojindex d5e4e63e7..240d57ef4 100644--- a/WorkflowsApp/WorkflowsApp.csproj+++ b/WorkflowsApp/WorkflowsApp.csproj@@ -7,13 +7,13 @@</PropertyGroup><ItemGroup>- <PackageReference Include="Elsa" Version="3.0.0-preview.541" />- <PackageReference Include="Elsa.EntityFrameworkCore" Version="3.0.0-preview.541" />- <PackageReference Include="Elsa.EntityFrameworkCore.Sqlite" Version="3.0.0-preview.541" />- <PackageReference Include="Elsa.Http" Version="3.0.0-preview.541" />- <PackageReference Include="Elsa.Identity" Version="3.0.0-preview.541" />- <PackageReference Include="Elsa.Workflows.Api" Version="3.0.0-preview.541" />- <PackageReference Include="Elsa.Workflows.Designer" Version="3.0.0-preview.541" /><!--+ <PackageReference Include="Elsa" Version="3.0.0-preview.552" />+ <PackageReference Include="Elsa.EntityFrameworkCore" Version="3.0.0-preview.552" />+ <PackageReference Include="Elsa.EntityFrameworkCore.Sqlite" Version="3.0.0-preview.552" />+ <PackageReference Include="Elsa.Http" Version="3.0.0-preview.552" />+ <PackageReference Include="Elsa.Identity" Version="3.0.0-preview.552" />+ <PackageReference Include="Elsa.Workflows.Api" Version="3.0.0-preview.552" />+ <PackageReference Include="Elsa.Workflows.Designer" Version="3.0.0-preview.552" /><!--<PackageReference Include="Elsa.EntityFrameworkCore" Version="3.0.0-rc1" /><PackageReference Include="Elsa.EntityFrameworkCore.Sqlite" Version="3.0.0-rc1" /><PackageReference Include="Elsa.Http" Version="3.0.0-rc1" />diff --git a/cli/Program.cs b/cli/Program.csindex 0479aaf35..977cce0ff 100644--- a/cli/Program.cs+++ b/cli/Program.cs@@ -7,7 +7,8 @@ using Microsoft.SemanticKernel.Connectors.Memory.Qdrant;using Microsoft.SemanticKernel.Memory;using Microsoft.SemanticKernel.Orchestration;using Microsoft.SemanticKernel.Reliability;-using skills;+using Microsoft.SKDevTeam;+class Program{@@ -34,7 +35,7 @@ class Programvar pmCommand = new Command("pm", "Commands for the PM team");var pmReadmeCommand = new Command("readme", "Produce a Readme for a given input");- pmReadmeCommand.SetHandler(async (file, maxRetry) => await CallWithFile<string>(nameof(PM), PM.Readme , file.FullName, maxRetry), fileOption, maxRetryOption);+ pmReadmeCommand.SetHandler(async (file, maxRetry) => await CallWithFile<string>(nameof(PM), PM.Readme, file.FullName, maxRetry), fileOption, maxRetryOption);var pmBootstrapCommand = new Command("bootstrap", "Bootstrap a project for a given input");pmBootstrapCommand.SetHandler(async (file, maxRetry) => await CallWithFile<string>(nameof(PM), PM.BootstrapProject, file.FullName, maxRetry), fileOption, maxRetryOption);@@ -51,7 +52,7 @@ class Programvar devPlanCommand = new Command("plan", "Implement the module for a given input");devPlanCommand.SetHandler(async (file, maxRetry) => await CallWithFile<string>(nameof(Developer), Developer.Implement, file.FullName, maxRetry), fileOption, maxRetryOption);devCommand.AddCommand(devPlanCommand);-+rootCommand.AddCommand(pmCommand);rootCommand.AddCommand(devleadCommand);rootCommand.AddCommand(devCommand);@@ -67,7 +68,7 @@ class ProgramConsole.WriteLine($"Using output directory: {outputPath}");- var readme = await CallWithFile<string>(nameof(PM), PM.Readme , file, maxRetry);+ var readme = await CallWithFile<string>(nameof(PM), PM.Readme, file, maxRetry);string readmeFile = Path.Combine(outputPath.FullName, "README.md");await SaveToFile(readmeFile, readme);Console.WriteLine($"Saved README to {readmeFile}");@@ -83,12 +84,13 @@ class Programvar implementationTasks = plan.steps.SelectMany((step) => step.subtasks.Select(- async (subtask) => {+ async (subtask) =>+ {Console.WriteLine($"Implementing {step.step}-{subtask.subtask}");var implementationResult = string.Empty;while (true){- try+ try{implementationResult = await CallFunction<string>(nameof(Developer), Developer.Implement, subtask.LLM_prompt, maxRetry);break;@@ -105,7 +107,8 @@ class Program}await sandboxSkill.RunInDotnetAlpineAsync(implementationResult);await SaveToFile(Path.Combine(outputPath.FullName, $"{step.step}-{subtask.subtask}.sh"), implementationResult);- return implementationResult; }));+ return implementationResult;+ }));await Task.WhenAll(implementationTasks);}@@ -115,8 +118,8 @@ class Program}public static async Task<T> CallWithFile<T>(string skillName, string functionName, string filePath, int maxRetry)- {- if(!File.Exists(filePath))+ {+ if (!File.Exists(filePath))throw new FileNotFoundException($"File not found: {filePath}", filePath);var input = File.ReadAllText(filePath);return await CallFunction<T>(skillName, functionName, input, maxRetry);@@ -173,10 +176,11 @@ class Programvar answer = await kernel.RunAsync(context, function).ConfigureAwait(false);var result = typeof(T) != typeof(string) ? JsonSerializer.Deserialize<T>(answer.ToString()) : (T)(object)answer.ToString();//Console.WriteLine(answer);+return result;}}public static class PM { public static string Readme = "Readme"; public static string BootstrapProject = "BootstrapProject"; }-public static class DevLead { public static string Plan="Plan"; }-public static class Developer { public static string Implement="Implement"; public static string Improve="Improve";}+public static class DevLead { public static string Plan = "Plan"; }+public static class Developer { public static string Implement = "Implement"; public static string Improve = "Improve"; }diff --git a/skills/Chat.cs b/skills/Chat.csindex 049cd3ac3..da30e19b2 100644--- a/skills/Chat.cs+++ b/skills/Chat.cs@@ -1,4 +1,4 @@-namespace skills;+namespace Microsoft.SKDevTeam;public static class Chat{public static SemanticFunctionConfig ChatCompletion = new SemanticFunctionConfigdiff --git a/skills/DevLead.cs b/skills/DevLead.csindex 583e024b7..fd7790399 100644--- a/skills/DevLead.cs+++ b/skills/DevLead.cs@@ -1,4 +1,4 @@-namespace skills;+namespace Microsoft.SKDevTeam;public static class DevLead {public static SemanticFunctionConfig Plan = new SemanticFunctionConfig{diff --git a/skills/Developer.cs b/skills/Developer.csindex 5f5cb3a94..0d072ed56 100644--- a/skills/Developer.cs+++ b/skills/Developer.cs@@ -1,5 +1,5 @@-namespace skills;+namespace Microsoft.SKDevTeam;public static class Developer {public static SemanticFunctionConfig Implement = new SemanticFunctionConfig{diff --git a/skills/PM.cs b/skills/PM.csindex 4f91c420e..2eaca6610 100644--- a/skills/PM.cs+++ b/skills/PM.cs@@ -1,4 +1,4 @@-namespace skills;+namespace Microsoft.SKDevTeam;public static class PM{public static SemanticFunctionConfig BootstrapProject = new SemanticFunctionConfigdiff --git a/skills/SemanticFunctionConfig.cs b/skills/SemanticFunctionConfig.csindex d9d3589c1..7badd440d 100644--- a/skills/SemanticFunctionConfig.cs+++ b/skills/SemanticFunctionConfig.cs@@ -1,4 +1,4 @@-namespace skills;+namespace Microsoft.SKDevTeam;public class SemanticFunctionConfig{diff --git a/skills/skills.csproj b/skills/skills.csprojindex 140c3454d..eb4501ad2 100644--- a/skills/skills.csproj+++ b/skills/skills.csproj@@ -4,7 +4,6 @@<TargetFramework>net7.0</TargetFramework><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable>- <RootNamespace>skills</RootNamespace></PropertyGroup><ItemGroup>