Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.2.0+

AI: Simple Stateless Streaming Response

See more AI Examples

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

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oAi
LOCAL oSbEventName
LOCAL oSbDelta
LOCAL oSbFullResponse
LOCAL nFinished
LOCAL nAbortFlag
LOCAL nMaxWaitMs
LOCAL nResult

nSuccess := 0

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

oAi := CreateObject("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-5.6-terra"

//  Indicate streaming mode is to be used.
oAi:Streaming := 1

//  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.
nSuccess := oAi:Ask("text")
IF (nSuccess == 0)
    ? oAi:LastErrorText
    oAi:destroy()
    RETURN
ENDIF

oSbEventName := CreateObject("Chilkat.StringBuilder")
oSbDelta := CreateObject("Chilkat.StringBuilder")
oSbFullResponse := CreateObject("Chilkat.StringBuilder")
nFinished := 0
nAbortFlag := 0
nMaxWaitMs := 5000

DO WHILE nFinished == 0

    nResult := oAi:PollAi(nAbortFlag)
    IF (nResult == 1)
        //  We have output waiting.  It should be instantly available.  The maxWaitMs is just-in-case.
        nSuccess := oAi:NextAiEvent(nMaxWaitMs, oSbEventName, oSbDelta)
        IF (nSuccess == 0)
            ? oAi:LastErrorText
            oAi:destroy()
            oSbEventName:destroy()
            oSbDelta:destroy()
            oSbFullResponse:destroy()
            RETURN
        ENDIF

        //  Some AI providers send many "empty" events.  Just ignore them.
        IF (oSbEventName:ContentsEqual("empty", 1) == 0)
            //  Log the non-empty events.
            ? oSbEventName:GetAsString()

            //  The delta contains the new output.  This could be emitted to a display or the terminal
            //  as real-time output.
            IF (oSbEventName:ContentsEqual("delta", 1) == 1)
                ? oSbDelta:GetAsString()

                //  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.
                nFinished := oSbEventName:ContentsEqual("null_terminator", 1)
            ENDIF

        ENDIF

    ELSE
        IF (nResult == 0)
            //  Nothing is immediately available. Sleep for 1/10 of a second before polling again.
            oAi:SleepMs(100)
        ELSE
            //  Something failed..
            ? oAi:LastErrorText
            nFinished := 1
        ENDIF

    ENDIF

ENDDO

//  Show the accumulated (full) response.
? "----"
? oSbFullResponse:GetAsString()

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

oAi:destroy()
oSbEventName:destroy()
oSbDelta:destroy()
oSbFullResponse:destroy()