Merge branch 'SciSharp:master' into master

This commit is contained in:
Haiping 2024-11-21 02:01:14 +00:00 committed by GitHub
commit fe8a46228a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 62 additions and 50 deletions

View file

@ -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.")

View file

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

View file

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

View file

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

View file

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

View file

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