Update bot.py
asynchronous processing and a basic structure for handling user-specific contexts. Needs further testing.
This commit is contained in:
parent
b101e7b7f0
commit
c5eec48876
62
bot.py
62
bot.py
|
@ -5,7 +5,9 @@ import configparser
|
||||||
import openai
|
import openai
|
||||||
import time
|
import time
|
||||||
import textwrap
|
import textwrap
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
# Configuration and Initialization
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read('/path/to/config/config.ini')
|
config.read('/path/to/config/config.ini')
|
||||||
openai.api_key = config['openai']['api_key']
|
openai.api_key = config['openai']['api_key']
|
||||||
|
@ -16,7 +18,7 @@ botnick = "Nickname"
|
||||||
nspass = "NICKSERV_PASSWORD"
|
nspass = "NICKSERV_PASSWORD"
|
||||||
modeset = "+B"
|
modeset = "+B"
|
||||||
|
|
||||||
print(channel)
|
# Connect to IRC
|
||||||
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
irc.connect((server, 6667))
|
irc.connect((server, 6667))
|
||||||
irc.send(bytes("USER "+ botnick +" "+ botnick +" "+ botnick + " " + botnick + "\n", "UTF-8"))
|
irc.send(bytes("USER "+ botnick +" "+ botnick +" "+ botnick + " " + botnick + "\n", "UTF-8"))
|
||||||
|
@ -27,24 +29,10 @@ time.sleep(5)
|
||||||
irc.send(bytes("MODE "+ botnick +" "+ modeset +"\n", "UTF-8"))
|
irc.send(bytes("MODE "+ botnick +" "+ modeset +"\n", "UTF-8"))
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
irc.send(bytes("JOIN "+ channel +" "+ server +"\n", "UTF-8"))
|
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)
|
|
||||||
|
|
||||||
|
|
||||||
# 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")
|
|
||||||
|
|
||||||
|
|
||||||
# Now start responding to new messages
|
|
||||||
|
|
||||||
previous_messages = []
|
|
||||||
max_history_length = 10
|
|
||||||
|
|
||||||
|
# Message Handling
|
||||||
|
async def handle_messages():
|
||||||
|
user_contexts = {}
|
||||||
while True:
|
while True:
|
||||||
text = irc.recv(2040).decode("UTF-8")
|
text = irc.recv(2040).decode("UTF-8")
|
||||||
print(text)
|
print(text)
|
||||||
|
@ -53,15 +41,25 @@ while True:
|
||||||
irc.send(bytes('PONG ' + text.split()[1] + '\r\n', "UTF-8"))
|
irc.send(bytes('PONG ' + text.split()[1] + '\r\n', "UTF-8"))
|
||||||
|
|
||||||
if "PRIVMSG" in text and botnick in text:
|
if "PRIVMSG" in text and botnick in text:
|
||||||
prompt = text.split(botnick)[1].strip()
|
username, prompt = parse_message(text)
|
||||||
previous_messages.append({"role": "system", "content": "You are a IRC chat bot for interacting with users using short answers only"})
|
context = user_contexts.get(username, [])
|
||||||
previous_messages.append({"role": "user", "content": prompt})
|
|
||||||
|
|
||||||
if len(previous_messages) > max_history_length:
|
context.append({"role": "user", "content": prompt})
|
||||||
previous_messages.pop(0)
|
if len(context) > max_history_length:
|
||||||
response = openai.ChatCompletion.create(
|
context.pop(0)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
async def get_openai_response(context):
|
||||||
|
return openai.ChatCompletion.create(
|
||||||
model="gpt-3.5-turbo",
|
model="gpt-3.5-turbo",
|
||||||
messages=previous_messages,
|
messages=context,
|
||||||
temperature=0.85,
|
temperature=0.85,
|
||||||
max_tokens=150,
|
max_tokens=150,
|
||||||
top_p=1,
|
top_p=1,
|
||||||
|
@ -69,9 +67,13 @@ while True:
|
||||||
presence_penalty=1.0,
|
presence_penalty=1.0,
|
||||||
)
|
)
|
||||||
|
|
||||||
print("Sending:", response)
|
def parse_message(text):
|
||||||
reply = response['choices'][0]['message']['content']
|
# Implement message parsing logic here
|
||||||
print(reply)
|
pass
|
||||||
|
|
||||||
for chunk in textwrap.wrap(reply, 200):
|
# Main
|
||||||
irc.send(bytes("PRIVMSG "+ channel +" :"+ chunk +"\n", "UTF-8"))
|
async def main():
|
||||||
|
await handle_messages()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
|
|
Loading…
Reference in New Issue