-
Notifications
You must be signed in to change notification settings - Fork 28
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
1 parent
78fb85d
commit 3d5da5d
Showing
70 changed files
with
653 additions
and
589 deletions.
There are no files selected for viewing
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 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
2 changes: 1 addition & 1 deletion
2
src/Cellm/Prompts/SystemMessages.cs → src/Cellm/AddIn/SystemMessages.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.Prompts; | ||
namespace Cellm.AddIn; | ||
|
||
internal static class SystemMessages | ||
{ | ||
|
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.
10 changes: 5 additions & 5 deletions
10
...s/ModelRequestBehavior/CachingBehavior.cs → src/Cellm/Models/Behaviors/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
2 changes: 1 addition & 1 deletion
2
...ls/ModelRequestBehavior/SentryBehavior.cs → src/Cellm/Models/Behaviors/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,24 @@ | ||
using Cellm.Models.Behaviors; | ||
using Cellm.Models.Providers; | ||
using MediatR; | ||
using Microsoft.Extensions.AI; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Cellm.Models.Tools; | ||
|
||
internal class ToolBehavior<TRequest, TResponse>(IOptions<ProviderConfiguration> providerConfiguration, IEnumerable<AIFunction> functions) | ||
: IPipelineBehavior<TRequest, TResponse> where TRequest : IModelRequest<TResponse> | ||
{ | ||
private readonly ProviderConfiguration _providerConfiguration = providerConfiguration.Value; | ||
private readonly List<AITool> _tools = new(functions); | ||
|
||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) | ||
{ | ||
if (_providerConfiguration.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 |
---|---|---|
@@ -1,73 +1,60 @@ | ||
using System.Text.Json; | ||
using Cellm.AddIn.Exceptions; | ||
using Cellm.Models.Anthropic; | ||
using Cellm.Models.Llamafile; | ||
using Cellm.Models.Ollama; | ||
using Cellm.Models.OpenAi; | ||
using Cellm.Models.OpenAiCompatible; | ||
using Cellm.Prompts; | ||
using Cellm.Services.Configuration; | ||
using Cellm.Models.Exceptions; | ||
using Cellm.Models.Prompts; | ||
using Cellm.Models.Providers; | ||
using Cellm.Models.Providers.Anthropic; | ||
using Cellm.Models.Providers.Llamafile; | ||
using Cellm.Models.Providers.Ollama; | ||
using Cellm.Models.Providers.OpenAi; | ||
using Cellm.Models.Providers.OpenAiCompatible; | ||
using MediatR; | ||
using Microsoft.Extensions.Options; | ||
using Polly.Timeout; | ||
|
||
namespace Cellm.Models; | ||
|
||
internal class Client(ISender sender, IOptions<CellmConfiguration> cellmConfiguration) | ||
public 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<Providers>(provider, true, out var parsedProvider)) | ||
if (!Enum.TryParse<Provider>(provider, true, out var parsedProvider)) | ||
{ | ||
throw new ArgumentException($"Unsupported provider: {provider}"); | ||
} | ||
|
||
IModelResponse response = parsedProvider switch | ||
{ | ||
Providers.Anthropic => await sender.Send(new AnthropicRequest(prompt, provider, baseAddress), cancellationToken), | ||
Providers.Llamafile => await sender.Send(new LlamafileRequest(prompt), cancellationToken), | ||
Providers.Ollama => await sender.Send(new OllamaRequest(prompt), cancellationToken), | ||
Providers.OpenAi => await sender.Send(new OpenAiRequest(prompt), cancellationToken), | ||
Providers.OpenAiCompatible => await sender.Send(new OpenAiCompatibleRequest(prompt, baseAddress), cancellationToken), | ||
Provider.Anthropic => await sender.Send(new AnthropicRequest(prompt, provider, baseAddress), cancellationToken), | ||
Provider.Llamafile => await sender.Send(new LlamafileRequest(prompt), cancellationToken), | ||
Provider.Ollama => await sender.Send(new OllamaRequest(prompt), cancellationToken), | ||
Provider.OpenAi => await sender.Send(new OpenAiRequest(prompt), cancellationToken), | ||
Provider.OpenAiCompatible => await sender.Send(new OpenAiCompatibleRequest(prompt, baseAddress), cancellationToken), | ||
_ => throw new InvalidOperationException($"Provider {parsedProvider} is defined but not implemented") | ||
}; | ||
|
||
return response.Prompt; | ||
} | ||
catch (HttpRequestException ex) | ||
{ | ||
throw new CellmException($"HTTP request failed: {ex.Message}", ex); | ||
} | ||
catch (JsonException ex) | ||
{ | ||
throw new CellmException($"JSON processing failed: {ex.Message}", ex); | ||
} | ||
catch (NotSupportedException ex) | ||
{ | ||
throw new CellmException($"Method not supported: {ex.Message}", ex); | ||
} | ||
catch (FileReaderException ex) | ||
{ | ||
throw new CellmException($"File could not be read: {ex.Message}", ex); | ||
throw new CellmModelException($"HTTP request failed: {ex.Message}", ex); | ||
} | ||
catch (NullReferenceException ex) | ||
{ | ||
throw new CellmException($"Null reference error: {ex.Message}", ex); | ||
throw new CellmModelException($"Null reference error: {ex.Message}", ex); | ||
} | ||
catch (TimeoutRejectedException ex) | ||
{ | ||
throw new CellmException($"Request timed out: {ex.Message}", ex); | ||
throw new CellmModelException($"Request timed out: {ex.Message}", ex); | ||
} | ||
catch (Exception ex) when (ex is not CellmException) | ||
catch (Exception ex) when (ex is not CellmModelException) | ||
{ | ||
// Handle any other unexpected exceptions | ||
throw new CellmException($"An unexpected error occurred: {ex.Message}", ex); | ||
throw new CellmModelException($"An unexpected error occurred: {ex.Message}", ex); | ||
} | ||
} | ||
} |
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.Exceptions; | ||
|
||
public class CellmModelException : Exception | ||
{ | ||
public CellmModelException(string message = "#CELLM_ERROR?") | ||
: base(message) { } | ||
|
||
public CellmModelException(string message, Exception inner) | ||
: base(message, inner) { } | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.