AI-Studio/app/MindWork AI Studio/Provider/X/ProviderX.cs

131 lines
5.8 KiB
C#
Raw Normal View History

2025-01-04 14:06:08 +00:00
using System.Runtime.CompilerServices;
using AIStudio.Chat;
using AIStudio.Provider.OpenAI;
using AIStudio.Settings;
namespace AIStudio.Provider.X;
public sealed class ProviderX() : BaseProvider(LLMProviders.X, new Uri("https://api.x.ai/v1/"), ExternalHttpTrustPolicy.SYSTEM_TRUST_ONLY, LOGGER)
2025-01-04 14:06:08 +00:00
{
2025-09-03 19:25:17 +00:00
private static readonly ILogger<ProviderX> LOGGER = Program.LOGGER_FACTORY.CreateLogger<ProviderX>();
2025-01-04 14:06:08 +00:00
#region Implementation of IProvider
/// <inheritdoc />
public override string Id => LLMProviders.X.ToSecretId();
2025-01-04 14:06:08 +00:00
/// <inheritdoc />
public override string InstanceName { get; set; } = "xAI";
/// <inheritdoc />
public override bool HasModelLoadingCapability => true;
2025-01-04 14:06:08 +00:00
/// <inheritdoc />
public override async IAsyncEnumerable<ContentStreamChunk> StreamChatCompletion(Model chatModel, ChatThread chatThread, SettingsManager settingsManager, [EnumeratorCancellation] CancellationToken token = default)
2025-01-04 14:06:08 +00:00
{
await foreach (var content in this.StreamOpenAICompatibleChatCompletion<ChatCompletionAPIRequest, ChatCompletionDeltaStreamLine, NoChatCompletionAnnotationStreamLine>(
"xAI",
chatModel,
chatThread,
settingsManager,
async (systemPrompt, apiParameters, tools) =>
{
// Build the list of messages:
var messages = await chatThread.Blocks.BuildMessagesUsingNestedImageUrlAsync(this.CreateSettingsProvider(chatModel));
2025-01-04 14:06:08 +00:00
return new ChatCompletionAPIRequest
{
Model = chatModel.Id,
2025-01-04 14:06:08 +00:00
// Build the messages:
// - First of all the system prompt
// - Then none-empty user and AI messages
Messages = [systemPrompt, ..messages],
2025-01-04 14:06:08 +00:00
// Right now, we only support streaming completions:
Stream = true,
Tools = tools,
AdditionalApiParameters = apiParameters
};
},
token: token))
2025-01-04 14:06:08 +00:00
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());
}
/// <inhertidoc />
public override Task<IReadOnlyList<IReadOnlyList<float>>> EmbedTextAsync(Model embeddingModel, SettingsManager settingsManager, CancellationToken token = default, params List<string> texts)
{
throw this.CreateEmbeddingsNotSupportedException();
}
2025-01-04 14:06:08 +00:00
/// <inheritdoc />
public override async Task<ModelLoadResult> GetTextModels(string? apiKeyProvisional = null, CancellationToken token = default)
2025-01-04 14:06:08 +00:00
{
var result = await this.LoadModels(SecretStoreType.LLM_PROVIDER, apiKeyProvisional, token);
return result with
{
//
// Asking what a model is made for rather than testing its name for a word. The word was
// "-image", which said nothing about grok-imagine-video: that one made films and stood
// in the list of things to chat with.
//
Models = [..result.Models.Where(model => model.IsChatModel(this.Provider))]
};
2025-01-04 14:06:08 +00:00
}
/// <inheritdoc />
public override Task<ModelLoadResult> GetImageModels(string? apiKeyProvisional = null, CancellationToken token = default)
2025-01-04 14:06:08 +00:00
{
return Task.FromResult(ModelLoadResult.FromModels([]));
2025-01-04 14:06:08 +00:00
}
/// <inheritdoc />
public override Task<ModelLoadResult> GetEmbeddingModels(string? apiKeyProvisional = null, CancellationToken token = default)
2025-01-04 14:06:08 +00:00
{
return Task.FromResult(ModelLoadResult.FromModels([]));
2025-01-04 14:06:08 +00:00
}
2025-05-11 10:51:35 +00:00
2026-01-09 11:45:21 +00:00
/// <inheritdoc />
public override Task<ModelLoadResult> GetTranscriptionModels(string? apiKeyProvisional = null, CancellationToken token = default)
2026-01-09 11:45:21 +00:00
{
return Task.FromResult(ModelLoadResult.FromModels([]));
2026-01-09 11:45:21 +00:00
}
2025-01-04 14:06:08 +00:00
#endregion
/// <summary>
/// Reads the xAI catalog, whole.
/// </summary>
/// <remarks>
/// Every name in it begins with "grok", which is why the prefix this used to filter by never
/// took anything away -- and why it said nothing either. What it did carry was Grok 2, appended
/// to every answer whether xAI still served it or not. It does not: the catalog has moved on to
/// Grok 4, and an entry nobody can talk to is worse than one missing from the list.
///
/// What the catalog does hold besides the chat models is five names which draw or film. The
/// caller asks the registry about those.
/// </remarks>
private Task<ModelLoadResult> LoadModels(SecretStoreType storeType, string? apiKeyProvisional, CancellationToken token)
2025-01-04 14:06:08 +00:00
{
return this.LoadModelsResponse<ModelsResponse>(
storeType,
"models",
modelResponse => modelResponse.Data,
apiKeyProvisional, token: token);
2025-01-04 14:06:08 +00:00
}
}