-
Notifications
You must be signed in to change notification settings - Fork 488
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 #554 from iceljc/features/add-image-edit
Features/add image edit
- Loading branch information
Showing
18 changed files
with
457 additions
and
675 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
5 changes: 0 additions & 5 deletions
5
src/Infrastructure/BotSharp.Abstraction/MLTasks/IImageEdit.cs
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
src/Infrastructure/BotSharp.Abstraction/MLTasks/IImageGeneration.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
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
71 changes: 71 additions & 0 deletions
71
...Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Image/ImageCompletionProvider.Generation.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,71 @@ | ||
using OpenAI.Images; | ||
|
||
namespace BotSharp.Plugin.AzureOpenAI.Providers.Image; | ||
|
||
public partial class ImageCompletionProvider | ||
{ | ||
public async Task<RoleDialogModel> GetImageGeneration(Agent agent, RoleDialogModel message) | ||
{ | ||
var client = ProviderHelper.GetClient(Provider, _model, _services); | ||
var (prompt, imageCount, options) = PrepareOptions(message); | ||
var imageClient = client.GetImageClient(_model); | ||
|
||
var response = imageClient.GenerateImages(prompt, imageCount, options); | ||
var values = response.Value; | ||
|
||
var generatedImages = new List<ImageGeneration>(); | ||
foreach (var value in values) | ||
{ | ||
if (value == null) continue; | ||
|
||
var generatedImage = new ImageGeneration { Description = value?.RevisedPrompt ?? string.Empty }; | ||
if (options.ResponseFormat == GeneratedImageFormat.Uri) | ||
{ | ||
generatedImage.ImageUrl = value?.ImageUri?.AbsoluteUri ?? string.Empty; | ||
} | ||
else if (options.ResponseFormat == GeneratedImageFormat.Bytes) | ||
{ | ||
var base64Str = string.Empty; | ||
var bytes = value?.ImageBytes?.ToArray(); | ||
if (!bytes.IsNullOrEmpty()) | ||
{ | ||
base64Str = Convert.ToBase64String(bytes); | ||
} | ||
generatedImage.ImageData = base64Str; | ||
} | ||
|
||
generatedImages.Add(generatedImage); | ||
} | ||
|
||
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, ImageGenerationOptions) PrepareOptions(RoleDialogModel message) | ||
{ | ||
var prompt = message?.Payload ?? message?.Content ?? string.Empty; | ||
|
||
var state = _services.GetRequiredService<IConversationStateService>(); | ||
var size = GetImageSize(state.GetState("image_size")); | ||
var quality = GetImageQuality(state.GetState("image_quality")); | ||
var style = GetImageStyle(state.GetState("image_style")); | ||
var format = GetImageFormat(state.GetState("image_format")); | ||
var count = GetImageCount(state.GetState("image_count", "1")); | ||
|
||
var options = new ImageGenerationOptions | ||
{ | ||
Size = size, | ||
Quality = quality, | ||
Style = style, | ||
ResponseFormat = format | ||
}; | ||
return (prompt, count, options); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Image/ImageCompletionProvider.Variation.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,65 @@ | ||
using OpenAI.Images; | ||
|
||
namespace BotSharp.Plugin.AzureOpenAI.Providers.Image; | ||
|
||
public partial class ImageCompletionProvider | ||
{ | ||
public async Task<RoleDialogModel> GetImageVariation(Agent agent, RoleDialogModel message, Stream image, string imageFileName) | ||
{ | ||
var client = ProviderHelper.GetClient(Provider, _model, _services); | ||
var (imageCount, options) = PrepareOptions(); | ||
var imageClient = client.GetImageClient(_model); | ||
|
||
var response = imageClient.GenerateImageVariations(image, imageFileName, imageCount, options); | ||
var values = response.Value; | ||
|
||
var generatedImages = new List<ImageGeneration>(); | ||
foreach (var value in values) | ||
{ | ||
if (value == null) continue; | ||
|
||
var generatedImage = new ImageGeneration { Description = value?.RevisedPrompt ?? string.Empty }; | ||
if (options.ResponseFormat == GeneratedImageFormat.Uri) | ||
{ | ||
generatedImage.ImageUrl = value?.ImageUri?.AbsoluteUri ?? string.Empty; | ||
} | ||
else if (options.ResponseFormat == GeneratedImageFormat.Bytes) | ||
{ | ||
var base64Str = string.Empty; | ||
var bytes = value?.ImageBytes?.ToArray(); | ||
if (!bytes.IsNullOrEmpty()) | ||
{ | ||
base64Str = Convert.ToBase64String(bytes); | ||
} | ||
generatedImage.ImageData = base64Str; | ||
} | ||
|
||
generatedImages.Add(generatedImage); | ||
} | ||
|
||
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 (int, ImageVariationOptions) PrepareOptions() | ||
{ | ||
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 ImageVariationOptions | ||
{ | ||
Size = size, | ||
ResponseFormat = format | ||
}; | ||
return (count, options); | ||
} | ||
} |
Oops, something went wrong.