refactor: modernize code, improve performance, and clean up various components.
This commit is contained in:
@@ -15,7 +15,7 @@ public class OpenAiRequest
|
||||
[JsonPropertyName("model")]
|
||||
public string Model { get; set; } = "llama-3.1-8b-instant";
|
||||
[JsonPropertyName("messages")]
|
||||
public OpenAiRequestMessage[] Messages { get; set; } = Array.Empty<OpenAiRequestMessage>();
|
||||
public OpenAiRequestMessage[] Messages { get; set; } = [];
|
||||
[JsonPropertyName("temperature")]
|
||||
public double Temperature { get; set; } = 0.0;
|
||||
[JsonPropertyName("stream")]
|
||||
@@ -27,7 +27,7 @@ public class OpenAiRequest
|
||||
public class OpenAiResponse
|
||||
{
|
||||
[JsonPropertyName("choices")]
|
||||
public OpenAiChoice[] Choices { get; set; } = Array.Empty<OpenAiChoice>();
|
||||
public OpenAiChoice[] Choices { get; set; } = [];
|
||||
}
|
||||
|
||||
public class OpenAiChoice
|
||||
@@ -39,7 +39,7 @@ public class OpenAiChoice
|
||||
public class OpenAiStreamResponse
|
||||
{
|
||||
[JsonPropertyName("choices")]
|
||||
public OpenAiStreamChoice[] Choices { get; set; } = Array.Empty<OpenAiStreamChoice>();
|
||||
public OpenAiStreamChoice[] Choices { get; set; } = [];
|
||||
}
|
||||
|
||||
public class OpenAiStreamChoice
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using Toak.Api.Models;
|
||||
using Toak.Serialization;
|
||||
using Toak.Core;
|
||||
@@ -22,17 +20,17 @@ public class OpenAiCompatibleClient : ISpeechClient, ILlmClient
|
||||
_reasoningEffort = reasoningEffort == "none" ? null : reasoningEffort;
|
||||
}
|
||||
|
||||
public async Task<string> TranscribeAsync(string filePath, string language = "", string model = Toak.Core.Constants.Defaults.WhisperModel)
|
||||
public async Task<string> TranscribeAsync(string filePath, string language = "", string model = Constants.Defaults.WhisperModel)
|
||||
{
|
||||
// ... (TranscribeAsync content remains same except maybe some internal comments or contexts)
|
||||
using var content = new MultipartFormDataContent();
|
||||
using var fileStream = File.OpenRead(filePath);
|
||||
await using var fileStream = File.OpenRead(filePath);
|
||||
using var streamContent = new StreamContent(fileStream);
|
||||
|
||||
streamContent.Headers.ContentType = new MediaTypeHeaderValue("audio/wav"); // or mpeg
|
||||
content.Add(streamContent, "file", Path.GetFileName(filePath));
|
||||
|
||||
string modelToUse = string.IsNullOrWhiteSpace(model) ? Toak.Core.Constants.Defaults.WhisperModel : model;
|
||||
var modelToUse = string.IsNullOrWhiteSpace(model) ? Constants.Defaults.WhisperModel : model;
|
||||
|
||||
content.Add(new StringContent(modelToUse), "model");
|
||||
|
||||
@@ -57,18 +55,18 @@ public class OpenAiCompatibleClient : ISpeechClient, ILlmClient
|
||||
return result?.Text ?? string.Empty;
|
||||
}
|
||||
|
||||
public async Task<string> RefineTextAsync(string rawTranscript, string systemPrompt, string model = Toak.Core.Constants.Defaults.LlmModel)
|
||||
public async Task<string> RefineTextAsync(string rawTranscript, string systemPrompt, string model = Constants.Defaults.LlmModel)
|
||||
{
|
||||
var requestBody = new OpenAiRequest
|
||||
{
|
||||
Model = string.IsNullOrWhiteSpace(model) ? Toak.Core.Constants.Defaults.LlmModel : model,
|
||||
Model = string.IsNullOrWhiteSpace(model) ? Constants.Defaults.LlmModel : model,
|
||||
Temperature = 0.0,
|
||||
ReasoningEffort = _reasoningEffort,
|
||||
Messages = new[]
|
||||
{
|
||||
Messages =
|
||||
[
|
||||
new OpenAiRequestMessage { Role = "system", Content = systemPrompt },
|
||||
new OpenAiRequestMessage { Role = "user", Content = $"<transcript>{rawTranscript}</transcript>" }
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var jsonContent = new StringContent(JsonSerializer.Serialize(requestBody, AppJsonSerializerContext.Default.OpenAiRequest), System.Text.Encoding.UTF8, "application/json");
|
||||
@@ -89,19 +87,19 @@ public class OpenAiCompatibleClient : ISpeechClient, ILlmClient
|
||||
return result?.Choices?.FirstOrDefault()?.Message?.Content ?? string.Empty;
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<string> RefineTextStreamAsync(string rawTranscript, string systemPrompt, string model = Toak.Core.Constants.Defaults.LlmModel)
|
||||
public async IAsyncEnumerable<string> RefineTextStreamAsync(string rawTranscript, string systemPrompt, string model = Constants.Defaults.LlmModel)
|
||||
{
|
||||
var requestBody = new OpenAiRequest
|
||||
{
|
||||
Model = string.IsNullOrWhiteSpace(model) ? Toak.Core.Constants.Defaults.LlmModel : model,
|
||||
Model = string.IsNullOrWhiteSpace(model) ? Constants.Defaults.LlmModel : model,
|
||||
Temperature = 0.0,
|
||||
Stream = true,
|
||||
ReasoningEffort = _reasoningEffort,
|
||||
Messages = new[]
|
||||
{
|
||||
Messages =
|
||||
[
|
||||
new OpenAiRequestMessage { Role = "system", Content = systemPrompt },
|
||||
new OpenAiRequestMessage { Role = "user", Content = $"<transcript>{rawTranscript}</transcript>" }
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var jsonContent = new StringContent(JsonSerializer.Serialize(requestBody, AppJsonSerializerContext.Default.OpenAiRequest), System.Text.Encoding.UTF8, "application/json");
|
||||
@@ -120,7 +118,7 @@ public class OpenAiCompatibleClient : ISpeechClient, ILlmClient
|
||||
throw new Exception($"OpenAi API Error: {response.StatusCode} - {error}");
|
||||
}
|
||||
|
||||
using var stream = await response.Content.ReadAsStreamAsync();
|
||||
await using var stream = await response.Content.ReadAsStreamAsync();
|
||||
using var reader = new StreamReader(stream);
|
||||
|
||||
string? line;
|
||||
|
||||
Reference in New Issue
Block a user