BotSharp/src/Infrastructure/BotSharp.OpenAPI/Controllers/TranslationController.cs

104 lines
3.6 KiB
C#
Raw Normal View History

2024-10-09 00:22:00 +00:00
using BotSharp.Abstraction.Options;
2025-01-08 22:05:29 +00:00
using BotSharp.Abstraction.Repositories;
2024-06-24 17:37:12 +00:00
using BotSharp.Abstraction.Translation;
using BotSharp.OpenAPI.ViewModels.Translations;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]
public class TranslationController : ControllerBase
{
private readonly IServiceProvider _services;
2024-10-09 00:22:00 +00:00
private readonly JsonSerializerOptions _jsonOptions;
2024-06-24 17:37:12 +00:00
2024-10-09 00:22:00 +00:00
public TranslationController(IServiceProvider services,
BotSharpOptions options)
2024-06-24 17:37:12 +00:00
{
_services = services;
2024-10-09 00:22:00 +00:00
_jsonOptions = InitJsonOptions(options);
2024-06-24 17:37:12 +00:00
}
[HttpPost("/translate")]
public async Task<TranslationResponseModel> Translate([FromBody] TranslationRequestModel model)
{
2025-01-08 22:05:29 +00:00
var db = _services.GetRequiredService<IBotSharpRepository>();
var agent = db.GetAgent(BuiltInAgentId.AIAssistant);
2024-06-24 17:37:12 +00:00
var translator = _services.GetRequiredService<ITranslationService>();
2024-10-11 00:32:22 +00:00
var states = _services.GetRequiredService<IConversationStateService>();
states.SetState("max_tokens", "8192");
var text = await translator.Translate(agent, Guid.NewGuid().ToString(), model.Text.Split("\r\n"), language: model.ToLang);
2024-06-24 17:37:12 +00:00
return new TranslationResponseModel
{
2024-10-11 00:32:22 +00:00
Text = string.Join("\r\n", text)
2024-06-24 17:37:12 +00:00
};
}
2024-10-09 00:22:00 +00:00
[HttpPost("/translate/long-text")]
public async Task SendMessageSse([FromBody] TranslationLongTextRequestModel model)
{
2025-01-08 22:05:29 +00:00
var db = _services.GetRequiredService<IBotSharpRepository>();
var agent = db.GetAgent(BuiltInAgentId.AIAssistant);
2024-10-09 00:22:00 +00:00
var translator = _services.GetRequiredService<ITranslationService>();
Response.StatusCode = 200;
Response.Headers.Append(Microsoft.Net.Http.Headers.HeaderNames.ContentType, "text/event-stream");
Response.Headers.Append(Microsoft.Net.Http.Headers.HeaderNames.CacheControl, "no-cache");
Response.Headers.Append(Microsoft.Net.Http.Headers.HeaderNames.Connection, "keep-alive");
foreach (var script in model.Texts)
{
var translatedText = await translator.Translate(agent, Guid.NewGuid().ToString(), script.Text, language: model.ToLang);
var json = JsonSerializer.Serialize(new TranslationScriptTimestamp
{
Text = translatedText,
Timestamp = script.Timestamp
}, _jsonOptions);
await OnChunkReceived(Response, json);
}
await OnEventCompleted(Response);
}
private async Task OnChunkReceived(HttpResponse response, string text)
{
var buffer = Encoding.UTF8.GetBytes($"data:{text}\n");
await response.Body.WriteAsync(buffer, 0, buffer.Length);
await Task.Delay(10);
buffer = Encoding.UTF8.GetBytes("\n");
await response.Body.WriteAsync(buffer, 0, buffer.Length);
}
private async Task OnEventCompleted(HttpResponse response)
{
var buffer = Encoding.UTF8.GetBytes("data:[DONE]\n");
await response.Body.WriteAsync(buffer, 0, buffer.Length);
buffer = Encoding.UTF8.GetBytes("\n");
await response.Body.WriteAsync(buffer, 0, buffer.Length);
}
private JsonSerializerOptions InitJsonOptions(BotSharpOptions options)
{
var jsonOption = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
AllowTrailingCommas = true
};
if (options?.JsonSerializerOptions != null)
{
foreach (var option in options.JsonSerializerOptions.Converters)
{
jsonOption.Converters.Add(option);
}
}
return jsonOption;
}
2024-06-24 17:37:12 +00:00
}