using System.Net.Http.Headers; using System.Runtime.CompilerServices; using System.Text; using System.Text.Json; using AIStudio.Chat; using AIStudio.Settings; namespace AIStudio.Provider.Fireworks; public class ProviderFireworks() : BaseProvider("https://api.fireworks.ai/inference/v1/"), IProvider { private static readonly JsonSerializerOptions JSON_SERIALIZER_OPTIONS = new() { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, }; #region Implementation of IProvider /// public string Id => "Fireworks.ai"; /// public string InstanceName { get; set; } = "Fireworks.ai"; /// public async IAsyncEnumerable StreamChatCompletion(IJSRuntime jsRuntime, SettingsManager settings, Model chatModel, ChatThread chatThread, [EnumeratorCancellation] CancellationToken token = default) { // Get the API key: var requestedSecret = await settings.GetAPIKey(jsRuntime, this); if(!requestedSecret.Success) yield break; // Prepare the system prompt: var systemPrompt = new Message { Role = "system", Content = chatThread.SystemPrompt, }; // Prepare the Fireworks HTTP chat request: var fireworksChatRequest = JsonSerializer.Serialize(new ChatRequest { Model = chatModel.Id, // Build the messages: // - First of all the system prompt // - Then none-empty user and AI messages Messages = [systemPrompt, ..chatThread.Blocks.Where(n => n.ContentType is ContentType.TEXT && !string.IsNullOrWhiteSpace((n.Content as ContentText)?.Text)).Select(n => new Message { Role = n.Role switch { ChatRole.USER => "user", ChatRole.AI => "assistant", ChatRole.SYSTEM => "system", _ => "user", }, Content = n.Content switch { ContentText text => text.Text, _ => string.Empty, } }).ToList()], // Right now, we only support streaming completions: Stream = true, }, JSON_SERIALIZER_OPTIONS); // Build the HTTP post request: var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); // Set the authorization header: request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", requestedSecret.Secret); // Set the content: request.Content = new StringContent(fireworksChatRequest, Encoding.UTF8, "application/json"); // Send the request with the ResponseHeadersRead option. // This allows us to read the stream as soon as the headers are received. // This is important because we want to stream the responses. var response = await this.httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token); // Open the response stream: var fireworksStream = await response.Content.ReadAsStreamAsync(token); // Add a stream reader to read the stream, line by line: var streamReader = new StreamReader(fireworksStream); // Read the stream, line by line: while(!streamReader.EndOfStream) { // Check if the token is canceled: if(token.IsCancellationRequested) yield break; // Read the next line: var line = await streamReader.ReadLineAsync(token); // Skip empty lines: if(string.IsNullOrWhiteSpace(line)) continue; // Skip lines that do not start with "data: ". Regard // to the specification, we only want to read the data lines: if(!line.StartsWith("data: ", StringComparison.InvariantCulture)) continue; // Check if the line is the end of the stream: if (line.StartsWith("data: [DONE]", StringComparison.InvariantCulture)) yield break; ResponseStreamLine fireworksResponse; try { // We know that the line starts with "data: ". Hence, we can // skip the first 6 characters to get the JSON data after that. var jsonData = line[6..]; // Deserialize the JSON data: fireworksResponse = JsonSerializer.Deserialize(jsonData, JSON_SERIALIZER_OPTIONS); } catch { // Skip invalid JSON data: continue; } // Skip empty responses: if(fireworksResponse == default || fireworksResponse.Choices.Count == 0) continue; // Yield the response: yield return fireworksResponse.Choices[0].Delta.Content; } } #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously /// public async IAsyncEnumerable StreamImageCompletion(IJSRuntime jsRuntime, SettingsManager settings, 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 /// public Task> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, string? apiKeyProvisional = null, CancellationToken token = default) { return Task.FromResult(Enumerable.Empty()); } /// public Task> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, string? apiKeyProvisional = null, CancellationToken token = default) { return Task.FromResult(Enumerable.Empty()); } #endregion }