namespace Moli.Desktop.Llm;
|
|
public static class LlmOpenAiUrls
|
{
|
public const string ChatCompletionsPath = "/chat/completions";
|
public const string ModelsPath = "/models";
|
|
public static string? ResolveBaseUrl(string? baseUrl, string? endpoint)
|
{
|
var raw = FirstNonEmpty(baseUrl, endpoint);
|
if (raw is null)
|
{
|
return null;
|
}
|
|
raw = raw.Trim().TrimEnd('/');
|
if (raw.EndsWith(ChatCompletionsPath, StringComparison.OrdinalIgnoreCase))
|
{
|
raw = raw[..^ChatCompletionsPath.Length].TrimEnd('/');
|
}
|
|
return string.IsNullOrWhiteSpace(raw) ? null : raw;
|
}
|
|
public static string? ToChatCompletionsUrl(string? baseUrl, string? endpoint)
|
{
|
var resolved = ResolveBaseUrl(baseUrl, endpoint);
|
return resolved is null ? null : resolved + ChatCompletionsPath;
|
}
|
|
public static string? ToModelsUrl(string? baseUrl, string? endpoint = null)
|
{
|
var resolved = ResolveBaseUrl(baseUrl, endpoint);
|
return resolved is null ? null : resolved + ModelsPath;
|
}
|
|
public static bool IsAbsoluteHttpUrl(string? url)
|
=> Uri.TryCreate(url, UriKind.Absolute, out var uri) &&
|
uri.Scheme is "http" or "https";
|
|
public static string Host(string? url)
|
=> Uri.TryCreate(url, UriKind.Absolute, out var uri) ? uri.Host : "";
|
|
private static string? FirstNonEmpty(string? primary, string? fallback)
|
{
|
if (!string.IsNullOrWhiteSpace(primary))
|
{
|
return primary;
|
}
|
|
return string.IsNullOrWhiteSpace(fallback) ? null : fallback;
|
}
|
}
|
|
public static class LlmProviderModelIds
|
{
|
public const string Prefix = "provider:";
|
|
public static string Create(string providerId, string apiModel)
|
=> $"{Prefix}{providerId}:{apiModel}";
|
|
public static bool TryParse(string? modelId, out string providerId, out string apiModel)
|
{
|
providerId = "";
|
apiModel = "";
|
if (string.IsNullOrWhiteSpace(modelId) ||
|
!modelId.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase))
|
{
|
return false;
|
}
|
|
var rest = modelId[Prefix.Length..];
|
var separator = rest.IndexOf(':');
|
if (separator <= 0 || separator >= rest.Length - 1)
|
{
|
return false;
|
}
|
|
providerId = rest[..separator];
|
apiModel = rest[(separator + 1)..];
|
return !string.IsNullOrWhiteSpace(providerId) && !string.IsNullOrWhiteSpace(apiModel);
|
}
|
}
|
|
public static class AiChatProjectStatuses
|
{
|
public const string NotConfigured = "not-configured";
|
public const string NotConnected = "not-connected";
|
public const string Ready = "ready";
|
}
|