Merge pull request #1168 from iceljc/features/refine-model-settings
Features/refine model settings
This commit is contained in:
commit
b09d294fcd
|
|
@ -5,6 +5,7 @@ public static class ChatEvent
|
||||||
public const string OnConversationInitFromClient = nameof(OnConversationInitFromClient);
|
public const string OnConversationInitFromClient = nameof(OnConversationInitFromClient);
|
||||||
public const string OnMessageReceivedFromClient = nameof(OnMessageReceivedFromClient);
|
public const string OnMessageReceivedFromClient = nameof(OnMessageReceivedFromClient);
|
||||||
public const string OnMessageReceivedFromAssistant = nameof(OnMessageReceivedFromAssistant);
|
public const string OnMessageReceivedFromAssistant = nameof(OnMessageReceivedFromAssistant);
|
||||||
|
public const string OnIntermediateMessageReceivedFromAssistant = nameof(OnIntermediateMessageReceivedFromAssistant);
|
||||||
|
|
||||||
public const string OnMessageDeleted = nameof(OnMessageDeleted);
|
public const string OnMessageDeleted = nameof(OnMessageDeleted);
|
||||||
public const string OnNotificationGenerated = nameof(OnNotificationGenerated);
|
public const string OnNotificationGenerated = nameof(OnNotificationGenerated);
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,4 @@ public class FileSelectItem
|
||||||
|
|
||||||
[JsonPropertyName("file_source")]
|
[JsonPropertyName("file_source")]
|
||||||
public string FileSource { get; set; }
|
public string FileSource { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("file_name")]
|
|
||||||
public string? FileName { get; set; }
|
|
||||||
}
|
}
|
||||||
|
|
@ -2,5 +2,13 @@ namespace BotSharp.Abstraction.MessageHub.Models;
|
||||||
|
|
||||||
public class HubObserveData<TData> : ObserveDataBase where TData : class, new()
|
public class HubObserveData<TData> : ObserveDataBase where TData : class, new()
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The observed data
|
||||||
|
/// </summary>
|
||||||
public TData Data { get; set; } = null!;
|
public TData Data { get; set; } = null!;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether to save the observed data To Db
|
||||||
|
/// </summary>
|
||||||
|
public bool SaveDataToDb { get; set; }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,17 @@ public class GetWeatherFn : IFunctionCallback
|
||||||
|
|
||||||
await Task.Delay(1500);
|
await Task.Delay(1500);
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
var temp = RoleDialogModel.From(message, AgentRole.Assistant, $"Here is your weather in {args?.City}");
|
||||||
|
messageHub.Push(new()
|
||||||
|
{
|
||||||
|
EventName = ChatEvent.OnIntermediateMessageReceivedFromAssistant,
|
||||||
|
Data = temp,
|
||||||
|
RefId = conv.ConversationId,
|
||||||
|
SaveDataToDb = true
|
||||||
|
});
|
||||||
|
#endif
|
||||||
|
|
||||||
message.Indication = $"Still working on it... Hold on, {args?.City}";
|
message.Indication = $"Still working on it... Hold on, {args?.City}";
|
||||||
messageHub.Push(new()
|
messageHub.Push(new()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
using BotSharp.Abstraction.Instructs.Models;
|
using BotSharp.Abstraction.Instructs.Models;
|
||||||
using BotSharp.Abstraction.Instructs;
|
using BotSharp.Abstraction.Instructs;
|
||||||
|
using BotSharp.Abstraction.Files.Converters;
|
||||||
|
|
||||||
namespace BotSharp.Core.Files.Services;
|
namespace BotSharp.Core.Files.Services;
|
||||||
|
|
||||||
|
|
@ -22,13 +23,16 @@ public partial class FileInstructService
|
||||||
{
|
{
|
||||||
var provider = options?.Provider ?? "openai";
|
var provider = options?.Provider ?? "openai";
|
||||||
var pdfFiles = await DownloadAndSaveFiles(sessionDir, files);
|
var pdfFiles = await DownloadAndSaveFiles(sessionDir, files);
|
||||||
|
|
||||||
var targetFiles = pdfFiles;
|
var targetFiles = pdfFiles;
|
||||||
if (provider != "google-ai")
|
|
||||||
|
var converter = GetImageConverter(options?.ImageConvertProvider);
|
||||||
|
if (converter == null && provider == "openai")
|
||||||
{
|
{
|
||||||
targetFiles = await ConvertPdfToImages(pdfFiles, options);
|
var fileCoreSettings = _services.GetRequiredService<FileCoreSettings>();
|
||||||
|
converter = GetImageConverter(fileCoreSettings?.ImageConverter?.Provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
targetFiles = await ConvertPdfToImages(converter, pdfFiles);
|
||||||
if (targetFiles.IsNullOrEmpty())
|
if (targetFiles.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
return content;
|
return content;
|
||||||
|
|
@ -115,15 +119,12 @@ public partial class FileInstructService
|
||||||
return locs;
|
return locs;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<IEnumerable<string>> ConvertPdfToImages(IEnumerable<string> files, InstructOptions? options = null)
|
private async Task<IEnumerable<string>> ConvertPdfToImages(IImageConverter converter, IEnumerable<string> files)
|
||||||
{
|
{
|
||||||
var images = new List<string>();
|
var images = new List<string>();
|
||||||
var settings = _services.GetRequiredService<FileCoreSettings>();
|
|
||||||
|
|
||||||
var converter = GetImageConverter(options?.ImageConvertProvider);
|
|
||||||
if (converter == null || files.IsNullOrEmpty())
|
if (converter == null || files.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
return images;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var file in files)
|
foreach (var file in files)
|
||||||
|
|
|
||||||
|
|
@ -91,9 +91,7 @@ public partial class FileInstructService : IFileInstructService
|
||||||
|
|
||||||
private IImageConverter? GetImageConverter(string? provider)
|
private IImageConverter? GetImageConverter(string? provider)
|
||||||
{
|
{
|
||||||
var settings = _services.GetRequiredService<FileCoreSettings>();
|
var converter = _services.GetServices<IImageConverter>().FirstOrDefault(x => x.Provider == provider);
|
||||||
var convertProvider = provider ?? settings?.ImageConverter?.Provider;
|
|
||||||
var converter = _services.GetServices<IImageConverter>().FirstOrDefault(x => x.Provider == convertProvider);
|
|
||||||
return converter;
|
return converter;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
|
||||||
|
|
@ -61,17 +61,6 @@ public class ExecuteTemplateFn : IFunctionCallback
|
||||||
new(AgentRole.User, text)
|
new(AgentRole.User, text)
|
||||||
});
|
});
|
||||||
|
|
||||||
await HookEmitter.Emit<IInstructHook>(_services, async hook =>
|
|
||||||
await hook.OnResponseGenerated(new InstructResponseModel
|
|
||||||
{
|
|
||||||
AgentId = agent.Id,
|
|
||||||
TemplateName = templateName,
|
|
||||||
Provider = completion.Provider,
|
|
||||||
Model = completion.Model,
|
|
||||||
UserMessage = text,
|
|
||||||
CompletionText = response.Content
|
|
||||||
}), agent.Id);
|
|
||||||
|
|
||||||
return response.Content;
|
return response.Content;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
You're {{router.name}} ({{router.description}}).
|
You're {{router.name}} ({{router.description}}).
|
||||||
You can understand messages sent by users in different languages, and route the request to appropriate agent.
|
You can route the request to appropriate agent. Default language is English(US).
|
||||||
Follow these steps to handle user request:
|
Follow these steps to handle user request:
|
||||||
1. Read the [CONVERSATION] content.
|
1. Read the [CONVERSATION] content.
|
||||||
2. Determine which agent is suitable to handle this conversation. Try to minimize the routing of human service.
|
2. Determine which agent is suitable to handle this conversation. Try to minimize the routing of human service.
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,9 @@ using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace BotSharp.Plugin.ChatHub.Helpers;
|
namespace BotSharp.Plugin.ChatHub.Helpers;
|
||||||
|
|
||||||
public class EventEmitter
|
internal class EventEmitter
|
||||||
{
|
{
|
||||||
public static async Task SendChatEvent<T>(
|
internal static async Task SendChatEvent<T>(
|
||||||
IServiceProvider services,
|
IServiceProvider services,
|
||||||
ILogger logger,
|
ILogger logger,
|
||||||
string @event,
|
string @event,
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,32 @@ public class ChatHubObserver : BotSharpObserverBase<HubObserveData<RoleDialogMod
|
||||||
_logger.LogCritical($"Receiving {value.EventName} ({value.Data.Indication}) in {nameof(ChatHubObserver)} - {conv.ConversationId}");
|
_logger.LogCritical($"Receiving {value.EventName} ({value.Data.Indication}) in {nameof(ChatHubObserver)} - {conv.ConversationId}");
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
|
case ChatEvent.OnIntermediateMessageReceivedFromAssistant:
|
||||||
|
if (!AllowSendingMessage()) return;
|
||||||
|
|
||||||
|
model = new ChatResponseDto
|
||||||
|
{
|
||||||
|
ConversationId = conv.ConversationId,
|
||||||
|
MessageId = message.MessageId,
|
||||||
|
MessageLabel = message.MessageLabel,
|
||||||
|
Text = !string.IsNullOrEmpty(message.SecondaryContent) ? message.SecondaryContent : message.Content,
|
||||||
|
Function = message.FunctionName,
|
||||||
|
RichContent = message.SecondaryRichContent ?? message.RichContent,
|
||||||
|
Data = message.Data,
|
||||||
|
Sender = new()
|
||||||
|
{
|
||||||
|
FirstName = "AI",
|
||||||
|
LastName = "Assistant",
|
||||||
|
Role = AgentRole.Assistant
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (value.SaveDataToDb)
|
||||||
|
{
|
||||||
|
var storage = _services.GetRequiredService<IConversationStorage>();
|
||||||
|
storage.Append(conv.ConversationId, message);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
SendEvent(value.EventName, model.ConversationId, model);
|
SendEvent(value.EventName, model.ConversationId, model);
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,6 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
|
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
|
||||||
<ProjectReference Include="..\BotSharp.Plugin.SqlDriver\BotSharp.Plugin.SqlDriver.csproj" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
using BotSharp.Plugin.ExcelHandler.Settings;
|
|
||||||
using NPOI.SS.UserModel;
|
using NPOI.SS.UserModel;
|
||||||
using NPOI.XSSF.UserModel;
|
using NPOI.XSSF.UserModel;
|
||||||
using System.Linq.Dynamic.Core;
|
using System.Linq.Dynamic.Core;
|
||||||
|
|
@ -32,7 +31,7 @@ public class ReadExcelFn : IFunctionCallback
|
||||||
_options = options;
|
_options = options;
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
_fileStorage = fileStorage;
|
_fileStorage = fileStorage;
|
||||||
_dbService = dbServices.FirstOrDefault(x => x.Provider == _settings.DbProvider);
|
_dbService = dbServices.FirstOrDefault(x => x.Provider == _settings.Database?.Provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> Execute(RoleDialogModel message)
|
public async Task<bool> Execute(RoleDialogModel message)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
using System.Data;
|
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using BotSharp.Plugin.SqlDriver.Settings;
|
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using NPOI.SS.UserModel;
|
using NPOI.SS.UserModel;
|
||||||
|
using System.Data;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace BotSharp.Plugin.ExcelHandler.Services;
|
namespace BotSharp.Plugin.ExcelHandler.Services;
|
||||||
|
|
||||||
|
|
@ -11,6 +10,7 @@ public class MySqlService : IDbService
|
||||||
{
|
{
|
||||||
private readonly IServiceProvider _services;
|
private readonly IServiceProvider _services;
|
||||||
private readonly ILogger<MySqlService> _logger;
|
private readonly ILogger<MySqlService> _logger;
|
||||||
|
private readonly ExcelHandlerSettings _settings;
|
||||||
|
|
||||||
private string _mysqlConnection = "";
|
private string _mysqlConnection = "";
|
||||||
private double _excelRowSize = 0;
|
private double _excelRowSize = 0;
|
||||||
|
|
@ -23,10 +23,12 @@ public class MySqlService : IDbService
|
||||||
|
|
||||||
public MySqlService(
|
public MySqlService(
|
||||||
IServiceProvider services,
|
IServiceProvider services,
|
||||||
ILogger<MySqlService> logger)
|
ILogger<MySqlService> logger,
|
||||||
|
ExcelHandlerSettings settings)
|
||||||
{
|
{
|
||||||
_services = services;
|
_services = services;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_settings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Provider => "mysql";
|
public string Provider => "mysql";
|
||||||
|
|
@ -250,8 +252,7 @@ public class MySqlService : IDbService
|
||||||
|
|
||||||
private void InitializeDatabase()
|
private void InitializeDatabase()
|
||||||
{
|
{
|
||||||
var sqlSettings = _services.GetRequiredService<SqlDriverSetting>();
|
_mysqlConnection = _settings.Database.ConnectionString;
|
||||||
_mysqlConnection = sqlSettings.MySqlTempConnectionString;
|
|
||||||
var databaseName = GetDatabaseName(_mysqlConnection);
|
var databaseName = GetDatabaseName(_mysqlConnection);
|
||||||
_logger.LogInformation($"Connected to MySQL database {databaseName}");
|
_logger.LogInformation($"Connected to MySQL database {databaseName}");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
using BotSharp.Plugin.SqlDriver.Settings;
|
|
||||||
using Microsoft.Data.Sqlite;
|
using Microsoft.Data.Sqlite;
|
||||||
using NPOI.SS.UserModel;
|
using NPOI.SS.UserModel;
|
||||||
|
|
||||||
|
|
@ -8,6 +7,7 @@ public class SqliteService : IDbService
|
||||||
{
|
{
|
||||||
private readonly IServiceProvider _services;
|
private readonly IServiceProvider _services;
|
||||||
private readonly ILogger<SqliteService> _logger;
|
private readonly ILogger<SqliteService> _logger;
|
||||||
|
private readonly ExcelHandlerSettings _settings;
|
||||||
|
|
||||||
private string _dbFilePath = string.Empty;
|
private string _dbFilePath = string.Empty;
|
||||||
private SqliteConnection _inMemoryDbConnection = null;
|
private SqliteConnection _inMemoryDbConnection = null;
|
||||||
|
|
@ -20,10 +20,12 @@ public class SqliteService : IDbService
|
||||||
|
|
||||||
public SqliteService(
|
public SqliteService(
|
||||||
IServiceProvider services,
|
IServiceProvider services,
|
||||||
ILogger<SqliteService> logger)
|
ILogger<SqliteService> logger,
|
||||||
|
ExcelHandlerSettings settings)
|
||||||
{
|
{
|
||||||
_services = services;
|
_services = services;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_settings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Provider => "sqlite";
|
public string Provider => "sqlite";
|
||||||
|
|
@ -251,8 +253,7 @@ public class SqliteService : IDbService
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(_dbFilePath))
|
if (string.IsNullOrEmpty(_dbFilePath))
|
||||||
{
|
{
|
||||||
var sqlSettings = _services.GetRequiredService<SqlDriverSetting>();
|
_dbFilePath = _settings.Database.ConnectionString;
|
||||||
_dbFilePath = sqlSettings.SqlLiteConnectionString;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var dbConnection = new SqliteConnection($"Data Source={_dbFilePath};Mode=ReadWrite");
|
var dbConnection = new SqliteConnection($"Data Source={_dbFilePath};Mode=ReadWrite");
|
||||||
|
|
|
||||||
|
|
@ -2,5 +2,11 @@ namespace BotSharp.Plugin.ExcelHandler.Settings;
|
||||||
|
|
||||||
public class ExcelHandlerSettings
|
public class ExcelHandlerSettings
|
||||||
{
|
{
|
||||||
public string DbProvider { get; set; } = "mysql";
|
public DatabaseSettings Database { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DatabaseSettings
|
||||||
|
{
|
||||||
|
public string Provider { get; set; } = "mysql";
|
||||||
|
public string ConnectionString { get; set; }
|
||||||
}
|
}
|
||||||
|
|
@ -26,6 +26,7 @@ global using BotSharp.Plugin.ExcelHandler.Enums;
|
||||||
global using BotSharp.Plugin.ExcelHandler.LlmContexts;
|
global using BotSharp.Plugin.ExcelHandler.LlmContexts;
|
||||||
global using BotSharp.Plugin.ExcelHandler.Models;
|
global using BotSharp.Plugin.ExcelHandler.Models;
|
||||||
global using BotSharp.Plugin.ExcelHandler.Services;
|
global using BotSharp.Plugin.ExcelHandler.Services;
|
||||||
|
global using BotSharp.Plugin.ExcelHandler.Settings;
|
||||||
|
|
||||||
global using Microsoft.Extensions.Logging;
|
global using Microsoft.Extensions.Logging;
|
||||||
global using Microsoft.Extensions.DependencyInjection;
|
global using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,9 @@ public class SqlDriverSetting
|
||||||
public string DatabaseType { get; set; } = "mysql";
|
public string DatabaseType { get; set; } = "mysql";
|
||||||
public string MySqlConnectionString { get; set; } = null!;
|
public string MySqlConnectionString { get; set; } = null!;
|
||||||
public string MySqlExecutionConnectionString { get; set; } = null!;
|
public string MySqlExecutionConnectionString { get; set; } = null!;
|
||||||
public string MySqlTempConnectionString { get; set; } = null!;
|
|
||||||
public string MySqlMetaConnectionString { get; set; } = null!;
|
public string MySqlMetaConnectionString { get; set; } = null!;
|
||||||
public string SqlServerConnectionString { get; set; } = null!;
|
public string SqlServerConnectionString { get; set; } = null!;
|
||||||
public string SqlServerExecutionConnectionString { get; set; } = null!;
|
public string SqlServerExecutionConnectionString { get; set; } = null!;
|
||||||
public string SqlLiteConnectionString { get; set; } = null!;
|
|
||||||
public string RedshiftConnectionString { get; set; } = null!;
|
public string RedshiftConnectionString { get; set; } = null!;
|
||||||
public bool ExecuteSqlSelectAutonomous { get; set; } = false;
|
public bool ExecuteSqlSelectAutonomous { get; set; } = false;
|
||||||
public bool FormattingResult { get; set; } = true;
|
public bool FormattingResult { get; set; } = true;
|
||||||
|
|
|
||||||
|
|
@ -472,7 +472,10 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
"ExcelHandler": {
|
"ExcelHandler": {
|
||||||
"DbProvider": "mysql"
|
"Database": {
|
||||||
|
"Provider": "mysql",
|
||||||
|
"ConnectionString": ""
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
"HttpHandler": {
|
"HttpHandler": {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue