Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasper Marstal committed Dec 6, 2024
1 parent 2c0eebc commit 3e8f14f
Show file tree
Hide file tree
Showing 36 changed files with 271 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using System.Text.Json;
using Cellm.AddIn;
using MediatR;
using Microsoft.Extensions.Caching.Hybrid;
using Microsoft.Extensions.Options;

namespace Cellm.Models.ModelRequestBehavior;
namespace Cellm.Models.Behaviors;

internal class CachingBehavior<TRequest, TResponse>(HybridCache cache, IOptions<CellmConfiguration> cellmConfiguration) : IPipelineBehavior<TRequest, TResponse>
internal class CacheBehavior<TRequest, TResponse>(HybridCache cache, IOptions<CacheConfiguration> cacheConfiguration) : IPipelineBehavior<TRequest, TResponse>
where TRequest : IModelRequest<TResponse>
where TResponse : IModelResponse
{
private readonly HybridCacheEntryOptions _cacheEntryOptions = new()
{
Expiration = TimeSpan.FromSeconds(cellmConfiguration.Value.CacheTimeoutInSeconds)
Expiration = TimeSpan.FromSeconds(cacheConfiguration.Value.CacheTimeoutInSeconds)
};

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
if (cellmConfiguration.Value.EnableCache)
if (cacheConfiguration.Value.EnableCache)
{
return await cache.GetOrCreateAsync(
JsonSerializer.Serialize(request.Prompt),
Expand Down
8 changes: 8 additions & 0 deletions src/Cellm.Models/Behaviors/Cache/CacheConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Cellm.Models.Behaviors;

internal class CacheConfiguration
{
public int CacheTimeoutInSeconds { get; init; }

public bool EnableCache { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using MediatR;

namespace Cellm.Models.ModelRequestBehavior;
namespace Cellm.Models.Behaviors;

internal class SentryBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : notnull
Expand Down
57 changes: 57 additions & 0 deletions src/Cellm.Models/Behaviors/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Cellm.Models.Tools;
using MediatR;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;

namespace Cellm.Models.Behaviors;

internal static class ServiceCollectionExtensions
{
public static IServiceCollection AddSentryBehavior(this IServiceCollection services)
{
services
.AddSingleton(typeof(IPipelineBehavior<,>), typeof(SentryBehavior<,>));

return services;
}

public static IServiceCollection AddCachingBehavior(this IServiceCollection services)
{
#pragma warning disable EXTEXP0018 // Type is for evaluation purposes only and is subject to change or removal in future updates.
services
.AddHybridCache();
#pragma warning restore EXTEXP0018 // Type is for evaluation purposes only and is subject to change or removal in future updates.

services
.AddSingleton(typeof(IPipelineBehavior<,>), typeof(CacheBehavior<,>));

return services;
}

public static IServiceCollection AddToolBehavior(this IServiceCollection services)
{
return services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(ToolBehavior<,>));
}

public static IServiceCollection AddTools(this IServiceCollection services, params Delegate[] tools)
{
foreach (var tool in tools)
{
services.AddSingleton(AIFunctionFactory.Create(tool));
}

return services;
}

public static IServiceCollection AddTools(this IServiceCollection services, params Func<IServiceProvider, AIFunction>[] toolBuilders)
{


foreach (var toolBuilder in toolBuilders)
{
services.AddSingleton((serviceProvider) => toolBuilder(serviceProvider));
}

return services;
}
}
23 changes: 23 additions & 0 deletions src/Cellm.Models/Behaviors/Tools/ToolBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Cellm.Models.Behaviors;
using MediatR;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Options;

namespace Cellm.Models.Tools;

internal class ToolBehavior<TRequest, TResponse>(IOptions<ToolConfiguration> toolConfiguration, IEnumerable<AIFunction> functions)
: IPipelineBehavior<TRequest, TResponse> where TRequest : IModelRequest<TResponse>
{
private readonly ToolConfiguration _toolConfiguration = toolConfiguration.Value;
private readonly List<AITool> _tools = new(functions);

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
if (_toolConfiguration.EnableTools)
{
request.Prompt.Options.Tools = _tools;
}

return await next();
}
}
6 changes: 6 additions & 0 deletions src/Cellm.Models/Behaviors/Tools/ToolConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Cellm.Models.Behaviors;

internal class ToolConfiguration
{
public bool EnableTools { get; init; }
}
51 changes: 27 additions & 24 deletions src/Cellm.Models/Cellm.Models.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,30 @@
<Import_RootNamespace>Cellm.Models</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Behaviors\Cache\CacheConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Behaviors\Tools\ToolConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Client.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\CircuitBreakerConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\IProviderConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\RateLimiterConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\ResiliencePipelineConfigurator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\RetryConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Configuration\SentryConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IModelRequest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IModelRequestHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IModelResponse.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IProviderRequest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IProviderRequestHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Local\Utilities\FileManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Local\Utilities\ProcessManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Local\Utilities\ServerManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ModelRequestBehavior\CachingBehavior.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ModelRequestBehavior\SentryBehavior.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ModelRequestBehavior\ToolBehavior.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Behaviors\ServiceCollectionExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ProviderConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\Anthropic\ServiceCollectionExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\Llamafile\ServiceCollectionExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Resilience\CircuitBreakerConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\IProviderConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Resilience\HttpConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Resilience\RateLimiterConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Resilience\ResiliencePipelineConfigurator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Resilience\RetryConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\IModelRequest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\IModelRequestHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\IModelResponse.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\IProviderRequest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\IProviderRequestHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\Utilities\FileManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\Utilities\ProcessManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\Utilities\ServerManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Behaviors\Cache\CacheBehavior.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Behaviors\Sentry\SentryBehavior.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Behaviors\Tools\ToolBehavior.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Prompts\Prompt.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Prompts\PromptBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Provider.cs" />
Expand All @@ -49,7 +55,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Providers\OpenAiCompatible\OpenAiCompatibleRequest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\OpenAiCompatible\OpenAiCompatibleRequestHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\OpenAiCompatible\OpenAiCompatibleResponse.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\OpenAiCompatible\SerrviceCollectionExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\OpenAiCompatible\ServiceCollectionExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\OpenAi\OpenAiConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\OpenAi\OpenAiRequest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Providers\OpenAi\OpenAiRequestHandler.cs" />
Expand All @@ -58,11 +64,8 @@
<Compile Include="$(MSBuildThisFileDirectory)Serde.cs" />
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)Providers\Llamafile\llama.log" />
<None Include="$(MSBuildThisFileDirectory)Providers\Llamafile\test.xlsb.xlsx" />
<None Include="$(MSBuildThisFileDirectory)Providers\Llamafile\test.xlsx" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)Local\" />
<Folder Include="$(MSBuildThisFileDirectory)Behaviors\Cache\" />
<Folder Include="$(MSBuildThisFileDirectory)Behaviors\Tools\" />
<Folder Include="$(MSBuildThisFileDirectory)Behaviors\Sentry\" />
</ItemGroup>
</Project>
7 changes: 3 additions & 4 deletions src/Cellm.Models/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@
using MediatR;
using Microsoft.Extensions.Options;
using Polly.Timeout;
using Cellm.AddIn;

namespace Cellm.Models;

internal class Client(ISender sender, IOptions<CellmConfiguration> cellmConfiguration)
internal class Client(ISender sender, IOptions<ProviderConfiguration> providerConfiguration)
{
private readonly CellmConfiguration _cellmConfiguration = cellmConfiguration.Value;
private readonly ProviderConfiguration _providerConfiguration = providerConfiguration.Value;

public async Task<Prompt> Send(Prompt prompt, string? provider, Uri? baseAddress, CancellationToken cancellationToken)
{
try
{
provider ??= _cellmConfiguration.DefaultProvider;
provider ??= _providerConfiguration.DefaultProvider;

if (!Enum.TryParse<Provider>(provider, true, out var parsedProvider))
{
Expand Down
27 changes: 0 additions & 27 deletions src/Cellm.Models/ModelRequestBehavior/ToolBehavior.cs

This file was deleted.

10 changes: 10 additions & 0 deletions src/Cellm.Models/ProviderConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Cellm.Models;

internal class ProviderConfiguration : IProviderConfiguration
{
public string DefaultProvider { get; init; } = string.Empty;

public string DefaultModel { get; init; } = string.Empty;

public double DefaultTemperature { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Cellm.Models.Prompts;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Options;
using Cellm.AddIn;
using Cellm.Services.Configuration;

namespace Cellm.Models.Anthropic;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Cellm.Models.Anthropic;
using Cellm.Services.Configuration;
using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Cellm.Models.Providers.Anthropic;

internal static class ServiceCollectionExtensions
{
public static IServiceCollection AddAnthropicChatClient(this IServiceCollection services, IConfiguration configuration)
{
var resiliencePipelineConfigurator = new ResiliencePipelineConfigurator(configuration);

var anthropicConfiguration = configuration.GetRequiredSection(nameof(AnthropicConfiguration)).Get<AnthropicConfiguration>()
?? throw new NullReferenceException(nameof(AnthropicConfiguration));

services
.AddHttpClient<IRequestHandler<AnthropicRequest, AnthropicResponse>, AnthropicRequestHandler>(anthropicHttpClient =>
{
anthropicHttpClient.BaseAddress = anthropicConfiguration.BaseAddress;
anthropicHttpClient.DefaultRequestHeaders.Add("x-api-key", anthropicConfiguration.ApiKey);
anthropicHttpClient.DefaultRequestHeaders.Add("anthropic-version", anthropicConfiguration.Version);
anthropicHttpClient.Timeout = TimeSpan.FromHours(1);
})
.AddResilienceHandler($"{nameof(AnthropicRequestHandler)}", resiliencePipelineConfigurator.ConfigureResiliencePipeline);

// TODO: Add IChatClient-compatible Anthropic client

return services;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Cellm.Services.Configuration;
namespace Cellm.Models;

internal interface IProviderConfiguration
{
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Diagnostics;
using Cellm.AddIn;
using Cellm.AddIn.Exceptions;
using Cellm.Models.Local.Utilities;
using Cellm.Models.OpenAiCompatible;
using Cellm.Services.Configuration;
using MediatR;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Cellm.Models.Anthropic;
using Cellm.Models.Local.Utilities;
using Cellm.Models.Providers.Anthropic;
using Cellm.Services.Configuration;
using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Cellm.Models.Providers.Llamafile;

internal static class ServiceCollectionExtensions
{
public static IServiceCollection AddLlamafileChatClient(this IServiceCollection services, IConfiguration configuration)
{
var resiliencePipelineConfigurator = new ResiliencePipelineConfigurator(configuration);

var anthropicConfiguration = configuration.GetRequiredSection(nameof(AnthropicConfiguration)).Get<AnthropicConfiguration>()
?? throw new NullReferenceException(nameof(AnthropicConfiguration));

services
.AddHttpClient<IRequestHandler<AnthropicRequest, AnthropicResponse>, AnthropicRequestHandler>(anthropicHttpClient =>
{
anthropicHttpClient.BaseAddress = anthropicConfiguration.BaseAddress;
anthropicHttpClient.DefaultRequestHeaders.Add("x-api-key", anthropicConfiguration.ApiKey);
anthropicHttpClient.DefaultRequestHeaders.Add("anthropic-version", anthropicConfiguration.Version);
anthropicHttpClient.Timeout = TimeSpan.FromHours(1);
})
.AddResilienceHandler($"{nameof(AnthropicRequestHandler)}", resiliencePipelineConfigurator.ConfigureResiliencePipeline);

services.TryAddSingleton<FileManager>();
services.TryAddSingleton<ProcessManager>();
services.TryAddSingleton<ServerManager>();

return services;
}
}
2 changes: 1 addition & 1 deletion src/Cellm.Models/Providers/Ollama/OllamaRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Cellm.Models.Providers;
using Cellm.AddIn;
using Cellm.Services.Configuration;

namespace Cellm.Models.Ollama;

Expand Down
Loading

0 comments on commit 3e8f14f

Please sign in to comment.