-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #555 from iceljc/features/add-image-mask-edit
add image mask edit
- Loading branch information
Showing
27 changed files
with
578 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/Infrastructure/BotSharp.Abstraction/Files/Models/InputMessageFiles.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace BotSharp.Abstraction.Files.Models; | ||
|
||
public class InputMessageFiles | ||
{ | ||
public List<BotSharpFile> Files { get; set; } = new List<BotSharpFile>(); | ||
public BotSharpFile? Mask { get; set; } | ||
} |
16 changes: 0 additions & 16 deletions
16
src/Infrastructure/BotSharp.Abstraction/Files/Models/LlmFileContext.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Infrastructure/BotSharp.Abstraction/Models/MessageConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Image/ImageCompletionProvider.Edit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using OpenAI.Images; | ||
|
||
namespace BotSharp.Plugin.AzureOpenAI.Providers.Image; | ||
|
||
public partial class ImageCompletionProvider | ||
{ | ||
public async Task<RoleDialogModel> GetImageEdits(Agent agent, RoleDialogModel message, Stream image, string imageFileName) | ||
{ | ||
var client = ProviderHelper.GetClient(Provider, _model, _services); | ||
var (prompt, imageCount, options) = PrepareEditOptions(message); | ||
var imageClient = client.GetImageClient(_model); | ||
|
||
var response = imageClient.GenerateImageEdits(image, imageFileName, prompt, imageCount, options); | ||
var images = response.Value; | ||
|
||
var generatedImages = GetImageGenerations(images, options.ResponseFormat); | ||
var content = string.Join("\r\n", generatedImages.Where(x => !string.IsNullOrWhiteSpace(x.Description)).Select(x => x.Description)); | ||
var responseMessage = new RoleDialogModel(AgentRole.Assistant, content) | ||
{ | ||
CurrentAgentId = agent.Id, | ||
MessageId = message?.MessageId ?? string.Empty, | ||
GeneratedImages = generatedImages | ||
}; | ||
|
||
return await Task.FromResult(responseMessage); | ||
} | ||
|
||
public async Task<RoleDialogModel> GetImageEdits(Agent agent, RoleDialogModel message, | ||
Stream image, string imageFileName, Stream mask, string maskFileName) | ||
{ | ||
var client = ProviderHelper.GetClient(Provider, _model, _services); | ||
var (prompt, imageCount, options) = PrepareEditOptions(message); | ||
var imageClient = client.GetImageClient(_model); | ||
|
||
var response = imageClient.GenerateImageEdits(image, imageFileName, prompt, mask, maskFileName, imageCount, options); | ||
var images = response.Value; | ||
|
||
var generatedImages = GetImageGenerations(images, options.ResponseFormat); | ||
var content = string.Join("\r\n", generatedImages.Where(x => !string.IsNullOrWhiteSpace(x.Description)).Select(x => x.Description)); | ||
var responseMessage = new RoleDialogModel(AgentRole.Assistant, content) | ||
{ | ||
CurrentAgentId = agent.Id, | ||
MessageId = message?.MessageId ?? string.Empty, | ||
GeneratedImages = generatedImages | ||
}; | ||
|
||
return await Task.FromResult(responseMessage); | ||
} | ||
|
||
private (string, int, ImageEditOptions) PrepareEditOptions(RoleDialogModel message) | ||
{ | ||
var prompt = message?.Payload ?? message?.Content ?? string.Empty; | ||
|
||
var state = _services.GetRequiredService<IConversationStateService>(); | ||
var size = GetImageSize(state.GetState("image_size")); | ||
var format = GetImageFormat(state.GetState("image_format")); | ||
var count = GetImageCount(state.GetState("image_count", "1")); | ||
|
||
var options = new ImageEditOptions | ||
{ | ||
Size = size, | ||
ResponseFormat = format | ||
}; | ||
return (prompt, count, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.