Skip to content

optimize sql driver #682

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

Merged
merged 21 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
23d0d5a
hdong: Add send verify code API with none login and login when reset …
YouWeiDH Oct 6, 2024
f760f07
Merge pull request #18 from Qtoss-AI/hdongDev
Oceania2018 Oct 7, 2024
7033ec2
Merge branch 'SciSharp:master' into master
Oceania2018 Oct 7, 2024
468f849
ReadInnerHTMLAsBody option
Oceania2018 Oct 8, 2024
f919ad7
add root role
Oct 8, 2024
0032f3a
Merge pull request #19 from Qtoss-AI/Issues-321
Oceania2018 Oct 8, 2024
cb1849c
Merge branch 'SciSharp:master' into master
Oceania2018 Oct 8, 2024
7e927dd
Merge branch 'SciSharp:master' into master
Oceania2018 Oct 9, 2024
edc0df0
translate long text
Oceania2018 Oct 9, 2024
527e689
Merge branch 'master' of https://github.com/Qtoss-AI/BotSharp
Oceania2018 Oct 9, 2024
730cc8b
Translate
Oceania2018 Oct 11, 2024
7e6ef34
Merge branch 'SciSharp:master' into master
Oceania2018 Oct 11, 2024
836fa0e
fix: Error caused by not including prefix when modifying user's mobil…
AnonymousDotNet Oct 11, 2024
0c7a59b
Merge branch 'master' into lida_dev
AnonymousDotNet Oct 11, 2024
878618c
hdong: When use verify fail, they need to re-register again.
YouWeiDH Oct 11, 2024
07fcc8d
Merge pull request #20 from AnonymousDotNet/lida_dev
Oceania2018 Oct 11, 2024
76a30c4
Merge pull request #21 from Qtoss-AI/hdongDev
Oceania2018 Oct 11, 2024
c95ac8b
Merge branch 'SciSharp:master' into master
Oceania2018 Oct 11, 2024
30591f4
optimize sql driver
Oceania2018 Oct 12, 2024
6ae74a4
Merge branch 'SciSharp:master' into master
Oceania2018 Oct 12, 2024
31322b2
Merge branch 'master' of https://github.com/Qtoss-AI/BotSharp
Oceania2018 Oct 12, 2024
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 @@ -43,4 +43,6 @@ public class PageActionArgs
/// Wait time in seconds after page is opened
/// </summary>
public int WaitTime { get; set; }

public bool ReadInnerHTMLAsBody { get; set; } = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public interface IBotSharpRepository
User? GetUserByAffiliateId(string affiliateId) => throw new NotImplementedException();
User? GetUserByUserName(string userName) => throw new NotImplementedException();
void CreateUser(User user) => throw new NotImplementedException();
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();
void UpdateUserVerified(string userId) => throw new NotImplementedException();
void UpdateUserVerificationCode(string userId, string verficationCode) => throw new NotImplementedException();
void UpdateUserPassword(string userId, string password) => throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
using BotSharp.Abstraction.Infrastructures.Enums;

namespace BotSharp.OpenAPI.ViewModels.Translations;

public class TranslationRequestModel
{
public string Text { get; set; } = null!;
public string ToLang { get; set; } = LanguageType.CHINESE;
}

public class TranslationScriptTimestamp
{
public string Text { set; get; } = null!;
public string Timestamp { get; set; } = null!;
}

public class TranslationLongTextRequestModel
{
public TranslationScriptTimestamp[] Texts { get; set; } = null!;
public string ToLang { get; set; } = LanguageType.CHINESE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public class UserRole
/// AI Assistant
/// </summary>
public const string Assistant = "assistant";

public const string Root = "root";
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public interface IUserService
Task<User> GetMyProfile();
Task<bool> VerifyUserNameExisting(string userName);
Task<bool> VerifyEmailExisting(string email);
Task<bool> SendVerificationCodeResetPassword(User user);
Task<bool> SendVerificationCodeResetPasswordNoLogin(User user);
Task<bool> SendVerificationCodeResetPasswordLogin();
Task<bool> ResetUserPassword(User user);
Task<bool> ModifyUserEmail(string email);
Task<bool> ModifyUserPhone(string phone);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ public async Task<T> Translate<T>(Agent router, string messageId, T data, string
{
var translatedStringList = await InnerTranslate(texts, language, template);

int retry = 0;
/*int retry = 0;
while (translatedStringList.Texts.Length != texts.Count && retry < 3)
{
translatedStringList = await InnerTranslate(texts, language, template);
retry++;
}
}*/

// Override language if it's Unknown, it's used to output the corresponding language.
var states = _services.GetRequiredService<IConversationStateService>();
Expand All @@ -119,7 +119,7 @@ public async Task<T> Translate<T>(Agent router, string messageId, T data, string
var translatedTexts = translatedStringList.Texts;
var memoryInputs = new List<TranslationMemoryInput>();

for (var i = 0; i < texts.Count; i++)
for (var i = 0; i < Math.Min(texts.Count, translatedTexts.Length); i++)
{
map[outOfMemoryList[i].OriginalText] = translatedTexts[i].Text;
memoryInputs.Add(new TranslationMemoryInput
Expand Down Expand Up @@ -375,6 +375,8 @@ private async Task<TranslationOutput> InnerTranslate(List<TranslationInput> text
var render = _services.GetRequiredService<ITemplateRender>();
var prompt = render.Render(template, translator.TemplateDict);

_logger.LogInformation($"Translation prompt: {prompt}");

var translationDialogs = new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, prompt)
Expand All @@ -384,6 +386,8 @@ private async Task<TranslationOutput> InnerTranslate(List<TranslationInput> text
}
};
var response = await _completion.GetChatCompletions(translator, translationDialogs);

_logger.LogInformation(response.Content);
return response.Content.JsonContent<TranslationOutput>();
}

Expand Down
78 changes: 58 additions & 20 deletions src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public UserService(IServiceProvider services,

public async Task<User> CreateUser(User user)
{
string hasRegisterId = null;
if (string.IsNullOrEmpty(user.UserName))
{
// generate unique name
Expand All @@ -48,7 +49,7 @@ public async Task<User> CreateUser(User user)

if (record != null)
{
return record;
hasRegisterId = record.Id;
}

if (string.IsNullOrEmpty(user.Id))
Expand All @@ -71,7 +72,14 @@ record = user;
record.Verified = false;
}

db.CreateUser(record);
if (hasRegisterId == null)
{
db.CreateUser(record);
}
else
{
db.UpdateExistUser(hasRegisterId, record);
}

_logger.LogWarning($"Created new user account: {record.Id} {record.UserName}");
Utilities.ClearCache();
Expand Down Expand Up @@ -386,8 +394,9 @@ public async Task<bool> VerifyUserNameExisting(string userName)
}

var db = _services.GetRequiredService<IBotSharpRepository>();

var user = db.GetUserByUserName(userName);
if (user != null)
if (user != null && user.Verified)
{
return true;
}
Expand All @@ -404,40 +413,64 @@ public async Task<bool> VerifyEmailExisting(string email)

var db = _services.GetRequiredService<IBotSharpRepository>();
var emailName = db.GetUserByEmail(email);
if (emailName != null)
if (emailName != null && emailName.Verified)
{
return true;
}

return false;
}

public async Task<bool> SendVerificationCodeResetPassword(User user)
public async Task<bool> SendVerificationCodeResetPasswordNoLogin(User user)
{
var db = _services.GetRequiredService<IBotSharpRepository>();

User? record = null;

if (!string.IsNullOrWhiteSpace(_user.Id))
if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
{
record = db.GetUserById(_user.Id);
return false;
}
else

if (!string.IsNullOrEmpty(user.Phone))
{
if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
{
return false;
}
record = db.GetUserByPhone(user.Phone);
}

if (!string.IsNullOrEmpty(user.Email))
{
record = db.GetUserByEmail(user.Email);
}
if (!string.IsNullOrEmpty(user.Email))
{
record = db.GetUserByEmail(user.Email);
}

if (!string.IsNullOrEmpty(user.Phone))
{
record = db.GetUserByPhone(user.Phone);
}
if (record == null)
{
return false;
}

record.VerificationCode = Nanoid.Generate(alphabet: "0123456789", size: 6);

//update current verification code.
db.UpdateUserVerificationCode(record.Id, record.VerificationCode);

//send code to user Email.
var hooks = _services.GetServices<IAuthenticationHook>();
foreach (var hook in hooks)
{
hook.VerificationCodeResetPassword(record);
}

return true;
}

public async Task<bool> SendVerificationCodeResetPasswordLogin()
{
var db = _services.GetRequiredService<IBotSharpRepository>();

User? record = null;

if (!string.IsNullOrWhiteSpace(_user.Id))
{
record = db.GetUserById(_user.Id);
}

if (record == null)
Expand Down Expand Up @@ -520,6 +553,11 @@ public async Task<bool> ModifyUserPhone(string phone)
return false;
}

if ((record.UserName.Substring(0, 3) == "+86" || record.FirstName.Substring(0, 3) == "+86") && phone.Substring(0, 3) != "+86")
{
phone = $"+86{phone}";
}

db.UpdateUserPhone(record.Id, phone);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
{{ text_list }}

=====
{% if language == "Chinese" %}
将以上所有句子翻译成中文。

要求:
* 以 JSON 格式输出翻译后的文本 {"input_lang":"原始文本语言", "output_count": {{ text_list_size }}, "output_lang":"{{ language }}", "texts":[{"id": 1, "text":""},{"id": 2, "text":""}]}。
* output_count 必须等于输出中texts数组的长度。
{% else %}
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":""},{"id": 2, "text":""}]}.
The "output_count" must equal the length of the "texts" array in the output.

Requirements:
* Output the translated text in JSON {"input_lang":"original text language", "output_count": {{ text_list_size }}, "output_lang":"{{ language }}", "texts":[{"id": 1, "text":""},{"id": 2, "text":""}]}.
* The "output_count" must equal the length of the "texts" array in the output.
{% endif %}



Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Options;
using BotSharp.Abstraction.Translation;
using BotSharp.OpenAPI.ViewModels.Translations;

Expand All @@ -9,10 +9,13 @@ namespace BotSharp.OpenAPI.Controllers;
public class TranslationController : ControllerBase
{
private readonly IServiceProvider _services;
private readonly JsonSerializerOptions _jsonOptions;

public TranslationController(IServiceProvider services)
public TranslationController(IServiceProvider services,
BotSharpOptions options)
{
_services = services;
_jsonOptions = InitJsonOptions(options);
}

[HttpPost("/translate")]
Expand All @@ -21,10 +24,79 @@ public async Task<TranslationResponseModel> Translate([FromBody] TranslationRequ
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(BuiltInAgentId.AIAssistant);
var translator = _services.GetRequiredService<ITranslationService>();
var text = await translator.Translate(agent, Guid.NewGuid().ToString(), model.Text, language: model.ToLang);
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);
return new TranslationResponseModel
{
Text = text
Text = string.Join("\r\n", text)
};
}

[HttpPost("/translate/long-text")]
public async Task SendMessageSse([FromBody] TranslationLongTextRequestModel model)
{
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(BuiltInAgentId.AIAssistant);
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;
}
}
12 changes: 10 additions & 2 deletions src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,20 @@ public async Task<bool> VerifyEmailExisting([FromQuery] string email)
{
return await _userService.VerifyEmailExisting(email);
}

[AllowAnonymous]
[HttpPost("/user/verifycode")]
[HttpPost("/user/verifycode-out")]
public async Task<bool> SendVerificationCodeResetPassword([FromBody] UserCreationModel user)
{
return await _userService.SendVerificationCodeResetPassword(user.ToUser());
return await _userService.SendVerificationCodeResetPasswordNoLogin(user.ToUser());
}

[HttpPost("/user/verifycode-in")]
public async Task<bool> SendVerificationCodeResetPasswordLogined()
{
return await _userService.SendVerificationCodeResetPasswordLogin();
}

[AllowAnonymous]
[HttpPost("/user/resetpassword")]
public async Task<bool> ResetUserPassword([FromBody] UserResetPasswordModel user)
Expand Down
Loading