Happy Saturday,
https://github.com/samfisherirl/AutoHot-GPT
I've been reading on the Forum and tracking various posts about ChatGPT and it's accuracy with autohotkey. I understand that there are many challenges in the synthesis of code but nonetheless I've been knocking around in my head the different ways one could go about solving for this. Lately I've been working with the ahk V2 script converter, and it's extremely well made. We also have this universe of over a decade if not two of autohotkey scripts and forum post that ChatGPT definitely has absorbed, but it just doesn't come out well. So this is what I came up with. I'll be adding credits to the converter creators, a better read me, etc.
Asking for Autohotkey functions and Scripts, running that data through the autohotkey converter, and returning clean, unerred v2 code.
Like any good developer, instead of testing it robustly I just Dove right in. I've got a working model to work relatively stably just for testing.
How this works
I have for my convenience used python as the ChatGPT bot communicator. I know there are a few AHK gpt bots on github, but for the first run through I chose python.
Once the python script grabs data from ChatGPT, ahk is listening and ready to convert in real time to ahkv2.
that code gets displayed in the console, it gets sent to your clipboard to copy or paste, and it gets written to a file right next to the app.
That's all I have for now, I just wanted to get this post out to see if it's worth trudging forward with this app or if my idea is really dumb. I appreciate anyone who took the time to read this Thank you.
Below are some examples.
Building out the github, attached is the zip with the python exe and the raw ahk.
Code: Select all
#Requires Autohotkey v2.0
#SingleInstance Force
#Include chatgpt_ahk_generator\convert\ConvertFuncs.ahk
#Include chatgpt_ahk_generator\convert\_menu_handler_mod.ahk
exe := "`"" A_ScriptDir "\chatgpt_ahk_generator\AutoHotKey Exe\AutoHotkeyV1.exe`" "
exe2 := "`"" A_ScriptDir "\chatgpt_ahk_generator\AutoHotKey Exe\AutoHotkeyV2.exe`" "
autogui := "`"" A_ScriptDir "\chatgpt_ahk_generator\AutoGUI.ahk`""
logs := A_ScriptDir "\chatgpt_ahk_generator\log.txt"
app := A_ScriptDir "\chatgpt_ahk_generator\AutoHot-ChatGPT.exe"
empty := A_ScriptDir "\chatgpt_ahk_generator\empty.txt"
temps := A_ScriptDir "\chatgpt_ahk_generator\temp.txt"
ret := A_ScriptDir "\chatgpt_ahk_generator\return.txt"
sets := A_ScriptDir "\chatgpt_ahk_generator\AutoGUI.ini"
runscript := A_ScriptDir "\chatgpt_ahk_generator\runscript.ahk"
if FileExist(logs) {
FileDelete(logs)
}
if FileExist(temps) {
FileDelete(temps)
}
if FileExist(ret) {
FileDelete(ret)
}
Run(app, A_ScriptDir "\chatgpt_ahk_generator", , &PID) ; run the concatenated command, which launches AutoGUI
Sleep(1000) ; wait for 1 second
findProcess(PID)
While ProcessExist(PID) ; while the AutoGUI process exists
; wait for %logs% to exist, that means AutoGui is trying to generate code.
; this loop will convert to v2 and notify AutoGUI via %retstat%
; wait for %logs% to exist, that means AutoGui is trying to generate code.
; this loop will convert to v2 and notify AutoGUI via %retstat%
{
if FileExist(logs) ; check if the log file exists
{
inscript := tryRead(logs) ; read the contents of the log file into a variable
if (inscript != "") ; if the variable is not empty
{
FileMove(logs, "temps.txt", 1) ; move the log file to the temporary file
Converter(inscript)
sleep(10)
} }
else {
Sleep(10)
continue
}
}
ExitApp
Converter(inscript) {
global ret
script := Convert(inscript) ; convert the script from AHK v1 to AHK v2
; final_code := add_menuhandler(script) ; add menu handlers to the script
outfile := FileOpen(temps, "w", "utf-8") ; open the file for writing
outfile.Write(script) ; write the final code to the file
outfile.Close() ; close the file
FileCopy(temps, ret, 1) ; append the return status to the return status file
FileCopy(temps, A_ScriptDir "\code.ahk", 1) ; append the return status to the return status file
A_Clipboard := ""
A_Clipboard := script
}
setDesignMode(ini) {
replaceSettings := ""
x := 0
Loop Parse, ini, "`n", "`r" {
if (x == 0) && InStr(A_LoopField, "DesignMode") {
replaceSettings .= "DesignMode=1`n"
}
else {
replaceSettings .= A_LoopField "`n"
}
}
f := FileOpen(sets, "w", "utf-8")
f.Write(replaceSettings)
f.Close()
}
findProcess(PID) {
Loop 10 { ; loop up to 10 times
if ProcessExist(PID) { ; check if the AutoGUI process exists
break ; if it does, break out of the loop
}
else {
Sleep(10) ; if it doesn't, wait for 1 second and check again
}
}
}
;try {out := FileRead(path)}
tryRead(path) {
try {
out := FileRead(path)
return out
}
catch {
Sleep(10)
return ""
}
}
python
Code: Select all
from revChatGPT.V3 import Chatbot
from pathlib import Path
cwd = Path(__file__).parent
from shutil import copy, move
from time import sleep
class Chat:
def __init__(self):
self.key = self.keyGet()
self.input = ""
self.output = ""
self.code = ""
self.ahk_mod = "Develope an autohotkey script. "
self.kill_loop = False
self.temp = cwd / "ahk.txt"
self.v1 = cwd / "v1.ahk"
self.v2 = cwd / "v2.ahk"
self.myscript = cwd / "MyScriptv2.ahk"
def keyGet(self):
try:
with open(cwd / "key.txt", "r") as f:
key = str(f.read())
return str(key.split("=")[1].strip())
except Exception as e:
print("\n\nNo key found. Make sure there's a text file named key.txt\napi_key=xxxxxxxxxx\n\n" + str(e))
return False
def ask_user(self):
if self.key:
self.input = input("\nType exit to quit. Tell me about your AHKv2 script need: ")
self.input = self.input.lower().strip()
if self.input == "exit" or self.input == "quit":
self.kill_loop = True
else:
self.input = self.ahk_mod + self.input
def process_chat(self):
how_much_code = self.output.count("```")
if how_much_code == 0:
print(self.output)
elif how_much_code > 0:
x = how_much_code / 2
print(self.output)
ar = self.output.split("```")
for i in range(1, len(ar)-1, 2):
self.code = self.code + ar[i]
with open(self.temp, 'w') as f:
f.write(self.code)
copy(self.temp, self.v1)
while True:
if self.v2.is_file():
copy(self.v2, self.temp)
with open(self.temp, "r") as f:
self.code = f.read()
self.code = "\n\nHere is your AHKv2 code. This is also locate in your clipboard and next to this app:\n\n" + self.code.strip() + "\n"
print(self.code)
move(self.v2, self.myscript)
break
else:
sleep(0.1)
def loop_ask():
chat = Chat()
key = chat.key
chatbot = Chatbot(api_key=key, engine="gpt-3.5-turbo")
while True:
chat.ask_user()
if chat.kill_loop:
break
if chat.input:
chat.output = chatbot.ask(chat.input)
chat.process_chat()
if __name__ == "__main__":
loop_ask()