Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image generation skill #74

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions BotNet.Services/BotCommands/OpenAI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
Expand All @@ -7,6 +8,7 @@
using System.Threading.Tasks;
using BotNet.Services.MarkdownV2;
using BotNet.Services.OpenAI;
using BotNet.Services.OpenAI.Models;
using BotNet.Services.OpenAI.Skills;
using BotNet.Services.RateLimit;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -811,11 +813,49 @@ await serviceProvider.GetRequiredService<VisionBot>().StreamChatAsync(
replyToMessageId: message.MessageId
);
} else {
await serviceProvider.GetRequiredService<FriendlyBot>().StreamChatAsync(
IntentDetector intentDetector = serviceProvider.GetRequiredService<IntentDetector>();
ChatIntent chatIntent = await intentDetector.DetectChatIntentAsync(
message: message.Text!,
chatId: message.Chat.Id,
replyToMessageId: message.MessageId
cancellationToken: cancellationToken
);

switch (chatIntent) {
case ChatIntent.Question:
await serviceProvider.GetRequiredService<FriendlyBot>().StreamChatAsync(
message: message.Text!,
chatId: message.Chat.Id,
replyToMessageId: message.MessageId
);
break;
case ChatIntent.ImageGeneration:
Message busyMessage = await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: "Generating image… ⏳",
parseMode: ParseMode.Markdown,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken
);
Uri generatedImageUrl = await serviceProvider.GetRequiredService<ImageGenerationBot>().GenerateImageAsync(
prompt: message.Text!,
cancellationToken: cancellationToken
);
try {
await botClient.DeleteMessageAsync(
chatId: busyMessage.Chat.Id,
messageId: busyMessage.MessageId,
cancellationToken: cancellationToken
);
} catch (OperationCanceledException) {
throw;
}
await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputFileUrl(generatedImageUrl),
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken
);
break;
}
}
} catch (RateLimitExceededException exc) when (exc is { Cooldown: var cooldown }) {
if (message.Chat.Type == ChatType.Private) {
Expand Down
13 changes: 13 additions & 0 deletions BotNet.Services/OpenAI/Models/ImageGenerationResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace BotNet.Services.OpenAI.Models {
public record ImageGenerationResult(
[property: JsonPropertyName("created")] int CreatedUnixTime,
List<GeneratedImage> Data
);

public record GeneratedImage(
string Url
);
}
28 changes: 26 additions & 2 deletions BotNet.Services/OpenAI/OpenAIClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Json;
Expand All @@ -20,7 +21,8 @@ public class OpenAIClient(
ILogger<OpenAIClient> logger
) {
private const string COMPLETION_URL_TEMPLATE = "https://api.openai.com/v1/engines/{0}/completions";
private const string CHAT_URL = "https://api.openai.com/v1/chat/completions";
private const string CHAT_URL = "https://api.openai.com/v1/chat/completions";
private const string IMAGE_GENERATION_URL = "https://api.openai.com/v1/images/generations";
private static readonly JsonSerializerOptions JSON_SERIALIZER_OPTIONS = new() {
PropertyNamingPolicy = new SnakeCaseNamingPolicy()
};
Expand Down Expand Up @@ -172,6 +174,28 @@ [EnumeratorCancellation] CancellationToken cancellationToken
);
}
}
}

public async Task<Uri> GenerateImageAsync(string model, string prompt, CancellationToken cancellationToken) {
using HttpRequestMessage request = new(HttpMethod.Post, IMAGE_GENERATION_URL) {
Headers = {
{ "Authorization", $"Bearer {_apiKey}" }
},
Content = JsonContent.Create(
inputValue: new {
Model = model,
Prompt = prompt,
N = 1,
Size = "1024x1024"
},
options: JSON_SERIALIZER_OPTIONS
)
};
using HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();

ImageGenerationResult? imageGenerationResult = await response.Content.ReadFromJsonAsync<ImageGenerationResult>(JSON_SERIALIZER_OPTIONS, cancellationToken);
return new(imageGenerationResult!.Data[0].Url);
}
}
}
1 change: 1 addition & 0 deletions BotNet.Services/OpenAI/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static IServiceCollection AddOpenAIClient(this IServiceCollection service
services.AddTransient<TldrGenerator>();
services.AddTransient<IntentDetector>();
services.AddTransient<VisionBot>();
services.AddTransient<ImageGenerationBot>();
return services;
}
}
Expand Down
20 changes: 20 additions & 0 deletions BotNet.Services/OpenAI/Skills/ImageGenerationBot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace BotNet.Services.OpenAI.Skills {
public class ImageGenerationBot(
OpenAIClient openAIClient
) {
private readonly OpenAIClient _openAIClient = openAIClient;

public Task<Uri> GenerateImageAsync(
string prompt,
CancellationToken cancellationToken
) => _openAIClient.GenerateImageAsync(
model: "dall-e-3",
prompt: prompt,
cancellationToken: cancellationToken
);
}
}
Loading