Erstmal alles Gute ich hoffe du bist bald genesen, bist du schon wieder Zuhause?
Zum Thema Einrückungen, du hast Recht, natürlich sieht das besser aus auch für mich als Anfänger+ kann ich dem Verlauf dann auch besser folgen. Wenn ich es in Scripte veröffentliche, habe ich das im Griff.
Zur Zeit bin ich aber noch am basteln, in der aktuellen Version habe ich die Sprachen erweitert
Dadurch dass ich jede Anfrage grundsätzlich vorher ins Englische übersetze, erweitere ich diese Beschränkung, danke Google. Ich weiß grade nicht wieviele Sprachen übersetzt werden, aber Japanisch ist schonmal dabei...
Wie du siehst, ist in der Ausgabe Datei, die ich zu jeder Anfrage speichere, jedes Japanische Zeichen durch einen Platzhalter ersetzt. Nicht das es mir wichtig wäre, aber vielleicht ist es ja auch nur wenig Aufwand.
Code: Select all
global ToggleLanguage ; := true
; Info: ChatGPT supports English, Spanish, French, German, Italian, Portuguese, Dutch, and Russian.
If !FileExist(A_ScriptDir "\history\")
FileCreateDir,% A_ScriptDir "\history\"
#singleinstance force
SetWorkingDir, %A_ScriptDir%
; Your OpenAI API key
; api_key := "INSERT_API_KEY_HERE" ; https://platform.openai.com/account/api-keys
; The endpoint URL for the GPT-3 API
api_url := "https://api.openai.com/v1/engines/text-davinci-003/completions"
; Set up the cURL session
curl := ComObjCreate("WinHttp.WinHttpRequest.5.1")
curl.Open("POST", api_url)
curl.SetRequestHeader("Content-Type", "application/json")
curl.SetRequestHeader("Authorization", "Bearer " api_key)
; Build the JSON payload
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, Font, s12
Gui, Add, Tab3,,Main|TranslateJson|ResultJson|Settings
Gui, Tab, 1
Gui, Add, Edit,Section vSucheingabe hwndSucheingabe w540 r1, ; Deine Frage an ChatGPT ...
Gui, Add, Text, xs y+13 Section, your language:
Gui, Add, Edit,ys-5 vLang w40 r1 center,
Gui, Add, DropDownList,xs vHistory w540 r1 choose1, History
Gui, Add, Edit,xs vTranslateSucheingabe hwndHTranslateSucheingabe center readonly w540 r1, If the input language is not English, it will be translated beforehand
Gui, Tab, 4
Gui, Add, Text,xs Section, API Key:
Gui, Add, Text,ys x+110, total_tokens:
Gui, Add, Edit,ys w220 vTextApiKey, Counter
Gui, Font, s11
Gui, Add, Edit, xs vApi_key w540 h25 center Password,% api_key
Gui, Font, s12
Gui, Tab, 1
Gui, Add, Button, xs ys gButtonClick w200 h30 default Section, Ask
Gui, Add, CheckBox, ys vErgebnisdatei w200 h30 checked, 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 ; immer en
Gui, Add, Edit, vAnswerEdit r10 w540,
Gui, Add, Text, vTextTranslate w300, Translate
Gui, Add, Edit, vAnswerEdit2 r10 w540,
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,% "x100 y" (A_ScreenHeight/10),% a_scriptname " - GuiHWND: " hGUIA
Gui, Show, x94 y11,% 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
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:
; msgbox % TESTAnswer := FromUTF8("Köln ist ein wichtiges Zentrum für Kultur")
try run, notepad.exe %A_ScriptName%
return
RadioGroupAuswahl:
return
ToggleListview:
return
ReloadApp:
Gui, Submit, NoHide
reload
return
ButtonClick:
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
;-----------
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% ; immer en
else{
errorMessage := thisArray["error", "message"]
GuiControl,, AnswerEdit,% errorMessage
try errorMessageTranslate := GoogleTranslate(errorMessage, "auto", "de")
GuiControl,, AnswerEdit2, %errorMessageTranslate%
}
if !errorMessage {
GuiControlGet, lang
tooltip % lang
; msgbox,,, % lang
; if !(lang="")
; MsgBox % lang "`ndumm gelaufen"
if (lang="de") {
try thisTextTranslate := GoogleTranslate(thisText, "auto", "de")
GuiControl,, lang,% "de"
GuiControl,, AnswerEdit2,% thisTextTranslate
GuiControl,, TextTranslate,% "Translate to: de"
; msgbox,,, % "`nA: " ToggleLanguage a_space lang ; test
}
if (lang="en") {
try thisTextTranslate := GoogleTranslate(thisText, "en", "de")
GuiControl,, lang,% "en"
GuiControl,, AnswerEdit2,% thisTextTranslate
GuiControl,, TextTranslate,% "Translate to: de"
; msgbox,,, % "`nB: " ToggleLanguage a_space lang ; test
}else{
; if !(lang="de") and if !(lang="en") {
try thisTextTranslate := GoogleTranslate(thisText, "en", lang)
GuiControl,, lang,% lang
GuiControl,, TextTranslate,% "Translate to: " lang
GuiControl,, AnswerEdit2,% thisTextTranslate
; msgbox,,, % "`nC: " ToggleLanguage a_space lang ; test
}
}
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
fileappend,% this_ADD,% a_scriptdir "\history\" Anow " Ergebnisdatei.txt"
if Ergebnisdatei
try run,% a_scriptdir "\history\" Anow " Ergebnisdatei.txt"
if !TranslateSucheingabe
GuiControl,, lang,% ""
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
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 . "')")
}
;################################################################################################################
;MsgBox, % GoogleTranslate("今日の天気はとても良いです")
;MsgBox, % GoogleTranslate("Hello, World!", "en", "ru")
;GoogleTranslate(str, from := "auto", to := "de", ByRef ToggleLanguage := true) {
GoogleTranslate(str, from := "auto", to := "de") {
; static ToggleLanguage
; global ToggleLanguage
static JS := CreateScriptObj(), _ := JS.( GetJScript() ) := JS.("delete ActiveXObject; delete GetObject;")
json := SendRequest(JS, str, to, from, proxy := "")
oJSON := JS.("(" . json . ")")
ATickCount := A_TickCount
try thisBeautifyJson := BeautifyJson(json)
GuiControl,, JsonEdit,% thisBeautifyJson
language := JsonToAHK(thisBeautifyJson)
language := (SubStr((language[1, 1, 9, 1, 1, 2]), 1, 2)) ? (SubStr((language[1, 1, 9, 1, 1, 2]), 1, 2))
: language[3] ? language[3]
: language[9, 1, 1] ? language[9, 1, 1]
: language[9, 4, 1] ? language[9, 4, 1]
: "en"
;MsgBox % "Toogle:`t" ToggleLanguage "`tA_LineNumber:`t" A_LineNumber "language:`t" language
fileappend,% "Toogle:`t" ToggleLanguage "`tA_LineNumber:`t" A_LineNumber "language:`t" language "`n",% a_scriptDir "\language.txt"
if ((ToggleLanguage := !ToggleLanguage) ? true : false) {
;MsgBox % "Toogle:`t" ToggleLanguage "`tA_LineNumber:`t" A_LineNumber "language:`t" language
fileappend,% "Toogle:`t" ToggleLanguage "`tA_LineNumber:`t" A_LineNumber "language:`t" language "`n",% a_scriptDir "\language.txt"
/*
if (language="en")
GuiControl,, lang,% "en"
else
GuiControl,, lang,% language
*/
GuiControl,, lang,% language
toolTip % "`n" ToggleLanguage a_space language
setTimer, ttStop, -2000
;MsgBox % "Toogle:`t" ToggleLanguage "`tA_LineNumber:`t" A_LineNumber "language:`t" language
}
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)
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
}