Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add translation memory setting #518

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ConversationSetting
public bool EnableExecutionLog { get; set; }
public bool EnableContentLog { get; set; }
public bool EnableStateLog { get; set; }
public bool EnableTranslationMemory { get; set; }
public CleanConversationSetting CleanSetting { get; set; } = new CleanConversationSetting();
public RateLimitSetting RateLimit { get; set; } = new RateLimitSetting();
}
Expand Down
4 changes: 0 additions & 4 deletions src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,4 @@
<ProjectReference Include="..\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Translation\Models\" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Infrastructure/BotSharp.Core/Files/FilePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddScoped<IBotSharpFileService, BotSharpFileService>();

services.AddScoped<IAgentHook, AttachmentProcessingHook>();
services.AddScoped<IAgentHook, FileAnalyzerHook>();
services.AddScoped<IAgentToolHook, FileAnalyzerToolHook>();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace BotSharp.Core.Files.Hooks;

public class AttachmentProcessingHook : AgentHookBase
public class FileAnalyzerHook : AgentHookBase
{
private static string TOOL_ASSISTANT = Guid.Empty.ToString();

public override string SelfId => string.Empty;

public AttachmentProcessingHook(IServiceProvider services, AgentSettings settings)
public FileAnalyzerHook(IServiceProvider services, AgentSettings settings)
: base(services, settings)
{
}
Expand Down
27 changes: 19 additions & 8 deletions src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class TranslationService : ITranslationService
{
private readonly IServiceProvider _services;
private readonly IBotSharpRepository _db;
private readonly ConversationSetting _convSettings;
private readonly ILogger<TranslationService> _logger;
private readonly BotSharpOptions _options;
private Agent _router;
Expand All @@ -22,11 +23,13 @@ public class TranslationService : ITranslationService
public TranslationService(
IServiceProvider services,
IBotSharpRepository db,
ConversationSetting convSettings,
ILogger<TranslationService> logger,
BotSharpOptions options)
{
_services = services;
_db = db;
_convSettings = convSettings;
_logger = logger;
_options = options;
}
Expand Down Expand Up @@ -69,18 +72,23 @@ public async Task<T> Translate<T>(Agent router, string messageId, T data, string
HashText = Utilities.HashTextSha256(x),
Language = language
}).ToList();
var memories = _db.GetTranslationMemories(queries);
var memoryHashes = memories.Select(x => x.HashText).ToList();
var outOfMemoryList = queries;

foreach (var memory in memories)
if (_convSettings.EnableTranslationMemory)
{
map[memory.OriginalText] = memory.TranslatedText;
}
var memories = _db.GetTranslationMemories(queries);
var memoryHashes = memories.Select(x => x.HashText).ToList();

foreach (var memory in memories)
{
map[memory.OriginalText] = memory.TranslatedText;
}

var outOfMemoryList = queries.Where(x => !memoryHashes.Contains(x.HashText)).ToList();
outOfMemoryList = queries.Where(x => !memoryHashes.Contains(x.HashText)).ToList();
}
#endregion

var texts = outOfMemoryList.ToArray()
var texts = outOfMemoryList
.Select((text, i) => new TranslationInput
{
Id = i + 1,
Expand Down Expand Up @@ -123,7 +131,10 @@ public async Task<T> Translate<T>(Agent router, string messageId, T data, string
});
}

_db.SaveTranslationMemories(memoryInputs);
if (_convSettings.EnableTranslationMemory)
{
_db.SaveTranslationMemories(memoryInputs);
}
}

clonedData = Assign(clonedData, map);
Expand Down
1 change: 1 addition & 0 deletions src/WebStarter/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"EnableExecutionLog": true,
"EnableContentLog": true,
"EnableStateLog": true,
"EnableTranslationMemory": false,
"CleanSetting": {
"Enable": true,
"BatchSize": 50,
Expand Down