Merge pull request #1165 from iceljc/features/refine-model-settings
fix audio and excel
This commit is contained in:
commit
c391051f5e
|
|
@ -1,3 +1,4 @@
|
||||||
|
using BotSharp.Abstraction.Routing;
|
||||||
using BotSharp.Core.Infrastructures;
|
using BotSharp.Core.Infrastructures;
|
||||||
using Microsoft.AspNetCore.StaticFiles;
|
using Microsoft.AspNetCore.StaticFiles;
|
||||||
|
|
||||||
|
|
@ -35,12 +36,18 @@ public class HandleAudioRequestFn : IFunctionCallback
|
||||||
{
|
{
|
||||||
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions);
|
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions);
|
||||||
var conv = _serviceProvider.GetRequiredService<IConversationService>();
|
var conv = _serviceProvider.GetRequiredService<IConversationService>();
|
||||||
|
var routingCtx = _serviceProvider.GetRequiredService<IRoutingContext>();
|
||||||
|
|
||||||
|
var wholeDialogs = routingCtx.GetDialogs();
|
||||||
|
if (wholeDialogs.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
wholeDialogs = conv.GetDialogHistory();
|
||||||
|
}
|
||||||
|
|
||||||
var wholeDialogs = conv.GetDialogHistory();
|
|
||||||
var dialogs = AssembleFiles(conv.ConversationId, wholeDialogs);
|
var dialogs = AssembleFiles(conv.ConversationId, wholeDialogs);
|
||||||
|
|
||||||
var response = await GetResponeFromDialogs(dialogs);
|
var response = await GetResponeFromDialogs(dialogs);
|
||||||
message.Content = response;
|
message.Content = response;
|
||||||
|
dialogs.ForEach(x => x.Files = null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,18 +59,23 @@ public class HandleAudioRequestFn : IFunctionCallback
|
||||||
}
|
}
|
||||||
|
|
||||||
var messageId = dialogs.Select(x => x.MessageId).Distinct().ToList();
|
var messageId = dialogs.Select(x => x.MessageId).Distinct().ToList();
|
||||||
var audioMessageFiles = _fileStorage.GetMessageFiles(convId, messageId, options: new()
|
var audioFiles = _fileStorage.GetMessageFiles(convId, messageId, options: new()
|
||||||
{
|
{
|
||||||
Sources = [FileSource.User],
|
Sources = [FileSource.User],
|
||||||
ContentTypes = _audioContentTypes
|
ContentTypes = _audioContentTypes
|
||||||
});
|
});
|
||||||
|
|
||||||
audioMessageFiles = audioMessageFiles.Where(x => x.ContentType.Contains("audio")).ToList();
|
audioFiles = audioFiles.Where(x => x.ContentType.Contains("audio")).ToList();
|
||||||
|
|
||||||
foreach (var dialog in dialogs)
|
foreach (var dialog in dialogs)
|
||||||
{
|
{
|
||||||
var found = audioMessageFiles.Where(x => x.MessageId == dialog.MessageId).ToList();
|
var found = audioFiles.Where(x => x.MessageId == dialog.MessageId
|
||||||
if (found.IsNullOrEmpty()) continue;
|
&& x.FileSource.IsEqualTo(FileSource.User)).ToList();
|
||||||
|
|
||||||
|
if (found.IsNullOrEmpty() || !dialog.IsFromUser)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
dialog.Files = found.Select(x => new BotSharpFile
|
dialog.Files = found.Select(x => new BotSharpFile
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
using System.Linq.Dynamic.Core;
|
|
||||||
using BotSharp.Abstraction.Files.Enums;
|
using BotSharp.Abstraction.Files.Enums;
|
||||||
using BotSharp.Abstraction.Files.Models;
|
using BotSharp.Abstraction.Files.Models;
|
||||||
using BotSharp.Abstraction.Files.Utilities;
|
using BotSharp.Abstraction.Files.Utilities;
|
||||||
|
using BotSharp.Abstraction.Routing;
|
||||||
using BotSharp.Plugin.ExcelHandler.Models;
|
using BotSharp.Plugin.ExcelHandler.Models;
|
||||||
using BotSharp.Plugin.ExcelHandler.Services;
|
using BotSharp.Plugin.ExcelHandler.Services;
|
||||||
using NPOI.SS.UserModel;
|
using NPOI.SS.UserModel;
|
||||||
using NPOI.XSSF.UserModel;
|
using NPOI.XSSF.UserModel;
|
||||||
|
using System.Linq.Dynamic.Core;
|
||||||
|
|
||||||
namespace BotSharp.Plugin.ExcelHandler.Functions;
|
namespace BotSharp.Plugin.ExcelHandler.Functions;
|
||||||
|
|
||||||
|
|
@ -49,14 +50,20 @@ public class HandleExcelRequestFn : IFunctionCallback
|
||||||
{
|
{
|
||||||
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions);
|
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions);
|
||||||
var conv = _serviceProvider.GetRequiredService<IConversationService>();
|
var conv = _serviceProvider.GetRequiredService<IConversationService>();
|
||||||
|
var states = _serviceProvider.GetRequiredService<IConversationStateService>();
|
||||||
|
var routingCtx = _serviceProvider.GetRequiredService<IRoutingContext>();
|
||||||
|
|
||||||
if (_excelMimeTypes.IsNullOrEmpty())
|
if (_excelMimeTypes.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
_excelMimeTypes = FileUtility.GetMimeFileTypes(new List<string> { "excel", "spreadsheet" }).ToHashSet<string>();
|
_excelMimeTypes = FileUtility.GetMimeFileTypes(new List<string> { "excel", "spreadsheet" }).ToHashSet<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
var dialogs = conv.GetDialogHistory();
|
var dialogs = routingCtx.GetDialogs();
|
||||||
|
if (dialogs.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
dialogs = conv.GetDialogHistory();
|
||||||
|
}
|
||||||
|
|
||||||
var isExcelExist = AssembleFiles(conv.ConversationId, dialogs);
|
var isExcelExist = AssembleFiles(conv.ConversationId, dialogs);
|
||||||
if (!isExcelExist)
|
if (!isExcelExist)
|
||||||
{
|
{
|
||||||
|
|
@ -65,30 +72,43 @@ public class HandleExcelRequestFn : IFunctionCallback
|
||||||
}
|
}
|
||||||
|
|
||||||
var resultList = GetResponeFromDialogs(dialogs);
|
var resultList = GetResponeFromDialogs(dialogs);
|
||||||
var states = _serviceProvider.GetRequiredService<IConversationStateService>();
|
|
||||||
|
|
||||||
message.Content = GenerateSqlExecutionSummary(resultList);
|
message.Content = GenerateSqlExecutionSummary(resultList);
|
||||||
states.SetState("excel_import_result",message.Content);
|
states.SetState("excel_import_result",message.Content);
|
||||||
|
dialogs.ForEach(x => x.Files = null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#region Private Methods
|
#region Private Methods
|
||||||
private bool AssembleFiles(string convId, List<RoleDialogModel> dialogs)
|
private bool AssembleFiles(string conversationId, List<RoleDialogModel> dialogs)
|
||||||
{
|
{
|
||||||
if (dialogs.IsNullOrEmpty()) return false;
|
if (dialogs.IsNullOrEmpty())
|
||||||
|
|
||||||
var messageId = dialogs.Select(x => x.MessageId).Distinct().ToList();
|
|
||||||
var contentType = FileUtility.GetContentFileTypes(mimeTypes: _excelMimeTypes);
|
|
||||||
var excelMessageFiles = _fileStorage.GetMessageFiles(convId, messageId, FileSourceType.User, contentType);
|
|
||||||
|
|
||||||
if (excelMessageFiles.IsNullOrEmpty()) return false;
|
|
||||||
|
|
||||||
dialogs.ForEach(dialog =>
|
|
||||||
{
|
{
|
||||||
var found = excelMessageFiles.Where(y => y.MessageId == dialog.MessageId).ToList();
|
return false;
|
||||||
if (found.IsNullOrEmpty()) return;
|
}
|
||||||
|
|
||||||
|
var messageIds = dialogs.Select(x => x.MessageId).Distinct().ToList();
|
||||||
|
var contentTypes = FileUtility.GetContentFileTypes(mimeTypes: _excelMimeTypes);
|
||||||
|
var excelFiles = _fileStorage.GetMessageFiles(conversationId, messageIds, options: new()
|
||||||
|
{
|
||||||
|
Sources = [FileSource.User],
|
||||||
|
ContentTypes = contentTypes
|
||||||
|
});
|
||||||
|
|
||||||
|
if (excelFiles.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var dialog in dialogs)
|
||||||
|
{
|
||||||
|
var found = excelFiles.Where(x => x.MessageId == dialog.MessageId
|
||||||
|
&& x.FileSource.IsEqualTo(FileSource.User)).ToList();
|
||||||
|
|
||||||
|
if (found.IsNullOrEmpty() || !dialog.IsFromUser)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
dialog.Files = found.Select(x => new BotSharpFile
|
dialog.Files = found.Select(x => new BotSharpFile
|
||||||
{
|
{
|
||||||
|
|
@ -96,7 +116,8 @@ public class HandleExcelRequestFn : IFunctionCallback
|
||||||
FileUrl = x.FileUrl,
|
FileUrl = x.FileUrl,
|
||||||
FileStorageUrl = x.FileStorageUrl
|
FileStorageUrl = x.FileStorageUrl
|
||||||
}).ToList();
|
}).ToList();
|
||||||
});
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -82,11 +82,11 @@ public class ReadImageFn : IFunctionCallback
|
||||||
if (found.IsNullOrEmpty()) continue;
|
if (found.IsNullOrEmpty()) continue;
|
||||||
|
|
||||||
var targets = found;
|
var targets = found;
|
||||||
if (dialog.Role == AgentRole.User)
|
if (dialog.IsFromUser)
|
||||||
{
|
{
|
||||||
targets = found.Where(x => x.FileSource.IsEqualTo(FileSource.User)).ToList();
|
targets = found.Where(x => x.FileSource.IsEqualTo(FileSource.User)).ToList();
|
||||||
}
|
}
|
||||||
else if (dialog.Role == AgentRole.Assistant || dialog.Role == AgentRole.Model)
|
else if (dialog.IsFromAssistant)
|
||||||
{
|
{
|
||||||
targets = found.Where(x => x.FileSource.IsEqualTo(FileSource.Bot)).ToList();
|
targets = found.Where(x => x.FileSource.IsEqualTo(FileSource.Bot)).ToList();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue