Merge branch 'master' of https://github.com/Qtoss-AI/BotSharp
This commit is contained in:
commit
a4e2bfc016
|
|
@ -49,6 +49,8 @@ public class UserDocument : MongoBase
|
|||
Verified = Verified,
|
||||
RegionCode = RegionCode,
|
||||
Permissions = Permissions,
|
||||
CreatedTime = CreatedTime,
|
||||
UpdatedTime = UpdatedTime,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,19 +1,3 @@
|
|||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Instructs;
|
||||
using BotSharp.Abstraction.Instructs.Models;
|
||||
using BotSharp.Abstraction.Routing;
|
||||
using BotSharp.Core.Agents.Services;
|
||||
using BotSharp.Core.Infrastructures;
|
||||
using BotSharp.Core.Instructs;
|
||||
using BotSharp.Plugin.SqlDriver.Interfaces;
|
||||
using BotSharp.Plugin.SqlDriver.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace BotSharp.Plugin.SqlDriver.Functions;
|
||||
|
||||
public class SqlValidateFn : IFunctionCallback
|
||||
|
|
@ -22,26 +6,33 @@ public class SqlValidateFn : IFunctionCallback
|
|||
public string Indication => "Performing data validate operation.";
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger _logger;
|
||||
public SqlValidateFn(IServiceProvider services)
|
||||
public SqlValidateFn(IServiceProvider services, ILogger<SqlValidateFn> logger)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
{
|
||||
string pattern = @"```sql\s*([\s\S]*?)\s*```";
|
||||
var sqls = Regex.Match(message.Content, pattern);
|
||||
if (!sqls.Success)
|
||||
// remove comments start with "--"
|
||||
string pattern = @"--.*";
|
||||
string sql = Regex.Replace(message.Content, pattern, string.Empty);
|
||||
|
||||
pattern = @"```sql\s*([\s\S]*?)\s*```";
|
||||
sql = Regex.Match(sql, pattern)?.Value;
|
||||
|
||||
if (!Regex.IsMatch(sql, pattern))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var sql = sqls.Groups[1].Value;
|
||||
|
||||
sql = Regex.Match(sql, pattern).Groups[1].Value;
|
||||
|
||||
var dbHook = _services.GetRequiredService<ISqlDriverHook>();
|
||||
var dbType = dbHook.GetDatabaseType(message);
|
||||
var validateSql = dbType.ToLower() switch
|
||||
{
|
||||
"mysql" => $"explain\r\n{sql.Replace("SET ", "-- SET ", StringComparison.InvariantCultureIgnoreCase).Replace(";", "; explain ").TrimEnd("explain ".ToCharArray())}",
|
||||
"mysql" => $"EXPLAIN\r\n{sql.Replace("SET ", "-- SET ", StringComparison.InvariantCultureIgnoreCase).Replace(";", "; EXPLAIN ").TrimEnd("EXPLAIN ".ToCharArray())}",
|
||||
"sqlserver" => $"SET PARSEONLY ON;\r\n{sql}\r\nSET PARSEONLY OFF;",
|
||||
"redshift" => $"explain\r\n{sql}",
|
||||
_ => throw new NotImplementedException($"Database type {dbType} is not supported.")
|
||||
|
|
|
|||
|
|
@ -1,17 +1,23 @@
|
|||
global using System;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Text;
|
||||
global using System.Data.Common;
|
||||
global using System.Text.RegularExpressions;
|
||||
global using System.Threading.Tasks;
|
||||
global using System.Linq;
|
||||
global using System.Text.Json;
|
||||
|
||||
global using Microsoft.Extensions.Configuration;
|
||||
global using Microsoft.Extensions.Logging;
|
||||
|
||||
global using BotSharp.Abstraction.Conversations;
|
||||
global using BotSharp.Abstraction.Plugins;
|
||||
global using System.Text.Json;
|
||||
global using BotSharp.Abstraction.Conversations.Models;
|
||||
global using Microsoft.Extensions.Configuration;
|
||||
global using System.Threading.Tasks;
|
||||
global using BotSharp.Plugin.SqlDriver.Models;
|
||||
global using BotSharp.Abstraction.Functions;
|
||||
global using BotSharp.Abstraction.Agents.Models;
|
||||
global using BotSharp.Abstraction.Templating;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
global using System.Linq;
|
||||
global using BotSharp.Abstraction.Agents;
|
||||
global using BotSharp.Abstraction.Utilities;
|
||||
global using BotSharp.Abstraction.Knowledges;
|
||||
|
|
@ -21,4 +27,8 @@ global using BotSharp.Plugin.SqlDriver.Hooks;
|
|||
global using BotSharp.Plugin.SqlDriver.Services;
|
||||
global using BotSharp.Plugin.SqlDriver.Enum;
|
||||
global using BotSharp.Plugin.SqlHero.Settings;
|
||||
global using System.Drawing;
|
||||
global using BotSharp.Abstraction.Agents.Enums;
|
||||
global using BotSharp.Abstraction.Instructs;
|
||||
global using BotSharp.Abstraction.Instructs.Models;
|
||||
global using BotSharp.Abstraction.Routing;
|
||||
global using BotSharp.Plugin.SqlDriver.Interfaces;
|
||||
|
|
|
|||
|
|
@ -36,31 +36,43 @@ namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Functions
|
|||
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions);
|
||||
if (args.PhoneNumber.Length != 12 || !args.PhoneNumber.StartsWith("+1", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_logger.LogError("Invalid phone number format: {phone}", args.PhoneNumber);
|
||||
var error = $"Invalid phone number format: {args.PhoneNumber}";
|
||||
_logger.LogError(error);
|
||||
message.Content = error;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(args.InitialMessage))
|
||||
{
|
||||
_logger.LogError("Initial message is empty.");
|
||||
message.Content = "There is an error when generating phone message.";
|
||||
return false;
|
||||
}
|
||||
var completion = CompletionProvider.GetAudioCompletion(_services, "openai", "tts-1");
|
||||
|
||||
var fileStorage = _services.GetRequiredService<IFileStorageService>();
|
||||
var sessionManager = _services.GetRequiredService<ITwilioSessionManager>();
|
||||
var convService = _services.GetRequiredService<IConversationService>();
|
||||
var conversationId = convService.ConversationId;
|
||||
|
||||
// Generate audio
|
||||
var completion = CompletionProvider.GetAudioCompletion(_services, "openai", "tts-1");
|
||||
var data = await completion.GenerateAudioFromTextAsync(args.InitialMessage);
|
||||
var conversationId = Guid.NewGuid().ToString();
|
||||
var fileName = $"intial.mp3";
|
||||
fileStorage.SaveSpeechFile(conversationId, fileName, data);
|
||||
// TODO: Add initial message in the new conversation
|
||||
var sessionManager = _services.GetRequiredService<ITwilioSessionManager>();
|
||||
|
||||
// Call phone number
|
||||
await sessionManager.SetAssistantReplyAsync(conversationId, 0, new AssistantMessage
|
||||
{
|
||||
Content = args.InitialMessage,
|
||||
SpeechFileName = fileName
|
||||
});
|
||||
|
||||
var call = await CallResource.CreateAsync(
|
||||
url: new Uri($"{_twilioSetting.CallbackHost}/twilio/voice/init-call?conversationId={conversationId}"),
|
||||
to: new PhoneNumber(args.PhoneNumber),
|
||||
from: new PhoneNumber(_twilioSetting.PhoneNumber));
|
||||
|
||||
message.Content = $"The generated phone message: {args.InitialMessage}" ?? message.Content;
|
||||
message.StopCompletion = true;
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts
|
||||
{
|
||||
public class LlmContextIn
|
||||
{
|
||||
[JsonPropertyName("phone_number")]
|
||||
public string PhoneNumber { get; set; }
|
||||
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
|
||||
|
||||
[JsonPropertyName("initial_message")]
|
||||
public string InitialMessage { get; set; }
|
||||
}
|
||||
public class LlmContextIn
|
||||
{
|
||||
[JsonPropertyName("phone_number")]
|
||||
public string PhoneNumber { get; set; }
|
||||
|
||||
[JsonPropertyName("initial_message")]
|
||||
public string InitialMessage { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts
|
||||
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
|
||||
|
||||
public class LlmContextOut
|
||||
{
|
||||
public class LlmContextOut
|
||||
{
|
||||
[JsonPropertyName("conversation_id")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string ConversationId { get; set; }
|
||||
}
|
||||
[JsonPropertyName("conversation_id")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string ConversationId { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ public class TwilioPlugin : IBotSharpPlugin
|
|||
public string Id => "943ffd4d-ac8b-44aa-8a1c-38c9279c1b65";
|
||||
public string Name => "Twilio";
|
||||
public string Description => "Communication APIs for SMS, Voice, Video & Authentication";
|
||||
public string IconUrl => "https://w7.pngwing.com/pngs/918/671/png-transparent-twilio-full-logo-tech-companies.png";
|
||||
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
|
|
@ -20,10 +21,10 @@ public class TwilioPlugin : IBotSharpPlugin
|
|||
var settingService = provider.GetRequiredService<ISettingService>();
|
||||
return settingService.Bind<TwilioSetting>("Twilio");
|
||||
});
|
||||
TwilioClient.Init(config["Twilio:AccountSid"], config["Twilio:AuthToken"]);
|
||||
TwilioClient.Init(config["Twilio:AccountSid"], config["Twilio:RequestValidation:AuthToken"]);
|
||||
services.AddScoped<TwilioService>();
|
||||
|
||||
var conn = ConnectionMultiplexer.Connect(config["Twilio:RedisConnectionString"]);
|
||||
var conn = ConnectionMultiplexer.Connect(config["Database:Redis"]);
|
||||
var sessionManager = new TwilioSessionManager(conn);
|
||||
|
||||
services.AddSingleton<ITwilioSessionManager>(sessionManager);
|
||||
|
|
|
|||
Loading…
Reference in a new issue