From e21e30ce8c52cb1dfb234149c01a11d1285e6719 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Mon, 24 Feb 2025 20:52:32 +0100 Subject: [PATCH] Implemented the new models API for the Anthropic provider (#297) --- .../Provider/Anthropic/ProviderAnthropic.cs | 56 ++++++++++++++----- .../wwwroot/changelog/v0.9.30.md | 2 + 2 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 app/MindWork AI Studio/wwwroot/changelog/v0.9.30.md diff --git a/app/MindWork AI Studio/Provider/Anthropic/ProviderAnthropic.cs b/app/MindWork AI Studio/Provider/Anthropic/ProviderAnthropic.cs index 6f3ef54..a900095 100644 --- a/app/MindWork AI Studio/Provider/Anthropic/ProviderAnthropic.cs +++ b/app/MindWork AI Studio/Provider/Anthropic/ProviderAnthropic.cs @@ -1,3 +1,4 @@ +using System.Net.Http.Headers; using System.Runtime.CompilerServices; using System.Text; using System.Text.Json; @@ -87,21 +88,15 @@ public sealed class ProviderAnthropic(ILogger logger) : BaseProvider("https://ap /// public override Task> GetTextModels(string? apiKeyProvisional = null, CancellationToken token = default) { - return Task.FromResult(new[] + var additionalModels = new[] { - new Model("claude-3-5-sonnet-latest", "Claude 3.5 Sonnet (latest)"), - new Model("claude-3-5-sonnet-20240620", "Claude 3.5 Sonnet (20. June 2024)"), - new Model("claude-3-5-sonnet-20241022", "Claude 3.5 Sonnet (22. October 2024)"), - - new Model("claude-3-5-haiku-latest", "Claude 3.5 Haiku (latest)"), - new Model("claude-3-5-heiku-20241022", "Claude 3.5 Haiku (22. October 2024)"), - - new Model("claude-3-opus-20240229", "Claude 3.0 Opus (29. February 2024)"), - new Model("claude-3-opus-latest", "Claude 3.0 Opus (latest)"), - - new Model("claude-3-sonnet-20240229", "Claude 3.0 Sonnet (29. February 2024)"), - new Model("claude-3-haiku-20240307", "Claude 3.0 Haiku (7. March 2024)"), - }.AsEnumerable()); + new Model("claude-3-7-sonnet-latest", "Claude 3.7 Sonnet (Latest)"), + new Model("claude-3-5-sonnet-latest", "Claude 3.5 Sonnet (Latest)"), + new Model("claude-3-5-haiku-latest", "Claude 3.5 Haiku (Latest)"), + new Model("claude-3-opus-latest", "Claude 3 Opus (Latest)"), + }; + + return this.LoadModels(token, apiKeyProvisional).ContinueWith(t => t.Result.Concat(additionalModels).OrderBy(x => x.Id).AsEnumerable(), token); } /// @@ -117,4 +112,37 @@ public sealed class ProviderAnthropic(ILogger logger) : BaseProvider("https://ap } #endregion + + private async Task> LoadModels(CancellationToken token, string? apiKeyProvisional = null) + { + var secretKey = apiKeyProvisional switch + { + not null => apiKeyProvisional, + _ => await RUST_SERVICE.GetAPIKey(this) switch + { + { Success: true } result => await result.Secret.Decrypt(ENCRYPTION), + _ => null, + } + }; + + if (secretKey is null) + return []; + + using var request = new HttpRequestMessage(HttpMethod.Get, "models?limit=100"); + + // Set the authorization header: + request.Headers.Add("x-api-key", secretKey); + + // Set the Anthropic version: + request.Headers.Add("anthropic-version", "2023-06-01"); + + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", secretKey); + + using var response = await this.httpClient.SendAsync(request, token); + if(!response.IsSuccessStatusCode) + return []; + + var modelResponse = await response.Content.ReadFromJsonAsync(JSON_SERIALIZER_OPTIONS, token); + return modelResponse.Data; + } } \ No newline at end of file diff --git a/app/MindWork AI Studio/wwwroot/changelog/v0.9.30.md b/app/MindWork AI Studio/wwwroot/changelog/v0.9.30.md new file mode 100644 index 0000000..1760352 --- /dev/null +++ b/app/MindWork AI Studio/wwwroot/changelog/v0.9.30.md @@ -0,0 +1,2 @@ +# v0.9.30, build 205 (2025-02-xx xx:xx UTC) +- Implemented the new models API for the Anthropic provider. This allows you to use the latest models, like Claude 3.7 Sonnet, which were released in the last hours. \ No newline at end of file