Backend/eSuite.Translator/TranslationService.cs
Colin Dawson 052b833dd6 Replaced IFailedLoopback with IOUtcome
Added a translation service that will update the base language conversions for each supported language.
2026-02-22 23:43:57 +00:00

41 lines
1.2 KiB
C#

using System.Net.Http.Json;
using System.Text.Json.Nodes;
namespace eSuite.Translator;
public interface ITranslationService
{
Task<string> TranslateAsync(string sourceText, string targetLanguage);
}
public class OllamaTranslationService : ITranslationService
{
private readonly HttpClient _http;
public OllamaTranslationService(HttpClient http)
{
_http = http;
}
public async Task<string> TranslateAsync(string sourceText, string targetLanguage)
{
Console.Write( $"Translating \"{sourceText}\" into \"{targetLanguage}\": ");
var request = new
{
model = "ZimaBlueAI/HY-MT1.5-1.8:7b",
prompt = $"Translate this text into {targetLanguage}: {sourceText}",
stream = false
};
var response = await _http.PostAsJsonAsync("/api/generate", request);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadFromJsonAsync<JsonNode>();
// Ollama returns streaming chunks; for simplicity assume "response" contains the text
var result = (json?["response"]?.ToString() ?? "").Trim();
Console.WriteLine(result);
return result;
}
}