Sample code for 30+ languages & platforms
AutoIt

Conversation with Streaming Responses

See more AI Examples

Demonstrates an AI conversation with receiving streaming responses.

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 = "google"

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

; Choose a model.
$oAi.Model = "gemini-2.5-flash"

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

; Create a new conversation to be maintained locally in memory.
; If the conversation is the first to be created, it is also automatically selected.
Local $systemMsg = "You are a creative storyteller"
Local $sDeveloperMsg = ""
Local $sConversationName = "test_conversation"
$oAi.NewConvo($sConversationName,$systemMsg,$sDeveloperMsg)

; 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 Not $bFinished

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 (Not $oSbEventName.ContentsEqual("empty",True)) Then

            ; 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)) Then
                ; This example will emit each delta to its own line.
                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

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

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

; ----------------------------------------------------------------------------------------------------------
; For the 2nd request in this conversation, ask for a shorter version of the story.
$oAi.InputAddText("Rewrite the story, but this time make it shorter, about one third as long.")
$bSuccess = $oAi.Ask("text")
If ($bSuccess = False) Then
    ConsoleWrite($oAi.LastErrorText & @CRLF)
    Exit
EndIf

$oSbFullResponse.Clear 
$bFinished = False

While Not $bFinished

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 (Not $oSbEventName.ContentsEqual("empty",True)) Then

            ; 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)) Then
                ; This example will emit each delta to its own line.
                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

ConsoleWrite("----" & @CRLF)
ConsoleWrite($oSbFullResponse.GetAsString() & @CRLF)