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

Allow continuing conversation by replying to generated image #91

Merged
merged 1 commit into from
Dec 5, 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
17 changes: 14 additions & 3 deletions BotNet.Services/BotCommands/Art.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BotNet.Services.OpenAI.Models;
using BotNet.Services.OpenAI;
using BotNet.Services.RateLimit;
using BotNet.Services.Stability.Models;
using BotNet.Services.ThisXDoesNotExist;
Expand Down Expand Up @@ -33,8 +35,8 @@ public static async Task GetRandomArtAsync(ITelegramBotClient botClient, IServic
);

try {
byte[] image = await serviceProvider.GetRequiredService<Stability.Skills.ImageGenerationBot>().GenerateImageAsync(commandArgument, CancellationToken.None);
using MemoryStream imageStream = new(image);
byte[] generatedImage = await serviceProvider.GetRequiredService<Stability.Skills.ImageGenerationBot>().GenerateImageAsync(commandArgument, CancellationToken.None);
using MemoryStream imageStream = new(generatedImage);

try {
await botClient.DeleteMessageAsync(
Expand All @@ -46,11 +48,20 @@ await botClient.DeleteMessageAsync(
throw;
}

await botClient.SendPhotoAsync(
Message generatedImageMessage = await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputFileStream(imageStream, "art.png"),
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);

// Track generated image for continuation
serviceProvider.GetRequiredService<ThreadTracker>().TrackMessage(
messageId: generatedImageMessage.MessageId,
sender: "AI",
text: null,
imageBase64: Convert.ToBase64String(generatedImage),
replyToMessageId: message.MessageId
);
} catch (ContentFilteredException exc) {
await botClient.EditMessageTextAsync(
chatId: message.Chat.Id,
Expand Down
34 changes: 27 additions & 7 deletions BotNet.Services/BotCommands/OpenAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -865,12 +865,22 @@
} catch (OperationCanceledException) {
throw;
}
await botClient.SendPhotoAsync(

Message generatedImageMessage = await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputFileStream(generatedImageStream, "art.png"),
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken
);

// Track generated image for continuation
serviceProvider.GetRequiredService<ThreadTracker>().TrackMessage(
messageId: generatedImageMessage.MessageId,
sender: "AI",
text: null,
imageBase64: Convert.ToBase64String(generatedImage),
replyToMessageId: message.MessageId
);
} catch (ContentFilteredException exc) {
await botClient.EditMessageTextAsync(
chatId: busyMessage.Chat.Id,
Expand Down Expand Up @@ -916,7 +926,7 @@
parseMode: ParseMode.Html,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} catch (HttpRequestException exc) {

Check warning on line 929 in BotNet.Services/BotCommands/OpenAI.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'exc' is declared but never used

Check warning on line 929 in BotNet.Services/BotCommands/OpenAI.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'exc' is declared but never used
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: "<code>Unknown error.</code>",
Expand All @@ -939,12 +949,22 @@
? CHAT_PRIVATE_RATE_LIMITER
: CHAT_GROUP_RATE_LIMITER
).ValidateActionRate(message.Chat.Id, message.From!.Id);
await serviceProvider.GetRequiredService<FriendlyBot>().StreamChatAsync(
message: message.Text!,
thread: thread,
chatId: message.Chat.Id,
replyToMessageId: message.MessageId
);

if (thread.FirstOrDefault().ImageBase64 is { } imageBase64) {
await serviceProvider.GetRequiredService<VisionBot>().StreamChatAsync(
message: message.Text!,
imageBase64: imageBase64,
chatId: message.Chat.Id,
replyToMessageId: message.MessageId
);
} else {
await serviceProvider.GetRequiredService<FriendlyBot>().StreamChatAsync(
message: message.Text!,
thread: thread,
chatId: message.Chat.Id,
replyToMessageId: message.MessageId
);
}
} catch (RateLimitExceededException exc) when (exc is { Cooldown: var cooldown }) {
if (message.Chat.Type == ChatType.Private) {
await botClient.SendTextMessageAsync(
Expand Down
20 changes: 15 additions & 5 deletions BotNet.Services/OpenAI/ThreadTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,25 @@ public void TrackMessage(
long messageId,
int maxLines
) {
bool firstLine = true;
while (_memoryCache.TryGetValue<Message>(
key: new MessageId(messageId),
value: out Message? message
) && message != null && maxLines-- > 0) {
yield return (
Sender: message.Sender,
Text: message.Text,
ImageBase64: message.ImageBase64
);
if (firstLine) {
yield return (
Sender: message.Sender,
Text: message.Text,
ImageBase64: message.ImageBase64
);
firstLine = false;
} else {
yield return (
Sender: message.Sender,
Text: message.Text,
ImageBase64: null // Strip images from the rest of thread
);
}

if (message.ReplyToMessageId == null) {
yield break;
Expand Down
Loading