-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpenAiConnector.cs
435 lines (344 loc) · 14.6 KB
/
OpenAiConnector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using Jumoo.TranslationManager.Core;
using Jumoo.TranslationManager.Core.Configuration;
using Jumoo.TranslationManager.Core.Hubs;
using Jumoo.TranslationManager.Core.Models;
using Jumoo.TranslationManager.Core.Providers;
using Jumoo.TranslationManager.OpenAi.Models;
using Jumoo.TranslationManager.OpenAi.Services;
using Jumoo.TranslationManager.Utilities;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core;
using Umbraco.Extensions;
namespace Jumoo.TranslationManager.OpenAi;
public class OpenAiConnector : ITranslationProvider
{
public static string ConnectorName = "OpenAi Connector";
public static string ConnectorAlias = "openAiConnector";
public static string ConnectorVersion = typeof(OpenAiConnector).Assembly.GetName().Version.ToString(3);
#if UMB_14_OR_GREATER
public static string ConnectorPluginPath = "/App_Plugins/Translations.OpenAi/modern/";
#else
public static string ConnectorPluginPath = "/App_Plugins/Translations.OpenAi/legacy/";
#endif
private readonly TranslationConfigService _configService;
private readonly ILogger<OpenAiConnector> _logger;
private readonly IHubContext<TranslationHub> _hubContext;
private readonly OpenAIServiceFactory _openAIServiceFactory;
private IOpenAiTranslationService _openAiService;
public string Name => ConnectorName;
public string Alias => ConnectorAlias;
public Guid Key => Guid.Parse("{D60C3B07-2FCE-4568-8A1D-C3286A73DF8A}");
private string _aiServiceName = nameof(BetalgoOpenAiService);
private int _throttle = 300;
private bool _split = false;
private bool _asHtml = true;
private string _model = OpenAIConstants.DefaultModel;
private string _prompt = OpenAIConstants.DefaultPrompt;
private string _systemPrompt = OpenAIConstants.DefaultSystemPrompt;
public OpenAiConnector(
TranslationConfigService configService,
ILogger<OpenAiConnector> logger,
OpenAIServiceFactory openAIServiceFactory,
IHubContext<TranslationHub> hubContext)
{
// defaults.
_configService = configService;
_logger = logger;
_openAIServiceFactory = openAIServiceFactory;
_hubContext = hubContext;
Reload();
}
public TranslationProviderViews Views => new TranslationProviderViews()
{
#if UMB_14_OR_GREATER
Config = "jumoo-openai-config",
Pending = "jumoo-openai-pending"
#else
Config = TranslateUriUtility.ToAbsolute(ConnectorPluginPath + "config.html"),
Pending = TranslateUriUtility.ToAbsolute(ConnectorPluginPath + "pending.html")
#endif
};
public async Task<Attempt<TranslationJob>> Submit(TranslationJob job)
{
if (!_openAiService.Enabled())
throw new Exception("OpenAi is not configured");
try
{
var sourceLang = job.SourceCulture.DisplayName;
var targetLang = job.TargetCulture.DisplayName;
_logger.LogDebug("Submitting translations via OpenApi");
var hub = GetTranslationClientHub();
int count = 0;
foreach (var node in job.Nodes)
{
_logger.LogDebug("Translating: {nodeId}", node.MasterNodeId);
hub.SendMessage($"Translating {node.MasterNodeName} via OpenAI");
foreach (var group in node.Groups)
{
foreach (var property in group.Properties)
{
count++;
_logger.LogDebug("Translation: {nodeId} {group} {property}",
node.MasterNodeId, group, property);
hub.SendMessage($"Translating {node.MasterNodeName} via OpenAI - {count}");
var result = await GetTranslatedValue(
property.Source, property.Target, sourceLang, targetLang);
if (result == null)
return Attempt<TranslationJob>.Fail(new Exception("No value translated"));
property.Target = result;
}
if (_throttle > 0)
{
_logger.LogDebug("Throttle: Waiting... {0}ms", _throttle);
await Task.Delay(_throttle);
}
}
}
job.Status = JobStatus.Received;
#if NET7_0_OR_GREATER
job.ProviderStatus = "Translated via OpenAI";
#endif
return Attempt.Succeed(job);
}
catch(Exception exception)
{
_logger.LogError(exception, "Error submitting job via openAI connector.");
return Attempt<TranslationJob>.Fail(exception);
}
}
private async Task<TranslationValue> GetTranslatedValue(TranslationValue source, TranslationValue target, string sourceLang, string targetLang)
{
_logger.LogDebug("GetTranslationValue: {name}", source.DisplayName);
if (source.HasChildValues())
{
foreach(var innerValue in source.InnerValues)
{
_logger.LogDebug("GetTranslatedValue: Child : {key}", innerValue.Key);
var innerTarget = target.GetInnerValue(innerValue.Key);
if (innerTarget == null)
{
_logger.LogWarning("No inner target to match child (bad setup)");
continue;
}
var translatedValue = await GetTranslatedValue(innerValue.Value, innerTarget, sourceLang, targetLang);
if (translatedValue != null)
{
innerTarget = translatedValue;
}
}
}
if (!string.IsNullOrWhiteSpace(source.Value))
{
_logger.LogDebug("Translating: [{value}] [{source} to {target}]",
source.Value, sourceLang, targetLang);
// has a value to translate
if (_split)
{
_logger.LogDebug("Splitting");
// if it's html, we split it up.
target.Value = await TranslateHtmlValue(source.Value, sourceLang, targetLang);
target.Translated = true;
}
else
{
_logger.LogDebug("Not splitting, treating as single string");
target.Value = await TranslateStringValue(source.Value, sourceLang, targetLang);
target.Translated = true;
}
}
return target;
}
/// <summary>
/// translate the text as if it's html,
/// We split by top level node (so hopefully paragraphs)
/// and return that
/// </summary>
private async Task<string> TranslateHtmlValue(string source, string sourceLang, string targetLang)
{
if (!IsHtml(source))
return await TranslateStringValue(source, sourceLang, targetLang);
var doc = new HtmlDocument();
doc.LoadHtml(source);
return await TranslateHtmlNodes(doc.DocumentNode.ChildNodes, sourceLang, targetLang);
}
private async Task<string> TranslateHtmlNodes(HtmlNodeCollection nodes, string sourceLang, string targetLang)
{
_logger.LogDebug("Treating as Html");
var result = "";
List<string> values = new List<string>();
foreach (var node in nodes)
{
var value = node.OuterHtml;
if (!string.IsNullOrWhiteSpace(value))
{
if (value.Length > 5000)
{
if (node.HasChildNodes)
{
// if we get here then the bulk values up and send process won't work.
// (because we need to know that the translations are wrapped in a html tag
// we haven't sent to translation)
// we have to send what we have already done to translation,
result += await TranslateStringValues(values, sourceLang, targetLang, _asHtml);
// and then send this block to translation
var translatedResult = await TranslateHtmlNodes(node.ChildNodes, sourceLang, targetLang);
result += $"<{node.Name}>{translatedResult}<{node.Name}>";
// and then resume adding things to a now empty list,
values.Clear();
}
else
{
_logger.LogWarning("Splitting single html element that spans more than 5000 charecters. " +
"This is larger than the request limit, splitting may result in some issues with translation.");
// we attempt to split the tag, we also wrap it in the nodeName, to make it fit
var innerValue = node.InnerHtml;
// take the tag name and the braces (< > < / > ) from the 5000 budget.
var size = 4995 - (node.Name.Length * 2);
values.AddRange(Split(innerValue, size, node.Name));
}
}
else
{
values.Add(value);
}
}
}
if (values.Count > 0)
{
result += await TranslateStringValues(values, sourceLang, targetLang, _asHtml);
}
return result;
}
/// <summary>
/// translates a string using the api, we assume the string isn't anything
/// fancy, and if it's super long, we just hard split it at 5000 chars
/// </summary>
private async Task<string> TranslateStringValue(string source, string sourceLang, string targetLang)
{
var values = Split(source, 5000);
return await TranslateStringValues(values, sourceLang, targetLang, false);
}
private List<string> GetBlocks(IList<string> values, int start, out int end)
{
int pos = start;
var block = new List<string>();
var length = 0;
while (pos < values.Count && block.Count < 25)
{
length += values[pos].Length;
if (length < 5000)
{
block.Add(values[pos]);
}
else
{
break;
}
pos++;
}
end = pos;
return block;
}
private IEnumerable<string> Split(string str, int maxChunkSize, string surroundingTag = "")
{
for (int i = 0; i < str.Length; i += maxChunkSize)
{
var chunk = str.Substring(i, Math.Min(maxChunkSize, str.Length - i));
if (!string.IsNullOrWhiteSpace(surroundingTag))
{
yield return $"<{surroundingTag}>{chunk}</{surroundingTag}>";
}
else
{
yield return chunk;
}
}
}
private bool IsHtml(string text)
{
if (!string.IsNullOrWhiteSpace(text))
{
var doc = new HtmlDocument();
doc.LoadHtml(text);
return !doc.DocumentNode.ChildNodes.All(x => x.NodeType == HtmlNodeType.Text);
}
return false;
}
/// <summary>
/// bit that calls the API to do the translation.
/// </summary>
private async Task<string> TranslateStringValues(IEnumerable<string> values, string sourceLang, string targetLang, bool isHtml)
{
_logger.LogDebug("Translating: {count} values", values.Count());
var valueList = values.ToList(); ;
var translatedText = new StringBuilder();
_logger.LogDebug("Splitting: {count} values", valueList.Count);
int end = 0;
while (end < valueList.Count)
{
var block = GetBlocks(valueList, end, out end).ToList();
if (block.Count > 0)
{
_logger.LogDebug("Blocks : {count}, {end}", block.Count, end);
_logger.LogDebug("Translating: {count} as one chunk", block.Count);
_logger.LogDebug("Chunks: {blocks}", string.Join("\r\n", block));
foreach (var b in block)
{
_logger.LogDebug("Chunk {length}", b.Length);
}
var translationOptions = new OpenAITranslationOptions
{
SourceLanguage = sourceLang,
TargetLanguage = targetLang,
IsHtml = isHtml,
Model = _model,
Prompt = _prompt,
SystemPrompt = _systemPrompt
};
var translated = await _openAiService.Translate(block, translationOptions);
var text = translated
.Select(x => x);
_logger.LogDebug("Returned: {count} translated values", text.Count());
translatedText.Append(string.Join("", text));
}
else
{
_logger.LogDebug("Empty Block");
break;
}
}
_logger.LogDebug("Translated: {translated}", translatedText.ToString());
return translatedText.ToString().Trim();
}
public bool Active() => _openAiService.Enabled();
public bool CanTranslate(TranslationJob job) => true;
public void Reload()
{
_model = _configService.GetProviderSetting(this.Alias, "model", "gpt-3.5-turbo-instruct");
_prompt = _configService.GetProviderSetting(this.Alias, "prompt", OpenAIConstants.DefaultPrompt);
_systemPrompt = _configService.GetProviderSetting(this.Alias, "systemPrompt", OpenAIConstants.DefaultSystemPrompt);
_split = _configService.GetProviderSetting(this.Alias, "split", false);
_asHtml = _configService.GetProviderSetting(this.Alias, "asHtml", false);
_aiServiceName = _configService.GetProviderSetting(Alias, "service", nameof(BetalgoOpenAiService));
_openAiService = _openAIServiceFactory.GetActiveService();
}
public IEnumerable<string> GetTargetLanguages(string sourceLanguage)
=> Enumerable.Empty<string>();
public Task<Attempt<TranslationJob>> Check(TranslationJob job)
=> Task.FromResult(Attempt.Succeed(job));
public Task<Attempt<TranslationJob>> Cancel(TranslationJob job)
=> Task.FromResult(Attempt.Succeed(job));
public Task<Attempt<TranslationJob>> Remove(TranslationJob job)
=> Task.FromResult(Attempt.Succeed(job));
private TranslationHubClient GetTranslationClientHub()
{
return new TranslationHubClient(_hubContext, string.Empty);
}
}