2023-04-09 07:53:11 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import socket
|
|
|
|
import configparser
|
|
|
|
import openai
|
|
|
|
import time
|
|
|
|
import textwrap
|
2024-01-22 16:13:28 +00:00
|
|
|
import asyncio
|
2023-04-09 07:53:11 +00:00
|
|
|
|
2024-01-22 16:13:28 +00:00
|
|
|
# Configuration and Initialization
|
2023-04-09 07:53:11 +00:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read('/path/to/config/config.ini')
|
|
|
|
openai.api_key = config['openai']['api_key']
|
|
|
|
|
|
|
|
server = "irc.newnet.net"
|
|
|
|
channel = "#channel"
|
|
|
|
botnick = "Nickname"
|
|
|
|
nspass = "NICKSERV_PASSWORD"
|
|
|
|
modeset = "+B"
|
|
|
|
|
2024-01-22 16:13:28 +00:00
|
|
|
# Connect to IRC
|
2023-04-09 07:53:11 +00:00
|
|
|
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
irc.connect((server, 6667))
|
|
|
|
irc.send(bytes("USER "+ botnick +" "+ botnick +" "+ botnick + " " + botnick + "\n", "UTF-8"))
|
|
|
|
irc.send(bytes("NICK "+ botnick +"\n", "UTF-8"))
|
|
|
|
time.sleep(5)
|
|
|
|
irc.send(bytes("NS IDENTIFY "+ nspass +"\n", "UTF-8"))
|
|
|
|
time.sleep(5)
|
|
|
|
irc.send(bytes("MODE "+ botnick +" "+ modeset +"\n", "UTF-8"))
|
|
|
|
time.sleep(5)
|
|
|
|
irc.send(bytes("JOIN "+ channel +" "+ server +"\n", "UTF-8"))
|
|
|
|
|
2024-01-22 16:13:28 +00:00
|
|
|
# Message Handling
|
|
|
|
async def handle_messages():
|
|
|
|
user_contexts = {}
|
|
|
|
while True:
|
|
|
|
text = irc.recv(2040).decode("UTF-8")
|
|
|
|
print(text)
|
2023-04-09 07:53:11 +00:00
|
|
|
|
2024-01-22 16:13:28 +00:00
|
|
|
if text.find('PING') != -1:
|
|
|
|
irc.send(bytes('PONG ' + text.split()[1] + '\r\n', "UTF-8"))
|
2023-04-09 07:53:11 +00:00
|
|
|
|
2024-01-22 16:13:28 +00:00
|
|
|
if "PRIVMSG" in text and botnick in text:
|
|
|
|
username, prompt = parse_message(text)
|
|
|
|
context = user_contexts.get(username, [])
|
2023-04-09 07:53:11 +00:00
|
|
|
|
2024-01-22 16:13:28 +00:00
|
|
|
context.append({"role": "user", "content": prompt})
|
|
|
|
if len(context) > max_history_length:
|
|
|
|
context.pop(0)
|
2023-04-09 07:53:11 +00:00
|
|
|
|
2024-01-22 16:13:28 +00:00
|
|
|
response = await get_openai_response(context)
|
|
|
|
reply = response['choices'][0]['message']['content']
|
|
|
|
print(reply)
|
2023-04-09 07:53:11 +00:00
|
|
|
|
2024-01-22 16:13:28 +00:00
|
|
|
for chunk in textwrap.wrap(reply, 200):
|
|
|
|
irc.send(bytes("PRIVMSG "+ channel +" :"+ chunk +"\n", "UTF-8"))
|
|
|
|
user_contexts[username] = context
|
2023-04-09 07:53:11 +00:00
|
|
|
|
2024-01-22 16:13:28 +00:00
|
|
|
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,
|
|
|
|
)
|
2023-04-09 07:53:11 +00:00
|
|
|
|
2024-01-22 16:13:28 +00:00
|
|
|
def parse_message(text):
|
|
|
|
# Implement message parsing logic here
|
|
|
|
pass
|
2023-04-09 07:53:11 +00:00
|
|
|
|
2024-01-22 16:13:28 +00:00
|
|
|
# Main
|
|
|
|
async def main():
|
|
|
|
await handle_messages()
|
2023-04-09 07:53:11 +00:00
|
|
|
|
2024-01-22 16:13:28 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
asyncio.run(main())
|