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 context to gemini #107

Merged
merged 1 commit into from
Feb 1, 2024
Merged
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
68 changes: 66 additions & 2 deletions BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using BotNet.Commands;
using BotNet.CommandHandlers.Art;
using BotNet.Commands;
using BotNet.Commands.AI.Gemini;
using BotNet.Commands.AI.OpenAI;
using BotNet.Commands.AI.Stability;
using BotNet.Commands.BotUpdate.Message;
using BotNet.Commands.ChatAggregate;
using BotNet.Commands.CommandPrioritization;
Expand All @@ -19,6 +22,7 @@ public sealed class GeminiTextPromptHandler(
GeminiClient geminiClient,
ITelegramMessageCache telegramMessageCache,
CommandPriorityCategorizer commandPriorityCategorizer,
ICommandQueue commandQueue,
ILogger<GeminiTextPromptHandler> logger
) : ICommandHandler<GeminiTextPrompt> {
internal static readonly RateLimiter CHAT_RATE_LIMITER = RateLimiter.PerChat(60, TimeSpan.FromMinutes(1));
Expand All @@ -27,6 +31,7 @@ ILogger<GeminiTextPromptHandler> logger
private readonly GeminiClient _geminiClient = geminiClient;
private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache;
private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer;
private readonly ICommandQueue _commandQueue = commandQueue;
private readonly ILogger<GeminiTextPromptHandler> _logger = logger;

public Task Handle(GeminiTextPrompt textPrompt, CancellationToken cancellationToken) {
Expand Down Expand Up @@ -58,7 +63,10 @@ public Task Handle(GeminiTextPrompt textPrompt, CancellationToken cancellationTo

// Fire and forget
Task.Run(async () => {
List<Content> messages = [];
List<Content> messages = [
Content.FromText("user", "Act as an AI assistant. The assistant is helpful, creative, direct, concise, and always get to the point. When user asks for an image to be generated, the AI assistant should respond with \"ImageGeneration:\" followed by comma separated list of features to be expected from the generated image."),
Content.FromText("model", "Sure.")
];

// Merge adjacent messages from same role
foreach (MessageBase message in textPrompt.Thread.Reverse()) {
Expand Down Expand Up @@ -103,6 +111,62 @@ public Task Handle(GeminiTextPrompt textPrompt, CancellationToken cancellationTo
cancellationToken: cancellationToken
);

// Handle image generation intent
if (response.StartsWith("ImageGeneration:")) {
if (textPrompt.Command.Sender is not VIPSender) {
try {
ArtCommandHandler.IMAGE_GENERATION_RATE_LIMITER.ValidateActionRate(textPrompt.Command.Chat.Id, textPrompt.Command.Sender.Id);
} catch (RateLimitExceededException exc) {
await _telegramBotClient.SendTextMessageAsync(
chatId: textPrompt.Command.Chat.Id,
text: $"Anda belum mendapat giliran. Coba lagi {exc.Cooldown}.",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
cancellationToken: cancellationToken
);
return;
}
}

string imageGenerationPrompt = response.Substring(response.IndexOf(':') + 1).Trim();
switch (textPrompt.Command) {
case { Sender: VIPSender }:
await _commandQueue.DispatchAsync(
command: new OpenAIImageGenerationPrompt(
callSign: "Gemini",
prompt: imageGenerationPrompt,
promptMessageId: textPrompt.Command.MessageId,
responseMessageId: new(responseMessage.MessageId),
chat: textPrompt.Command.Chat,
sender: textPrompt.Command.Sender
)
);
break;
case { Chat: HomeGroupChat }:
await _commandQueue.DispatchAsync(
command: new StabilityTextToImagePrompt(
callSign: "Gemini",
prompt: imageGenerationPrompt,
promptMessageId: textPrompt.Command.MessageId,
responseMessageId: new(responseMessage.MessageId),
chat: textPrompt.Command.Chat,
sender: textPrompt.Command.Sender
)
);
break;
default:
await _telegramBotClient.EditMessageTextAsync(
chatId: textPrompt.Command.Chat.Id,
messageId: responseMessage.MessageId,
text: MarkdownV2Sanitizer.Sanitize("Image generation tidak bisa dipakai di sini."),
parseMode: ParseMode.MarkdownV2,
cancellationToken: cancellationToken
);
break;
}
return;
}

// Finalize message
try {
responseMessage = await telegramBotClient.EditMessageTextAsync(
Expand Down
Loading