insert better commit message here

main
nebula 2024-12-15 01:00:05 -06:00
parent 186da00054
commit 329f1f9d59
2 changed files with 44 additions and 39 deletions

View File

@ -0,0 +1,7 @@
{
"llama_key": "Bearer $KEY",
"nick": "test_cube",
"channels": [
"#test"
]
}

48
main.py
View File

@ -1,33 +1,20 @@
# what you have:
# QuizBot: answer "true" or "false": tilde.town is primarily run by ~vilmibomb
# QuizBot: AI response: TRUE
# RealHuman: false
# QuizBot: the correct answer is FALSE!
#
# what I'd prefer:
# QuizBot: will LLM answer "correct" or "incorrect": tilde.town is primarily run by ~vilmibomb
# RealHuman: correct
# QuizBot: your guess is CORRECT! the LLM answered: "TRUE! tilde.town is run by vilmibm"
from random import choice from random import choice
import requests import requests
from time import sleep from time import sleep
import socket import socket
import re import re
from base64 import b64decode
from json import dump, load, dumps from json import dump, load, dumps
try: try:
with open("secrets.json", "r") as f: with open("config.json", "r") as f:
secrets = load(f) config = load(f)
except FileNotFoundError: except FileNotFoundError:
exit("Please create secrets.json with api key(s)") exit("Please create config.json with api key(s) and channels")
llama_url = "https://llama.mcopp.com/v1/chat/completions" llama_url = "https://llama.mcopp.com/v1/chat/completions"
llama_headers = { llama_headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": secrets["llama_key"] "Authorization": config["llama_key"]
} }
channel_re = re.compile(r"PRIVMSG (#\w+)") channel_re = re.compile(r"PRIVMSG (#\w+)")
@ -36,13 +23,10 @@ llm_answer_re = re.compile(r"^(true|false)")
host = "localhost" host = "localhost"
port = 6667 port = 6667
nick = "the_cube" nick = config["nick"]
realname = "a bot by ~nebula" realname = "a bot by ~nebula"
helptext = "!trivia, !trscores, !aitrivia, !aiscores for trivia game. contact ~nebula for help, feedback or problem reports." helptext = "!trivia, !trscores, !aitrivia, !aiscores for trivia game. contact ~nebula for help, feedback or problem reports."
channels = [ channels = config["channels"]
"#tildetown",
"#bots"
]
url = "https://opentdb.com/api.php?amount=50&type=boolean&encode=base64" url = "https://opentdb.com/api.php?amount=50&type=boolean&encode=base64"
questions_file = "trivia.questions" questions_file = "trivia.questions"
@ -277,13 +261,14 @@ def llama_response(question):
return response["choices"][0]["message"]["content"] return response["choices"][0]["message"]["content"]
class IRCBot(): class IRCBot():
def __init__(self, nick, realname, helptext, commands, channels): def __init__(self, nick, realname, helptext, commands, searchers, channels):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((host, port)) self.s.connect((host, port))
self.nick = nick self.nick = nick
self.realname = realname self.realname = realname
self.helptext = helptext self.helptext = helptext
self.commands = commands self.commands = commands
self.searchers = searchers
self.channels = channels self.channels = channels
self.sendline(f"NICK {self.nick}") self.sendline(f"NICK {self.nick}")
self.sendline(f"USER {self.nick} 0 * :{self.realname}") self.sendline(f"USER {self.nick} 0 * :{self.realname}")
@ -316,8 +301,14 @@ class IRCBot():
self.sendline(pong) self.sendline(pong)
continue continue
channel_search = channel_re.search(line) channel_search = channel_re.search(line)
if channel_search: if not channel_search:
continue
channel = channel_search.group(1) channel = channel_search.group(1)
try:
message_body = line[line.index(" :") + 2:]
except Exception as e:
print(e)
message_body = ""
if line.endswith("!rollcall"): if line.endswith("!rollcall"):
self.send(channel, self.helptext) self.send(channel, self.helptext)
continue continue
@ -331,13 +322,17 @@ class IRCBot():
result = callback(channel, name) result = callback(channel, name)
if result: if result:
self.send(channel, result) self.send(channel, result)
if message_body:
for callback in self.searchers:
callback(message_body)
def run(): def run():
bot = IRCBot( bot = IRCBot(
nick, nick,
realname, realname,
helptext, helptext,
[ [ # endswith commands
("!trivia", post_question), ("!trivia", post_question),
("!aitrivia", post_ai_question), ("!aitrivia", post_ai_question),
("!trscores", post_top_scores), ("!trscores", post_top_scores),
@ -347,6 +342,9 @@ def run():
("right", answer_correct), ("right", answer_correct),
("wrong", answer_incorrect) ("wrong", answer_incorrect)
], ],
[ # message searchers
# empty
],
channels channels
) )