Sample code for 30+ languages & platforms
AutoIt

AI: Simple Stateless Streaming Response

See more AI Examples

Shows a simple stateless query and receives the response in streaming mode.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.

$oAi = ObjCreate("Chilkat.Ai")

; The provider can be "openai", "google", "claude", "deepseek", "xai", or "perplexity".
; Support for additional providers will be added in future versions of Chilkat.
$oAi.Provider = "openai"

; Use your provider's API key.
$oAi.ApiKey = "MY_API_KEY"

; Choose a model.
$oAi.Model = "gpt-4o"

; Indicate streaming mode is to be used.
$oAi.Streaming = True

; Add a text input.
$oAi.InputAddText("Write a detailed story about a turtle who decides to run a bakery.  Describe the setting, the kinds of pastries, how the turtle feels, and include at least three paragraphs.")

; Ask the AI for text output.
$bSuccess = $oAi.Ask("text")
If ($bSuccess = False) Then
    ConsoleWrite($oAi.LastErrorText & @CRLF)
    Exit
EndIf

$oSbEventName = ObjCreate("Chilkat.StringBuilder")
$oSbDelta = ObjCreate("Chilkat.StringBuilder")
$oSbFullResponse = ObjCreate("Chilkat.StringBuilder")
Local $bFinished = False
Local $bAbortFlag = False
Local $iMaxWaitMs = 5000

While $bFinished = False

Local $iResult = $oAi.PollAi($bAbortFlag)
    If ($iResult = 1) Then
        ; We have output waiting.  It should be instantly available.  The maxWaitMs is just-in-case.
        $bSuccess = $oAi.NextAiEvent($iMaxWaitMs,$oSbEventName,$oSbDelta)
        If ($bSuccess = False) Then
            ConsoleWrite($oAi.LastErrorText & @CRLF)
            Exit
        EndIf

        ; Some AI providers send many "empty" events.  Just ignore them.
        If ($oSbEventName.ContentsEqual("empty",True) = False) Then
            ; Log the non-empty events.
            ConsoleWrite($oSbEventName.GetAsString() & @CRLF)

            ; The delta contains the new output.  This could be emitted to a display or the terminal
            ; as real-time output.
            If ($oSbEventName.ContentsEqual("delta",True) = True) Then
                ConsoleWrite($oSbDelta.GetAsString() & @CRLF)

                ; Accumulate the delta's so we can show the full response later.
                $oSbFullResponse.AppendSb($oSbDelta)
            Else
                ; A streaming AI response is always terminated by a single "null_terminator" event.
                $bFinished = $oSbEventName.ContentsEqual("null_terminator",True)
            EndIf

        EndIf

    Else
        If ($iResult = 0) Then
            ; Nothing is immediately available. Sleep for 1/10 of a second before polling again.
            $oAi.SleepMs 100
        Else
            ; Something failed..
            ConsoleWrite($oAi.LastErrorText & @CRLF)
            $bFinished = True
        EndIf

    EndIf

Wend

; Show the accumulated (full) response.
ConsoleWrite("----" & @CRLF)
ConsoleWrite($oSbFullResponse.GetAsString() & @CRLF)

; -------------------------------------------------------------
; The response is in markdown format.
; Also see Markdown to HTML Conversion Examples.
; -------------------------------------------------------------