Jump to content

Recommended Posts

Posted

Okay, now I need your help @argumentum. I need to understand your scenario/data which lead to a problem.
I didn't test/use it (Ollama) yet. What data needs to be adjusted/escaped?

19 hours ago, argumentum said:
... ...
    $sRequest &= '"system": "' & $sSystem & '", '
    $sPrompt = StringReplace($sPrompt, @CRLF, "\r\n")
    $sPrompt = StringReplace($sPrompt, @TAB, "\t")
    $sPrompt = StringReplace($sPrompt, '"', "\""")
    $sRequest &= '"prompt": "' & $sPrompt & '", '
... ...

..it needed to escape stuff. All good :) 

__JSON_FormatString($sPrompt) is the issue as far as I understood. Am I right, that this version of the function is good in your case (for your data) ...

; before
Func __JSON_FormatString(ByRef $s_String)
    $s_String = _
        StringReplace( _
            StringReplace( _
                StringReplace( _
                    StringReplace( _
                        StringReplace( _
                            StringReplace( _
                                StringReplace( _
                                    StringReplace($s_String, '\', '\\', 0, 1) _
                                , Chr(8), "\b", 0, 1) _
                            , Chr(12), "\f", 0, 1) _
                        , @CRLF, "\n", 0, 1) _
                    , @LF, "\n", 0, 1) _
                , @CR, "\r", 0, 1) _
            , @TAB, "\t", 0, 1) _
        , '"', '\"', 0, 1)
EndFunc   ;==>__JSON_FormatString

... and the current version of the function not, right?

; current version
Func __JSON_FormatString(ByRef $sString)
    Return StringLen($sString) < 50 ? _
    StringTrimRight(StringRegExpReplace($sString & '\\\b\f\n\r\t\"', '(?s)(?|\\(?=.*(\\\\))|[\b](?=.*(\\b))|\f(?=.*(\\f))|\r\n(?=.*(\\n))|\n(?=.*(\\n))|\r(?=.*(\\r))|\t(?=.*(\\t))|"(?=.*(\\")))', '\1'), 15) : _
    StringReplace( _
        StringReplace( _
            StringReplace( _
                StringReplace( _
                    StringReplace( _
                        StringReplace( _
                            StringReplace( _
                                StringReplace($sString, '\', '\\', 0, 1) _
                            , Chr(8), "\b", 0, 1) _
                        , Chr(12), "\f", 0, 1) _
                    , @CRLF, "\n", 0, 1) _
                , @LF, "\n", 0, 1) _
            , @CR, "\r", 0, 1) _
        , @TAB, "\t", 0, 1) _
    , '"', '\"', 0, 1)
EndFunc

Depending on your scenario, I am sure @AspirinJunkie can help to fix that.

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted
14 minutes ago, SOLVE-SMART said:

Okay, now I need your help @argumentum. I need to understand your scenario/data which lead to a problem.
I didn't test/use it (Ollama) yet. What data needs to be adjusted/escaped?

I wrote this:

Spoiler
Global $__g_OllamaAssistant_DefaultURL = "http://localhost:11434"

Func OllamaAssistant_DefaultURL($sStr = Default)
    If $sStr <> Default Then $__g_OllamaAssistant_DefaultURL = $sStr
    Return $__g_OllamaAssistant_DefaultURL
EndFunc

Local $apiEndpoint = OllamaAssistant_DefaultURL() & "/api/generate"

; Define your question as JSON
;~ Local $jsonQuestion = '{ "model": "qwen2.5-coder:3b", "prompt": "What is the capital of France?' & @CRLF & 'What language is spoken there ?", "stream": false}' ; Response: {"error":"invalid character '\\r' in string literal"}
Local $jsonQuestion = '{ "model": "qwen2.5-coder:3b", "prompt": "What is the capital of Spain?\r\nWhat \"language\" is spoken there ?", "stream": false}' ; it likes it
Local $jsonQuestion = '{ "model": "qwen2.5-coder:3b", "role": "user", "prompt": "What is the capital of Spain?\r\nWhat \"language\" is spoken there ?", "stream": false}' ; it likes it

; Create an instance of WinHttpRequest
;~ Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest")
Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")

; Connect to the API endpoint and set headers
$oHTTP.Open("POST", $apiEndpoint, False)
$oHTTP.setRequestHeader("Content-Type", "application/json")

; Send the JSON data as POST body
$oHTTP.send($jsonQuestion)

; Check for errors
If @error Then
    MsgBox(0, "Error", "Failed to send request: " & @error)
    $oHTTP = 0
    Exit
Else
    ; Check for errors in HTTP response
    If $oHttp.Status <> 200 Then ConsoleWrite('! Error: $oHttp.Status:' & $oHttp.Status & @CRLF)

    ; Output the response from the API
    ConsoleWrite("Response: " & $oHTTP.ResponseText & @CRLF)
EndIf

; Clean up
$oHTTP = 0

and ollama will tells why it failed.

So I added code to fix those 3 events. @ioa747 said (in not so many words) that those lines were not needed and it all took off from there.
I posted in @AspirinJunkie's thread, and once his coded gets fixed, these 2 past pages can be removed. ( ...well, disregarded ) 

23 minutes ago, SOLVE-SMART said:

Depending on your scenario,

24 minutes ago, SOLVE-SMART said:
; current version
Func __JSON_FormatString(ByRef $sString)
    Return StringLen($sString) < 50 ? _
    StringTrimRight(StringRegExpReplace($sString &

...that byRef and return would not fly as is. Am sure he'll fix it once he sees it.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted
18 hours ago, argumentum said:

I apologize,  😔
I still didn't understand what had happened and it wasn't working for you.
Then when I saw here that you started escaping control characters, I was surprised.

(I still didn't understand what had happened)
That's why I passed you   just  '__JSON_FormatString($sPrompt) '
I thought you just removed the __JSON_FormatString($sPrompt)

Even after you showed me  here with the scary
StringRegExpReplace($sString & '\\\b\f\n\r\t\"', '(?s)(?|\\(?=.*(\\\\))|[\b](?=.*(\\b))|\f(?=.*(\\f))|\r\n(?=.*(\\n))|\n(?=.*(\\n))|\r(?=.*(\\r))|\t(?=.*(\\t))|"(?=.*(\\")))', '\1'),
I thought it was your addition because something didn't work for you,   or   you have some old version of JSON.au3

and that's why I asked you (and which is the right one?
; Version .......: 0.10 ? )
because I checked on github what the current version is, and it was 10, which is the one I had
(I didn't go and see if the function was the same, I just looked at the header)

I got the full picture today when I read all the relevant posts.
I concluded that, I was misusing a UDF function, which is intended for internal use only.
 

After all this I came to the conclusion.
That I need to make an internal function for escaping control characters, so that I have better control.
and since my JSON parser requirements are basic, for a specific JSON format,
I will also make a fake JSON parser, so that I can extract the data I need from JSON, without external additions (at least until needed)
This will also help me to better understand the JSON format

thanks for understanding
I apologize to everyone for the upset I caused you, it was not my intention.

I know that I know nothing

Posted

You are such a nice person @ioa747. In my opinion there is no apology necessary at all. There was a misunderstanding and that's it. But thanks for the clarification - no worries and thanks for your effort regarding this LLM <==> AutoIt thingy 😁 .

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted
On 5/11/2025 at 7:37 PM, ioa747 said:

update to Version: 0.7

basic changes:

I changed the Timeouts, to WinHttp.WinHttpRequest.5.1 , because it ended unexpectedly, in larger models
; ResolveTimeout: 5 sec ; ConnectTimeout: 10 sec ; SendTimeout: 30 sec ; ReceiveTimeout: 120 sec

I added an additional Unescape, because some models return it like this
; Unescape \u00XX characters
$sString = StringReplace($sString, '\u003c', '<', 0, 1)
 $sString = StringReplace($sString, '\u003e', '>', 0, 1)

I expanded Unescape to
Json_EscapeString($sAssistant)
Json_EscapeString($sSystem)

I know that I know nothing

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...