Skip to content

Commit

Permalink
Fix thread tracker issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Dec 3, 2023
1 parent 1280ce4 commit b3d3069
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
8 changes: 4 additions & 4 deletions BotNet.Services/BotCommands/OpenAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -761,15 +761,15 @@ await botClient.SendTextMessageAsync(
}
}

public static async Task StreamChatWithFriendlyBotAsync(ITelegramBotClient botClient, IServiceProvider serviceProvider, Message message, CancellationToken cancellationToken) {
public static async Task StreamChatWithFriendlyBotAsync(ITelegramBotClient botClient, IServiceProvider serviceProvider, string callSign, Message message, CancellationToken cancellationToken) {
try {
(message.Chat.Type == ChatType.Private
? CHAT_PRIVATE_RATE_LIMITER
: CHAT_GROUP_RATE_LIMITER
).ValidateActionRate(message.Chat.Id, message.From!.Id);
await serviceProvider.GetRequiredService<FriendlyBot>().StreamChatAsync(
message: message.Text!,
from: message.From!,
callSign: callSign,
chatId: message.Chat.Id,
replyToMessageId: message.MessageId
);
Expand Down Expand Up @@ -802,7 +802,7 @@ await botClient.SendTextMessageAsync(
}
}

public static async Task StreamChatWithFriendlyBotAsync(ITelegramBotClient botClient, IServiceProvider serviceProvider, Message message, ImmutableList<(string Sender, string Text)> thread, CancellationToken cancellationToken) {
public static async Task StreamChatWithFriendlyBotAsync(ITelegramBotClient botClient, IServiceProvider serviceProvider, string callSign, Message message, ImmutableList<(string Sender, string Text)> thread, CancellationToken cancellationToken) {
try {
(message.Chat.Type == ChatType.Private
? CHAT_PRIVATE_RATE_LIMITER
Expand All @@ -811,7 +811,7 @@ public static async Task StreamChatWithFriendlyBotAsync(ITelegramBotClient botCl
await serviceProvider.GetRequiredService<FriendlyBot>().StreamChatAsync(
message: message.Text!,
thread: thread,
from: message.From!,
callSign: callSign,
chatId: message.Chat.Id,
replyToMessageId: message.MessageId
);
Expand Down
14 changes: 13 additions & 1 deletion BotNet.Services/MarkdownV2/MarkdownV2Sanitizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace BotNet.Services.MarkdownV2 {
public static class MarkdownV2Sanitizer {
private static readonly HashSet<char> CHARACTERS_TO_ESCAPE = [
'_', '*', '[', ']', '(', ')', '~', '>', '#',
'[', ']', '(', ')', '~', '>', '#',
'+', '-', '=', '|', '{', '}', '.', '!'
];

Expand All @@ -25,5 +25,17 @@ public static string Sanitize(string input) {

return sanitized.ToString();
}

private static int CountOccurences(string text, string substring) {
int count = 0;
int i = 0;

while ((i = text.IndexOf(substring, i)) != -1) {
i += substring.Length;
count++;
}

return count;
}
}
}
16 changes: 13 additions & 3 deletions BotNet.Services/OpenAI/OpenAIStreamingClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task StreamChatAsync(
string model,
IEnumerable<ChatMessage> messages,
int maxTokens,
User from,
string callSign,
long chatId,
int replyToMessageId
) {
Expand Down Expand Up @@ -57,12 +57,22 @@ await Task.WhenAny(

// If downstream task is completed, send the last result
if (downstreamTask.IsCompleted) {
await telegramBotClient.SendTextMessageAsync(
Message completeMessage = await telegramBotClient.SendTextMessageAsync(
chatId: chatId,
text: MarkdownV2Sanitizer.Sanitize(lastResult!),
parseMode: ParseMode.MarkdownV2,
replyToMessageId: replyToMessageId
);

// Track thread
ThreadTracker threadTracker = serviceScope.ServiceProvider.GetRequiredService<ThreadTracker>();
threadTracker.TrackMessage(
messageId: completeMessage.MessageId,
sender: callSign,
text: lastResult!,
replyToMessageId: replyToMessageId
);

serviceScope.Dispose();
return;
}
Expand Down Expand Up @@ -122,7 +132,7 @@ await telegramBotClient.EditMessageTextAsync(
ThreadTracker threadTracker = serviceScope.ServiceProvider.GetRequiredService<ThreadTracker>();
threadTracker.TrackMessage(
messageId: incompleteMessage.MessageId,
sender: $"{from.FirstName}{from.LastName?.Let(lastName => " " + lastName)}",
sender: callSign,
text: lastResult!,
replyToMessageId: replyToMessageId
);
Expand Down
8 changes: 4 additions & 4 deletions BotNet.Services/OpenAI/Skills/FriendlyBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Task<string> ChatAsync(string message, CancellationToken cancellationToke
);
}

public async Task StreamChatAsync(string message, User from, long chatId, int replyToMessageId) {
public async Task StreamChatAsync(string message, string callSign, long chatId, int replyToMessageId) {
List<ChatMessage> messages = [
new("system", "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly."),
new("user", message)
Expand All @@ -81,7 +81,7 @@ await _openAIStreamingClient.StreamChatAsync(
model: "gpt-4-1106-preview",
messages: messages,
maxTokens: 512,
from: from,
callSign: callSign,
chatId: chatId,
replyToMessageId: replyToMessageId
);
Expand Down Expand Up @@ -111,7 +111,7 @@ from tuple in thread
);
}

public async Task StreamChatAsync(string message, ImmutableList<(string Sender, string Text)> thread, User from, long chatId, int replyToMessageId) {
public async Task StreamChatAsync(string message, ImmutableList<(string Sender, string Text)> thread, string callSign, long chatId, int replyToMessageId) {
List<ChatMessage> messages = new() {
new("system", "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly."),

Expand All @@ -131,7 +131,7 @@ await _openAIStreamingClient.StreamChatAsync(
model: "gpt-4-1106-preview",
messages: messages,
maxTokens: 512,
from: from,
callSign: callSign,
chatId: chatId,
replyToMessageId: replyToMessageId
);
Expand Down
4 changes: 2 additions & 2 deletions BotNet/Bot/UpdateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ await botClient.SendTextMessageAsync(
// Respond to call sign
switch (callSign) {
case "AI":
await OpenAI.StreamChatWithFriendlyBotAsync(botClient, _serviceProvider, update.Message, cancellationToken);
await OpenAI.StreamChatWithFriendlyBotAsync(botClient, _serviceProvider, "AI", update.Message, cancellationToken);
break;
case "Pakde":
Message? sentMessage = await OpenAI.ChatWithSarcasticBotAsync(botClient, _serviceProvider, update.Message, callSign, cancellationToken);
Expand Down Expand Up @@ -139,7 +139,7 @@ await botClient.SendTextMessageAsync(
// Respond to thread
switch (callSign) {
case "AI":
await OpenAI.StreamChatWithFriendlyBotAsync(botClient, _serviceProvider, update.Message, thread, cancellationToken);
await OpenAI.StreamChatWithFriendlyBotAsync(botClient, _serviceProvider, "AI", update.Message, thread, cancellationToken);
break;
case "Pakde":
Message? sentMessage = await OpenAI.ChatWithSarcasticBotAsync(botClient, _serviceProvider, update.Message, thread, callSign, cancellationToken);
Expand Down

0 comments on commit b3d3069

Please sign in to comment.