35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
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;
|
|
}
|
|
}
|