| | |
| | | private readonly ICredentialStore _credentialStore; |
| | | private readonly ILlmProviderAdapterFactory _adapterFactory; |
| | | private readonly IDiagnosticsService _diagnostics; |
| | | private readonly IAiChatClient? _aiChatClient; |
| | | private readonly ConcurrentDictionary<string, CancellationTokenSource> _activeRequests = new(StringComparer.OrdinalIgnoreCase); |
| | | |
| | | public LlmGatewayService( |
| | | ILlmModelRegistry registry, |
| | | ICredentialStore credentialStore, |
| | | ILlmProviderAdapterFactory adapterFactory, |
| | | IDiagnosticsService diagnostics) |
| | | IDiagnosticsService diagnostics, |
| | | IAiChatClient? aiChatClient = null) |
| | | { |
| | | _registry = registry; |
| | | _credentialStore = credentialStore; |
| | | _adapterFactory = adapterFactory; |
| | | _diagnostics = diagnostics; |
| | | _aiChatClient = aiChatClient; |
| | | } |
| | | |
| | | public async Task<IReadOnlyList<LlmModelSummary>> GetModelsAsync(CancellationToken cancellationToken = default) |
| | |
| | | .ToList(); |
| | | } |
| | | |
| | | public async Task<IReadOnlyList<LlmModelSummary>> GetModelsAsync( |
| | | LlmPluginRuntimeConfig runtimeConfig, |
| | | CancellationToken cancellationToken = default) |
| | | { |
| | | if (runtimeConfig.IsAiChatMode) |
| | | { |
| | | return _aiChatClient is null |
| | | ? [] |
| | | : await _aiChatClient.GetModelsAsync(runtimeConfig, cancellationToken).ConfigureAwait(false); |
| | | } |
| | | |
| | | var configuredModels = runtimeConfig.Models |
| | | .Where(IsUsableRuntimeModel) |
| | | .Select(model => new LlmModelSummary |
| | | { |
| | | Id = model.Id, |
| | | Label = string.Equals(model.Id, runtimeConfig.DefaultModel, StringComparison.OrdinalIgnoreCase) |
| | | ? $"{model.Label} *" |
| | | : model.Label, |
| | | Provider = model.Provider, |
| | | SupportsStreaming = model.SupportsStreaming, |
| | | Enabled = model.Enabled |
| | | }) |
| | | .ToList(); |
| | | if (configuredModels.Count > 0) |
| | | { |
| | | return configuredModels; |
| | | } |
| | | |
| | | return await GetModelsAsync(cancellationToken).ConfigureAwait(false); |
| | | } |
| | | |
| | | public Task<IReadOnlyList<AiChatProjectSummary>> GetProjectsAsync( |
| | | LlmPluginRuntimeConfig runtimeConfig, |
| | | CancellationToken cancellationToken = default) |
| | | => _aiChatClient is null |
| | | ? Task.FromResult<IReadOnlyList<AiChatProjectSummary>>([]) |
| | | : _aiChatClient.GetProjectsAsync(runtimeConfig, cancellationToken); |
| | | |
| | | public Task<AiChatMemoryCapabilities> GetMemoryCapabilitiesAsync( |
| | | LlmPluginRuntimeConfig runtimeConfig, |
| | | CancellationToken cancellationToken = default) |
| | | => _aiChatClient is null |
| | | ? Task.FromResult(new AiChatMemoryCapabilities()) |
| | | : _aiChatClient.GetMemoryCapabilitiesAsync(runtimeConfig, cancellationToken); |
| | | |
| | | public Task<AiChatJsonResult> GetMemoriesAsync( |
| | | LlmPluginRuntimeConfig runtimeConfig, |
| | | AiChatGetMemoriesRequest request, |
| | | CancellationToken cancellationToken = default) |
| | | => _aiChatClient is null |
| | | ? Task.FromResult(new AiChatJsonResult()) |
| | | : _aiChatClient.GetMemoriesAsync(runtimeConfig, request, cancellationToken); |
| | | |
| | | public Task<AiChatJsonResult> CreateMemoryAsync( |
| | | LlmPluginRuntimeConfig runtimeConfig, |
| | | AiChatCreateMemoryRequest request, |
| | | CancellationToken cancellationToken = default) |
| | | => _aiChatClient is null |
| | | ? Task.FromResult(new AiChatJsonResult()) |
| | | : _aiChatClient.CreateMemoryAsync(runtimeConfig, request, cancellationToken); |
| | | |
| | | public Task DeleteMemoryAsync( |
| | | LlmPluginRuntimeConfig runtimeConfig, |
| | | string memoryId, |
| | | CancellationToken cancellationToken = default) |
| | | => _aiChatClient is null |
| | | ? Task.CompletedTask |
| | | : _aiChatClient.DeleteMemoryAsync(runtimeConfig, memoryId, cancellationToken); |
| | | |
| | | public Task<AiChatJsonResult> RecallMemoriesAsync( |
| | | LlmPluginRuntimeConfig runtimeConfig, |
| | | AiChatRecallMemoriesRequest request, |
| | | CancellationToken cancellationToken = default) |
| | | => _aiChatClient is null |
| | | ? Task.FromResult(new AiChatJsonResult()) |
| | | : _aiChatClient.RecallMemoriesAsync(runtimeConfig, request, cancellationToken); |
| | | |
| | | public async IAsyncEnumerable<LlmStreamEvent> StreamChatAsync( |
| | | LlmChatRequest request, |
| | | [EnumeratorCancellation] CancellationToken cancellationToken = default) |
| | |
| | | yield break; |
| | | } |
| | | |
| | | var model = await _registry.GetEnabledModelAsync(request.ModelId, cancellationToken).ConfigureAwait(false); |
| | | if (string.Equals(request.Mode, LlmConnectionModes.AiChatApi, StringComparison.OrdinalIgnoreCase)) |
| | | { |
| | | if (_aiChatClient is null) |
| | | { |
| | | yield return new LlmErrorEvent |
| | | { |
| | | RequestId = request.RequestId, |
| | | Code = LlmErrorCodes.AiChatApiFailed, |
| | | Message = "AiChat client is not available." |
| | | }; |
| | | yield break; |
| | | } |
| | | |
| | | await foreach (var streamEvent in _aiChatClient.StreamChatAsync(request, request.RuntimeConfig, cancellationToken).ConfigureAwait(false)) |
| | | { |
| | | yield return streamEvent; |
| | | } |
| | | |
| | | yield break; |
| | | } |
| | | |
| | | var model = request.RuntimeConfig.Models.FirstOrDefault(model => |
| | | IsUsableRuntimeModel(model) && |
| | | string.Equals(model.Id, request.ModelId, StringComparison.OrdinalIgnoreCase)); |
| | | model ??= await _registry.GetEnabledModelAsync(request.ModelId, cancellationToken).ConfigureAwait(false); |
| | | if (model is null) |
| | | { |
| | | yield return new LlmErrorEvent |
| | |
| | | return true; |
| | | } |
| | | |
| | | private static bool IsUsableRuntimeModel(LlmModelConfig model) |
| | | => model.Enabled && |
| | | !string.IsNullOrWhiteSpace(model.Id) && |
| | | !string.IsNullOrWhiteSpace(model.Label) && |
| | | !string.IsNullOrWhiteSpace(model.Provider) && |
| | | !string.IsNullOrWhiteSpace(model.Endpoint) && |
| | | !string.IsNullOrWhiteSpace(model.ApiModel); |
| | | |
| | | private void RecordFailure(LlmChatRequest request, LlmModelConfig model, string code, string exception) |
| | | { |
| | | var host = Uri.TryCreate(model.Endpoint, UriKind.Absolute, out var endpoint) |