kdalanon / ChatGPT-AutoHotkey-Utility

An AutoHotkey script that uses ChatGPT API to process text.
GNU General Public License v3.0
72 stars 11 forks source link

Issue with JSON escaping #7

Closed angeloprado closed 3 months ago

angeloprado commented 7 months ago

For certain text, JSON escaping functionality fails.

Please consider updating ProcessRequest as follows:


/*
====================================================
Connect to ChatGPT API and process request
====================================================
*/

ProcessRequest(ChatGPT_Prompt, Status_Message, API_Model, Retry_Status) {
    if (Retry_Status != "Retry") {
        A_Clipboard := ""
        Send "^c"
        if !ClipWait(2) {
            MsgBox "The attempt to copy text onto the clipboard failed."
            return
        }
        CopiedText := A_Clipboard
        ChatGPT_Prompt := ChatGPT_Prompt "`n`n" CopiedText
        ChatGPT_Prompt := RegExReplace(ChatGPT_Prompt, '(\\|")+', '\$1') ; Clean back spaces and quotes
        ChatGPT_Prompt := RegExReplace(ChatGPT_Prompt, "`n", "\n") ; Clean newlines
        ChatGPT_Prompt := RegExReplace(ChatGPT_Prompt, "`r", "") ; Remove carriage returns
        global Previous_ChatGPT_Prompt := ChatGPT_Prompt
        global Previous_Status_Message := Status_Message
        global Previous_API_Model := API_Model
        global Response_Window_Status
    }

    OnMessage 0x200, WM_MOUSEHOVER
    Response.Value := Status_Message
    if (Response_Window_Status = "Closed") {
        Response_Window.Show("AutoSize Center")
        Response_Window_Status := "Open"
        RetryButton.Enabled := 0
        CopyButton.Enabled := 0
    }    
    DllCall("SetFocus", "Ptr", 0)

    global HTTP_Request := ComObject("WinHttp.WinHttpRequest.5.1")
    HTTP_Request.open("POST", API_URL, true)
    HTTP_Request.SetRequestHeader("Content-Type", "application/json")
    HTTP_Request.SetRequestHeader("Authorization", "Bearer " API_Key)
    Messages := '{ "role": "user", "content": "' ChatGPT_Prompt '" }'
    JSON_Request := '{ "model": "' API_Model '", "messages": [' Messages '] }'
    HTTP_Request.SetTimeouts(60000, 60000, 60000, 60000)
    HTTP_Request.Send(JSON_Request)
    SetTimer LoadingCursor, 1
    if WinExist("Response") {
        WinActivate "Response"
    }
    HTTP_Request.WaitForResponse
    try {
        if (HTTP_Request.status == 200) {
            SafeArray := HTTP_Request.responseBody
        pData := NumGet(ComObjValue(SafeArray) + 8 + A_PtrSize, 'Ptr')
        length := SafeArray.MaxIndex() + 1
        JSON_Response := StrGet(pData, length, 'UTF-8')
            var := Jxon_Load(&JSON_Response)
            JSON_Response := var.Get("choices")[1].Get("message").Get("content")
            RetryButton.Enabled := 1
            CopyButton.Enabled := 1
            Response.Value := JSON_Response

            SetTimer LoadingCursor, 0
            OnMessage 0x200, WM_MOUSEHOVER, 0
            Cursor := DllCall("LoadCursor", "uint", 0, "uint", 32512) ; Arrow cursor
            DllCall("SetCursor", "UPtr", Cursor)

            Response_Window.Flash()
            DllCall("SetFocus", "Ptr", 0)
        } else {
            RetryButton.Enabled := 1
            CopyButton.Enabled := 1
            Response.Value := "Status " HTTP_Request.status " " HTTP_Request.responseText

            SetTimer LoadingCursor, 0
            OnMessage 0x200, WM_MOUSEHOVER, 0
            Cursor := DllCall("LoadCursor", "uint", 0, "uint", 32512) ; Arrow cursor
            DllCall("SetCursor", "UPtr", Cursor)

            Response_Window.Flash()
            DllCall("SetFocus", "Ptr", 0)
        }
    }
}
kdalanon commented 7 months ago

Will update this when I have time.

kdalanon commented 7 months ago

May I ask what lines need to be changed? I've diff them and there seems to be no change: https://www.diffchecker.com/PWgCfXS3/

angeloprado commented 7 months ago

Hi @kdalanon , I am having issues posting the code here due to markdown. Let me prepare a quick diff for you

angeloprado commented 7 months ago

@kdalanon here we go https://www.diffchecker.com/lW1zuGOh/

angeloprado commented 7 months ago

The relevant changes for you would be lines 185 and 208. Feel free to incorporate the other suggestions

kdalanon commented 6 months ago

The relevant changes for you would be lines 185 and 208. Feel free to incorporate the other suggestions

Thank you. I am now incorporating your suggestions. Can you provide some texts where JSON escaping functionality fails, so that I can test if the revised code is working?

Avastgard commented 1 month ago

Might want to include ChatGPT_Prompt := RegExReplace(ChatGPT_Prompt, "\t", "") ; Remove horizontal tabs to remove horizontal tabs. This is useful if you select text from a numbered list in Microsoft Word (my use case most of the time).