by garry » 07 Apr 2023, 15:16
Im Moment für mich OK, definiere Sprache (Answer in Edit2 ) , evtl Korrekturen nötig ... da keine Ahnung wie's funktioniert ...
EDIT : clear button angefügt
Code: Select all
;=====================================================================================================
;- ChatGPT-3 = Generative Pre-trained Transformer-3
;- Info: ChatGPT supports = English, Spanish, French, German, Italian, Portuguese, Dutch, Russian
;- it translates QUESTION first to english and show ANSWER in english and to your desired language
;- https://36kr.com/p/2196628560234373 2023-04-01 about GPT-4
;-----------------------------------------------------------------------------------------------------
;#Warn
#singleinstance force
SetWorkingDir, %A_ScriptDir%
;-------------------------------------------------------------------------
api_key := "INSERT_API_KEY_HERE"
api_url := "https://api.openai.com/v1/engines/text-davinci-003/completions" ;- The endpoint URL for the GPT-3 API -
AnswerLanguage:="de" ;- in edit-2
;----------------------------------------------------------------------------------------------------
if (api_key="INSERT_API_KEY_HERE")
{
msgbox, 262180,API-KEY needed,You must define the API-KEY`nAuthentication required / Please log in to access this page=`nhttps://platform.openai.com/account/api-keys`nURL open when hit 'YES'
ifmsgbox,NO
exitapp
run,https://platform.openai.com/account/api-keys
exitapp
}
;----------------------------------------------------------------------------------------------------
;e0:="Woher kommt das Wort Caran d'Ache ?" ;- wrong answer
;e0:="что значит caran d'ache?" ;- wrong answer
;e0:="Was heisst Caran d'Ache ?" ;- wrong answer
;e0:="Hi there, is it possible for you to predict the future ?"
;e0:="The cafeteria had 23 apples . If they used 20 to make lunch and bought 6 more , how many apples do they have ?" ;-9
;e0:="Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls . How many tennis balls does he have now ?" ;-11
;e0:="Радянщина чи боротьба за права: як з’явилося 8 березня та які альтернативи свята в Україні?"
;---------------------------- SHARP PC-1262 ----------------------------------------------------------
e0:="Wie berechne ich die Gegenkathete ? Zeige mir den Formel wenn die Ankathete und Winkel bekannt ist ." ;- GK=AK*TAN(grad)
;The formula for calculating the opposite leg of a right triangle when the adjacent leg and angle are known is:
;Opposite Leg = Adjacent Leg * Tan(Angle)
;e0:="Wie berechne ich die Hypotenuse ? Zeige mir den Formel wenn die Ankathete und Winkel bekannt ist ." ;- HY=AK/COS(grad)
;The formula for calculating the hypotenuse when the adjacent side and angle are known is:
;Hypotenuse = Adjacent Side / cos(Angle)
;e0:="Wie berechne ich den Winkel ? Zeige mir den Formel wenn die Ankathete und Gegenkathete bekannt ist ." ;- GRAD=ATN(GK/AK)
e0:="How calculate angle ? Show me formula if you know opposite side and adjacent side ."
;The formula for calculating the angle is:
;angle = arctan(opposite/adjacent) ; arctan is the inverse tangent function.
;e0:="Wie berechne ich die Ankathete ? Zeige mir den Formel wenn Gegenkathete und der Winkel bekannt ist ." ;- AK=GK/TAN(grad)
;The formula for calculating the opposite leg of a right triangle when the adjacent leg and angle are known is:
;Opposite Leg = Adjacent Leg * Tan(Angle)
;-----------------------------------------------------------------------------------------------------
If !FileExist(A_ScriptDir "\history\")
FileCreateDir,% A_ScriptDir "\history\"
;-----------------------------------------------------------------------------------------------------
curl := ComObjCreate("WinHttp.WinHttpRequest.5.1")
curl.Open("POST", api_url)
curl.SetRequestHeader("Content-Type", "application/json")
curl.SetRequestHeader("Authorization", "Bearer " api_key)
jsonY=
;----------
Gui, PleaseWait: +LastFound +AlwaysOnTop +Disabled -Caption +ToolWindow +hwndhGUIPleaseWait
Gui, PleaseWait: Color, FFFF00
Gui, PleaseWait: Add, Text,xm vTextA w200 Center, Please wait...
Gui, PleaseWait: Add, Text,xs vTextB w200 Center, Loading...
Gui, PleaseWait: Show,% "x300 y" (A_ScreenHeight/2),% hGUIPleaseWait
;----------
Gui, New, +hwndhGUIA +AlwaysOnTop ;- +Owner +LastFound +E0x20 +Border +Caption +Resize -ToolWindow, hGUIA
Gui,default
global jsony,doc,thisbeautifyjson
null:="",diff:=""
Gui, Font, s12
Gui, Add, Tab3,,Main|TranslateJson|ResultJson|Settings
Gui, Tab, 1
;---------------------------------------------------------------------
Gui, Add, Edit,-multi Section vSucheingabe hwndSucheingabe w540 r4,%e0% ; Deine Frage an ChatGPT ...
;----------------------------------------------------------------------
Gui, Add, Text, xs y+13 Section,Answer language:
Gui, Add, Edit,ys-5 vLangx w40 r1 center,%AnswerLanguage%
Gui,add,button,xs+250 ys-5 w120 h27 gClearx,CLEAR
Gui, Tab, 4
Gui, Add, Text,xs Section, API Key:
Gui, Add, Text,ys x+70, total_tokens:
Gui, Add, Edit,ys w270 vTextApiKey, Counter
Gui, Font, s11
;Gui, Add, Edit, xs vApi_key w540 h25 center Password,% api_key
Gui, Add, Edit, xs vApi_key w540 h25 center ,% api_key
Gui, Font, s12
Gui, Tab, 1
Gui, Add, Button, xs ys gA1 w200 h30 default Section, Ask
Gui, Add, CheckBox, ys vErgebnisdatei w200 h30 , Show result file
Gui, Add, Button, ys gEditScript w30 h30,% Chr(0xE115)
Gui, Add, Button, ys gReloadApp w60 h30,% "Reload"
;--
Gui, Add, Text,xs vAnswer, Answer ;- always en
Gui, Add, Edit, vAnswerEdit r19 w540,
;--
Gui, Add, Text, vTextTranslate w300, Translate en > Selected-Language
Gui, Add, Edit, vAnswerEdit2 r20 w540, ;- Answer in your selected Language
;----------------------------------------------------------
Gui, Tab, 2
Gui, Add, Text,, Json
Gui, Add, Edit, vJsonEdit r30 w540,
Gui, Tab, 3
Gui, Add, Text,, Result
Gui, Add, Edit, vResultEdit r30 w540,
;----------------------------------------------------------
Gui, Show, x94 y1,% a_scriptname " - GuiHWND: " hGUIA
Gui, PleaseWait: show, Hide
EM_SETCUEBANNER(Sucheingabe, a_space "Deine Frage an ChatGPT -- your question to ChatGPT")
;EM_SETCUEBANNER(HTranslateSucheingabe, "Request will be translated into English beforehand")
Return
;----------------------------------------------------------
Clearx:
GuiControl,, Sucheingabe
GuiControl,, AnswerEdit
GuiControl,, AnswerEdit2
GuiControl,, JsonEdit
GuiControl,, ResultEdit
return
;----------------------------------------------------------
EM_SETCUEBANNER(HWND, Text) {
; EM_SETCUEBANNER = 0x1501 -> msdn.microsoft.com/en-us/library/bb761639(v=vs.85).aspx
Return DllCall("SendMessage", "Ptr", HWND, "UInt", 0x1501, "Ptr", True, "WStr", Text, "Ptr")
}
;-------------------------------------------------------
EditScript:
try run, notepad.exe %A_ScriptName%
return
;-------------------------------------------------------
RadioGroupAuswahl:
return
;-------------------------------------------------------
ToggleListview:
return
;-------------------------------------------------------
ReloadApp:
Gui, Submit, NoHide
reload
return
;-------------------------------------------------------
A1:
Gui, Submit, NoHide
if Sucheingabe=
Return
GuiControlGet, Sucheingabe ; https://ahkde.github.io/docs/v1/lib/GuiControlGet.htm#ExNoControlID
try TranslateSucheingabe:=GoogleTranslate(sucheingabe, "auto", "en")
GuiControl,, TranslateSucheingabe,% TranslateSucheingabe
if TranslateSucheingabe
Sucheingabe := TranslateSucheingabe
AttemptNo := 1
Gui, PleaseWait: Show,% "x300 y" (A_ScreenHeight/2),% AttemptNo
;AttemptNo := 1
jsonY := thisJson(Sucheingabe)
curl.Send(jsonY)
JsonResult := curl.ResponseText
JsonResult:= FromUTF8(JsonResult)
thisArray := []
thisArray := JsonToAHK(JsonResult)
thisText := Trim((thisArray["choices", 1, "text"]), " `n`r`t") ; remove leading and trailing whitespaces
if thisText
GuiControl,, AnswerEdit, %thisText% ;- always en
else{
errorMessage := thisArray["error", "message"]
GuiControl,, AnswerEdit,% errorMessage
try errorMessageTranslate := GoogleTranslate(errorMessage, "auto", "de")
GuiControl,, AnswerEdit2, %errorMessageTranslate%
}
try thisTextTranslate := GoogleTranslate(thisText, "auto",langx)
;GuiControl,, lang,% "de"
GuiControl,, AnswerEdit2,% thisTextTranslate
try thisBeautifyJson := BeautifyJson(JsonResult)
GuiControl,, ResultEdit, %thisBeautifyJson%
Gui, PleaseWait: show, Hide
thisAPI_completion_tokens := thisArray["usage", "completion_tokens"]
thisAPI_prompt_tokens := thisArray["usage", "prompt_tokens"]
thisAPI_total_tokens := thisArray["usage", "total_tokens"]
thisAPI_created := thisArray["created"] ; "`t"
logprobs := thisArray["choices", 1, "logprobs"]
time := 1970
time += thisAPI_created, s
diff -= A_NowUTC, h
time += diff, h
FormatTime, TimeStamp, %time%, dd.MM.yyyy HH-mm-ss tt ; 24.März.2018 05-20-37
GuiControl,, TextApiKey,% "[ " thisAPI_total_tokens " ] " TimeStamp
logprobs := (logprobs=null) ? "null" : (thisArray["choices", 1, "logprobs"])
;msgbox % thisArray[1, 1, 9, 1, 1, 2]
this_ADD := "finish_reason:`n" thisArray["choices", 1, "finish_reason"] "`n"
. "################################################################`n"
. "index:`n" thisArray["choices", 1, "index"] "`n"
. "################################################################`n"
. "logprobs:`n" logprobs "`n"
. "################################################################`n"
. "text:`n" FromUTF8(thisText) "`n"
. "################################################################`n"
. "created:`n" thisArray["created"] a_space TimeStamp "`n"
. "################################################################`n"
. "id:`n" thisArray["id"] "`n"
. "################################################################`n"
. "model:`n" thisArray["model"] "`n"
. "################################################################`n"
. "object:`n" thisArray["object"] "`n"
. "################################################################`n"
. "completion_tokens:`n" thisArray["usage", "completion_tokens"] "`n"
. "################################################################`n"
. "prompt_tokens:`n" thisArray["usage", "prompt_tokens"] "`n"
. "################################################################`n"
. "total_tokens:`n" thisArray["usage", "total_tokens"] "`n"
. "################################################################`n"
GuiControlGet, Sucheingabe ; https://ahkde.github.io/docs/v1/lib/GuiControlGet.htm#ExNoControlID
GuiControlGet, MeinRadioA
GuiControlGet, MeinRadioB
GuiControlGet, MeinRadioC
GuiControlGet, ToggleListview
GuiControlGet, TextApiKey
GuiControlGet, Api_key
GuiControlGet, Answer
GuiControlGet, AnswerEdit
GuiControlGet, TextTranslate
GuiControlGet, AnswerEdit2
GuiControlGet, JsonEdit
GuiControlGet, ResultEdit
allVars := "Sucheingabe:`n" Sucheingabe "`n"
. "################################################################`n"
. "MeinRadioA:`n" MeinRadioA "`n"
. "################################################################`n"
. "MeinRadioB:`n" MeinRadioB "`n"
. "################################################################`n"
. "MeinRadioC:`n" MeinRadioC "`n"
. "################################################################`n"
. "ToggleListview:`n" ToggleListview "`n"
. "################################################################`n"
. "TextApiKey:`n" TextApiKey "`n"
. "################################################################`n"
. "Api_key:`n" "******************************`n"
. "Api_key:`n" Api_key "`n"
. "################################################################`n"
. "AnswerEdit:`n" AnswerEdit "`n"
. "################################################################`n"
. "AnswerEdit2:`n" AnswerEdit2 "`n"
. "################################################################`n"
. "Json File von ChatGPT:`n" ResultEdit "`n"
. "################################################################`n"
. "Json File der Google Übersetzung:`n" JsonEdit "`n"
this_ADD := this_ADD allVars
Anow := A_now
if Ergebnisdatei
{
fileappend,% this_ADD,% a_scriptdir "\history\" Anow " Ergebnisdatei.txt",utf-8
try run,% a_scriptdir "\history\" Anow " Ergebnisdatei.txt"
}
Return
;-------------------------------------------------------
FromUTF8(ByRef Txt, CP := "CP28591") { ; ISO-8859-1
Size := StrPut(Txt, CP)
VarSetCapacity(UTF8, Size, 0)
StrPut(Txt, &UTF8, CP)
Return StrGet(&UTF8, "UTF-8")
}
;-------------------------------------------------------
ttStop:
toolTip
return
;-------------------------------------------------------
GuiClose:
ExitApp
;-------------------------------------------------------
esc::exitapp
;-------------------------------------------------------
JsonToAHK(json, rec := false) {
static doc := ComObjCreate("htmlfile")
, __ := doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
, JS := doc.parentWindow
if !rec
obj := %A_ThisFunc%(JS.eval("(" . json . ")"), true)
else if !IsObject(json)
obj := json
else if JS.Object.prototype.toString.call(json) == "[object Array]" {
obj := []
Loop % json.length
obj.Push( %A_ThisFunc%(json[A_Index - 1], true) )
}
else {
obj := {}
keys := JS.Object.keys(json)
Loop % keys.length {
k := keys[A_Index - 1]
obj[k] := %A_ThisFunc%(json[k], true)
}
}
Return obj
}
;-------------------------------------------------------
thisJson(ByRef Sucheingabe := "PROMPT") {
; Build the JSON payload
Sucheingabe := RegExReplace(Sucheingabe, "\""", """""")
Sucheingabe := RegExReplace(Sucheingabe, "\n", "``n")
jsonY=
(
{
"prompt": "%Sucheingabe%",
"max_tokens": 4000,
"temperature": 0.1
}
)
Return jsonY
}
;-------------------------------------------------------
BeautifyJson(json, indent := " ") {
static Doc, JS
if !Doc {
Doc := ComObjCreate("htmlfile")
Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
JS := Doc.parentWindow
}
Return JS.eval("JSON.stringify(" . json . ",'','" . indent . "')")
}
;=======================================================
;--------------------------
;- ==== FUNCTION GoogleTranslate() by teadrinker ===========================
;- Last edited by teadrinker on 25 Mar 2021, 18:48, edited 3 times in total
;- Using Google Translate to automate text translation
;- https://www.autohotkey.com/boards/viewtopic.php?f=6&t=63835
;MsgBox, % GoogleTranslate("今日の天気はとても良いです")
;MsgBox, % GoogleTranslate("Hello, World!", "en", "ru")
GoogleTranslate(str, from := "auto", to := "en") {
static JS := CreateScriptObj(), _ := JS.( GetJScript() ) := JS.("delete ActiveXObject; delete GetObject;")
json := SendRequest(JS, str, to, from, proxy := "")
oJSON := JS.("(" . json . ")")
trans:=""
;-------------------------------------------
try thisBeautifyJson := BeautifyJson(json)
GuiControl,, JsonEdit,% thisBeautifyJson
;-------------------------------------------
if !IsObject(oJSON[1]) {
Loop % oJSON[0].length
trans .= oJSON[0][A_Index - 1][0]
}
else {
MainTransText := oJSON[0][0][0]
Loop % oJSON[1].length {
trans .= "`n+"
obj := oJSON[1][A_Index-1][1]
Loop % obj.length {
txt := obj[A_Index - 1]
trans .= (MainTransText = txt ? "" : "`n" txt)
}
}
}
if !IsObject(oJSON[1])
MainTransText := trans := Trim(trans, ",+`n ")
else
trans := MainTransText . "`n+`n" . Trim(trans, ",+`n ")
from := oJSON[2]
trans := Trim(trans, ",+`n ")
Return trans
}
;---
SendRequest(JS, str, tl, sl, proxy) {
static http
ComObjError(false)
if !http
{
http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
( proxy && http.SetProxy(2, proxy) )
http.open("GET", "https://translate.google.com", true)
http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
http.send()
http.WaitForResponse(-1)
}
http.open("POST", "https://translate.googleapis.com/translate_a/single?client=gtx"
; or "https://clients5.google.com/translate_a/t?client=dict-chrome-ex"
. "&sl=" . sl . "&tl=" . tl . "&hl=" . tl
. "&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=0&ssel=0&tsel=0&pc=1&kc=1"
. "&tk=" . JS.("tk").(str), true)
http.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
http.send("q=" . URIEncode(str))
http.WaitForResponse(-1)
Return http.responsetext
}
;---
URIEncode(str, encoding := "UTF-8") {
VarSetCapacity(var, StrPut(str, encoding))
StrPut(str, &var, encoding)
urlstr:=""
while code := NumGet(Var, A_Index - 1, "UChar") {
bool := (code > 0x7F || code < 0x30 || code = 0x3D)
UrlStr .= bool ? "%" . Format("{:02X}", code) : Chr(code)
}
Return UrlStr
}
;---
GetJScript()
{
script =
(
var TKK = ((function() {
var a = 561666268;
var b = 1526272306;
return 406398 + '.' + (a + b);
})());
function b(a, b) {
for (var d = 0; d < b.length - 2; d += 3) {
var c = b.charAt(d + 2),
c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c),
c = "+" == b.charAt(d + 1) ? a >>> c : a << c;
a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c
}
return a
}
function tk(a) {
for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {
var c = a.charCodeAt(f);
128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ?
(c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240,
g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
}
a = h;
for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6");
a = b(a, "+-3^+b+-f");
a ^= Number(e[1]) || 0;
0 > a && (a = (a & 2147483647) + 2147483648);
a `%= 1E6;
return a.toString() + "." + (a ^ h)
}
)
Return script
}
;---
CreateScriptObj() {
static doc, JS, _JS
if !doc {
doc := ComObjCreate("htmlfile")
doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'>")
JS := doc.parentWindow
if (doc.documentMode < 9)
JS.execScript()
_JS := ObjBindMethod(JS, "eval")
}
Return _JS
}
;------------------ END TRANSLATOR from user teadrinker -------------------------
;================== END SCRIPT ChatGPT-3 ========================================
Im Moment für mich OK, definiere Sprache (Answer in Edit2 ) , evtl Korrekturen nötig ... da keine Ahnung wie's funktioniert ... :)
EDIT : clear button angefügt
[code]
;=====================================================================================================
;- ChatGPT-3 = Generative Pre-trained Transformer-3
;- Info: ChatGPT supports = English, Spanish, French, German, Italian, Portuguese, Dutch, Russian
;- it translates QUESTION first to english and show ANSWER in english and to your desired language
;- https://36kr.com/p/2196628560234373 2023-04-01 about GPT-4
;-----------------------------------------------------------------------------------------------------
;#Warn
#singleinstance force
SetWorkingDir, %A_ScriptDir%
;-------------------------------------------------------------------------
api_key := "INSERT_API_KEY_HERE"
api_url := "https://api.openai.com/v1/engines/text-davinci-003/completions" ;- The endpoint URL for the GPT-3 API -
AnswerLanguage:="de" ;- in edit-2
;----------------------------------------------------------------------------------------------------
if (api_key="INSERT_API_KEY_HERE")
{
msgbox, 262180,API-KEY needed,You must define the API-KEY`nAuthentication required / Please log in to access this page=`nhttps://platform.openai.com/account/api-keys`nURL open when hit 'YES'
ifmsgbox,NO
exitapp
run,https://platform.openai.com/account/api-keys
exitapp
}
;----------------------------------------------------------------------------------------------------
;e0:="Woher kommt das Wort Caran d'Ache ?" ;- wrong answer
;e0:="что значит caran d'ache?" ;- wrong answer
;e0:="Was heisst Caran d'Ache ?" ;- wrong answer
;e0:="Hi there, is it possible for you to predict the future ?"
;e0:="The cafeteria had 23 apples . If they used 20 to make lunch and bought 6 more , how many apples do they have ?" ;-9
;e0:="Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls . How many tennis balls does he have now ?" ;-11
;e0:="Радянщина чи боротьба за права: як з’явилося 8 березня та які альтернативи свята в Україні?"
;---------------------------- SHARP PC-1262 ----------------------------------------------------------
e0:="Wie berechne ich die Gegenkathete ? Zeige mir den Formel wenn die Ankathete und Winkel bekannt ist ." ;- GK=AK*TAN(grad)
;The formula for calculating the opposite leg of a right triangle when the adjacent leg and angle are known is:
;Opposite Leg = Adjacent Leg * Tan(Angle)
;e0:="Wie berechne ich die Hypotenuse ? Zeige mir den Formel wenn die Ankathete und Winkel bekannt ist ." ;- HY=AK/COS(grad)
;The formula for calculating the hypotenuse when the adjacent side and angle are known is:
;Hypotenuse = Adjacent Side / cos(Angle)
;e0:="Wie berechne ich den Winkel ? Zeige mir den Formel wenn die Ankathete und Gegenkathete bekannt ist ." ;- GRAD=ATN(GK/AK)
e0:="How calculate angle ? Show me formula if you know opposite side and adjacent side ."
;The formula for calculating the angle is:
;angle = arctan(opposite/adjacent) ; arctan is the inverse tangent function.
;e0:="Wie berechne ich die Ankathete ? Zeige mir den Formel wenn Gegenkathete und der Winkel bekannt ist ." ;- AK=GK/TAN(grad)
;The formula for calculating the opposite leg of a right triangle when the adjacent leg and angle are known is:
;Opposite Leg = Adjacent Leg * Tan(Angle)
;-----------------------------------------------------------------------------------------------------
If !FileExist(A_ScriptDir "\history\")
FileCreateDir,% A_ScriptDir "\history\"
;-----------------------------------------------------------------------------------------------------
curl := ComObjCreate("WinHttp.WinHttpRequest.5.1")
curl.Open("POST", api_url)
curl.SetRequestHeader("Content-Type", "application/json")
curl.SetRequestHeader("Authorization", "Bearer " api_key)
jsonY=
;----------
Gui, PleaseWait: +LastFound +AlwaysOnTop +Disabled -Caption +ToolWindow +hwndhGUIPleaseWait
Gui, PleaseWait: Color, FFFF00
Gui, PleaseWait: Add, Text,xm vTextA w200 Center, Please wait...
Gui, PleaseWait: Add, Text,xs vTextB w200 Center, Loading...
Gui, PleaseWait: Show,% "x300 y" (A_ScreenHeight/2),% hGUIPleaseWait
;----------
Gui, New, +hwndhGUIA +AlwaysOnTop ;- +Owner +LastFound +E0x20 +Border +Caption +Resize -ToolWindow, hGUIA
Gui,default
global jsony,doc,thisbeautifyjson
null:="",diff:=""
Gui, Font, s12
Gui, Add, Tab3,,Main|TranslateJson|ResultJson|Settings
Gui, Tab, 1
;---------------------------------------------------------------------
Gui, Add, Edit,-multi Section vSucheingabe hwndSucheingabe w540 r4,%e0% ; Deine Frage an ChatGPT ...
;----------------------------------------------------------------------
Gui, Add, Text, xs y+13 Section,Answer language:
Gui, Add, Edit,ys-5 vLangx w40 r1 center,%AnswerLanguage%
Gui,add,button,xs+250 ys-5 w120 h27 gClearx,CLEAR
Gui, Tab, 4
Gui, Add, Text,xs Section, API Key:
Gui, Add, Text,ys x+70, total_tokens:
Gui, Add, Edit,ys w270 vTextApiKey, Counter
Gui, Font, s11
;Gui, Add, Edit, xs vApi_key w540 h25 center Password,% api_key
Gui, Add, Edit, xs vApi_key w540 h25 center ,% api_key
Gui, Font, s12
Gui, Tab, 1
Gui, Add, Button, xs ys gA1 w200 h30 default Section, Ask
Gui, Add, CheckBox, ys vErgebnisdatei w200 h30 , Show result file
Gui, Add, Button, ys gEditScript w30 h30,% Chr(0xE115)
Gui, Add, Button, ys gReloadApp w60 h30,% "Reload"
;--
Gui, Add, Text,xs vAnswer, Answer ;- always en
Gui, Add, Edit, vAnswerEdit r19 w540,
;--
Gui, Add, Text, vTextTranslate w300, Translate en > Selected-Language
Gui, Add, Edit, vAnswerEdit2 r20 w540, ;- Answer in your selected Language
;----------------------------------------------------------
Gui, Tab, 2
Gui, Add, Text,, Json
Gui, Add, Edit, vJsonEdit r30 w540,
Gui, Tab, 3
Gui, Add, Text,, Result
Gui, Add, Edit, vResultEdit r30 w540,
;----------------------------------------------------------
Gui, Show, x94 y1,% a_scriptname " - GuiHWND: " hGUIA
Gui, PleaseWait: show, Hide
EM_SETCUEBANNER(Sucheingabe, a_space "Deine Frage an ChatGPT -- your question to ChatGPT")
;EM_SETCUEBANNER(HTranslateSucheingabe, "Request will be translated into English beforehand")
Return
;----------------------------------------------------------
Clearx:
GuiControl,, Sucheingabe
GuiControl,, AnswerEdit
GuiControl,, AnswerEdit2
GuiControl,, JsonEdit
GuiControl,, ResultEdit
return
;----------------------------------------------------------
EM_SETCUEBANNER(HWND, Text) {
; EM_SETCUEBANNER = 0x1501 -> msdn.microsoft.com/en-us/library/bb761639(v=vs.85).aspx
Return DllCall("SendMessage", "Ptr", HWND, "UInt", 0x1501, "Ptr", True, "WStr", Text, "Ptr")
}
;-------------------------------------------------------
EditScript:
try run, notepad.exe %A_ScriptName%
return
;-------------------------------------------------------
RadioGroupAuswahl:
return
;-------------------------------------------------------
ToggleListview:
return
;-------------------------------------------------------
ReloadApp:
Gui, Submit, NoHide
reload
return
;-------------------------------------------------------
A1:
Gui, Submit, NoHide
if Sucheingabe=
Return
GuiControlGet, Sucheingabe ; https://ahkde.github.io/docs/v1/lib/GuiControlGet.htm#ExNoControlID
try TranslateSucheingabe:=GoogleTranslate(sucheingabe, "auto", "en")
GuiControl,, TranslateSucheingabe,% TranslateSucheingabe
if TranslateSucheingabe
Sucheingabe := TranslateSucheingabe
AttemptNo := 1
Gui, PleaseWait: Show,% "x300 y" (A_ScreenHeight/2),% AttemptNo
;AttemptNo := 1
jsonY := thisJson(Sucheingabe)
curl.Send(jsonY)
JsonResult := curl.ResponseText
JsonResult:= FromUTF8(JsonResult)
thisArray := []
thisArray := JsonToAHK(JsonResult)
thisText := Trim((thisArray["choices", 1, "text"]), " `n`r`t") ; remove leading and trailing whitespaces
if thisText
GuiControl,, AnswerEdit, %thisText% ;- always en
else{
errorMessage := thisArray["error", "message"]
GuiControl,, AnswerEdit,% errorMessage
try errorMessageTranslate := GoogleTranslate(errorMessage, "auto", "de")
GuiControl,, AnswerEdit2, %errorMessageTranslate%
}
try thisTextTranslate := GoogleTranslate(thisText, "auto",langx)
;GuiControl,, lang,% "de"
GuiControl,, AnswerEdit2,% thisTextTranslate
try thisBeautifyJson := BeautifyJson(JsonResult)
GuiControl,, ResultEdit, %thisBeautifyJson%
Gui, PleaseWait: show, Hide
thisAPI_completion_tokens := thisArray["usage", "completion_tokens"]
thisAPI_prompt_tokens := thisArray["usage", "prompt_tokens"]
thisAPI_total_tokens := thisArray["usage", "total_tokens"]
thisAPI_created := thisArray["created"] ; "`t"
logprobs := thisArray["choices", 1, "logprobs"]
time := 1970
time += thisAPI_created, s
diff -= A_NowUTC, h
time += diff, h
FormatTime, TimeStamp, %time%, dd.MM.yyyy HH-mm-ss tt ; 24.März.2018 05-20-37
GuiControl,, TextApiKey,% "[ " thisAPI_total_tokens " ] " TimeStamp
logprobs := (logprobs=null) ? "null" : (thisArray["choices", 1, "logprobs"])
;msgbox % thisArray[1, 1, 9, 1, 1, 2]
this_ADD := "finish_reason:`n" thisArray["choices", 1, "finish_reason"] "`n"
. "################################################################`n"
. "index:`n" thisArray["choices", 1, "index"] "`n"
. "################################################################`n"
. "logprobs:`n" logprobs "`n"
. "################################################################`n"
. "text:`n" FromUTF8(thisText) "`n"
. "################################################################`n"
. "created:`n" thisArray["created"] a_space TimeStamp "`n"
. "################################################################`n"
. "id:`n" thisArray["id"] "`n"
. "################################################################`n"
. "model:`n" thisArray["model"] "`n"
. "################################################################`n"
. "object:`n" thisArray["object"] "`n"
. "################################################################`n"
. "completion_tokens:`n" thisArray["usage", "completion_tokens"] "`n"
. "################################################################`n"
. "prompt_tokens:`n" thisArray["usage", "prompt_tokens"] "`n"
. "################################################################`n"
. "total_tokens:`n" thisArray["usage", "total_tokens"] "`n"
. "################################################################`n"
GuiControlGet, Sucheingabe ; https://ahkde.github.io/docs/v1/lib/GuiControlGet.htm#ExNoControlID
GuiControlGet, MeinRadioA
GuiControlGet, MeinRadioB
GuiControlGet, MeinRadioC
GuiControlGet, ToggleListview
GuiControlGet, TextApiKey
GuiControlGet, Api_key
GuiControlGet, Answer
GuiControlGet, AnswerEdit
GuiControlGet, TextTranslate
GuiControlGet, AnswerEdit2
GuiControlGet, JsonEdit
GuiControlGet, ResultEdit
allVars := "Sucheingabe:`n" Sucheingabe "`n"
. "################################################################`n"
. "MeinRadioA:`n" MeinRadioA "`n"
. "################################################################`n"
. "MeinRadioB:`n" MeinRadioB "`n"
. "################################################################`n"
. "MeinRadioC:`n" MeinRadioC "`n"
. "################################################################`n"
. "ToggleListview:`n" ToggleListview "`n"
. "################################################################`n"
. "TextApiKey:`n" TextApiKey "`n"
. "################################################################`n"
. "Api_key:`n" "******************************`n"
. "Api_key:`n" Api_key "`n"
. "################################################################`n"
. "AnswerEdit:`n" AnswerEdit "`n"
. "################################################################`n"
. "AnswerEdit2:`n" AnswerEdit2 "`n"
. "################################################################`n"
. "Json File von ChatGPT:`n" ResultEdit "`n"
. "################################################################`n"
. "Json File der Google Übersetzung:`n" JsonEdit "`n"
this_ADD := this_ADD allVars
Anow := A_now
if Ergebnisdatei
{
fileappend,% this_ADD,% a_scriptdir "\history\" Anow " Ergebnisdatei.txt",utf-8
try run,% a_scriptdir "\history\" Anow " Ergebnisdatei.txt"
}
Return
;-------------------------------------------------------
FromUTF8(ByRef Txt, CP := "CP28591") { ; ISO-8859-1
Size := StrPut(Txt, CP)
VarSetCapacity(UTF8, Size, 0)
StrPut(Txt, &UTF8, CP)
Return StrGet(&UTF8, "UTF-8")
}
;-------------------------------------------------------
ttStop:
toolTip
return
;-------------------------------------------------------
GuiClose:
ExitApp
;-------------------------------------------------------
esc::exitapp
;-------------------------------------------------------
JsonToAHK(json, rec := false) {
static doc := ComObjCreate("htmlfile")
, __ := doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
, JS := doc.parentWindow
if !rec
obj := %A_ThisFunc%(JS.eval("(" . json . ")"), true)
else if !IsObject(json)
obj := json
else if JS.Object.prototype.toString.call(json) == "[object Array]" {
obj := []
Loop % json.length
obj.Push( %A_ThisFunc%(json[A_Index - 1], true) )
}
else {
obj := {}
keys := JS.Object.keys(json)
Loop % keys.length {
k := keys[A_Index - 1]
obj[k] := %A_ThisFunc%(json[k], true)
}
}
Return obj
}
;-------------------------------------------------------
thisJson(ByRef Sucheingabe := "PROMPT") {
; Build the JSON payload
Sucheingabe := RegExReplace(Sucheingabe, "\""", """""")
Sucheingabe := RegExReplace(Sucheingabe, "\n", "``n")
jsonY=
(
{
"prompt": "%Sucheingabe%",
"max_tokens": 4000,
"temperature": 0.1
}
)
Return jsonY
}
;-------------------------------------------------------
BeautifyJson(json, indent := " ") {
static Doc, JS
if !Doc {
Doc := ComObjCreate("htmlfile")
Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
JS := Doc.parentWindow
}
Return JS.eval("JSON.stringify(" . json . ",'','" . indent . "')")
}
;=======================================================
;--------------------------
;- ==== FUNCTION GoogleTranslate() by teadrinker ===========================
;- Last edited by teadrinker on 25 Mar 2021, 18:48, edited 3 times in total
;- Using Google Translate to automate text translation
;- https://www.autohotkey.com/boards/viewtopic.php?f=6&t=63835
;MsgBox, % GoogleTranslate("今日の天気はとても良いです")
;MsgBox, % GoogleTranslate("Hello, World!", "en", "ru")
GoogleTranslate(str, from := "auto", to := "en") {
static JS := CreateScriptObj(), _ := JS.( GetJScript() ) := JS.("delete ActiveXObject; delete GetObject;")
json := SendRequest(JS, str, to, from, proxy := "")
oJSON := JS.("(" . json . ")")
trans:=""
;-------------------------------------------
try thisBeautifyJson := BeautifyJson(json)
GuiControl,, JsonEdit,% thisBeautifyJson
;-------------------------------------------
if !IsObject(oJSON[1]) {
Loop % oJSON[0].length
trans .= oJSON[0][A_Index - 1][0]
}
else {
MainTransText := oJSON[0][0][0]
Loop % oJSON[1].length {
trans .= "`n+"
obj := oJSON[1][A_Index-1][1]
Loop % obj.length {
txt := obj[A_Index - 1]
trans .= (MainTransText = txt ? "" : "`n" txt)
}
}
}
if !IsObject(oJSON[1])
MainTransText := trans := Trim(trans, ",+`n ")
else
trans := MainTransText . "`n+`n" . Trim(trans, ",+`n ")
from := oJSON[2]
trans := Trim(trans, ",+`n ")
Return trans
}
;---
SendRequest(JS, str, tl, sl, proxy) {
static http
ComObjError(false)
if !http
{
http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
( proxy && http.SetProxy(2, proxy) )
http.open("GET", "https://translate.google.com", true)
http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
http.send()
http.WaitForResponse(-1)
}
http.open("POST", "https://translate.googleapis.com/translate_a/single?client=gtx"
; or "https://clients5.google.com/translate_a/t?client=dict-chrome-ex"
. "&sl=" . sl . "&tl=" . tl . "&hl=" . tl
. "&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=0&ssel=0&tsel=0&pc=1&kc=1"
. "&tk=" . JS.("tk").(str), true)
http.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
http.send("q=" . URIEncode(str))
http.WaitForResponse(-1)
Return http.responsetext
}
;---
URIEncode(str, encoding := "UTF-8") {
VarSetCapacity(var, StrPut(str, encoding))
StrPut(str, &var, encoding)
urlstr:=""
while code := NumGet(Var, A_Index - 1, "UChar") {
bool := (code > 0x7F || code < 0x30 || code = 0x3D)
UrlStr .= bool ? "%" . Format("{:02X}", code) : Chr(code)
}
Return UrlStr
}
;---
GetJScript()
{
script =
(
var TKK = ((function() {
var a = 561666268;
var b = 1526272306;
return 406398 + '.' + (a + b);
})());
function b(a, b) {
for (var d = 0; d < b.length - 2; d += 3) {
var c = b.charAt(d + 2),
c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c),
c = "+" == b.charAt(d + 1) ? a >>> c : a << c;
a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c
}
return a
}
function tk(a) {
for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {
var c = a.charCodeAt(f);
128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ?
(c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240,
g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
}
a = h;
for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6");
a = b(a, "+-3^+b+-f");
a ^= Number(e[1]) || 0;
0 > a && (a = (a & 2147483647) + 2147483648);
a `%= 1E6;
return a.toString() + "." + (a ^ h)
}
)
Return script
}
;---
CreateScriptObj() {
static doc, JS, _JS
if !doc {
doc := ComObjCreate("htmlfile")
doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'>")
JS := doc.parentWindow
if (doc.documentMode < 9)
JS.execScript()
_JS := ObjBindMethod(JS, "eval")
}
Return _JS
}
;------------------ END TRANSLATOR from user teadrinker -------------------------
;================== END SCRIPT ChatGPT-3 ========================================
[/code]