diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/ILlmProviderService.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/ILlmProviderService.cs index 120304d2..20762fe0 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/ILlmProviderService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/ILlmProviderService.cs @@ -6,6 +6,6 @@ public interface ILlmProviderService { LlmModelSetting GetSetting(string provider, string model); List GetProviders(); - LlmModelSetting GetProviderModel(string provider, string id, bool multiModal = false); + LlmModelSetting GetProviderModel(string provider, string id, bool? multiModal = null); List GetProviderModels(string provider); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Translation/Models/TranslationInput.cs b/src/Infrastructure/BotSharp.Abstraction/Translation/Models/TranslationInput.cs new file mode 100644 index 00000000..6897ca42 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Translation/Models/TranslationInput.cs @@ -0,0 +1,10 @@ +namespace BotSharp.Abstraction.Translation.Models; + +public class TranslationInput +{ + [JsonPropertyName("id")] + public int Id { get; set; } = -1; + + [JsonPropertyName("text")] + public string Text { get; set; } = null!; +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs b/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs index 35d74aa4..debf68f4 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs @@ -6,6 +6,6 @@ public interface IUserService { Task GetUser(string id); Task CreateUser(User user); - Task GetToken(string authorization); + Task GetToken(string authorization); Task GetMyProfile(); } \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs index ace1e664..4655b1de 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs @@ -36,7 +36,7 @@ public class CompletionProvider string? provider = null, string? model = null, string? modelId = null, - bool multiModal = false, + bool? multiModal = null, AgentLlmConfig? agentConfig = null) { var completions = services.GetServices(); @@ -59,7 +59,7 @@ public class CompletionProvider string? provider = null, string? model = null, string? modelId = null, - bool multiModal = false, + bool? multiModal = null, AgentLlmConfig? agentConfig = null) { var agentSetting = services.GetRequiredService(); @@ -82,7 +82,7 @@ public class CompletionProvider { var modelIdentity = state.ContainsState("model_id") ? state.GetState("model_id") : modelId; var llmProviderService = services.GetRequiredService(); - model = llmProviderService.GetProviderModel(provider, modelIdentity, multiModal)?.Name; + model = llmProviderService.GetProviderModel(provider, modelIdentity, multiModal: multiModal)?.Name; } } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/LlmProviderService.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/LlmProviderService.cs index 7d92a687..8320bdb7 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/LlmProviderService.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/LlmProviderService.cs @@ -44,11 +44,15 @@ public class LlmProviderService : ILlmProviderService ?.Models ?? new List(); } - public LlmModelSetting GetProviderModel(string provider, string id, bool multiModal = false) + public LlmModelSetting GetProviderModel(string provider, string id, bool? multiModal = null) { var models = GetProviderModels(provider) - .Where(x => x.Id == id && x.MultiModal == multiModal) - .ToList(); + .Where(x => x.Id == id); + + if (multiModal.HasValue) + { + models = models.Where(x => x.MultiModal == multiModal); + } var random = new Random(); var index = random.Next(0, models.Count()); diff --git a/src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs b/src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs index 5119bc91..463013bf 100644 --- a/src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs +++ b/src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs @@ -1,5 +1,3 @@ -using BotSharp.Abstraction.Repositories; -using BotSharp.Abstraction.Routing.Models; using BotSharp.Abstraction.Templating; using System.Reflection; diff --git a/src/Infrastructure/BotSharp.Core/Templating/TemplateRender.cs b/src/Infrastructure/BotSharp.Core/Templating/TemplateRender.cs index 3bdbf7e1..33b27177 100644 --- a/src/Infrastructure/BotSharp.Core/Templating/TemplateRender.cs +++ b/src/Infrastructure/BotSharp.Core/Templating/TemplateRender.cs @@ -3,6 +3,7 @@ using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Models; using BotSharp.Abstraction.Routing.Models; using BotSharp.Abstraction.Templating; +using BotSharp.Abstraction.Translation.Models; using Fluid; namespace BotSharp.Core.Templating; @@ -30,6 +31,7 @@ public class TemplateRender : ITemplateRender _options.MemberAccessStrategy.Register(); _options.MemberAccessStrategy.Register(); _options.MemberAccessStrategy.Register(); + _options.MemberAccessStrategy.Register(); } public string Render(string template, Dictionary dict) diff --git a/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs b/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs index d24fd7d4..5c60e108 100644 --- a/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs +++ b/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs @@ -57,8 +57,11 @@ public class TranslationService : ITranslationService var keys = unique.ToArray(); var texts = unique.ToArray() - .Select((text, i) => $"{i + 1}. \"{text}\"") - .ToList(); + .Select((text, i) => new TranslationInput + { + Id = i + 1, + Text = text + }).ToList(); var translatedStringList = await InnerTranslate(texts, language, template); try @@ -297,15 +300,18 @@ public class TranslationService : ITranslationService /// /// /// - private async Task InnerTranslate(List texts, string language, string template) + private async Task InnerTranslate(List texts, string language, string template) { + var jsonString = JsonSerializer.Serialize(texts); var translator = new Agent { Id = Guid.Empty.ToString(), Name = "Translator", + Instruction = "You are a translation expert.", TemplateDict = new Dictionary { - { "text_list", texts }, + { "text_list", jsonString }, + { "text_list_size", texts.Count }, { StateConst.LANGUAGE, language } } }; diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index 322b300d..9508dd2b 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -59,7 +59,7 @@ public class UserService : IUserService return record; } - public async Task GetToken(string authorization) + public async Task GetToken(string authorization) { var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization)); var (id, password) = base64.SplitAsTuple(":"); @@ -71,13 +71,14 @@ public class UserService : IUserService record = db.GetUserByUserName(id); } + User? user = null; var hooks = _services.GetServices(); if (record == null || record.Source != "internal") { // check 3rd party user foreach (var hook in hooks) { - var user = await hook.Authenticate(id, password); + user = await hook.Authenticate(id, password); if (user == null) { continue; @@ -108,7 +109,7 @@ public class UserService : IUserService } } - if (record == null) + if ((!hooks.IsNullOrEmpty() && user == null) || record == null) { return default; } diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/translation_prompt.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/translation_prompt.liquid index be4f1077..3d33375b 100644 --- a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/translation_prompt.liquid +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/translation_prompt.liquid @@ -1,9 +1,5 @@ -{% for text in text_list %} -{{ text }} -{% endfor %} +{{ text_list }} + ===== -Translate the above sentences into {{ language }}. -Output the translated text in JSON {"input_lang":"original text language", "output_lang":"{{ language }}", "texts":[""]}. -Do not include the serial number before each sentence. -Do not include double quotes outside the sentence. -The number of output sentences must be {{ text_list | size }}. +Translate all the above sentences into {{ language }}. +Output the translated text in JSON {"input_lang":"original text language", "output_count": {{ text_list_size }}, "output_lang":"{{ language }}", "texts":[{"id": 1, "text":""}]}. diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 0946084c..08e4c0a0 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -1,8 +1,4 @@ using BotSharp.Abstraction.Routing; -using Newtonsoft.Json.Serialization; -using Newtonsoft.Json; -using BotSharp.Abstraction.Users.Enums; -using BotSharp.Abstraction.Users.Models; namespace BotSharp.OpenAPI.Controllers; @@ -332,12 +328,7 @@ public class ConversationController : ControllerBase private async Task OnChunkReceived(HttpResponse response, RoleDialogModel message) { - var json = JsonConvert.SerializeObject(message, new JsonSerializerSettings - { - Formatting = Formatting.None, - ContractResolver = new CamelCasePropertyNamesContractResolver(), - NullValueHandling = NullValueHandling.Ignore, - }); + var json = JsonSerializer.Serialize(message); var buffer = Encoding.UTF8.GetBytes($"data:{json}\n"); await response.Body.WriteAsync(buffer, 0, buffer.Length); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs index a884fd7e..45120a26 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs @@ -83,7 +83,8 @@ public class InstructModeController : ControllerBase try { - var completion = CompletionProvider.GetChatCompletion(_services, provider: "openai", modelId: "gpt-4", multiModal: true); + var completion = CompletionProvider.GetChatCompletion(_services, provider: input.Provider ?? "openai", + modelId: input.ModelId ?? "gpt-4", multiModal: true); var message = await completion.GetChatCompletions(new Agent() { Id = Guid.Empty.ToString(),