merge with dev

This commit is contained in:
Jicheng Lu 2024-05-21 11:19:06 -05:00
commit 02f4cbd6a1
12 changed files with 45 additions and 36 deletions

View file

@ -6,6 +6,6 @@ public interface ILlmProviderService
{
LlmModelSetting GetSetting(string provider, string model);
List<string> GetProviders();
LlmModelSetting GetProviderModel(string provider, string id, bool multiModal = false);
LlmModelSetting GetProviderModel(string provider, string id, bool? multiModal = null);
List<LlmModelSetting> GetProviderModels(string provider);
}

View file

@ -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!;
}

View file

@ -6,6 +6,6 @@ public interface IUserService
{
Task<User> GetUser(string id);
Task<User> CreateUser(User user);
Task<Token> GetToken(string authorization);
Task<Token?> GetToken(string authorization);
Task<User> GetMyProfile();
}

View file

@ -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<IChatCompletion>();
@ -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<AgentSettings>();
@ -82,7 +82,7 @@ public class CompletionProvider
{
var modelIdentity = state.ContainsState("model_id") ? state.GetState("model_id") : modelId;
var llmProviderService = services.GetRequiredService<ILlmProviderService>();
model = llmProviderService.GetProviderModel(provider, modelIdentity, multiModal)?.Name;
model = llmProviderService.GetProviderModel(provider, modelIdentity, multiModal: multiModal)?.Name;
}
}

View file

@ -44,11 +44,15 @@ public class LlmProviderService : ILlmProviderService
?.Models ?? new List<LlmModelSetting>();
}
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());

View file

@ -1,5 +1,3 @@
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Templating;
using System.Reflection;

View file

@ -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<FunctionDef>();
_options.MemberAccessStrategy.Register<FunctionParametersDef>();
_options.MemberAccessStrategy.Register<UserIdentity>();
_options.MemberAccessStrategy.Register<TranslationInput>();
}
public string Render(string template, Dictionary<string, object> dict)

View file

@ -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
/// <param name="list"></param>
/// <param name="language"></param>
/// <returns></returns>
private async Task<TranslationOutput> InnerTranslate(List<string> texts, string language, string template)
private async Task<TranslationOutput> InnerTranslate(List<TranslationInput> 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<string, object>
{
{ "text_list", texts },
{ "text_list", jsonString },
{ "text_list_size", texts.Count },
{ StateConst.LANGUAGE, language }
}
};

View file

@ -59,7 +59,7 @@ public class UserService : IUserService
return record;
}
public async Task<Token> GetToken(string authorization)
public async Task<Token?> 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<IAuthenticationHook>();
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;
}

View file

@ -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":""}]}.

View file

@ -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);

View file

@ -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(),