Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Cellm/Models/Anthropic/AnthropicClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public async Task<Prompt> Send(Prompt prompt, string? provider, string? model)
unit: MeasurementUnit.Custom("token"),
tags: new Dictionary<string, string> {
{ nameof(provider), provider?.ToLower() ?? _cellmConfiguration.DefaultModelProvider },
{ nameof(model), model?.ToLower() ?? _cellmConfiguration.DefaultModelProvider },
{ nameof(model), model?.ToLower() ?? _anthropicConfiguration.DefaultModel },
{ nameof(_httpClient.BaseAddress), _httpClient.BaseAddress?.ToString() ?? string.Empty }
}
);
Expand All @@ -95,7 +95,7 @@ public async Task<Prompt> Send(Prompt prompt, string? provider, string? model)
unit: MeasurementUnit.Custom("token"),
tags: new Dictionary<string, string> {
{ nameof(provider), provider?.ToLower() ?? _cellmConfiguration.DefaultModelProvider },
{ nameof(model), model?.ToLower() ?? _cellmConfiguration.DefaultModelProvider },
{ nameof(model), model?.ToLower() ?? _anthropicConfiguration.DefaultModel },
{ nameof(_httpClient.BaseAddress), _httpClient.BaseAddress?.ToString() ?? string.Empty }
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/Cellm/Models/ClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cellm.Models.Anthropic;
using Cellm.Models.Google;
using Cellm.Models.GoogleAi;
using Cellm.Models.Llamafile;
using Cellm.Models.OpenAi;
using Cellm.Services;
Expand All @@ -14,7 +14,7 @@ public IClient GetClient(string modelProvider)
return modelProvider.ToLower() switch
{
"anthropic" => ServiceLocator.Get<AnthropicClient>(),
"google" => ServiceLocator.Get<GoogleClient>(),
"googleai" => ServiceLocator.Get<GoogleAiClient>(),
"openai" => ServiceLocator.Get<OpenAiClient>(),
"llamafile" => ServiceLocator.Get<LlamafileClient>(),
_ => throw new ArgumentException($"Unsupported client type: {modelProvider}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
using Cellm.AddIn.Prompts;
using Microsoft.Extensions.Options;

namespace Cellm.Models.Google;
namespace Cellm.Models.GoogleAi;

internal class GoogleClient : IClient
internal class GoogleAiClient : IClient
{
private readonly GoogleConfiguration _googleConfiguration;
private readonly GoogleAiConfiguration _googleAiConfiguration;
private readonly CellmConfiguration _cellmConfiguration;
private readonly HttpClient _httpClient;
private readonly ICache _cache;
private readonly ISerde _serde;

public GoogleClient(
IOptions<GoogleConfiguration> googleConfiguration,
public GoogleAiClient(
IOptions<GoogleAiConfiguration> googleAiConfiguration,
IOptions<CellmConfiguration> cellmConfiguration,
HttpClient httpClient,
ICache cache,
ISerde serde)
{
_googleConfiguration = googleConfiguration.Value;
_googleAiConfiguration = googleAiConfiguration.Value;
_cellmConfiguration = cellmConfiguration.Value;
_httpClient = httpClient;
_cache = cache;
Expand All @@ -31,7 +31,7 @@ public GoogleClient(

public async Task<Prompt> Send(Prompt prompt, string? provider, string? model)
{
var transaction = SentrySdk.StartTransaction(typeof(GoogleClient).Name, nameof(Send));
var transaction = SentrySdk.StartTransaction(typeof(GoogleAiClient).Name, nameof(Send));
SentrySdk.ConfigureScope(scope => scope.Transaction = transaction);

var requestBody = new RequestBody
Expand All @@ -57,7 +57,7 @@ public async Task<Prompt> Send(Prompt prompt, string? provider, string? model)
var json = _serde.Serialize(requestBody);
var jsonAsString = new StringContent(json, Encoding.UTF8, "application/json");

var response = await _httpClient.PostAsync($"/v1beta/models/{model ?? _googleConfiguration.DefaultModel}:generateContent?key={_googleConfiguration.ApiKey}", jsonAsString);
var response = await _httpClient.PostAsync($"/v1beta/models/{model ?? _googleAiConfiguration.DefaultModel}:generateContent?key={_googleAiConfiguration.ApiKey}", jsonAsString);
var responseBodyAsString = await response.Content.ReadAsStringAsync();

if (!response.IsSuccessStatusCode)
Expand Down Expand Up @@ -87,7 +87,7 @@ public async Task<Prompt> Send(Prompt prompt, string? provider, string? model)
unit: MeasurementUnit.Custom("token"),
tags: new Dictionary<string, string> {
{ nameof(provider), provider?.ToLower() ?? _cellmConfiguration.DefaultModelProvider },
{ nameof(model), model?.ToLower() ?? _cellmConfiguration.DefaultModelProvider },
{ nameof(model), model?.ToLower() ?? _googleAiConfiguration.DefaultModel },
{ nameof(_httpClient.BaseAddress), _httpClient.BaseAddress?.ToString() ?? string.Empty }
}
);
Expand All @@ -101,7 +101,7 @@ public async Task<Prompt> Send(Prompt prompt, string? provider, string? model)
unit: MeasurementUnit.Custom("token"),
tags: new Dictionary<string, string> {
{ nameof(provider), provider?.ToLower() ?? _cellmConfiguration.DefaultModelProvider },
{ nameof(model), model?.ToLower() ?? _cellmConfiguration.DefaultModelProvider },
{ nameof(model), model?.ToLower() ?? _googleAiConfiguration.DefaultModel },
{ nameof(_httpClient.BaseAddress), _httpClient.BaseAddress?.ToString() ?? string.Empty }
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace Cellm.Models.Google;
namespace Cellm.Models.GoogleAi;

internal class GoogleConfiguration
internal class GoogleAiConfiguration
{
public Uri BaseAddress { get; init; }

public string DefaultModel { get; init; }

public string ApiKey { get; init; }

public GoogleConfiguration()
public GoogleAiConfiguration()
{
BaseAddress = default!;
DefaultModel = default!;
Expand Down
4 changes: 2 additions & 2 deletions src/Cellm/Models/OpenAi/OpenAiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public async Task<Prompt> Send(Prompt prompt, string? provider, string? model)
unit: MeasurementUnit.Custom("token"),
tags: new Dictionary<string, string> {
{ nameof(provider), provider?.ToLower() ?? _cellmConfiguration.DefaultModelProvider },
{ nameof(model), model?.ToLower() ?? _cellmConfiguration.DefaultModelProvider },
{ nameof(model), model?.ToLower() ?? _openAiConfiguration.DefaultModel },
{ nameof(_httpClient.BaseAddress), _httpClient.BaseAddress?.ToString() ?? string.Empty }
}
);
Expand All @@ -102,7 +102,7 @@ public async Task<Prompt> Send(Prompt prompt, string? provider, string? model)
tags: new Dictionary<string, string>
{
{ nameof(provider), provider?.ToLower() ?? _cellmConfiguration.DefaultModelProvider },
{ nameof(model), model?.ToLower() ?? _cellmConfiguration.DefaultModelProvider },
{ nameof(model), model?.ToLower() ?? _openAiConfiguration.DefaultModel },
{ nameof(_httpClient.BaseAddress), _httpClient.BaseAddress?.ToString() ?? string.Empty }
}
);
Expand Down
18 changes: 9 additions & 9 deletions src/Cellm/Services/ServiceLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Cellm.AddIn.Exceptions;
using Cellm.Models;
using Cellm.Models.Anthropic;
using Cellm.Models.Google;
using Cellm.Models.GoogleAi;
using Cellm.Models.Llamafile;
using Cellm.Models.OpenAi;
using Cellm.Services.Configuration;
Expand Down Expand Up @@ -41,7 +41,7 @@ private static IServiceCollection ConfigureServices(IServiceCollection services)
services
.Configure<CellmConfiguration>(configuration.GetRequiredSection(nameof(CellmConfiguration)))
.Configure<AnthropicConfiguration>(configuration.GetRequiredSection(nameof(AnthropicConfiguration)))
.Configure<GoogleConfiguration>(configuration.GetRequiredSection(nameof(GoogleConfiguration)))
.Configure<GoogleAiConfiguration>(configuration.GetRequiredSection(nameof(GoogleAiConfiguration)))
.Configure<OpenAiConfiguration>(configuration.GetRequiredSection(nameof(OpenAiConfiguration)))
.Configure<LlamafileConfiguration>(configuration.GetRequiredSection(nameof(LlamafileConfiguration)))
.Configure<RateLimiterConfiguration>(configuration.GetRequiredSection(nameof(RateLimiterConfiguration)))
Expand Down Expand Up @@ -109,15 +109,15 @@ private static IServiceCollection ConfigureServices(IServiceCollection services)
anthropicHttpClient.BaseAddress = anthropicConfiguration.BaseAddress;
anthropicHttpClient.DefaultRequestHeaders.Add("x-api-key", anthropicConfiguration.ApiKey);
anthropicHttpClient.DefaultRequestHeaders.Add("anthropic-version", anthropicConfiguration.Version);
}).AddResilienceHandler("AnthropicResiliencePipeline", resiliencePipelineConfigurator.ConfigureResiliencePipeline);
}).AddResilienceHandler($"{nameof(AnthropicClient)}ResiliencePipeline", resiliencePipelineConfigurator.ConfigureResiliencePipeline);

var googleConfiguration = configuration.GetRequiredSection(nameof(GoogleConfiguration)).Get<GoogleConfiguration>()
?? throw new NullReferenceException(nameof(GoogleConfiguration));
var googleAiConfiguration = configuration.GetRequiredSection(nameof(GoogleAiConfiguration)).Get<GoogleAiConfiguration>()
?? throw new NullReferenceException(nameof(GoogleAiConfiguration));

services.AddHttpClient<GoogleClient>(googleHttpClient =>
services.AddHttpClient<GoogleAiClient>(googleHttpClient =>
{
googleHttpClient.BaseAddress = googleConfiguration.BaseAddress;
}).AddResilienceHandler("GoogleResiliencePipeline", resiliencePipelineConfigurator.ConfigureResiliencePipeline);
googleHttpClient.BaseAddress = googleAiConfiguration.BaseAddress;
}).AddResilienceHandler($"{nameof(GoogleAiClient)}ResiliencePipeline", resiliencePipelineConfigurator.ConfigureResiliencePipeline);

var openAiConfiguration = configuration.GetRequiredSection(nameof(OpenAiConfiguration)).Get<OpenAiConfiguration>()
?? throw new NullReferenceException(nameof(OpenAiConfiguration));
Expand All @@ -126,7 +126,7 @@ private static IServiceCollection ConfigureServices(IServiceCollection services)
{
openAiHttpClient.BaseAddress = openAiConfiguration.BaseAddress;
openAiHttpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {openAiConfiguration.ApiKey}");
}).AddResilienceHandler("OpenAiResiliencePipeline", resiliencePipelineConfigurator.ConfigureResiliencePipeline);
}).AddResilienceHandler($"{nameof(OpenAiClient)}ResiliencePipeline", resiliencePipelineConfigurator.ConfigureResiliencePipeline);

services.AddSingleton<LlamafileClient>();

Expand Down
8 changes: 0 additions & 8 deletions src/Cellm/appsettings.Local.Google.json

This file was deleted.

8 changes: 8 additions & 0 deletions src/Cellm/appsettings.Local.GoogleAi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"GoogleAiConfiguration": {
"ApiKey": "YOUR_GOOGLEAI_APIKEY"
},
"CellmConfiguration": {
"DefaultModelProvider": "GoogleAI"
}
}
8 changes: 4 additions & 4 deletions src/Cellm/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"DefaultModel": "claude-3-5-sonnet-20240620",
"Version": "2023-06-01"
},
"GoogleAiConfiguration": {
"BaseAddress": "https://generativelanguage.googleapis.com",
"DefaultModel": "gemini-1.5-flash-latest"
},
"OpenAiConfiguration": {
"BaseAddress": "https://api.openai.com",
"DefaultModel": "gpt-4o-mini"
},
"GoogleConfiguration": {
"BaseAddress": "https://generativelanguage.googleapis.com",
"DefaultModel": "gemini-1.5-flash-latest"
},
"CellmConfiguration": {
"DefaultModelProvider": "Anthropic",
"DefaultTemperature": 0,
Expand Down