Skip to content

Commit

Permalink
Update the kernel
Browse files Browse the repository at this point in the history
Added the ServiceCollectionExtensions for the Clients and Kernel
  • Loading branch information
RGBKnights committed May 8, 2024
1 parent 8afedaa commit 053b63d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 38 deletions.
72 changes: 72 additions & 0 deletions src/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Boundless.OmniAdapter.Anthropic;
using Boundless.OmniAdapter.AzureAi;
using Boundless.OmniAdapter.Groq;
using Boundless.OmniAdapter.Interfaces;
using Boundless.OmniAdapter.Kernel;
using Boundless.OmniAdapter.OpenAi;
using Boundless.OmniAdapter.Perplexity;
using Microsoft.Extensions.DependencyInjection;

namespace Boundless.OmniAdapter;

public static class ServiceCollectionExtensions
{
public static void AddOpenAiClient(this IServiceCollection services)
{
const string key = "OpenAi";
services.AddKeyedSingleton<IChatCompletion, OpenAiClient>(key);
services.AddKeyedSingleton<IChatStream, OpenAiClient>(key);
services.AddKeyedSingleton<IAudioCompletion, OpenAiClient>(key);
services.AddKeyedSingleton<IImageCompletion, OpenAiClient>(key);
services.AddSingleton<OpenAiClient>();
}
public static void AddPerplexityClient(this IServiceCollection services)
{
const string key = "Perplexity";
services.AddKeyedSingleton<IChatCompletion, PerplexityClient>(key);
services.AddKeyedSingleton<IChatStream, PerplexityClient>(key);
services.AddSingleton<PerplexityClient>();
}
public static void AddAzureAiClient(this IServiceCollection services)
{
const string key = "AzureAi";
services.AddKeyedSingleton<IChatCompletion, AzureAiClient>(key);
services.AddKeyedSingleton<IChatStream, AzureAiClient>(key);
services.AddSingleton<AzureAiClient>();

}
public static void AddAnthropicClient(this IServiceCollection services)
{
const string key = "Anthropic";
services.AddKeyedSingleton<IChatCompletion, AnthropicClient>(key);
services.AddKeyedSingleton<IChatStream, AnthropicClient>(key);
services.AddSingleton<AnthropicClient>();
}
public static void AddGroqClient(this IServiceCollection services)
{
const string key = "Groq";
services.AddKeyedSingleton<IChatCompletion, GroqClient>(key);
services.AddKeyedSingleton<IChatStream, GroqClient>(key);
services.AddSingleton<GroqClient>();
}

public static void AddKernel(this IServiceCollection services, Action<IServiceProvider, KernelBuilder> fn)
{
services.AddSingleton<IKernel>(sp =>
{
var builder = new KernelBuilder();
fn(sp, builder);
return builder.Build();
});
}

public static void AddKernel(this IServiceCollection services, string key, Action<IServiceProvider, KernelBuilder> fn)
{
services.AddKeyedSingleton<IKernel>(key, (sp, _) =>
{
var builder = new KernelBuilder();
fn(sp, builder);
return builder.Build();
});
}
}
38 changes: 0 additions & 38 deletions src/Kernel/KernelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,11 @@ public class KernelBuilder
private IChatCompletion? chatCompletion;
private ChatSettings? chatSetting;
private List<FunctionBinding> bindings = new List<FunctionBinding>();
private IAudioCompletion? audioCompletion;
private IImageCompletion? imageCompletion;

public KernelBuilder()
{
}

public KernelBuilder AddClient<C>(C completion) where C : IChatCompletion, IAudioCompletion, IImageCompletion
{
this.chatCompletion = completion;
this.audioCompletion = completion;
this.imageCompletion = completion;
return this;
}

public KernelBuilder WithChatCompletion(ChatSettings settings)
{
this.chatSetting = settings;
return this;
}
public KernelBuilder WithChatCompletion(IChatCompletion completion, ChatSettings settings)
{
this.chatCompletion = completion;
Expand All @@ -42,29 +27,6 @@ public KernelBuilder AddFunction<T>(T action) where T : Delegate
return this;
}


public KernelBuilder WithAudioCompletion(AudioSettings settings)
{
return this;
}
public KernelBuilder WithAudioCompletion(IAudioCompletion completion, AudioSettings settings)
{
this.audioCompletion = completion;
return this;
}

public KernelBuilder WithImageCompletion(ImageSettings settings)
{
return this;
}
public KernelBuilder WithImageCompletion(IImageCompletion completion, ImageSettings settings)
{
this.imageCompletion = completion;
return this;
}



public IKernel Build()
{
if (this.chatCompletion is null || this.chatSetting is null)
Expand Down

0 comments on commit 053b63d

Please sign in to comment.