Xbase++ Requires Chilkat v11.2.0+
Xbase++
Conversation with Streaming Responses
See more AI Examples
Demonstrates an AI conversation with receiving streaming responses.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oAi
LOCAL cSystemMsg
LOCAL cDeveloperMsg
LOCAL cConversationName
LOCAL oSbEventName
LOCAL oSbDelta
LOCAL oSbFullResponse
LOCAL nFinished
LOCAL nAbortFlag
LOCAL nMaxWaitMs
LOCAL nResult
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 := "google"
// Use your provider's API key.
oAi:ApiKey := "MY_API_KEY"
// Choose a model.
oAi:Model := "gemini-3.6-flash"
// Indicate streaming mode is to be used.
oAi:Streaming := 1
// Create a new conversation to be maintained locally in memory.
// If the conversation is the first to be created, it is also automatically selected.
cSystemMsg := "You are a creative storyteller"
cDeveloperMsg := ""
cConversationName := "test_conversation"
oAi:NewConvo(cConversationName, cSystemMsg, cDeveloperMsg)
// 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 .NOT. nFinished
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 (.NOT. oSbEventName:ContentsEqual("empty", 1))
// 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))
// This example will emit each delta to its own line.
? 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
// -------------------------------------------------------------
// The response is in markdown format.
// Also see Markdown to HTML Conversion Examples.
// -------------------------------------------------------------
// Show the accumulated (full) response.
? "----"
? oSbFullResponse:GetAsString()
? "----"
// ----------------------------------------------------------------------------------------------------------
// 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.")
nSuccess := oAi:Ask("text")
IF (nSuccess == 0)
? oAi:LastErrorText
oAi:destroy()
oSbEventName:destroy()
oSbDelta:destroy()
oSbFullResponse:destroy()
RETURN
ENDIF
oSbFullResponse:Clear()
nFinished := 0
DO WHILE .NOT. nFinished
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 (.NOT. oSbEventName:ContentsEqual("empty", 1))
// 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))
// This example will emit each delta to its own line.
? 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
? "----"
? oSbFullResponse:GetAsString()
oAi:destroy()
oSbEventName:destroy()
oSbDelta:destroy()
oSbFullResponse:destroy()