Skip to content

Commit 997bfc3

Browse files
authored
Merge pull request #616 from iceljc/features/refine-audio-handler
Features/refine audio handler
2 parents 0e156c3 + c2b870b commit 997bfc3

File tree

156 files changed

+2213
-1234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

156 files changed

+2213
-1234
lines changed

BotSharp.sln

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.PythonInter
111111
EndProject
112112
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Graph", "Graph", "{97A0B191-64D7-4F8A-BFE8-1BFCC5E247E1}"
113113
EndProject
114-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.Graph", "src\Plugins\BotSharp.Plugin.Graph\BotSharp.Plugin.Graph.csproj", "{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}"
114+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.Graph", "src\Plugins\BotSharp.Plugin.Graph\BotSharp.Plugin.Graph.csproj", "{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}"
115+
EndProject
116+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.AudioHandler", "src\Plugins\BotSharp.Plugin.AudioHandler\BotSharp.Plugin.AudioHandler.csproj", "{F57F4862-F8D4-44A1-AC12-5C131B5C9785}"
115117
EndProject
116118
Global
117119
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -449,6 +451,14 @@ Global
449451
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}.Release|Any CPU.Build.0 = Release|Any CPU
450452
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}.Release|x64.ActiveCfg = Release|Any CPU
451453
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}.Release|x64.Build.0 = Release|Any CPU
454+
{F57F4862-F8D4-44A1-AC12-5C131B5C9785}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
455+
{F57F4862-F8D4-44A1-AC12-5C131B5C9785}.Debug|Any CPU.Build.0 = Debug|Any CPU
456+
{F57F4862-F8D4-44A1-AC12-5C131B5C9785}.Debug|x64.ActiveCfg = Debug|Any CPU
457+
{F57F4862-F8D4-44A1-AC12-5C131B5C9785}.Debug|x64.Build.0 = Debug|Any CPU
458+
{F57F4862-F8D4-44A1-AC12-5C131B5C9785}.Release|Any CPU.ActiveCfg = Release|Any CPU
459+
{F57F4862-F8D4-44A1-AC12-5C131B5C9785}.Release|Any CPU.Build.0 = Release|Any CPU
460+
{F57F4862-F8D4-44A1-AC12-5C131B5C9785}.Release|x64.ActiveCfg = Release|Any CPU
461+
{F57F4862-F8D4-44A1-AC12-5C131B5C9785}.Release|x64.Build.0 = Release|Any CPU
452462
EndGlobalSection
453463
GlobalSection(SolutionProperties) = preSolution
454464
HideSolutionNode = FALSE
@@ -503,6 +513,7 @@ Global
503513
{05E6E405-5021-406E-8A5E-0A7CEC881F6D} = {C4C59872-3C8A-450D-83D5-2BE402D610D5}
504514
{97A0B191-64D7-4F8A-BFE8-1BFCC5E247E1} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
505515
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D} = {97A0B191-64D7-4F8A-BFE8-1BFCC5E247E1}
516+
{F57F4862-F8D4-44A1-AC12-5C131B5C9785} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
506517
EndGlobalSection
507518
GlobalSection(ExtensibilityGlobals) = postSolution
508519
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}

src/BotSharp.AppHost/BotSharp.AppHost.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Aspire.Hosting.AppHost" Version="8.0.0" />
13+
<PackageReference Include="Aspire.Hosting.AppHost" Version="8.2.0" />
1414
<PackageReference Include="Aspire.Hosting.NodeJs" Version="8.0.1" />
1515
</ItemGroup>
1616

src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>$(TargetFramework)</TargetFramework>

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using BotSharp.Abstraction.Functions.Models;
22
using BotSharp.Abstraction.Messaging;
33
using BotSharp.Abstraction.Messaging.Models.RichContent;
4-
using BotSharp.Abstraction.MLTasks;
54

65
namespace BotSharp.Abstraction.Conversations.Models;
76

@@ -75,6 +74,9 @@ public class RoleDialogModel : ITrackableMessage
7574
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
7675
public RichContent<IRichMessage>? RichContent { get; set; }
7776

77+
/// <summary>
78+
/// Rich content for secondary language
79+
/// </summary>
7880
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
7981
public RichContent<IRichMessage>? SecondaryRichContent { get; set; }
8082

@@ -86,6 +88,9 @@ public class RoleDialogModel : ITrackableMessage
8688

8789
public FunctionCallFromLlm Instruction { get; set; }
8890

91+
/// <summary>
92+
/// Files to be used in conversation
93+
/// </summary>
8994
public List<BotSharpFile> Files { get; set; } = new List<BotSharpFile>();
9095

9196
/// <summary>
@@ -94,7 +99,8 @@ public class RoleDialogModel : ITrackableMessage
9499
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
95100
[JsonPropertyName("generated_images")]
96101
public List<ImageGeneration> GeneratedImages { get; set; } = new List<ImageGeneration>();
97-
public float KnowledgeConfidence { get; set; } = 0.5f;
102+
103+
98104
private RoleDialogModel()
99105
{
100106
}
@@ -134,8 +140,7 @@ public static RoleDialogModel From(RoleDialogModel source,
134140
Payload = source.Payload,
135141
StopCompletion = source.StopCompletion,
136142
Instruction = source.Instruction,
137-
Data = source.Data,
138-
KnowledgeConfidence = source.KnowledgeConfidence
143+
Data = source.Data
139144
};
140145
}
141146
}

src/Infrastructure/BotSharp.Abstraction/Files/Converters/IPdf2ImageConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace BotSharp.Abstraction.Files.Converters;
22

33
public interface IPdf2ImageConverter
44
{
5-
public string Name { get; }
5+
public string Provider { get; }
66

77
/// <summary>
88
/// Convert pdf pages to images, and return a list of image file paths

src/Infrastructure/BotSharp.Abstraction/Files/FileCoreSettings.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ namespace BotSharp.Abstraction.Files;
55
public class FileCoreSettings
66
{
77
public string Storage { get; set; } = FileStorageEnum.LocalFileStorage;
8-
public string Pdf2TextConverter { get; set; }
9-
public string Pdf2ImageConverter { get; set; }
8+
public SettingBase Pdf2TextConverter { get; set; }
9+
public SettingBase Pdf2ImageConverter { get; set; }
1010
}
11+
12+
public class SettingBase
13+
{
14+
public string Provider { get; set; }
15+
}

src/Infrastructure/BotSharp.Abstraction/Files/IFileInstructService.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ namespace BotSharp.Abstraction.Files;
33
public interface IFileInstructService
44
{
55
#region Image
6-
Task<RoleDialogModel> ReadImages(string? provider, string? model, string text, IEnumerable<BotSharpFile> images);
6+
Task<string> ReadImages(string? provider, string? model, string text, IEnumerable<InstructFileModel> images);
77
Task<RoleDialogModel> GenerateImage(string? provider, string? model, string text);
8-
Task<RoleDialogModel> VaryImage(string? provider, string? model, BotSharpFile image);
9-
Task<RoleDialogModel> EditImage(string? provider, string? model, string text, BotSharpFile image);
10-
Task<RoleDialogModel> EditImage(string? provider, string? model, string text, BotSharpFile image, BotSharpFile mask);
8+
Task<RoleDialogModel> VaryImage(string? provider, string? model, InstructFileModel image);
9+
Task<RoleDialogModel> EditImage(string? provider, string? model, string text, InstructFileModel image);
10+
Task<RoleDialogModel> EditImage(string? provider, string? model, string text, InstructFileModel image, InstructFileModel mask);
1111
#endregion
1212

1313
#region Pdf
@@ -17,7 +17,11 @@ public interface IFileInstructService
1717
/// <param name="prompt"></param>
1818
/// <param name="files">Pdf files</param>
1919
/// <returns></returns>
20-
Task<string> ReadPdf(string? provider, string? model, string? modelId, string prompt, List<BotSharpFile> files);
20+
Task<string> ReadPdf(string? provider, string? model, string? modelId, string prompt, List<InstructFileModel> files);
21+
#endregion
22+
23+
#region Audio
24+
Task<string> SpeechToText(string? provider, string? model, InstructFileModel audio, string? text = null);
2125
#endregion
2226

2327
#region Select file

src/Infrastructure/BotSharp.Abstraction/Files/IFileStorageService.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public interface IFileStorageService
66
{
77
#region Common
88
string GetDirectory(string conversationId);
9+
IEnumerable<string> GetFiles(string relativePath, string? searchQuery = null);
910
byte[] GetFileBytes(string fileStorageUrl);
1011
bool SaveFileStreamToPath(string filePath, Stream stream);
1112
bool SaveFileBytesToPath(string filePath, byte[] bytes);
@@ -16,15 +17,14 @@ public interface IFileStorageService
1617
string BuildDirectory(params string[] segments);
1718
#endregion
1819

19-
2020
#region Conversation
2121
/// <summary>
2222
/// Get the message file screenshots for specific content types, e.g., pdf
2323
/// </summary>
2424
/// <param name="conversationId"></param>
2525
/// <param name="messageIds"></param>
2626
/// <returns></returns>
27-
Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshots(string conversationId, IEnumerable<string> messageIds);
27+
Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshotsAsync(string conversationId, IEnumerable<string> messageIds);
2828

2929
/// <summary>
3030
/// Get the files that have been uploaded in the chat. No screenshot images are included.
@@ -37,7 +37,7 @@ public interface IFileStorageService
3737
IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, string source, IEnumerable<string>? contentTypes = null);
3838
string GetMessageFile(string conversationId, string messageId, string source, string index, string fileName);
3939
IEnumerable<MessageFileModel> GetMessagesWithFile(string conversationId, IEnumerable<string> messageIds);
40-
bool SaveMessageFiles(string conversationId, string messageId, string source, List<BotSharpFile> files);
40+
bool SaveMessageFiles(string conversationId, string messageId, string source, List<InputFileModel> files);
4141

4242
/// <summary>
4343
/// Delete files under messages
@@ -54,10 +54,11 @@ public interface IFileStorageService
5454

5555
#region User
5656
string GetUserAvatar();
57-
bool SaveUserAvatar(BotSharpFile file);
57+
bool SaveUserAvatar(InputFileModel file);
5858
#endregion
59+
5960
#region Speech
60-
Task SaveSpeechFileAsync(string conversationId, string fileName, BinaryData data);
61-
Task<BinaryData> RetrieveSpeechFileAsync(string conversationId, string fileName);
61+
bool SaveSpeechFile(string conversationId, string fileName, BinaryData data);
62+
BinaryData GetSpeechFile(string conversationId, string fileName);
6263
#endregion
6364
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11

22
namespace BotSharp.Abstraction.Files.Models;
33

4-
public class BotSharpFile : FileBase
4+
public class BotSharpFile : FileInformation
55
{
6-
6+
/// <summary>
7+
/// File data => format: "data:image/png;base64,aaaaaaaa"
8+
/// </summary>
9+
[JsonPropertyName("file_data")]
10+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
11+
public string? FileData { get; set; } = string.Empty;
712
}

src/Infrastructure/BotSharp.Abstraction/Files/Models/FileBase.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@ namespace BotSharp.Abstraction.Files.Models;
22

33
public class FileBase
44
{
5-
/// <summary>
6-
/// External file url
7-
/// </summary>
8-
[JsonPropertyName("file_url")]
9-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
10-
public string? FileUrl { get; set; } = string.Empty;
11-
12-
/// <summary>
13-
/// Internal file storage url
14-
/// </summary>
15-
[JsonPropertyName("file_storage_url")]
16-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
17-
public string? FileStorageUrl { get; set; } = string.Empty;
18-
195
/// <summary>
206
/// File name without extension
217
/// </summary>
@@ -29,18 +15,4 @@ public class FileBase
2915
[JsonPropertyName("file_data")]
3016
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3117
public string? FileData { get; set; } = string.Empty;
32-
33-
/// <summary>
34-
/// File content type
35-
/// </summary>
36-
[JsonPropertyName("content_type")]
37-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
38-
public string? ContentType { get; set; } = string.Empty;
39-
40-
/// <summary>
41-
/// File extension without dot
42-
/// </summary>
43-
[JsonPropertyName("file_extension")]
44-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
45-
public string? FileExtension { get; set; } = string.Empty;
4618
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace BotSharp.Abstraction.Files.Models;
2+
3+
public class FileInformation
4+
{
5+
/// <summary>
6+
/// External file url
7+
/// </summary>
8+
[JsonPropertyName("file_url")]
9+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
10+
public string? FileUrl { get; set; } = string.Empty;
11+
12+
/// <summary>
13+
/// Internal file storage url
14+
/// </summary>
15+
[JsonPropertyName("file_storage_url")]
16+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
17+
public string? FileStorageUrl { get; set; } = string.Empty;
18+
19+
/// <summary>
20+
/// File content type
21+
/// </summary>
22+
[JsonPropertyName("content_type")]
23+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
24+
public string? ContentType { get; set; } = string.Empty;
25+
26+
/// <summary>
27+
/// File name without extension
28+
/// </summary>
29+
[JsonPropertyName("file_name")]
30+
public string FileName { get; set; } = string.Empty;
31+
32+
/// <summary>
33+
/// File extension without dot
34+
/// </summary>
35+
[JsonPropertyName("file_extension")]
36+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
37+
public string? FileExtension { get; set; } = string.Empty;
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace BotSharp.Abstraction.Files.Models;
2+
3+
public class InputFileModel : FileBase
4+
{
5+
/// <summary>
6+
/// File name with extension
7+
/// </summary>
8+
[JsonPropertyName("file_name")]
9+
public new string FileName { get; set; } = string.Empty;
10+
11+
/// <summary>
12+
/// File data => format: "data:image/png;base64,aaaaaaaa"
13+
/// </summary>
14+
[JsonPropertyName("file_data")]
15+
public new string FileData { get; set; } = string.Empty;
16+
}

src/Infrastructure/BotSharp.Abstraction/Files/Models/InputMessageFiles.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace BotSharp.Abstraction.Files.Models;
2+
3+
public class InstructFileModel : FileBase
4+
{
5+
/// <summary>
6+
/// File extension without dot
7+
/// </summary>
8+
[JsonPropertyName("file_extension")]
9+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
10+
public string? FileExtension { get; set; } = string.Empty;
11+
12+
/// <summary>
13+
/// External file url
14+
/// </summary>
15+
[JsonPropertyName("file_url")]
16+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
17+
public string? FileUrl { get; set; } = string.Empty;
18+
}

src/Infrastructure/BotSharp.Abstraction/Files/Models/MessageFileModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace BotSharp.Abstraction.Files.Models;
22

3-
public class MessageFileModel : FileBase
3+
public class MessageFileModel : FileInformation
44
{
55
[JsonPropertyName("message_id")]
66
public string MessageId { get; set; }

src/Infrastructure/BotSharp.Abstraction/Files/Utilities/FileUtility.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using Microsoft.AspNetCore.Http;
12
using Microsoft.AspNetCore.StaticFiles;
3+
using System.IO;
24

35
namespace BotSharp.Abstraction.Files.Utilities;
46

@@ -26,11 +28,30 @@ public static (string, byte[]) GetFileInfoFromData(string data)
2628
return (contentType, Convert.FromBase64String(base64Str));
2729
}
2830

29-
public static string GetFileContentType(string filePath)
31+
public static string BuildFileDataFromFile(string fileName, byte[] bytes)
32+
{
33+
var contentType = GetFileContentType(fileName);
34+
var base64 = Convert.ToBase64String(bytes);
35+
return $"data:{contentType};base64,{base64}";
36+
}
37+
38+
public static string BuildFileDataFromFile(IFormFile file)
39+
{
40+
using var stream = new MemoryStream();
41+
file.CopyTo(stream);
42+
stream.Position = 0;
43+
var contentType = GetFileContentType(file.FileName);
44+
var base64 = Convert.ToBase64String(stream.ToArray());
45+
stream.Close();
46+
47+
return $"data:{contentType};base64,{base64}";
48+
}
49+
50+
public static string GetFileContentType(string fileName)
3051
{
3152
string contentType;
3253
var provider = new FileExtensionContentTypeProvider();
33-
if (!provider.TryGetContentType(filePath, out contentType))
54+
if (!provider.TryGetContentType(fileName, out contentType))
3455
{
3556
contentType = string.Empty;
3657
}

src/Infrastructure/BotSharp.Abstraction/Graph/IGraphDb.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace BotSharp.Abstraction.Graph;
44

55
public interface IGraphDb
66
{
7-
public string Name { get; }
7+
public string Provider { get; }
88

99
Task<GraphSearchData> Search(string query, GraphSearchOptions options);
1010
}

0 commit comments

Comments
 (0)