Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -5,6 +5,7 @@ public static class ChatEvent
public const string OnConversationInitFromClient = nameof(OnConversationInitFromClient);
public const string OnMessageReceivedFromClient = nameof(OnMessageReceivedFromClient);
public const string OnMessageReceivedFromAssistant = nameof(OnMessageReceivedFromAssistant);
public const string OnIntermediateMessageReceivedFromAssistant = nameof(OnIntermediateMessageReceivedFromAssistant);

public const string OnMessageDeleted = nameof(OnMessageDeleted);
public const string OnNotificationGenerated = nameof(OnNotificationGenerated);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@ public class FileSelectItem

[JsonPropertyName("file_source")]
public string FileSource { get; set; }

[JsonPropertyName("file_name")]
public string? FileName { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@ namespace BotSharp.Abstraction.MessageHub.Models;

public class HubObserveData<TData> : ObserveDataBase where TData : class, new()
{
/// <summary>
/// The observed data
/// </summary>
public TData Data { get; set; } = null!;

/// <summary>
/// Whether to save the observed data To Db
/// </summary>
public bool SaveDataToDb { get; set; }
}
11 changes: 11 additions & 0 deletions src/Infrastructure/BotSharp.Core/Demo/Functions/GetWeatherFn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public async Task<bool> Execute(RoleDialogModel message)

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}";
messageHub.Push(new()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BotSharp.Abstraction.Instructs.Models;
using BotSharp.Abstraction.Instructs;
using BotSharp.Abstraction.Files.Converters;

namespace BotSharp.Core.Files.Services;

Expand All @@ -22,13 +23,16 @@ public async Task<string> ReadPdf(string text, List<InstructFileModel> files, In
{
var provider = options?.Provider ?? "openai";
var pdfFiles = await DownloadAndSaveFiles(sessionDir, files);

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())
{
return content;
Expand Down Expand Up @@ -115,15 +119,12 @@ private async Task<IEnumerable<string>> DownloadAndSaveFiles(string dir, List<In
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 settings = _services.GetRequiredService<FileCoreSettings>();

var converter = GetImageConverter(options?.ImageConvertProvider);
if (converter == null || files.IsNullOrEmpty())
{
return images;
return files;
}

foreach (var file in files)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ private string BuildFileName(string? name, string? extension, string defaultName

private IImageConverter? GetImageConverter(string? provider)
{
var settings = _services.GetRequiredService<FileCoreSettings>();
var convertProvider = provider ?? settings?.ImageConverter?.Provider;
var converter = _services.GetServices<IImageConverter>().FirstOrDefault(x => x.Provider == convertProvider);
var converter = _services.GetServices<IImageConverter>().FirstOrDefault(x => x.Provider == provider);
return converter;
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,6 @@ private async Task<string> GetAiResponse(Agent agent, string templateName)
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;
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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:
1. Read the [CONVERSATION] content.
2. Determine which agent is suitable to handle this conversation. Try to minimize the routing of human service.
Expand Down
4 changes: 2 additions & 2 deletions src/Plugins/BotSharp.Plugin.ChatHub/Helpers/EventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

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,
ILogger logger,
string @event,
Expand Down
26 changes: 26 additions & 0 deletions src/Plugins/BotSharp.Plugin.ChatHub/Observers/ChatHubObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,32 @@ public override void OnNext(HubObserveData<RoleDialogModel> value)
_logger.LogCritical($"Receiving {value.EventName} ({value.Data.Indication}) in {nameof(ChatHubObserver)} - {conv.ConversationId}");
#endif
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
<ProjectReference Include="..\BotSharp.Plugin.SqlDriver\BotSharp.Plugin.SqlDriver.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using BotSharp.Plugin.ExcelHandler.Settings;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Linq.Dynamic.Core;
Expand Down Expand Up @@ -32,7 +31,7 @@ public ReadExcelFn(
_options = options;
_settings = settings;
_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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System.Data;
using System.Text.RegularExpressions;
using BotSharp.Plugin.SqlDriver.Settings;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using NPOI.SS.UserModel;
using System.Data;
using System.Text.RegularExpressions;

namespace BotSharp.Plugin.ExcelHandler.Services;

public class MySqlService : IDbService
{
private readonly IServiceProvider _services;
private readonly ILogger<MySqlService> _logger;
private readonly ExcelHandlerSettings _settings;

private string _mysqlConnection = "";
private double _excelRowSize = 0;
Expand All @@ -23,10 +23,12 @@ public class MySqlService : IDbService

public MySqlService(
IServiceProvider services,
ILogger<MySqlService> logger)
ILogger<MySqlService> logger,
ExcelHandlerSettings settings)
{
_services = services;
_logger = logger;
_settings = settings;
}

public string Provider => "mysql";
Expand Down Expand Up @@ -250,8 +252,7 @@ private MySqlConnection GetDbConnection()

private void InitializeDatabase()
{
var sqlSettings = _services.GetRequiredService<SqlDriverSetting>();
_mysqlConnection = sqlSettings.MySqlTempConnectionString;
_mysqlConnection = _settings.Database.ConnectionString;
var databaseName = GetDatabaseName(_mysqlConnection);
_logger.LogInformation($"Connected to MySQL database {databaseName}");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using BotSharp.Plugin.SqlDriver.Settings;
using Microsoft.Data.Sqlite;
using NPOI.SS.UserModel;

Expand All @@ -8,6 +7,7 @@ public class SqliteService : IDbService
{
private readonly IServiceProvider _services;
private readonly ILogger<SqliteService> _logger;
private readonly ExcelHandlerSettings _settings;

private string _dbFilePath = string.Empty;
private SqliteConnection _inMemoryDbConnection = null;
Expand All @@ -20,10 +20,12 @@ public class SqliteService : IDbService

public SqliteService(
IServiceProvider services,
ILogger<SqliteService> logger)
ILogger<SqliteService> logger,
ExcelHandlerSettings settings)
{
_services = services;
_logger = logger;
_settings = settings;
}

public string Provider => "sqlite";
Expand Down Expand Up @@ -251,8 +253,7 @@ private SqliteConnection GetPhysicalDbConnection()
{
if (string.IsNullOrEmpty(_dbFilePath))
{
var sqlSettings = _services.GetRequiredService<SqlDriverSetting>();
_dbFilePath = sqlSettings.SqlLiteConnectionString;
_dbFilePath = _settings.Database.ConnectionString;
}

var dbConnection = new SqliteConnection($"Data Source={_dbFilePath};Mode=ReadWrite");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@ namespace BotSharp.Plugin.ExcelHandler.Settings;

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; }
}
1 change: 1 addition & 0 deletions src/Plugins/BotSharp.Plugin.ExcelHandler/Using.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
global using BotSharp.Plugin.ExcelHandler.LlmContexts;
global using BotSharp.Plugin.ExcelHandler.Models;
global using BotSharp.Plugin.ExcelHandler.Services;
global using BotSharp.Plugin.ExcelHandler.Settings;

global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.DependencyInjection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ public class SqlDriverSetting
public string DatabaseType { get; set; } = "mysql";
public string MySqlConnectionString { get; set; } = null!;
public string MySqlExecutionConnectionString { get; set; } = null!;
public string MySqlTempConnectionString { get; set; } = null!;
public string MySqlMetaConnectionString { get; set; } = null!;
public string SqlServerConnectionString { get; set; } = null!;
public string SqlServerExecutionConnectionString { get; set; } = null!;
public string SqlLiteConnectionString { get; set; } = null!;
public string RedshiftConnectionString { get; set; } = null!;
public bool ExecuteSqlSelectAutonomous { get; set; } = false;
public bool FormattingResult { get; set; } = true;
Expand Down
5 changes: 4 additions & 1 deletion src/WebStarter/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,10 @@
},

"ExcelHandler": {
"DbProvider": "mysql"
"Database": {
"Provider": "mysql",
"ConnectionString": ""
}
},

"HttpHandler": {
Expand Down
Loading