Skip to content

Commit b3fd13a

Browse files
author
Jicheng Lu
committed
temp save
1 parent aecff92 commit b3fd13a

File tree

13 files changed

+301
-47
lines changed

13 files changed

+301
-47
lines changed

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ namespace BotSharp.Abstraction.Files;
22

33
public interface IBotSharpFileService
44
{
5-
string GetDirectory(string conversationId);
6-
5+
#region Conversation
76
/// <summary>
87
/// Get the files that have been uploaded in the chat.
98
/// 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.
@@ -19,14 +18,19 @@ Task<IEnumerable<MessageFileModel>> GetChatFiles(string conversationId, string s
1918
IEnumerable<RoleDialogModel> conversations, IEnumerable<string> contentTypes,
2019
bool includeScreenShot = false, int? offset = null);
2120

21+
/// <summary>
22+
/// Get the files that have been uploaded in the chat. No screenshot images are included.
23+
/// </summary>
24+
/// <param name="conversationId"></param>
25+
/// <param name="messageIds"></param>
26+
/// <param name="source"></param>
27+
/// <param name="imageOnly"></param>
28+
/// <returns></returns>
2229
IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, string source, bool imageOnly = false);
2330
string GetMessageFile(string conversationId, string messageId, string source, string index, string fileName);
2431
IEnumerable<MessageFileModel> GetMessagesWithFile(string conversationId, IEnumerable<string> messageIds);
2532
bool SaveMessageFiles(string conversationId, string messageId, string source, List<BotSharpFile> files);
2633

27-
string GetUserAvatar();
28-
bool SaveUserAvatar(BotSharpFile file);
29-
3034
/// <summary>
3135
/// Delete files under messages
3236
/// </summary>
@@ -37,21 +41,35 @@ Task<IEnumerable<MessageFileModel>> GetChatFiles(string conversationId, string s
3741
/// <returns></returns>
3842
bool DeleteMessageFiles(string conversationId, IEnumerable<string> messageIds, string targetMessageId, string? newMessageId = null);
3943
bool DeleteConversationFiles(IEnumerable<string> conversationIds);
44+
#endregion
45+
46+
#region Image
4047

48+
#endregion
49+
50+
#region Pdf
4151
/// <summary>
4252
/// Take screenshots of pdf pages and get response from llm
4353
/// </summary>
4454
/// <param name="prompt"></param>
4555
/// <param name="files">Pdf files</param>
4656
/// <returns></returns>
4757
Task<string> InstructPdf(string? provider, string? model, string? modelId, string prompt, List<BotSharpFile> files);
58+
#endregion
59+
60+
#region User
61+
string GetUserAvatar();
62+
bool SaveUserAvatar(BotSharpFile file);
63+
#endregion
4864

65+
#region Common
4966
/// <summary>
5067
/// Get file bytes and content type from data, e.g., "data:image/png;base64,aaaaaaaaa"
5168
/// </summary>
5269
/// <param name="data"></param>
5370
/// <returns></returns>
5471
(string, byte[]) GetFileInfoFromData(string data);
55-
72+
string GetDirectory(string conversationId);
5673
string GetFileContentType(string filePath);
74+
#endregion
5775
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace BotSharp.Abstraction.MLTasks;
2+
3+
public interface IImageEdit
4+
{
5+
}

src/Infrastructure/BotSharp.Abstraction/MLTasks/IImageGeneration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ public interface IImageGeneration
1313
/// <param name="model">deployment name</param>
1414
void SetModelName(string model);
1515

16-
Task<RoleDialogModel> GetImageGeneration(Agent agent, List<RoleDialogModel> conversations);
16+
Task<RoleDialogModel> GetImageGeneration(Agent agent, RoleDialogModel message);
1717
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.IO;
2+
3+
namespace BotSharp.Abstraction.MLTasks;
4+
5+
public interface IImageVariation
6+
{
7+
/// <summary>
8+
/// The LLM provider like Microsoft Azure, OpenAI, ClaudAI
9+
/// </summary>
10+
string Provider { get; }
11+
12+
/// <summary>
13+
/// Set model name, one provider can consume different model or version(s)
14+
/// </summary>
15+
/// <param name="model">deployment name</param>
16+
void SetModelName(string model);
17+
18+
RoleDialogModel GetImageVariation(Agent agent, RoleDialogModel message, Stream image, string imageFileName);
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace BotSharp.Core.Files.Services;
2+
3+
public partial class BotSharpFileService
4+
{
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace BotSharp.Core.Files.Services;
2+
3+
public partial class BotSharpFileService
4+
{
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace BotSharp.Core.Files.Services;
2+
3+
public partial class BotSharpFileService
4+
{
5+
}

src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace BotSharp.Core.Infrastructures;
55

66
public class CompletionProvider
77
{
8-
public static object GetCompletion(IServiceProvider services,
8+
public static object? GetCompletion(IServiceProvider services,
99
string? provider = null,
1010
string? model = null,
1111
AgentLlmConfig? agentConfig = null)
@@ -23,13 +23,21 @@ public static object GetCompletion(IServiceProvider services,
2323
model: model,
2424
agentConfig: agentConfig);
2525
}
26-
else
26+
else if (settings.Type == LlmModelType.Embedding)
2727
{
28-
return GetChatCompletion(services,
29-
provider: provider,
30-
model: model,
28+
return GetTextEmbedding(services,
29+
provider: provider,
30+
model: model);
31+
}
32+
else if (settings.Type == LlmModelType.Chat)
33+
{
34+
return GetChatCompletion(services,
35+
provider: provider,
36+
model: model,
3137
agentConfig: agentConfig);
3238
}
39+
40+
return null;
3341
}
3442

3543
public static IChatCompletion GetChatCompletion(IServiceProvider services,
@@ -51,7 +59,6 @@ public static IChatCompletion GetChatCompletion(IServiceProvider services,
5159
}
5260

5361
completer?.SetModelName(model);
54-
5562
return completer;
5663
}
5764

@@ -72,7 +79,6 @@ public static ITextCompletion GetTextCompletion(IServiceProvider services,
7279
}
7380

7481
completer.SetModelName(model);
75-
7682
return completer;
7783
}
7884

@@ -95,7 +101,26 @@ public static IImageGeneration GetImageGeneration(IServiceProvider services,
95101
}
96102

97103
completer?.SetModelName(model);
104+
return completer;
105+
}
98106

107+
public static IImageVariation GetImageVariation(IServiceProvider services,
108+
string? provider = null,
109+
string? model = null,
110+
string? modelId = null,
111+
bool imageGenerate = false)
112+
{
113+
var completions = services.GetServices<IImageVariation>();
114+
(provider, model) = GetProviderAndModel(services, provider: provider, model: model, modelId: modelId, imageGenerate: imageGenerate);
115+
116+
var completer = completions.FirstOrDefault(x => x.Provider == provider);
117+
if (completer == null)
118+
{
119+
var logger = services.GetRequiredService<ILogger<CompletionProvider>>();
120+
logger.LogError($"Can't resolve completion provider by {provider}");
121+
}
122+
123+
completer?.SetModelName(model);
99124
return completer;
100125
}
101126

@@ -152,7 +177,6 @@ private static (string, string) GetProviderAndModel(IServiceProvider services,
152177

153178
state.SetState("provider", provider);
154179
state.SetState("model", model);
155-
156180
return (provider, model);
157181
}
158182
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ public async Task<ImageGenerationViewModel> ImageGeneration([FromBody] IncomingM
119119
var message = await completion.GetImageGeneration(new Agent()
120120
{
121121
Id = Guid.Empty.ToString(),
122-
}, new List<RoleDialogModel>
123-
{
124-
new RoleDialogModel(AgentRole.User, input.Text)
125-
});
122+
}, new RoleDialogModel(AgentRole.User, input.Text));
126123

127124
imageViewModel.Content = message.Content;
128125
imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
@@ -137,6 +134,34 @@ public async Task<ImageGenerationViewModel> ImageGeneration([FromBody] IncomingM
137134
}
138135
}
139136

137+
//[HttpPost("/instruct/image-variation")]
138+
//public ImageGenerationViewModel ImageVariation([FromBody] IncomingMessageModel input)
139+
//{
140+
// var state = _services.GetRequiredService<IConversationStateService>();
141+
// input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
142+
// var imageViewModel = new ImageGenerationViewModel();
143+
144+
// try
145+
// {
146+
// var completion = CompletionProvider.GetImageVariation(_services, provider: input.Provider ?? "openai", model: input.Model ?? "dall-e-2");
147+
// var message = completion.GetImageVariation(new Agent()
148+
// {
149+
// Id = Guid.Empty.ToString(),
150+
// }, new RoleDialogModel(AgentRole.User, input.Text));
151+
152+
// imageViewModel.Content = message.Content;
153+
// imageViewModel.Images = message.GeneratedImages.Select(x => ImageViewModel.ToViewModel(x)).ToList();
154+
// return imageViewModel;
155+
// }
156+
// catch (Exception ex)
157+
// {
158+
// var error = $"Error in image generation. {ex.Message}";
159+
// _logger.LogError(error);
160+
// imageViewModel.Message = error;
161+
// return imageViewModel;
162+
// }
163+
//}
164+
140165
[HttpPost("/instruct/pdf-completion")]
141166
public async Task<PdfCompletionViewModel> PdfCompletion([FromBody] IncomingMessageModel input)
142167
{

src/Plugins/BotSharp.Plugin.FileHandler/Functions/GenerateImageFn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private async Task<string> GetImageGeneration(Agent agent, RoleDialogModel messa
6262
var completion = CompletionProvider.GetImageGeneration(_services, provider: "openai", model: "dall-e-3");
6363
var text = !string.IsNullOrWhiteSpace(description) ? description : message.Content;
6464
var dialog = RoleDialogModel.From(message, AgentRole.User, text);
65-
var result = await completion.GetImageGeneration(agent, new List<RoleDialogModel> { dialog });
65+
var result = await completion.GetImageGeneration(agent, dialog);
6666
SaveGeneratedImages(result?.GeneratedImages);
6767
return result?.Content ?? string.Empty;
6868
}

0 commit comments

Comments
 (0)