Skip to content

Commit

Permalink
Add conversation support
Browse files Browse the repository at this point in the history
  • Loading branch information
smallketchup82 committed Sep 26, 2024
1 parent 714e568 commit 8db7ae7
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion ketchupbot-discord/GalaxyGPT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,81 @@ public static async Task HandleMessage(SocketMessage messageParam, DiscordSocket

if (allowedChannels != null && !allowedChannels.Contains(message.Channel.Id)) return;

if (message.Type == MessageType.Reply && message.ReferencedMessage.Author == client.CurrentUser)
{
// The message is a reply to the bot. Let's start by adding the message to the list, and setting the bot's message as the current message in the chain to process
var messages = new List<IUserMessage> { message };
IUserMessage messageToProcess = message.ReferencedMessage;

while (true)
{
// Find messages that are the user replying to the bot, or the bot replying to the user
if (messageToProcess.Type == MessageType.Reply)
{
// Message is a reply to another message

if (messageToProcess.ReferencedMessage.Author == client.CurrentUser)
{
// The message is a reply to the bot, add it to the list and set the replied message as the current message in the chain
messages.Add(messageToProcess);
messageToProcess = messageToProcess.ReferencedMessage;
continue;
}

if (messageToProcess.ReferencedMessage.Author == message.Author)
{
// The message is a reply to the user, add it to the list and set the replied message as the current message in the chain
messages.Add(messageToProcess);
messageToProcess = messageToProcess.ReferencedMessage;
continue;
}
}

// The message is not a reply to the bot, and is not a reply to the user. This is the end of the chain
messages.Add(messageToProcess);
break;
}

messages.Reverse();

// I can't be bothered to create a class for this so we're gonna be using dictionaries
var messagesFormatted = new List<Dictionary<string, string>>();

foreach (IUserMessage userMessage in messages)
{
if (userMessage.Author == client.CurrentUser)
{
messagesFormatted.Add(new Dictionary<string, string>()
{
{ "role", "assistant" },
{ "message", userMessage.Content }
});
}
else if (userMessage.Author == message.Author)
{
messagesFormatted.Add(new Dictionary<string, string>()
{
{ "role", "user" },
{ "message", userMessage.Content }
});
}
else
{
throw new InvalidOperationException("what the fuck is this?");
}
}

using HttpResponseMessage response =
await HttpClient.PostAsJsonAsync(
"http://localhost:3636/api/v1/completeChat", new
{
conversation = messagesFormatted,
username = message.Author.Username
});

return;
}

if (message.Content.Trim() == client.CurrentUser.Mention)
{
await message.ReplyAsync("""
Expand Down Expand Up @@ -138,4 +213,4 @@ await HttpClient.PostAsJsonAsync(

return responseJson;
}
}
}

0 comments on commit 8db7ae7

Please sign in to comment.