-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created initial service for local ai
- Loading branch information
1 parent
5846bcd
commit 5163e53
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
Lingarr.Server/Services/Translation/LocalAiTranslationService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |