2023-07-14 13:57:07 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Elsa.Extensions;
|
|
|
|
using Elsa.Workflows.Core;
|
|
|
|
using Elsa.Workflows.Core.Contracts;
|
|
|
|
using Elsa.Workflows.Core.Models;
|
2023-10-26 22:09:18 +02:00
|
|
|
using Microsoft.AI.DevTeam.Skills;
|
2023-07-14 13:57:07 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.SemanticKernel;
|
2024-02-19 11:38:51 +01:00
|
|
|
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
|
2023-07-14 13:57:07 -07:00
|
|
|
using Microsoft.SemanticKernel.Orchestration;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2023-07-17 16:19:59 -07:00
|
|
|
// get the kernel
|
|
|
|
var kernel = KernelBuilder();
|
|
|
|
|
|
|
|
// get a list of skills in the assembly
|
|
|
|
var skills = LoadSkillsFromAssemblyAsync("skills", kernel);
|
2023-07-18 08:50:22 -07:00
|
|
|
SKContext context = kernel.CreateNewContext();
|
2024-02-19 11:38:51 +01:00
|
|
|
var functionsAvailable = context.Functions.GetFunctionViews();
|
2023-07-17 16:19:59 -07:00
|
|
|
|
|
|
|
// create activity descriptors for each skilland function
|
|
|
|
var activities = new List<ActivityDescriptor>();
|
2024-02-19 11:38:51 +01:00
|
|
|
foreach (var function in functionsAvailable)
|
2023-07-14 13:57:07 -07:00
|
|
|
{
|
2024-02-19 11:38:51 +01:00
|
|
|
Console.WriteLine($"Creating Activities for Plugin: {function.PluginName}");
|
|
|
|
activities.Add(CreateActivityDescriptorFromSkillAndFunction(function, cancellationToken));
|
2023-07-14 13:57:07 -07:00
|
|
|
}
|
2023-07-17 16:19:59 -07:00
|
|
|
|
|
|
|
return activities;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates an activity descriptor from a skill and function.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="function">The semantic kernel function</param>
|
|
|
|
/// <param name="cancellationToken">An optional cancellation token.</param>
|
|
|
|
/// <returns>An activity descriptor.</returns>
|
|
|
|
private ActivityDescriptor CreateActivityDescriptorFromSkillAndFunction(FunctionView function, CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
// Create a fully qualified type name for the activity
|
|
|
|
var thisNamespace = GetType().Namespace;
|
2024-02-19 11:38:51 +01:00
|
|
|
var fullTypeName = $"{thisNamespace}.{function.PluginName}.{function.Name}";
|
2023-07-17 16:19:59 -07:00
|
|
|
Console.WriteLine($"Creating Activity: {fullTypeName}");
|
|
|
|
|
|
|
|
// create inputs from the function parameters - the SemanticKernelSkill activity will be the base for each activity
|
|
|
|
var inputs = new List<InputDescriptor>();
|
|
|
|
foreach (var p in function.Parameters) { inputs.Add(CreateInputDescriptorFromSKParameter(p)); }
|
2024-02-19 11:38:51 +01:00
|
|
|
inputs.Add(CreateInputDescriptor(typeof(string), "SkillName", function.PluginName, "The name of the skill to use (generated, do not change)"));
|
2023-07-17 16:19:59 -07:00
|
|
|
inputs.Add(CreateInputDescriptor(typeof(string), "FunctionName", function.Name, "The name of the function to use (generated, do not change)"));
|
|
|
|
inputs.Add(CreateInputDescriptor(typeof(int), "MaxRetries", KernelSettings.DefaultMaxRetries, "Max Retries to contact AI Service"));
|
|
|
|
|
|
|
|
return new ActivityDescriptor
|
|
|
|
{
|
|
|
|
Kind = ActivityKind.Task,
|
|
|
|
Category = "Semantic Kernel",
|
|
|
|
Description = function.Description,
|
|
|
|
Name = function.Name,
|
|
|
|
TypeName = fullTypeName,
|
2024-02-19 11:38:51 +01:00
|
|
|
Namespace = $"{thisNamespace}.{function.PluginName}",
|
|
|
|
DisplayName = $"{function.PluginName}.{function.Name}",
|
2023-07-17 16:19:59 -07:00
|
|
|
Inputs = inputs,
|
|
|
|
Outputs = new[] {new OutputDescriptor()},
|
|
|
|
Constructor = context =>
|
|
|
|
{
|
|
|
|
// The constructor is called when an activity instance of this type is requested.
|
|
|
|
|
|
|
|
// Create the activity instance.
|
|
|
|
var activityInstance = _activityFactory.Create<SemanticKernelSkill>(context);
|
|
|
|
|
|
|
|
// Customize the activity type name.
|
|
|
|
activityInstance.Type = fullTypeName;
|
|
|
|
|
|
|
|
// Configure the activity's URL and method properties.
|
2024-02-19 11:38:51 +01:00
|
|
|
activityInstance.SkillName = new Input<string?>(function.PluginName);
|
2023-07-17 16:19:59 -07:00
|
|
|
activityInstance.FunctionName = new Input<string?>(function.Name);
|
|
|
|
|
|
|
|
return activityInstance;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// Creates an input descriptor for a single line string
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name of the input field</param>
|
|
|
|
/// <param name="description">The description of the input field</param>
|
|
|
|
private InputDescriptor CreateInputDescriptor(Type inputType, string name, Object defaultValue, string description)
|
|
|
|
{
|
|
|
|
var inputDescriptor = new InputDescriptor
|
|
|
|
{
|
|
|
|
Description = description,
|
|
|
|
DefaultValue = defaultValue,
|
|
|
|
Type = inputType,
|
|
|
|
Name = name,
|
|
|
|
DisplayName = name,
|
|
|
|
IsSynthetic = true, // This is a synthetic property, i.e. it is not part of the activity's .NET type.
|
|
|
|
IsWrapped = true, // This property is wrapped within an Input<T> object.
|
|
|
|
UIHint = InputUIHints.SingleLine,
|
|
|
|
ValueGetter = activity => activity.SyntheticProperties.GetValueOrDefault(name),
|
|
|
|
ValueSetter = (activity, value) => activity.SyntheticProperties[name] = value!,
|
|
|
|
};
|
|
|
|
return inputDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates an input descriptor from an sk funciton parameter definition.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="parameter">The function parameter.</param>
|
|
|
|
/// <returns>An input descriptor.</returns>
|
|
|
|
private InputDescriptor CreateInputDescriptorFromSKParameter(ParameterView parameter)
|
|
|
|
{
|
|
|
|
var inputDescriptor = new InputDescriptor
|
|
|
|
{
|
|
|
|
Description = string.IsNullOrEmpty(parameter.Description) ? parameter.Name : parameter.Description,
|
|
|
|
DefaultValue = string.IsNullOrEmpty(parameter.DefaultValue) ? string.Empty : parameter.DefaultValue,
|
|
|
|
Type = typeof(string),
|
|
|
|
Name = parameter.Name,
|
|
|
|
DisplayName = parameter.Name,
|
|
|
|
IsSynthetic = true, // This is a synthetic property, i.e. it is not part of the activity's .NET type.
|
|
|
|
IsWrapped = true, // This property is wrapped within an Input<T> object.
|
|
|
|
UIHint = InputUIHints.MultiLine,
|
|
|
|
ValueGetter = activity => activity.SyntheticProperties.GetValueOrDefault(parameter.Name),
|
|
|
|
ValueSetter = (activity, value) => activity.SyntheticProperties[parameter.Name] = value!,
|
|
|
|
|
|
|
|
};
|
|
|
|
return inputDescriptor;
|
2023-07-14 13:57:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
///<summary>
|
|
|
|
/// Gets a list of the skills in the assembly
|
|
|
|
///</summary>
|
2023-07-17 16:19:59 -07:00
|
|
|
private IEnumerable<string> LoadSkillsFromAssemblyAsync(string assemblyName, IKernel kernel)
|
2023-07-14 13:57:07 -07:00
|
|
|
{
|
|
|
|
var skills = new List<string>();
|
|
|
|
var assembly = Assembly.Load(assemblyName);
|
2023-07-17 16:19:59 -07:00
|
|
|
Type[] skillTypes = assembly.GetTypes().ToArray();
|
|
|
|
foreach (Type skillType in skillTypes)
|
|
|
|
{
|
|
|
|
if (skillType.Namespace.Equals("Microsoft.SKDevTeam"))
|
2023-07-14 13:57:07 -07:00
|
|
|
{
|
2023-07-17 16:19:59 -07:00
|
|
|
skills.Add(skillType.Name);
|
|
|
|
var functions = skillType.GetFields();
|
|
|
|
foreach (var function in functions)
|
2023-07-14 13:57:07 -07:00
|
|
|
{
|
2023-07-17 16:19:59 -07:00
|
|
|
string field = function.FieldType.ToString();
|
|
|
|
if (field.Equals("Microsoft.SKDevTeam.SemanticFunctionConfig"))
|
|
|
|
{
|
2024-02-19 11:38:51 +01:00
|
|
|
var promptTemplate = Skills.ForSkillAndFunction(skillType.Name, function.Name);
|
2023-07-17 16:19:59 -07:00
|
|
|
var skfunc = kernel.CreateSemanticFunction(
|
2024-02-19 11:38:51 +01:00
|
|
|
promptTemplate, new OpenAIRequestSettings { MaxTokens = 8000, Temperature = 0.4, TopP = 1 });
|
|
|
|
|
|
|
|
Console.WriteLine($"SKActivityProvider Added SK function: {skfunc.PluginName}.{skfunc.Name}");
|
2023-07-17 16:19:59 -07:00
|
|
|
}
|
2023-07-14 13:57:07 -07:00
|
|
|
}
|
|
|
|
}
|
2023-07-17 16:19:59 -07:00
|
|
|
}
|
2023-07-14 13:57:07 -07:00
|
|
|
return skills;
|
|
|
|
}
|
|
|
|
|
2023-07-17 16:19:59 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a semantic kernel instance
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Microsoft.SemanticKernel.IKernel</returns>
|
|
|
|
private IKernel KernelBuilder()
|
|
|
|
{
|
|
|
|
var kernelSettings = KernelSettings.LoadSettings();
|
|
|
|
|
|
|
|
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
|
|
|
|
{
|
|
|
|
builder.SetMinimumLevel(kernelSettings.LogLevel ?? LogLevel.Warning);
|
|
|
|
});
|
2023-07-14 13:57:07 -07:00
|
|
|
|
2023-07-17 16:19:59 -07:00
|
|
|
var kernel = new KernelBuilder()
|
2023-10-11 21:40:35 +02:00
|
|
|
.WithLoggerFactory(loggerFactory)
|
2023-07-17 16:19:59 -07:00
|
|
|
.WithAzureChatCompletionService(kernelSettings.DeploymentOrModelId, kernelSettings.Endpoint, kernelSettings.ApiKey, true, kernelSettings.ServiceId, true)
|
|
|
|
.Build();
|
2023-07-14 13:57:07 -07:00
|
|
|
|
2023-07-17 16:19:59 -07:00
|
|
|
return kernel;
|
2023-07-14 13:57:07 -07:00
|
|
|
}
|
2023-07-17 16:19:59 -07:00
|
|
|
|
2023-07-14 13:57:07 -07:00
|
|
|
}
|
|
|
|
|