[FA-misc] Translation engine work
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Services.TranslationEngines.NanoGpt;
|
||||
|
||||
public class NanoGptApiClient
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger<NanoGptApiClient> _logger;
|
||||
|
||||
public NanoGptApiClient(HttpClient httpClient, ILogger<NanoGptApiClient> logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<NanoGptChatResponse> CompleteAsync(
|
||||
string model,
|
||||
IReadOnlyList<NanoGptMessage> messages,
|
||||
double temperature,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var request = new NanoGptChatRequest(model, messages, temperature);
|
||||
|
||||
using var response = await _httpClient.PostAsJsonAsync("v1/chat/completions", request, ct);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var body = await response.Content.ReadFromJsonAsync<NanoGptChatResponse>(ct);
|
||||
if (body is null)
|
||||
{
|
||||
throw new InvalidOperationException("NanoGPT returned an empty response body.");
|
||||
}
|
||||
return body;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user