Skip to content

Commit

Permalink
created initial service for local ai
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanfuchs committed Oct 13, 2024
1 parent 5846bcd commit 5163e53
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Lingarr.Server/Models/Api/LocalAiResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Text.Json.Serialization;

namespace Lingarr.Server.Models.Api;

public class LocalAiResponse
{
[JsonPropertyName("created")]
public long Created { get; set; }

[JsonPropertyName("object")]
public string Object { get; set; }

[JsonPropertyName("id")]
public string Id { get; set; }

[JsonPropertyName("model")]
public string Model { get; set; }

[JsonPropertyName("choices")]
public List<Choice> Choices { get; set; }

[JsonPropertyName("usage")]
public Usage Usage { get; set; }
}

public class Choice
{
[JsonPropertyName("index")]
public int Index { get; set; }

[JsonPropertyName("finish_reason")]
public string FinishReason { get; set; }

[JsonPropertyName("message")]
public Message Message { get; set; }
}

public class Message
{
[JsonPropertyName("role")]
public string Role { get; set; }

[JsonPropertyName("content")]
public string Content { get; set; }
}

public class Usage
{
[JsonPropertyName("prompt_tokens")]
public int PromptTokens { get; set; }

[JsonPropertyName("completion_tokens")]
public int CompletionTokens { get; set; }

[JsonPropertyName("total_tokens")]
public int TotalTokens { get; set; }
}
52 changes: 52 additions & 0 deletions Lingarr.Server/Services/Translation/LocalAiTranslationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Text;
using System.Text.Json;
using Lingarr.Server.Exceptions;
using Lingarr.Server.Interfaces.Services;
using Lingarr.Server.Models.Api;

namespace Lingarr.Server.Services.Translation;

public class LocalAiTranslationService : TranslationServiceBase
{
private readonly HttpClient _httpClient;

public LocalAiTranslationService(ISettingService settings,
HttpClient httpClient,
ILogger<LocalAiTranslationService> logger) : base(settings, logger)
{
_httpClient = httpClient;
}

/// <inheritdoc />
public override async Task<string> TranslateAsync(string text, string sourceLanguage, string targetLanguage)
{
var settings = await _settings.GetSettings(["local_ai_model", "local_ai_endpoint"]);
if (string.IsNullOrEmpty(settings["local_ai_model"]) || string.IsNullOrEmpty(settings["local_ai_endpoint"]))
{
throw new InvalidOperationException("Local AI API key or model is not configured.");
}
var prompt = $"Translate the following text from {sourceLanguage} to {targetLanguage}. Only provide the translation, without any additional comments or explanations: {text}";

_httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
var content = new StringContent(JsonSerializer.Serialize(new
{
model = settings["local_ai_model"],
messages = new[]
{
new { role = "user", content = prompt }
}
}), Encoding.UTF8, "application/json");

var response = await _httpClient.PostAsync(settings["local_ai_endpoint"], content);
if (!response.IsSuccessStatusCode)
{
_logger.LogError("Response Status Code: {StatusCode}", response.StatusCode);
_logger.LogError("Response Content: {ResponseContent}", await response.Content.ReadAsStringAsync());
throw new TranslationException("Translation using Local AI failed.");
}

var responseBody = await response.Content.ReadAsStringAsync();
var jsonResponse = JsonSerializer.Deserialize<LocalAiResponse>(responseBody);
return jsonResponse.Choices[0].Message.Content ?? throw new InvalidOperationException();
}
}

0 comments on commit 5163e53

Please sign in to comment.