Skip to content

Features/refine file select #581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 8, 2024
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 @@ -4,21 +4,27 @@ namespace BotSharp.Abstraction.Files;

public interface IFileStorageService
{
#region Common
string GetDirectory(string conversationId);
byte[] GetFileBytes(string fileStorageUrl);
bool SaveFileStreamToPath(string filePath, Stream stream);
bool SaveFileBytesToPath(string filePath, byte[] bytes);
string GetParentDir(string dir, int level = 1);
bool ExistDirectory(string? dir);
void CreateDirectory(string dir);
void DeleteDirectory(string dir);
string BuildDirectory(params string[] segments);
#endregion


#region Conversation
/// <summary>
/// Get the files that have been uploaded in the chat.
/// If includeScreenShot is true, it will take the screenshots of non-image files, such as pdf, and return the screenshots instead of the original file.
/// Get the message file screenshots for specific content types, e.g., pdf
/// </summary>
/// <param name="conversationId"></param>
/// <param name="source"></param>
/// <param name="dialogs"></param>
/// <param name="contentTypes"></param>
/// <param name="includeScreenShot"></param>
/// <param name="offset"></param>
/// <param name="messageIds"></param>
/// <returns></returns>
Task<IEnumerable<MessageFileModel>> GetChatFiles(string conversationId, string source,
IEnumerable<RoleDialogModel> dialogs, IEnumerable<string>? contentTypes,
bool includeScreenShot = false, int? offset = null);
Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshots(string conversationId, IEnumerable<string> messageIds);

/// <summary>
/// Get the files that have been uploaded in the chat. No screenshot images are included.
Expand All @@ -45,20 +51,9 @@ Task<IEnumerable<MessageFileModel>> GetChatFiles(string conversationId, string s
bool DeleteConversationFiles(IEnumerable<string> conversationIds);
#endregion


#region User
string GetUserAvatar();
bool SaveUserAvatar(BotSharpFile file);
#endregion

#region Common
string GetDirectory(string conversationId);
byte[] GetFileBytes(string filePath);
bool SaveFileStreamToPath(string filePath, Stream stream);
bool SaveFileBytesToPath(string filePath, byte[] bytes);
string GetParentDir(string dir, int level = 1);
bool ExistDirectory(string? dir);
void CreateDirectory(string dir);
void DeleteDirectory(string dir);
string BuildDirectory(params string[] segments);
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class FileBase
/// <summary>
/// File extension without dot
/// </summary>
[JsonPropertyName("file_type")]
[JsonPropertyName("file_extension")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? FileType { get; set; } = string.Empty;
public string? FileExtension { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public MessageFileModel()

public override string ToString()
{
return $"File name: {FileName}, File type: {FileType}, Content type: {ContentType}, Source: {FileSource}";
return $"File name: {FileName}, File extension: {FileExtension}, Content type: {ContentType}, Source: {FileSource}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,48 @@ namespace BotSharp.Abstraction.Files.Models;

public class SelectFileOptions
{
/// <summary>
/// Llm provider
/// </summary>
public string? Provider { get; set; }

/// <summary>
/// Llm model id
/// </summary>
public string? ModelId { get; set; }

/// <summary>
/// Agent id
/// </summary>
public string? AgentId { get; set; }

/// <summary>
/// Template (prompt) name
/// </summary>
public string? Template { get; set; }

/// <summary>
/// Description that user provides to select files
/// </summary>
public string? Description { get; set; }

/// <summary>
/// Whether include bot generated files
/// </summary>
public bool IncludeBotFile { get; set; }

/// <summary>
/// Conversation breakpoint
/// </summary>
public bool FromBreakpoint { get; set; }

/// <summary>
/// Message offset from last
/// </summary>
public int? Offset { get; set; }

/// <summary>
/// File content types. If null, all types of files will be retrived
/// </summary>
public IEnumerable<string>? ContentTypes { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using BotSharp.Abstraction.Repositories.Enums;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using System.Net.Http;
using System.Net.Mime;

namespace BotSharp.Abstraction.Files.Utilities;

Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<IEnumerable<MessageFileModel>> SelectMessageFiles(string conve
if (options.IncludeBotFile)
{
var botFiles = _fileStorage.GetMessageFiles(conversationId, messageIds, FileSourceType.Bot, options.ContentTypes);
files = files.Concat(botFiles);
files = MergeMessageFiles(messageIds, files, botFiles);
}

if (files.IsNullOrEmpty())
Expand All @@ -31,6 +31,24 @@ public async Task<IEnumerable<MessageFileModel>> SelectMessageFiles(string conve
return await SelectFiles(files, dialogs, options);
}

private IEnumerable<MessageFileModel> MergeMessageFiles(IEnumerable<string> messageIds, IEnumerable<MessageFileModel> userFiles, IEnumerable<MessageFileModel> botFiles)
{
var files = new List<MessageFileModel>();

if (messageIds.IsNullOrEmpty()) return files;

foreach (var messageId in messageIds)
{
var users = userFiles.Where(x => x.MessageId == messageId).ToList();
var bots = botFiles.Where(x => x.MessageId == messageId).ToList();

if (!users.IsNullOrEmpty()) files.AddRange(users);
if (!bots.IsNullOrEmpty()) files.AddRange(bots);
}

return files;
}

private async Task<IEnumerable<MessageFileModel>> SelectFiles(IEnumerable<MessageFileModel> files, IEnumerable<RoleDialogModel> dialogs, SelectFileOptions options)
{
if (files.IsNullOrEmpty()) return new List<MessageFileModel>();
Expand All @@ -43,7 +61,7 @@ private async Task<IEnumerable<MessageFileModel>> SelectFiles(IEnumerable<Messag
{
var promptFiles = files.Select((x, idx) =>
{
return $"id: {idx + 1}, file_name: {x.FileName}.{x.FileType}, content_type: {x.ContentType}, author: {x.FileSource}";
return $"id: {idx + 1}, file_name: {x.FileName}.{x.FileExtension}, content_type: {x.ContentType}, author: {x.FileSource}";
}).ToList();

var agentId = !string.IsNullOrWhiteSpace(options.AgentId) ? options.AgentId : BuiltInAgentId.UtilityAssistant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public string GetDirectory(string conversationId)
return dir;
}

public byte[] GetFileBytes(string filePath)
public byte[] GetFileBytes(string fileStorageUrl)
{
using var stream = File.OpenRead(filePath);
using var stream = File.OpenRead(fileStorageUrl);
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
return bytes;
Expand Down
Loading