mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-02-12 11:41:38 +00:00
155 lines
6.5 KiB
C#
155 lines
6.5 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
using AIStudio.Chat;
|
|
using AIStudio.Provider.OpenAI;
|
|
using AIStudio.Settings;
|
|
|
|
namespace AIStudio.Provider.GWDG;
|
|
|
|
public sealed class ProviderGWDG() : BaseProvider(LLMProviders.GWDG, "https://chat-ai.academiccloud.de/v1/", LOGGER)
|
|
{
|
|
private static readonly ILogger<ProviderGWDG> LOGGER = Program.LOGGER_FACTORY.CreateLogger<ProviderGWDG>();
|
|
|
|
#region Implementation of IProvider
|
|
|
|
/// <inheritdoc />
|
|
public override string Id => LLMProviders.GWDG.ToName();
|
|
|
|
/// <inheritdoc />
|
|
public override string InstanceName { get; set; } = "GWDG SAIA";
|
|
|
|
/// <inheritdoc />
|
|
public override async IAsyncEnumerable<ContentStreamChunk> StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default)
|
|
{
|
|
// Get the API key:
|
|
var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.LLM_PROVIDER);
|
|
if(!requestedSecret.Success)
|
|
yield break;
|
|
|
|
// Prepare the system prompt:
|
|
var systemPrompt = new TextMessage
|
|
{
|
|
Role = "system",
|
|
Content = chatThread.PrepareSystemPrompt(settingsManager),
|
|
};
|
|
|
|
// Parse the API parameters:
|
|
var apiParameters = this.ParseAdditionalApiParameters();
|
|
|
|
// Build the list of messages:
|
|
var messages = await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.Provider, chatModel);
|
|
|
|
// Prepare the GWDG HTTP chat request:
|
|
var gwdgChatRequest = JsonSerializer.Serialize(new ChatCompletionAPIRequest
|
|
{
|
|
Model = chatModel.Id,
|
|
|
|
// Build the messages:
|
|
// - First of all the system prompt
|
|
// - Then none-empty user and AI messages
|
|
Messages = [systemPrompt, ..messages],
|
|
|
|
Stream = true,
|
|
AdditionalApiParameters = apiParameters
|
|
}, JSON_SERIALIZER_OPTIONS);
|
|
|
|
async Task<HttpRequestMessage> RequestBuilder()
|
|
{
|
|
// Build the HTTP post request:
|
|
var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions");
|
|
|
|
// Set the authorization header:
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await requestedSecret.Secret.Decrypt(ENCRYPTION));
|
|
|
|
// Set the content:
|
|
request.Content = new StringContent(gwdgChatRequest, Encoding.UTF8, "application/json");
|
|
return request;
|
|
}
|
|
|
|
await foreach (var content in this.StreamChatCompletionInternal<ChatCompletionDeltaStreamLine, ChatCompletionAnnotationStreamLine>("GWDG", RequestBuilder, token))
|
|
yield return content;
|
|
}
|
|
|
|
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
|
/// <inheritdoc />
|
|
public override async IAsyncEnumerable<ImageURL> StreamImageCompletion(Model imageModel, string promptPositive, string promptNegative = FilterOperator.String.Empty, ImageURL referenceImageURL = default, [EnumeratorCancellation] CancellationToken token = default)
|
|
{
|
|
yield break;
|
|
}
|
|
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
|
|
|
|
/// <inheritdoc />
|
|
public override async Task<string> TranscribeAudioAsync(Model transcriptionModel, string audioFilePath, SettingsManager settingsManager, CancellationToken token = default)
|
|
{
|
|
var requestedSecret = await RUST_SERVICE.GetAPIKey(this, SecretStoreType.TRANSCRIPTION_PROVIDER);
|
|
return await this.PerformStandardTranscriptionRequest(requestedSecret, transcriptionModel, audioFilePath, token: token);
|
|
}
|
|
|
|
/// <inhertidoc />
|
|
public override Task<IReadOnlyList<IReadOnlyList<float>>> EmbedTextAsync(Provider.Model embeddingModel, SettingsManager settingsManager, CancellationToken token = default, params List<string> texts)
|
|
{
|
|
return Task.FromResult<IReadOnlyList<IReadOnlyList<float>>>(Array.Empty<IReadOnlyList<float>>());
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override async Task<IEnumerable<Model>> GetTextModels(string? apiKeyProvisional = null, CancellationToken token = default)
|
|
{
|
|
var models = await this.LoadModels(SecretStoreType.LLM_PROVIDER, token, apiKeyProvisional);
|
|
return models.Where(model => !model.Id.StartsWith("e5-mistral-7b-instruct", StringComparison.InvariantCultureIgnoreCase));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override Task<IEnumerable<Model>> GetImageModels(string? apiKeyProvisional = null, CancellationToken token = default)
|
|
{
|
|
return Task.FromResult(Enumerable.Empty<Model>());
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override async Task<IEnumerable<Model>> GetEmbeddingModels(string? apiKeyProvisional = null, CancellationToken token = default)
|
|
{
|
|
var models = await this.LoadModels(SecretStoreType.EMBEDDING_PROVIDER, token, apiKeyProvisional);
|
|
return models.Where(model => model.Id.StartsWith("e5-", StringComparison.InvariantCultureIgnoreCase));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override Task<IEnumerable<Model>> GetTranscriptionModels(string? apiKeyProvisional = null, CancellationToken token = default)
|
|
{
|
|
// Source: https://docs.hpc.gwdg.de/services/saia/index.html#voice-to-text
|
|
return Task.FromResult<IEnumerable<Model>>(
|
|
new List<Model>
|
|
{
|
|
new("whisper-large-v2", "Whisper v2 Large"),
|
|
});
|
|
}
|
|
|
|
#endregion
|
|
|
|
private async Task<IEnumerable<Model>> LoadModels(SecretStoreType storeType, CancellationToken token, string? apiKeyProvisional = null)
|
|
{
|
|
var secretKey = apiKeyProvisional switch
|
|
{
|
|
not null => apiKeyProvisional,
|
|
_ => await RUST_SERVICE.GetAPIKey(this, storeType) switch
|
|
{
|
|
{ Success: true } result => await result.Secret.Decrypt(ENCRYPTION),
|
|
_ => null,
|
|
}
|
|
};
|
|
|
|
if (secretKey is null)
|
|
return [];
|
|
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, "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<ModelsResponse>(token);
|
|
return modelResponse.Data;
|
|
}
|
|
} |