-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kasper Marstal
committed
Dec 6, 2024
1 parent
2c0eebc
commit 3e8f14f
Showing
36 changed files
with
271 additions
and
138 deletions.
There are no files selected for viewing
9 changes: 4 additions & 5 deletions
9
...s/ModelRequestBehavior/CachingBehavior.cs → ...m.Models/Behaviors/Cache/CacheBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
2 changes: 1 addition & 1 deletion
2
...ls/ModelRequestBehavior/SentryBehavior.cs → ...Models/Behaviors/Sentry/SentryBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Cellm.Models/Providers/Anthropic/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
2 changes: 1 addition & 1 deletion
2
...s/Configuration/IProviderConfiguration.cs → ...odels/Providers/IProviderConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace Cellm.Services.Configuration; | ||
namespace Cellm.Models; | ||
|
||
internal interface IProviderConfiguration | ||
{ | ||
|
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
src/Cellm.Models/Providers/Llamafile/LlamafileRequestHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Cellm.Models/Providers/Llamafile/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.