Implemented the embedding support

This commit is contained in:
Thorsten Sommer 2025-12-15 18:23:21 +01:00
parent 89781c9a3a
commit 664062a622
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 30 additions and 2 deletions

View File

@ -132,7 +132,7 @@ public static class LLMProvidersExtensions
LLMProviders.DEEP_SEEK => false,
LLMProviders.HUGGINGFACE => false,
LLMProviders.PERPLEXITY => false,
LLMProviders.OPEN_ROUTER => false,
LLMProviders.OPEN_ROUTER => true,
//
// Self-hosted providers are treated as a special case anyway.

View File

@ -122,7 +122,7 @@ public sealed class ProviderOpenRouter() : BaseProvider("https://openrouter.ai/a
/// <inheritdoc />
public override Task<IEnumerable<Model>> GetEmbeddingModels(string? apiKeyProvisional = null, CancellationToken token = default)
{
return Task.FromResult(Enumerable.Empty<Model>());
return this.LoadEmbeddingModels(token, apiKeyProvisional);
}
#endregion
@ -164,4 +164,32 @@ public sealed class ProviderOpenRouter() : BaseProvider("https://openrouter.ai/a
!n.Id.Contains("midjourney", StringComparison.OrdinalIgnoreCase))
.Select(n => new Model(n.Id, n.Name));
}
private async Task<IEnumerable<Model>> LoadEmbeddingModels(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, "embeddings/models");
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<OpenRouterModelsResponse>(token);
// Convert all embedding models to Model
return modelResponse.Data.Select(n => new Model(n.Id, n.Name));
}
}