Sample code for 30+ languages & platforms
AutoIt

Streaming AI with Automatic JavaScript AI Tool Function Calling

See more AI Examples

Demonstrates how to get AI responses in streaming mode, including possible tool function calling using Chilkat with embedded Chilkat.Js JavaScript. Automatic JavaScript tool calls are characterized by:
  • Tool function implementations are in JavaScript.
  • The JavaScript also provides a tool registry and permissions.
  • The JavaScript runs embedded within your application using Chilkat.Js.
  • Tool calls are handled entirely within Chilkat. Your application does not need to manually check for function calls in the AI response. Internal to the Ask function, Chilkat will handle function calls by calling your registered JavaScript tools and will send results back to the AI model until the final response is received.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; ----------------------------------------------------------------------------------
; The Javascript file loaded here is shown at the bottom of this page.
; -----------------------------------------------------------------------------------

$oSbJs = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oSbJs.LoadFile("qa_data/js_tools/horoscope_tools.js","utf-8")
If ($bSuccess = False) Then
    ConsoleWrite($oSbJs.LastErrorText & @CRLF)
    Exit
EndIf

$oAi = ObjCreate("Chilkat.Ai")

; Register the tools that will be made available to the AI.
Local $bEvalOnly = False
Local $bAllowAllKeyword = True
$oAi.RegisterJsTools($oSbJs,$bEvalOnly,$bAllowAllKeyword)

; The provider can be "openai", "google", "claude", "grok", "mistral", "custom", etc.
$oAi.Provider = "openai"
; Use your provider's API key.
$oAi.ApiKey = "MY_API_KEY"
; Choose a model.
$oAi.Model = "gpt-5-mini"

; Tool function calling must always occur within a conversation.
Local $sConversation_name = "convo_astrology"
Local $sysMessage = "You are a helpful astrologer"
Local $sDevMessage = "Respond only with markdown."
$oAi.NewConvo($sConversation_name,$sysMessage,$sDevMessage)

; Provide inputs
$oAi.InputAddText("What is my horoscope? I am an Aquarius.")

; Get the response in streaming mode.
$oAi.Streaming = True

; In streaming mode, if we receive an AI event that is a request for tool use,
; we'll need to make the call to the JavaScript and then continue with a followup Ask,
; until the final response is received.

$oSbEventName = ObjCreate("Chilkat.StringBuilder")
$oSbDelta = ObjCreate("Chilkat.StringBuilder")
$oSbFullResponse = ObjCreate("Chilkat.StringBuilder")

; When PollAi returns with an event, it's highly unlikely the
; call to NextAiEvent does not immediately return.  Setting a max
; timeout is just a precaution..
Local $iMaxWaitMs = 5000

Local $bFinished = False
Local $iNumAsks = 0
; Set a max # of followup Asks to prevent any unexpected infinite looping.
While Not $bFinished And ($iNumAsks < 10)

    ; Send the request to the AI model.
    $bSuccess = $oAi.Ask("text")
    If ($bSuccess = False) Then
        ConsoleWrite($oAi.LastErrorText & @CRLF)
        Exit
    EndIf

Local $bMadeFunctionCalls = False
Local $bStreamingDone = False

    While Not $bStreamingDone
Local $iResult = $oAi.PollAi(False)
        If ($iResult < 0) Then
            ConsoleWrite($oAi.LastErrorText & @CRLF)
            ConsoleWrite("Failed." & @CRLF)
            Exit
        EndIf

        If ($iResult > 0) Then
            ; We have an event..
            $bSuccess = $oAi.NextAiEvent($iMaxWaitMs,$oSbEventName,$oSbDelta)
            If ($bSuccess = False) Then
                ConsoleWrite($oAi.LastErrorText & @CRLF)
                Exit
            EndIf

            ; Is this an event where the AI is requesting a function call?
            If ($oSbEventName.ContentsEqual("js_function_call",True)) Then
                ; Call the JavaScript function.
                ; We don't need to find the exact function and make the call.  
                ; We can simply pass the sbDelta (which contains information about the function to be called and the args).
                ; Chilkat will find the registered JavaScript function, call it, and add the results to the conversation.
                ; We indicate that function calls were made, which results in a followup Ask (in the outer loop).
                $bSuccess = $oAi.StreamingJsToolCall($oSbDelta)
                If ($bSuccess = False) Then
                    ; The JS function call failed.
                    $bStreamingDone = True
                Else
                    $bMadeFunctionCalls = True
                EndIf

            Else
                If (Not $oSbEventName.ContentsEqual("empty",True)) Then
                    $oSbFullResponse.AppendSb($oSbDelta)

                    If ($oSbEventName.ContentsEqual("null_terminator",True)) Then
                        $bStreamingDone = True
                    EndIf

                EndIf

            EndIf

        Else
            ; No event arrived, so wait a short time rather than spin in a loop..
            $oAi.SleepMs 100
        EndIf

    Wend

    If (Not $bMadeFunctionCalls) Then
        $bFinished = True
    EndIf

    $iNumAsks = $iNumAsks + 1
Wend

ConsoleWrite("Full Response:" & @CRLF)
ConsoleWrite($oSbFullResponse.GetAsString() & @CRLF)