Skip to content

Commit

Permalink
Update SK (autogenhub#15)
Browse files Browse the repository at this point in the history
* update sk all around - memory

* enable all roslyn rules

* remove githubjwt dependency

* add kernel-memory
  • Loading branch information
kostapetan authored Mar 19, 2024
1 parent baabd41 commit 04ddb4b
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 286 deletions.
2 changes: 0 additions & 2 deletions src/apps/WorkflowsApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using Elsa.EntityFrameworkCore.Modules.Management;
using Elsa.EntityFrameworkCore.Modules.Runtime;
using Elsa.Extensions;
using Elsa.Workflows.Core.Models;
using Elsa.Identity.Features;
using Elsa.SemanticKernel;

var builder = WebApplication.CreateBuilder(args);
Expand Down
17 changes: 4 additions & 13 deletions src/apps/WorkflowsApp/WorkflowsApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Elsa" Version="3.0.0-preview.727" />
<PackageReference Include="Elsa.EntityFrameworkCore" Version="3.0.0-preview.727" />
<PackageReference Include="Elsa.EntityFrameworkCore.Sqlite" Version="3.0.0-preview.727" />
<PackageReference Include="Elsa.Http" Version="3.0.0-preview.727" />
<PackageReference Include="Elsa.Identity" Version="3.0.0-preview.727" />
<PackageReference Include="Elsa.Workflows.Api" Version="3.0.0-preview.727" />
<PackageReference Include="Elsa.Workflows.Designer" Version="3.0.0-preview.727" /><!--
<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" />
<PackageReference Include="Elsa.Identity" Version="3.0.0-rc1" />
<PackageReference Include="Elsa.Workflows.Api" Version="3.0.0-rc1" />
<PackageReference Include="Elsa.Workflows.Designer" Version="3.0.0-rc1" /> -->
<PackageReference Include="Elsa.EntityFrameworkCore" Version="3.0.6" />
<PackageReference Include="Elsa.EntityFrameworkCore.Sqlite" Version="3.0.6" />
<PackageReference Include="Elsa.Identity" Version="3.0.6" />
<PackageReference Include="Elsa.Workflows.Designer" Version="3.0.0-preview.727" />
</ItemGroup>

<ItemGroup>
Expand Down
77 changes: 36 additions & 41 deletions src/apps/gh-flow/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@
using Microsoft.AI.DevTeam;
using Microsoft.Extensions.Options;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
using Microsoft.SemanticKernel.Connectors.Memory.Qdrant;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Plugins.Memory;
using Microsoft.SemanticKernel.Reliability.Basic;
using Octokit.Webhooks;
using Octokit.Webhooks.AspNetCore;
using Azure.Identity;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Http.Resilience;
using Microsoft.KernelMemory;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<WebhookEventProcessor, GithubWebHookProcessor>();
builder.Services.AddTransient(CreateKernel);
builder.Services.AddTransient(CreateMemory);
builder.Services.AddSingleton<IKernelMemory>(CreateMemory);
builder.Services.AddHttpClient();

builder.Services.AddSingleton(s =>
Expand Down Expand Up @@ -99,48 +96,46 @@

app.Run();

static ISemanticTextMemory CreateMemory(IServiceProvider provider)
static IKernelMemory CreateMemory(IServiceProvider provider)
{
var openAiConfig = provider.GetService<IOptions<OpenAIOptions>>().Value;
var qdrantConfig = provider.GetService<IOptions<QdrantOptions>>().Value;

var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.SetMinimumLevel(LogLevel.Debug)
.AddConsole()
.AddDebug();
});

var memoryBuilder = new MemoryBuilder();
return memoryBuilder.WithLoggerFactory(loggerFactory)
.WithQdrantMemoryStore(qdrantConfig.Endpoint, qdrantConfig.VectorSize)
.WithAzureTextEmbeddingGenerationService(openAiConfig.EmbeddingDeploymentOrModelId, openAiConfig.Endpoint, openAiConfig.ApiKey)
.Build();
var openAiConfig = provider.GetService<IOptions<OpenAIOptions>>().Value;
return new KernelMemoryBuilder()
.WithQdrantMemoryDb(qdrantConfig.Endpoint)
.WithAzureOpenAITextGeneration(new AzureOpenAIConfig
{
APIType = AzureOpenAIConfig.APITypes.ChatCompletion,
Endpoint = openAiConfig.Endpoint,
Deployment = openAiConfig.DeploymentOrModelId,
Auth = AzureOpenAIConfig.AuthTypes.APIKey,
APIKey = openAiConfig.ApiKey
})
.WithAzureOpenAITextEmbeddingGeneration(new AzureOpenAIConfig
{
APIType = AzureOpenAIConfig.APITypes.EmbeddingGeneration,
Endpoint = openAiConfig.Endpoint,
Deployment =openAiConfig.EmbeddingDeploymentOrModelId,
Auth = AzureOpenAIConfig.AuthTypes.APIKey,
APIKey = openAiConfig.ApiKey
})
.Build<MemoryServerless>();
}

static IKernel CreateKernel(IServiceProvider provider)
static Kernel CreateKernel(IServiceProvider provider)
{
var openAiConfig = provider.GetService<IOptions<OpenAIOptions>>().Value;

var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.SetMinimumLevel(LogLevel.Debug)
.AddConsole()
.AddDebug();
});

var clientOptions = new OpenAIClientOptions();
clientOptions.Retry.NetworkTimeout = TimeSpan.FromMinutes(5);
var openAIClient = new OpenAIClient(new Uri(openAiConfig.Endpoint), new AzureKeyCredential(openAiConfig.ApiKey), clientOptions);

return new KernelBuilder()
.WithLoggerFactory(loggerFactory)
.WithAzureChatCompletionService(openAiConfig.DeploymentOrModelId, openAIClient)
.WithRetryBasic(new BasicRetryConfig
{
MaxRetryCount = 5,
UseExponentialBackoff = true
}).Build();
var builder = Kernel.CreateBuilder();
builder.Services.AddLogging( c=> c.AddConsole().AddDebug().SetMinimumLevel(LogLevel.Debug));
builder.Services.AddAzureOpenAIChatCompletion(openAiConfig.DeploymentOrModelId, openAIClient);
builder.Services.ConfigureHttpClientDefaults(c=>
{
c.AddStandardResilienceHandler().Configure( o=> {
o.Retry.MaxRetryAttempts = 5;
o.Retry.BackoffType = Polly.DelayBackoffType.Exponential;
});
});
return builder.Build();
}
7 changes: 6 additions & 1 deletion src/apps/gh-flow/gh-flow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
<ServerGarbageCollection>true</ServerGarbageCollection>
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
<UserSecretsId>c073c86e-8483-4956-942f-331fd09172d4</UserSecretsId>
<AnalysisMode>All</AnalysisMode>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Octokit.Webhooks.AspNetCore" Version="2.0.3" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Qdrant" Version="1.0.0-beta5" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Qdrant" Version="1.6.2-alpha" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.6.2-alpha" />
<PackageReference Include="Microsoft.Orleans.Server" Version="8.0.0" />
<PackageReference Include="Microsoft.Orleans.Persistence.Cosmos" Version="8.0.0" />
<PackageReference Include="Microsoft.Orleans.Clustering.Cosmos" Version="8.0.0" />
Expand All @@ -25,6 +27,9 @@
<PackageReference Include="Microsoft.Orleans.Streaming.EventHubs" Version="8.0.0" />
<PackageReference Include="OrleansDashboard" Version="7.2.2" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.2" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.0.0" />
<PackageReference Include="Microsoft.KernelMemory.MemoryDb.Qdrant" Version="0.35.240318.1" />

</ItemGroup>


Expand Down
64 changes: 27 additions & 37 deletions src/apps/seed-memory/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
using UglyToad.PdfPig;
using UglyToad.PdfPig.DocumentLayoutAnalysis.TextExtractor;
using Microsoft.SemanticKernel;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel.Connectors.Memory.Qdrant;
using Microsoft.SemanticKernel.Memory;
using System.Reflection;
using Microsoft.SemanticKernel.Plugins.Memory;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory;

class Program
{
Expand All @@ -22,37 +15,34 @@ static async Task Main(string[] args)
.AddConsole()
.AddDebug();
});

var memoryBuilder = new MemoryBuilder();
var memory = memoryBuilder.WithLoggerFactory(loggerFactory)
.WithQdrantMemoryStore(kernelSettings.QdrantEndpoint, 1536)
.WithAzureTextEmbeddingGenerationService(kernelSettings.EmbeddingDeploymentOrModelId, kernelSettings.Endpoint, kernelSettings.ApiKey)
.Build();

var memory = new KernelMemoryBuilder()
.WithQdrantMemoryDb(kernelSettings.QdrantEndpoint)
.WithAzureOpenAITextGeneration(new AzureOpenAIConfig
{
APIType = AzureOpenAIConfig.APITypes.ChatCompletion,
Endpoint =kernelSettings.Endpoint,
Deployment = kernelSettings.DeploymentOrModelId,
Auth = AzureOpenAIConfig.AuthTypes.APIKey,
APIKey = kernelSettings.ApiKey
})
.WithAzureOpenAITextEmbeddingGeneration(new AzureOpenAIConfig
{
APIType = AzureOpenAIConfig.APITypes.EmbeddingGeneration,
Endpoint = kernelSettings.Endpoint,
Deployment =kernelSettings.EmbeddingDeploymentOrModelId,
Auth = AzureOpenAIConfig.AuthTypes.APIKey,
APIKey = kernelSettings.ApiKey
})
.Build<MemoryServerless>();
await ImportDocumentAsync(memory, WafFileName);
}

public static async Task ImportDocumentAsync(ISemanticTextMemory memory, string filename)
public static async Task ImportDocumentAsync(IKernelMemory memory, string filename)
{
var currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var filePath = Path.Combine(currentDirectory, filename);
using var pdfDocument = PdfDocument.Open(File.OpenRead(filePath));
var pages = pdfDocument.GetPages();
foreach (var page in pages)
{
try
{
var text = ContentOrderTextExtractor.GetText(page);
var descr = text.Take(100);
await memory.SaveInformationAsync(
collection: "waf-pages",
text: text,
id: $"{Guid.NewGuid()}",
description: $"Document: {descr}");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
await memory.ImportDocumentAsync(new Document("wafdoc")
.AddFiles([
filename
]), index: "waf");
}
}
9 changes: 3 additions & 6 deletions src/apps/seed-memory/seed-memory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta5" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Qdrant" Version="1.0.0-beta5" />
<PackageReference Include="PdfPig" Version="0.1.8-alpha-20230529-6daa2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.6.2" />
<PackageReference Include="Microsoft.KernelMemory.MemoryDb.Qdrant" Version="0.35.240318.1" />
<PackageReference Include="Microsoft.KernelMemory.Core" Version="0.35.240318.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.AI.OpenAI;
using Elsa.Extensions;
using Elsa.Workflows.Core;
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Core.Models;
using Elsa.Workflows;
using Elsa.Workflows.Contracts;
using Elsa.Workflows.Models;
using Elsa.Workflows.UIHints;
using Microsoft.AI.DevTeam.Skills;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http.Resilience;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.Connectors.OpenAI;

namespace Elsa.SemanticKernel;

Expand All @@ -36,9 +40,8 @@ public async ValueTask<IEnumerable<ActivityDescriptor>> GetDescriptorsAsync(Canc

// get a list of skills in the assembly
var skills = LoadSkillsFromAssemblyAsync("skills", kernel);
SKContext context = kernel.CreateNewContext();
var functionsAvailable = context.Functions.GetFunctionViews();

var functionsAvailable = kernel.Plugins.GetFunctionsMetadata();

// create activity descriptors for each skilland function
var activities = new List<ActivityDescriptor>();
foreach (var function in functionsAvailable)
Expand All @@ -56,7 +59,7 @@ public async ValueTask<IEnumerable<ActivityDescriptor>> GetDescriptorsAsync(Canc
/// <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)
private ActivityDescriptor CreateActivityDescriptorFromSkillAndFunction(KernelFunctionMetadata function, CancellationToken cancellationToken = default)
{
// Create a fully qualified type name for the activity
var thisNamespace = GetType().Namespace;
Expand Down Expand Up @@ -128,12 +131,12 @@ private InputDescriptor CreateInputDescriptor(Type inputType, string name, Objec
/// </summary>
/// <param name="parameter">The function parameter.</param>
/// <returns>An input descriptor.</returns>
private InputDescriptor CreateInputDescriptorFromSKParameter(ParameterView parameter)
private InputDescriptor CreateInputDescriptorFromSKParameter(KernelParameterMetadata parameter)
{
var inputDescriptor = new InputDescriptor
{
Description = string.IsNullOrEmpty(parameter.Description) ? parameter.Name : parameter.Description,
DefaultValue = string.IsNullOrEmpty(parameter.DefaultValue) ? string.Empty : parameter.DefaultValue,
DefaultValue = parameter.DefaultValue == null ? string.Empty : parameter.DefaultValue,
Type = typeof(string),
Name = parameter.Name,
DisplayName = parameter.Name,
Expand All @@ -150,7 +153,7 @@ private InputDescriptor CreateInputDescriptorFromSKParameter(ParameterView param
///<summary>
/// Gets a list of the skills in the assembly
///</summary>
private IEnumerable<string> LoadSkillsFromAssemblyAsync(string assemblyName, IKernel kernel)
private IEnumerable<string> LoadSkillsFromAssemblyAsync(string assemblyName, Kernel kernel)
{
var skills = new List<string>();
var assembly = Assembly.Load(assemblyName);
Expand All @@ -167,10 +170,10 @@ private IEnumerable<string> LoadSkillsFromAssemblyAsync(string assemblyName, IKe
if (field.Equals("Microsoft.SKDevTeam.SemanticFunctionConfig"))
{
var promptTemplate = Skills.ForSkillAndFunction(skillType.Name, function.Name);
var skfunc = kernel.CreateSemanticFunction(
promptTemplate, new OpenAIRequestSettings { MaxTokens = 8000, Temperature = 0.4, TopP = 1 });
var skfunc = kernel.CreateFunctionFromPrompt(
promptTemplate, new OpenAIPromptExecutionSettings { MaxTokens = 8000, Temperature = 0.4, TopP = 1 });

Console.WriteLine($"SKActivityProvider Added SK function: {skfunc.PluginName}.{skfunc.Name}");
Console.WriteLine($"SKActivityProvider Added SK function: {skfunc.Metadata.PluginName}.{skfunc.Name}");
}
}
}
Expand All @@ -182,22 +185,24 @@ private IEnumerable<string> LoadSkillsFromAssemblyAsync(string assemblyName, IKe
/// Gets a semantic kernel instance
/// </summary>
/// <returns>Microsoft.SemanticKernel.IKernel</returns>
private IKernel KernelBuilder()
private Kernel KernelBuilder()
{
var kernelSettings = KernelSettings.LoadSettings();

using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
var clientOptions = new OpenAIClientOptions();
clientOptions.Retry.NetworkTimeout = TimeSpan.FromMinutes(5);
var openAIClient = new OpenAIClient(new Uri(kernelSettings.Endpoint), new AzureKeyCredential(kernelSettings.ApiKey), clientOptions);
var builder = Kernel.CreateBuilder();
builder.Services.AddLogging( c=> c.AddConsole().AddDebug().SetMinimumLevel(LogLevel.Debug));
builder.Services.AddAzureOpenAIChatCompletion(kernelSettings.DeploymentOrModelId, openAIClient);
builder.Services.ConfigureHttpClientDefaults(c=>
{
builder.SetMinimumLevel(kernelSettings.LogLevel ?? LogLevel.Warning);
c.AddStandardResilienceHandler().Configure( o=> {
o.Retry.MaxRetryAttempts = 5;
o.Retry.BackoffType = Polly.DelayBackoffType.Exponential;
});
});

var kernel = new KernelBuilder()
.WithLoggerFactory(loggerFactory)
.WithAzureChatCompletionService(kernelSettings.DeploymentOrModelId, kernelSettings.Endpoint, kernelSettings.ApiKey, true, kernelSettings.ServiceId, true)
.Build();

return kernel;
return builder.Build();
}

}

Loading

0 comments on commit 04ddb4b

Please sign in to comment.