use select file option

This commit is contained in:
Jicheng Lu 2024-08-07 19:26:22 -05:00
parent a92c76104e
commit ef0468e213
5 changed files with 46 additions and 28 deletions

View file

@ -21,9 +21,6 @@ public interface IFileInstructService
#endregion
#region Select file
Task<IEnumerable<MessageFileModel>> SelectMessageFiles(string conversationId,
string? agentId = null, string? template = null, string? description = null,
bool includeBotFile = false, bool fromBreakpoint = false,
int? offset = null, IEnumerable<string>? contentTypes = null);
Task<IEnumerable<MessageFileModel>> SelectMessageFiles(string conversationId, SelectFileOptions options);
#endregion
}

View file

@ -0,0 +1,14 @@
namespace BotSharp.Abstraction.Files.Models;
public class SelectFileOptions
{
public string? Provider { get; set; }
public string? ModelId { get; set; }
public string? AgentId { get; set; }
public string? Template { get; set; }
public string? Description { get; set; }
public bool IncludeBotFile { get; set; }
public bool FromBreakpoint { get; set; }
public int? Offset { get; set; }
public IEnumerable<string>? ContentTypes { get; set; }
}

View file

@ -5,10 +5,7 @@ namespace BotSharp.Core.Files.Services;
public partial class FileInstructService
{
public async Task<IEnumerable<MessageFileModel>> SelectMessageFiles(string conversationId,
string? agentId = null, string? template = null, string? description = null,
bool includeBotFile = false, bool fromBreakpoint = false,
int? offset = null, IEnumerable<string>? contentTypes = null)
public async Task<IEnumerable<MessageFileModel>> SelectMessageFiles(string conversationId, SelectFileOptions options)
{
if (string.IsNullOrEmpty(conversationId))
{
@ -16,13 +13,13 @@ public partial class FileInstructService
}
var convService = _services.GetRequiredService<IConversationService>();
var dialogs = convService.GetDialogHistory(fromBreakpoint: fromBreakpoint);
var messageIds = GetMessageIds(dialogs, offset);
var dialogs = convService.GetDialogHistory(fromBreakpoint: options.FromBreakpoint);
var messageIds = GetMessageIds(dialogs, options.Offset);
var files = _fileBasic.GetMessageFiles(conversationId, messageIds, FileSourceType.User, contentTypes);
if (includeBotFile)
var files = _fileBasic.GetMessageFiles(conversationId, messageIds, FileSourceType.User, options.ContentTypes);
if (options.IncludeBotFile)
{
var botFiles = _fileBasic.GetMessageFiles(conversationId, messageIds, FileSourceType.Bot, contentTypes);
var botFiles = _fileBasic.GetMessageFiles(conversationId, messageIds, FileSourceType.Bot, options.ContentTypes);
files = files.Concat(botFiles);
}
@ -31,11 +28,10 @@ public partial class FileInstructService
return Enumerable.Empty<MessageFileModel>();
}
return await SelectFiles(agentId, template, description, files, dialogs);
return await SelectFiles(files, dialogs, options);
}
private async Task<IEnumerable<MessageFileModel>> SelectFiles(string? agentId, string? template, string? description,
IEnumerable<MessageFileModel> files, List<RoleDialogModel> dialogs)
private async Task<IEnumerable<MessageFileModel>> SelectFiles(IEnumerable<MessageFileModel> files, IEnumerable<RoleDialogModel> dialogs, SelectFileOptions options)
{
if (files.IsNullOrEmpty()) return new List<MessageFileModel>();
@ -50,8 +46,8 @@ public partial class FileInstructService
return $"id: {idx + 1}, file_name: {x.FileName}.{x.FileType}, content_type: {x.ContentType}, author: {x.FileSource}";
}).ToList();
agentId = !string.IsNullOrWhiteSpace(agentId) ? agentId : BuiltInAgentId.UtilityAssistant;
template = !string.IsNullOrWhiteSpace(template) ? template : "select_file_prompt";
var agentId = !string.IsNullOrWhiteSpace(options.AgentId) ? options.AgentId : BuiltInAgentId.UtilityAssistant;
var template = !string.IsNullOrWhiteSpace(options.Template) ? options.Template : "select_file_prompt";
var foundAgent = db.GetAgent(agentId);
var prompt = db.GetAgentTemplate(BuiltInAgentId.UtilityAssistant, template);
@ -67,16 +63,23 @@ public partial class FileInstructService
Instruction = prompt
};
var provider = llmProviderService.GetProviders().FirstOrDefault(x => x == "openai");
var model = llmProviderService.GetProviderModel(provider: provider, id: "gpt-4");
var message = dialogs.LastOrDefault();
var text = !string.IsNullOrWhiteSpace(options.Description) ? options.Description : message?.Content;
if (message == null)
{
message = new RoleDialogModel(AgentRole.User, text);
}
else
{
message = RoleDialogModel.From(message, AgentRole.User, text);
}
var providerName = options.Provider ?? "openai";
var modelId = options?.ModelId ?? "gpt-4";
var provider = llmProviderService.GetProviders().FirstOrDefault(x => x == providerName);
var model = llmProviderService.GetProviderModel(provider: provider, id: modelId);
var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model.Name);
var message = dialogs.Last();
if (!string.IsNullOrWhiteSpace(description))
{
message = RoleDialogModel.From(message, AgentRole.User, description);
}
var response = await completion.GetChatCompletions(agent, new List<RoleDialogModel> { message });
var content = response?.Content ?? string.Empty;
var selecteds = JsonSerializer.Deserialize<FileSelectContext>(content);

View file

@ -77,7 +77,7 @@ public class HandleEmailSenderFn : IFunctionCallback
var conversationId = convService.ConversationId;
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
var selecteds = await fileInstruct.SelectMessageFiles(conversationId, includeBotFile: true);
var selecteds = await fileInstruct.SelectMessageFiles(conversationId, new SelectFileOptions { IncludeBotFile = true });
return selecteds;
}

View file

@ -51,7 +51,11 @@ public class EditImageFn : IFunctionCallback
private async Task<MessageFileModel?> SelectImage(string? description)
{
var fileInstruct = _services.GetRequiredService<IFileInstructService>();
var selecteds = await fileInstruct.SelectMessageFiles(_conversationId, description: description, contentTypes: new List<string> { MediaTypeNames.Image.Png });
var selecteds = await fileInstruct.SelectMessageFiles(_conversationId, new SelectFileOptions
{
Description = description,
ContentTypes = new List<string> { MediaTypeNames.Image.Png }
});
return selecteds?.FirstOrDefault();
}