aboutsummaryrefslogtreecommitdiffstats
path: root/transformer
diff options
context:
space:
mode:
authorterminaldweller <devi@terminaldweller.com>2023-09-06 07:30:36 +0000
committerterminaldweller <devi@terminaldweller.com>2023-09-06 07:30:36 +0000
commit5d3a131d2f334a5db9b68a941af2a46889c86cd6 (patch)
treeca9e584c7400db2dcbf657f57346ed3e6887a55f /transformer
parentremoved the fork, the script is no longer dog slow (diff)
downloadirssi-scripts-master.tar.gz
irssi-scripts-master.zip
added two new scripts, transformer and gotifyHEADmaster
Diffstat (limited to '')
-rw-r--r--transformer/transformer.py133
1 files changed, 133 insertions, 0 deletions
diff --git a/transformer/transformer.py b/transformer/transformer.py
new file mode 100644
index 0000000..4c46357
--- /dev/null
+++ b/transformer/transformer.py
@@ -0,0 +1,133 @@
+import irssi
+import json
+import typing
+import urllib
+from urllib import request, parse
+
+IRSSI = {
+ "author": "terminaldweller",
+ "contact": "https://terminaldweller.com",
+ "name": "transformer",
+ "description": "transforms incoming text in the channel",
+ "license": "GPL3 or newer",
+ "url": "https://github.com/irssi/scripts.irssi.org",
+}
+
+
+def do_post(url: str, content: bytes, target: bytes, nick: bytes) -> None:
+ api_key = irssi.settings_get_str(b"transformer_api_key")
+ api_key = api_key.decode("utf-8")
+ model = irssi.settings_get_str(b"transformer_model")
+ role = irssi.settings_get_str(b"transformer_role")
+ temp = irssi.settings_get_int(b"transformer_temperature")
+ n = irssi.settings_get_int(b"transformer_n")
+ debug = irssi.settings_get_bool(b"transformer_debug")
+ prompt_system = irssi.settings_get_str(b"transformer_prompt_system")
+ prompt_user = irssi.settings_get_str(b"transformer_prompt_user")
+
+ headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
+
+ data = {
+ "model": model.decode("utf-8"),
+ "temperature": temp / 1000.0,
+ "n": n,
+ "messages": [
+ {
+ "role": "system",
+ "content": prompt_system.decode("utf-8") + content.decode("utf-8"),
+ },
+ {
+ "role": "user",
+ "content": prompt_user.decode("utf-8") + content.decode("utf-8"),
+ },
+ ],
+ }
+
+ result = ""
+ post_data = json.dumps(data).encode("utf-8")
+ req = request.Request(url, post_data, headers, method="POST")
+ try:
+ resp = request.urlopen(req)
+ json_response = json.load(resp)
+ window = irssi.window_find_item(target)
+ window.prnt(
+ bytes("%N%z005faf%k %9Transformed %N%Z005faf%0%N", encoding="utf-8")
+ )
+ for choice in json_response["choices"]:
+ result = choice["message"]["content"]
+ if result != "":
+ window.prnt(
+ bytes(nick.decode("utf-8") + " >>> " + result, encoding="utf-8")
+ )
+ except urllib.error.HTTPError as e:
+ resp = e.read().decode("utf-8")
+ json_response = json.load(resp)
+ print(json_response)
+
+
+def transformer_sig_handler(*args, **kwargs) -> None:
+ URL = "https://api.openai.com/v1/chat/completions"
+ server = args[0]
+ msg = args[1]
+ nick = args[2]
+ address = args[3]
+ target = args[4]
+ channels = (
+ irssi.settings_get_str(b"transformer_channel_list").decode("utf-8").split(" ")
+ )
+ source = server.tag + b"/" + target
+ if any(source.decode("utf-8") in channel for channel in channels):
+ do_post(URL, msg, target, nick)
+
+
+def run_on_script_load() -> None:
+ irssi.settings_add_bool(
+ b"misc",
+ b"transformer_debug",
+ False,
+ )
+ irssi.settings_add_str(
+ b"misc",
+ b"transformer_api_key",
+ b"",
+ )
+ irssi.settings_add_str(
+ b"misc",
+ b"transformer_model",
+ b"gpt-3.5-turbo",
+ )
+ irssi.settings_add_str(
+ b"misc",
+ b"transformer_role",
+ b"user",
+ )
+ irssi.settings_add_int(
+ b"misc",
+ b"transformer_temperature",
+ 700,
+ )
+ irssi.settings_add_int(
+ b"misc",
+ b"transformer_n",
+ 1,
+ )
+ irssi.settings_add_str(
+ b"misc",
+ b"transformer_prompt_system",
+ b"if you can't translate parts of the provided text use the original piece of text. the text will ocassionally inlcude URLs. if the original text is in the target language, return an empty response. Do not ask for follow up question.",
+ )
+ irssi.settings_add_str(
+ b"misc",
+ b"transformer_prompt_user",
+ b"translate this into english: ",
+ )
+ irssi.settings_add_str(
+ b"misc",
+ b"transformer_channel_list",
+ b"",
+ )
+
+ irssi.signal_add(b"message public", transformer_sig_handler)
+
+
+run_on_script_load()