Search the Community
Showing results for tags 'openai'.
-
Hello I'm excited to share a piece of code with you that I've been working on. This code uses the GPT-4 api language model from OpenAI to correct German translations based on Dutch (nl-be) source texts. It can be especially helpful for those who are translating content between these languages or need to ensure the accuracy of translations. Here's an overview of what the code does: It uses the GPT-4 model from the OpenAI API to process the input text. The input consists of a Dutch source text, its German translation, and a prompt to correct the translation. It sends an HTTP POST request to the OpenAI API with the required headers and data. The code receives a JSON response from the API, which includes the corrected German translation. #include <InetConstants.au3> #include <Date.au3> #include <File.au3> #include <SQLite.au3> #include <SQLite.dll.au3> #include <GuiImageList.au3> #include <GuiListView.au3> #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <Excel.au3> #include <GUIConstantsEx.au3> #include <GuiListBox.au3> #include <GuiEdit.au3> #include <WindowsConstants.au3> #include <WinHttp.au3> #include <WinHttpConstants.au3> #include <Json.au3> #include <WinAPISys.au3> #include <WinAPIConv.au3> #include <WinAPILocale.au3> #include <Array.au3> #include <String.au3> #include <WinAPIConv.au3> #include <XMLWrapperEx.au3> While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch ;~ You can use this when the function ProcessWithGPT would have a loop in it. This way if the api fails, it tries again after 60 seconds. ;~ if $Go = 1 then ;~ If TimerDiff($iTimer) >= 60000 Then ;~ $iTimer = TimerInit() ;~ If Not $bFunctieActief Then ;~ $bFunctieActief = True ;~ ConsoleWrite(@CRLF & "Restart!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" & @CRLF) ;~ sleep(60000) ;~ ProcessWithGPT() ;~ EndIf ;~ EndIf ;~ EndIf WEnd Func ProcessWithGPT() $bFunctieActief = True $bron = "Zelf chape leggen met Staenis" $taal = "de" $vertaling = "Estrich selbst verlegen mit Staenis" Local $sPrompt = "Source text (Dutch, nl-be): " & $bron & " (" & $taal & "): " & $vertaling $sPrompt = EncodeAnchorTags($sPrompt) $opdracht = "Correct the German translation based on the Dutch (nl-be) source text, just make sure that the corrected translation is displayed." Local $iLength = StringLen($opdracht & $sPrompt) If $iLength > 4096 Then MsgBox(0, "error", "Te lang", 1) Return EndIf ; Store your API key and OpenAI organization key in a variable Local $api_key = "" Local $OpenAIOrganization = "" ; Create the WinHttpRequest object Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1") ; Open a POST request to the OpenAI API $oHTTP.Open("POST", "https://api.openai.com/v1/chat/completions", False) ; Set the headers for the request $oHTTP.SetRequestHeader("Content-Type", "application/json; charset=UTF-8") ;~ $oHTTP.SetRequestHeader("Content-Type", "application/json") $oHTTP.SetRequestHeader("Authorization", "Bearer " & $api_key) $oHTTP.SetRequestHeader("OpenAI-Organization", $OpenAIOrganization) ;~ ConsoleWrite('{"model": "gpt-4", "temperature": 0, "messages":[{"role": "system", "content": "' & $opdracht & '"}, {"role": "user","content": "' & $sPrompt & '"}]}') ConsoleWrite("{""model"": ""gpt-4"", ""temperature"": 0, ""messages"":[{""role"": ""system"", ""content"": " & Chr(34) & StringReplace($opdracht, Chr(34), "\" & Chr(34)) & Chr(34) & "}, {""role"": ""user"", ""content" & Chr(34) & ": " & Chr(34) & StringReplace($sPrompt, Chr(34), "\" & Chr(34)) & Chr(34) & "}]}") $oHTTP.SetTimeouts("130000", "130000", "130000", "130000") ; Try sending the HTTP request ;~ $oHTTP.Send('{"model": "gpt-4", "temperature": 0, "messages":[{"role": "system", "content": "' & $opdracht & '"}, {"role": "user","content": "' & $sPrompt & '"}]}') $oHTTP.Send("{""model"": ""gpt-4"", ""temperature"": 0, ""messages"":[{""role"": ""system"", ""content"": " & Chr(34) & StringReplace($opdracht, Chr(34), "\" & Chr(34)) & Chr(34) & "}, {""role"": ""user"", ""content" & Chr(34) & ": " & Chr(34) & StringReplace($sPrompt, Chr(34), "\" & Chr(34)) & Chr(34) & "}]}") If (@error) Then ConsoleWrite("Error: An error occurred while sending the HTTP request." & @CRLF) $bFunctieActief = False Return SetError(2, 0, 0) EndIf ; Waiting for a response from the server $oHTTP.WaitForResponse("130000") ; Check the status code of the HTTP request If ($oHttp.Status <> $HTTP_STATUS_OK) Then ConsoleWrite("Error: The HTTP request failed with status code " & $oHttp.Status & "." & @CRLF) EndIf ; Get the response text Local $sResponse = $oHTTP.ResponseText $sResponse = BinaryToString($oHTTP.ResponseBody, 4) ; Decode the JSON response to an AutoIt variable Local $oJsonResponse = Json_Decode($sResponse) ConsoleWrite("JSON-respons: " & $sResponse) ; Verify that JSON decoding has been done correctly If Not Json_IsObject($oJsonResponse) Then ConsoleWrite(@CRLF & "Error: JSON response cannot be decoded." & @CRLF) $bFunctieActief = False Return EndIf ; Remove the "choices" property from the JSON response Local $aChoices = Json_ObjGet($oJsonResponse, "choices") ; Check that the property was retrieved correctly If @error Then ConsoleWrite(@CRLF & "Error: Cannot extract the choices from the JSON response." & @CRLF) ConsoleWrite("JSON-respons: " & $sResponse & @CRLF) If StringInStr($sResponse, "That model is currently overloaded with other requests.") Then $retry += 1 ConsoleWrite("Server error detected, retry in 1 minute...." & @CRLF) Else $bFunctieActief = False Return EndIf EndIf ; Check for choices If UBound($aChoices) = 0 Then ConsoleWrite("Error: No choices are present in the JSON response." & @CRLF) ConsoleWrite("JSON-respons: " & $sResponse & @CRLF) $bFunctieActief = False Return EndIf ; Get the text from the "message" object of the first choice Local $oFirstChoiceMessage = Json_ObjGet($aChoices[0], "message") Global $sText = Json_ObjGet($oFirstChoiceMessage, "content") $sText = DecodeAnchorTags($sText) ; Get the response text ConsoleWrite(@CRLF & "Generated response: " & $sResponse & @CRLF) ConsoleWrite(@CRLF & "Generated text: " & $sText & @CRLF) $bFunctieActief = False EndFunc Func EncodeAnchorTags($sText) ; This is something i need to keep links alive in the resource files $sText = StringReplace($sText, "} '", "}'") $sText = StringReplace($sText, "' {", "'{") For $i = 0 To 9 $sText = StringReplace($sText, "<a href='{" & $i & "}'>", "(" & $i & $i & $i & ")") $sText = StringReplace($sText, "</a>", "(" & $i & $i & ")") Next Return $sText EndFunc Func DecodeAnchorTags($sEncodedText) ; This is something i need to keep links alive in the resource files For $i = 0 To 9 $sEncodedText = StringReplace($sEncodedText, "(" & $i & $i & $i & ")", "<a href='{" & $i & "}'>") $sEncodedText = StringReplace($sEncodedText, "(" & $i & $i & ")", "</a>") Next Return $sEncodedText EndFunc Func ReplaceSpecialChars($sText) $sText = StringReplace($sText, "'", "\'") $sText = StringReplace($sText, '"', '\"') $sText = StringReplace($sText, '„', '\"') $sText = StringReplace($sText, '‘', "\'") $sText = StringReplace($sText, '’', "\'") $sText = StringReplace($sText, '“', '\"') Return $sText EndFunc Please note that you will need to obtain an API key and an OpenAI organization key to use this code. Replace the empty strings in the following lines with your keys: Local $api_key = "" Local $OpenAIOrganization = "" You may also need to install some necessary AutoIt libraries (such as Json.au3) to use the code successfully. Feel free to use, modify, or share this code as needed. If you have any questions or suggestions, please don't hesitate to reply to this thread. I hope you find this code helpful! Best regards, KillaH229