2024-07-02 20:43:38 +00:00
|
|
|
using BotSharp.Abstraction.Functions;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Files.Functions;
|
|
|
|
|
|
|
|
|
|
public class GenerateImageFn : IFunctionCallback
|
|
|
|
|
{
|
|
|
|
|
public string Name => "generate_image";
|
|
|
|
|
public string Indication => "Generating image";
|
|
|
|
|
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly ILogger<GenerateImageFn> _logger;
|
|
|
|
|
private string _conversationId;
|
|
|
|
|
private string _messageId;
|
|
|
|
|
|
|
|
|
|
public GenerateImageFn(
|
|
|
|
|
IServiceProvider services,
|
|
|
|
|
ILogger<GenerateImageFn> logger)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<bool> Execute(RoleDialogModel message)
|
|
|
|
|
{
|
|
|
|
|
var args = JsonSerializer.Deserialize<LlmFileContext>(message.FunctionArgs);
|
|
|
|
|
Init(message);
|
|
|
|
|
SetImageOptions();
|
|
|
|
|
|
|
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
2024-07-10 04:36:49 +00:00
|
|
|
var agent = await agentService.LoadAgent(BuiltInAgentId.UtilityAssistant);
|
2024-07-02 20:43:38 +00:00
|
|
|
var imageAgent = new Agent
|
|
|
|
|
{
|
|
|
|
|
Id = agent?.Id ?? Guid.Empty.ToString(),
|
|
|
|
|
Name = agent?.Name ?? "Unkown",
|
|
|
|
|
Instruction = args?.ImageDescription,
|
|
|
|
|
TemplateDict = new Dictionary<string, object>()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await GetImageGeneration(imageAgent, message, args?.ImageDescription);
|
|
|
|
|
message.Content = response;
|
|
|
|
|
message.StopCompletion = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Init(RoleDialogModel message)
|
|
|
|
|
{
|
|
|
|
|
var convService = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
_conversationId = convService.ConversationId;
|
|
|
|
|
_messageId = message.MessageId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetImageOptions()
|
|
|
|
|
{
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
2024-07-02 23:35:09 +00:00
|
|
|
state.SetState("image_format", "bytes");
|
|
|
|
|
state.SetState("image_count", "1");
|
2024-07-02 20:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<string> GetImageGeneration(Agent agent, RoleDialogModel message, string? description)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var completion = CompletionProvider.GetImageGeneration(_services, provider: "openai", model: "dall-e-3", imageGenerate: true);
|
|
|
|
|
var text = !string.IsNullOrWhiteSpace(description) ? description : message.Content;
|
|
|
|
|
var dialog = RoleDialogModel.From(message, AgentRole.User, text);
|
|
|
|
|
var result = await completion.GetImageGeneration(agent, new List<RoleDialogModel> { dialog });
|
2024-07-02 23:35:09 +00:00
|
|
|
SaveGeneratedImages(result?.GeneratedImages);
|
2024-07-02 20:43:38 +00:00
|
|
|
return result?.Content ?? string.Empty;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error when generating image.";
|
|
|
|
|
_logger.LogWarning($"{error} {ex.Message}");
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-02 23:35:09 +00:00
|
|
|
private void SaveGeneratedImages(List<ImageGeneration>? images)
|
2024-07-02 20:43:38 +00:00
|
|
|
{
|
|
|
|
|
if (images.IsNullOrEmpty()) return;
|
|
|
|
|
|
2024-07-02 23:39:06 +00:00
|
|
|
var files = images.Where(x => !string.IsNullOrEmpty(x?.ImageData)).Select(x => new BotSharpFile
|
2024-07-02 20:43:38 +00:00
|
|
|
{
|
2024-07-02 23:39:06 +00:00
|
|
|
FileName = $"{Guid.NewGuid()}.png",
|
|
|
|
|
FileData = $"data:image/png;base64,{x.ImageData}"
|
|
|
|
|
}).ToList();
|
2024-07-02 20:43:38 +00:00
|
|
|
|
|
|
|
|
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
|
|
|
|
fileService.SaveMessageFiles(_conversationId, _messageId, FileSourceType.Bot, files);
|
|
|
|
|
}
|
|
|
|
|
}
|