mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-08-20 07:32:10 +00:00
Add LiteLLM as a first-class provider. LiteLLM is a self-hosted AI gateway that exposes a single OpenAI-compatible endpoint in front of 100+ models across providers (OpenAI, Anthropic, Google, Azure, AWS Bedrock, and more). The provider mirrors the existing OpenAI-compatible providers: it uses the shared StreamOpenAICompatibleChatCompletion helper for streaming chat and LoadModelsResponse against /v1/models for automatic model discovery. The user supplies the base URL of their proxy; it is normalized and the OpenAI-compatible /v1/ path is appended. It uses the self-hosted trust policy and confidence handling, since the gateway owner decides which downstream providers requests are routed to. Wired through the standard plumbing: the LLMProviders enum, LLMProvidersExtensions (name, confidence, factory, hostname/API-key requirements), ProviderExtensions (capabilities + reasoning), and SettingsManager (per-scheme confidence levels). Hostname and API key are both required. Added a user-facing changelog entry and updated the configuration-plugin docs to include the new LITE_LLM confidence key.
119 lines
5.1 KiB
C#
119 lines
5.1 KiB
C#
using System.Runtime.CompilerServices;
|
|
|
|
using AIStudio.Chat;
|
|
using AIStudio.Provider.OpenAI;
|
|
using AIStudio.Settings;
|
|
|
|
namespace AIStudio.Provider.LiteLLM;
|
|
|
|
public sealed class ProviderLiteLLM(string hostname) : BaseProvider(LLMProviders.LITE_LLM, BuildBaseUri(hostname), ExternalHttpTrustPolicy.ALLOW_CUSTOM_ROOTS_WHEN_HOST_WHITELISTED, LOGGER)
|
|
{
|
|
private static readonly ILogger<ProviderLiteLLM> LOGGER = Program.LOGGER_FACTORY.CreateLogger<ProviderLiteLLM>();
|
|
|
|
#region Implementation of IProvider
|
|
|
|
/// <inheritdoc />
|
|
public override string Id => LLMProviders.LITE_LLM.ToSecretId();
|
|
|
|
/// <inheritdoc />
|
|
public override string InstanceName { get; set; } = "LiteLLM";
|
|
|
|
/// <inheritdoc />
|
|
public override bool HasModelLoadingCapability => true;
|
|
|
|
/// <inheritdoc />
|
|
public override async IAsyncEnumerable<ContentStreamChunk> StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default)
|
|
{
|
|
await foreach (var content in this.StreamOpenAICompatibleChatCompletion<ChatCompletionAPIRequest, ChatCompletionDeltaStreamLine, NoChatCompletionAnnotationStreamLine>(
|
|
"LiteLLM",
|
|
chatModel,
|
|
chatThread,
|
|
settingsManager,
|
|
async (systemPrompt, apiParameters) =>
|
|
{
|
|
// Build the list of messages:
|
|
var messages = await chatThread.Blocks.BuildMessagesUsingDirectImageUrlAsync(this.Provider, chatModel);
|
|
|
|
return 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
|
|
};
|
|
},
|
|
token: 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 Task<TranscriptionResult> TranscribeAudioAsync(Model transcriptionModel, string audioFilePath, SettingsManager settingsManager, CancellationToken token = default)
|
|
{
|
|
return Task.FromResult(TranscriptionResult.Failure());
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override Task<IReadOnlyList<IReadOnlyList<float>>> EmbedTextAsync(Model embeddingModel, SettingsManager settingsManager, CancellationToken token = default, params List<string> texts)
|
|
{
|
|
return Task.FromResult<IReadOnlyList<IReadOnlyList<float>>>([]);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override Task<ModelLoadResult> GetTextModels(string? apiKeyProvisional = null, CancellationToken token = default)
|
|
{
|
|
return this.LoadModels(SecretStoreType.LLM_PROVIDER, token, apiKeyProvisional);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override Task<ModelLoadResult> GetImageModels(string? apiKeyProvisional = null, CancellationToken token = default)
|
|
{
|
|
return Task.FromResult(ModelLoadResult.FromModels([]));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override Task<ModelLoadResult> GetEmbeddingModels(string? apiKeyProvisional = null, CancellationToken token = default)
|
|
{
|
|
return Task.FromResult(ModelLoadResult.FromModels([]));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override Task<ModelLoadResult> GetTranscriptionModels(string? apiKeyProvisional = null, CancellationToken token = default)
|
|
{
|
|
return Task.FromResult(ModelLoadResult.FromModels([]));
|
|
}
|
|
|
|
#endregion
|
|
|
|
private static Uri BuildBaseUri(string hostname)
|
|
{
|
|
// LiteLLM exposes an OpenAI-compatible API under the "/v1/" path. Users configure the
|
|
// base URL of their LiteLLM proxy (e.g. http://localhost:4000); we normalize any trailing
|
|
// slash and append the OpenAI-compatible path.
|
|
var normalizedHostname = (hostname ?? string.Empty).TrimEnd('/');
|
|
return new Uri($"{normalizedHostname}/v1/");
|
|
}
|
|
|
|
private Task<ModelLoadResult> LoadModels(SecretStoreType storeType, CancellationToken token, string? apiKeyProvisional = null)
|
|
{
|
|
return this.LoadModelsResponse<ModelsResponse>(
|
|
storeType,
|
|
"models",
|
|
modelResponse => modelResponse.Data,
|
|
token,
|
|
apiKeyProvisional);
|
|
}
|
|
}
|