From c5eec48876ef0e579c783d081144cfe5793405f0 Mon Sep 17 00:00:00 2001 From: deepend-tildeclub <58404188+deepend-tildeclub@users.noreply.github.com> Date: Mon, 22 Jan 2024 09:13:28 -0700 Subject: [PATCH 1/2] Update bot.py asynchronous processing and a basic structure for handling user-specific contexts. Needs further testing. --- bot.py | 80 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/bot.py b/bot.py index 07ef263..b721a97 100644 --- a/bot.py +++ b/bot.py @@ -5,7 +5,9 @@ import configparser import openai import time import textwrap +import asyncio +# Configuration and Initialization config = configparser.ConfigParser() config.read('/path/to/config/config.ini') openai.api_key = config['openai']['api_key'] @@ -16,7 +18,7 @@ botnick = "Nickname" nspass = "NICKSERV_PASSWORD" modeset = "+B" -print(channel) +# Connect to IRC irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) irc.connect((server, 6667)) irc.send(bytes("USER "+ botnick +" "+ botnick +" "+ botnick + " " + botnick + "\n", "UTF-8")) @@ -27,51 +29,51 @@ time.sleep(5) irc.send(bytes("MODE "+ botnick +" "+ modeset +"\n", "UTF-8")) time.sleep(5) irc.send(bytes("JOIN "+ channel +" "+ server +"\n", "UTF-8")) -response = irc.recv(2040).decode("UTF-8") -if "JOIN " + channel in response: - print("Joined channel", channel) -else: - print("Error joining channel", channel) +# Message Handling +async def handle_messages(): + user_contexts = {} + while True: + text = irc.recv(2040).decode("UTF-8") + print(text) -# Read and discard any messages in the channel for the next 30 seconds -start_time = time.time() -while time.time() - start_time < 15: - text = irc.recv(2040).decode("UTF-8") + if text.find('PING') != -1: + irc.send(bytes('PONG ' + text.split()[1] + '\r\n', "UTF-8")) + if "PRIVMSG" in text and botnick in text: + username, prompt = parse_message(text) + context = user_contexts.get(username, []) -# Now start responding to new messages + context.append({"role": "user", "content": prompt}) + if len(context) > max_history_length: + context.pop(0) -previous_messages = [] -max_history_length = 10 + response = await get_openai_response(context) + reply = response['choices'][0]['message']['content'] + print(reply) -while True: - text=irc.recv(2040).decode("UTF-8") - print(text) + for chunk in textwrap.wrap(reply, 200): + irc.send(bytes("PRIVMSG "+ channel +" :"+ chunk +"\n", "UTF-8")) + user_contexts[username] = context - if text.find('PING') != -1: - irc.send(bytes('PONG ' + text.split()[1] + '\r\n', "UTF-8")) +async def get_openai_response(context): + return openai.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=context, + temperature=0.85, + max_tokens=150, + top_p=1, + frequency_penalty=0.5, + presence_penalty=1.0, + ) - if "PRIVMSG" in text and botnick in text: - prompt = text.split(botnick)[1].strip() - previous_messages.append({"role": "system", "content": "You are a IRC chat bot for interacting with users using short answers only"}) - previous_messages.append({"role": "user", "content": prompt}) +def parse_message(text): + # Implement message parsing logic here + pass - if len(previous_messages) > max_history_length: - previous_messages.pop(0) - response = openai.ChatCompletion.create( - model="gpt-3.5-turbo", - messages=previous_messages, - temperature=0.85, - max_tokens=150, - top_p=1, - frequency_penalty=0.5, - presence_penalty=1.0, - ) +# Main +async def main(): + await handle_messages() - print("Sending:", response) - reply = response['choices'][0]['message']['content'] - print(reply) - - for chunk in textwrap.wrap(reply, 200): - irc.send(bytes("PRIVMSG "+ channel +" :"+ chunk +"\n", "UTF-8")) +if __name__ == "__main__": + asyncio.run(main()) From 8e07726cab4045636f23fc75f25c95bed1c79067 Mon Sep 17 00:00:00 2001 From: deepend-tildeclub <58404188+deepend-tildeclub@users.noreply.github.com> Date: Mon, 22 Jan 2024 09:17:25 -0700 Subject: [PATCH 2/2] Update bot.py including some message parsing logic, basic commands, maintain user-specific contexts, and provide more structured interaction with the IRC server --- bot.py | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/bot.py b/bot.py index b721a97..671dc0c 100644 --- a/bot.py +++ b/bot.py @@ -40,21 +40,27 @@ async def handle_messages(): if text.find('PING') != -1: irc.send(bytes('PONG ' + text.split()[1] + '\r\n', "UTF-8")) - if "PRIVMSG" in text and botnick in text: - username, prompt = parse_message(text) - context = user_contexts.get(username, []) + if "PRIVMSG" in text: + username, message = parse_message(text) + if username and message: + if message.startswith('!'): + await handle_command(username, message) + else: + await handle_conversation(username, message, user_contexts) - context.append({"role": "user", "content": prompt}) - if len(context) > max_history_length: - context.pop(0) +async def handle_conversation(username, message, user_contexts): + context = user_contexts.get(username, []) + context.append({"role": "user", "content": message}) + if len(context) > 10: # Adjust history length as needed + context.pop(0) - response = await get_openai_response(context) - reply = response['choices'][0]['message']['content'] - print(reply) + response = await get_openai_response(context) + reply = response['choices'][0]['message']['content'] + print(reply) - for chunk in textwrap.wrap(reply, 200): - irc.send(bytes("PRIVMSG "+ channel +" :"+ chunk +"\n", "UTF-8")) - user_contexts[username] = context + for chunk in textwrap.wrap(reply, 200): + irc.send(bytes("PRIVMSG "+ channel +" :"+ chunk +"\n", "UTF-8")) + user_contexts[username] = context async def get_openai_response(context): return openai.ChatCompletion.create( @@ -68,8 +74,21 @@ async def get_openai_response(context): ) def parse_message(text): - # Implement message parsing logic here - pass + if botnick in text: + parts = text.split('!') + username = parts[0].split(':')[1] + message = parts[1].split(':')[1] + return username, message + return None, None + +async def handle_command(username, command): + if command.startswith('!help'): + send_message(f"Hello {username}, I'm a GPT-powered IRC bot. Ask me anything!") + # Add more commands as needed + +def send_message(message): + for chunk in textwrap.wrap(message, 200): + irc.send(bytes("PRIVMSG "+ channel +" :"+ chunk +"\n", "UTF-8")) # Main async def main():