PureBasic
PureBasic
Streaming AI with Manual AI Tool Function Calling
See more AI Examples
Demonstrates how to get AI responses in streaming mode, including manual tool function calls.Chilkat PureBasic Downloads
IncludeFile "CkAi.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
; Create the following JSON to define tool functions available for the AI to use.
; Note: You'll use the following JSON format regardless of the AI provider, whether
; it be ChatGPT, Gemini, Claude, Grok, etc. Chilkat automatically converts to the required
; format needed for a given AI provider.
; In this example, the application is providing a single function the AI may choose to call.
; {
; "tools": [
; {
; "name": "get_horoscope",
; "description": "Get today's horoscope for an astrological sign.",
; "parameters": {
; "properties": {
; "sign": {
; "type": "string",
; "description": "An astrological sign like Taurus or Aquarius"
; }
; }
; }
; }
; ]
; }
jsonTools.i = CkJsonObject::ckCreate()
If jsonTools.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
toolIdx.i = 0
CkJsonObject::setCkI(jsonTools, toolIdx)
CkJsonObject::ckUpdateString(jsonTools,"tools[i].name","get_horoscope")
CkJsonObject::ckUpdateString(jsonTools,"tools[i].description","Get today's horoscope for an astrological sign.")
CkJsonObject::ckUpdateString(jsonTools,"tools[i].parameters.properties.sign.type","string")
CkJsonObject::ckUpdateString(jsonTools,"tools[i].parameters.properties.sign.description","An astrological sign like Taurus or Aquarius")
; More tools can be added as desired..
CkJsonObject::setCkEmitCompact(jsonTools, 0)
Debug CkJsonObject::ckEmit(jsonTools)
ai.i = CkAi::ckCreate()
If ai.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Register the tools that will be made available to the AI.
CkAi::ckRegisterManualTools(ai,jsonTools)
; The provider can be "openai", "google", "claude", "grok", "mistral", "custom", etc.
CkAi::setCkProvider(ai, "openai")
; Use your provider's API key.
CkAi::setCkApiKey(ai, "MY_API_KEY")
; Choose a model.
CkAi::setCkModel(ai, "gpt-5-mini")
; Tool function calling must always occur within a conversation.
conversation_name.s = "convo_astrology"
sysMessage.s = "You are a helpful astrologer"
devMessage.s = "Respond only with markdown."
CkAi::ckNewConvo(ai,conversation_name,sysMessage,devMessage)
; Provide inputs
CkAi::ckInputAddText(ai,"What is my horoscope? I am an Aquarius.")
; Get the response in streaming mode.
CkAi::setCkStreaming(ai, 1)
; 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.
sbEventName.i = CkStringBuilder::ckCreate()
If sbEventName.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
sbDelta.i = CkStringBuilder::ckCreate()
If sbDelta.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
sbFullResponse.i = CkStringBuilder::ckCreate()
If sbFullResponse.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; 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..
maxWaitMs.i = 5000
jsonFn.i = CkJsonObject::ckCreate()
If jsonFn.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
finished.i = 0
numAsks.i = 0
; Set a max # of followup Asks to prevent any unexpected infinite looping.
While Not finished AND (numAsks < 10)
; Send the request to the AI model.
success = CkAi::ckAsk(ai,"text")
If success = 0
Debug CkAi::ckLastErrorText(ai)
CkJsonObject::ckDispose(jsonTools)
CkAi::ckDispose(ai)
CkStringBuilder::ckDispose(sbEventName)
CkStringBuilder::ckDispose(sbDelta)
CkStringBuilder::ckDispose(sbFullResponse)
CkJsonObject::ckDispose(jsonFn)
ProcedureReturn
EndIf
madeFunctionCalls.i = 0
streamingDone.i = 0
While Not streamingDone
result.i = CkAi::ckPollAi(ai,0)
If result < 0
Debug CkAi::ckLastErrorText(ai)
Debug "Failed."
CkJsonObject::ckDispose(jsonTools)
CkAi::ckDispose(ai)
CkStringBuilder::ckDispose(sbEventName)
CkStringBuilder::ckDispose(sbDelta)
CkStringBuilder::ckDispose(sbFullResponse)
CkJsonObject::ckDispose(jsonFn)
ProcedureReturn
EndIf
If result > 0
; We have an event..
success = CkAi::ckNextAiEvent(ai,maxWaitMs,sbEventName,sbDelta)
If success = 0
Debug CkAi::ckLastErrorText(ai)
CkJsonObject::ckDispose(jsonTools)
CkAi::ckDispose(ai)
CkStringBuilder::ckDispose(sbEventName)
CkStringBuilder::ckDispose(sbDelta)
CkStringBuilder::ckDispose(sbFullResponse)
CkJsonObject::ckDispose(jsonFn)
ProcedureReturn
EndIf
; Is this an event where the AI is requesting a function call?
If CkStringBuilder::ckContentsEqual(sbEventName,"function_call",1)
CkJsonObject::ckLoadSb(jsonFn,sbDelta)
; Note: Chilkat will convert responses from all AI providers to this format:
; {
; "function_call": [
; {
; "name": "get_horoscope",
; "call_id": "call_RYmeysYQFocFc7Z2ofkv61dW",
; "arguments": "{\"sign\":\"Aquarius\"}",
; "args": {
; "sign": "Aquarius"
; }
; }
; ]
; }
numFnCalls.i = CkJsonObject::ckSizeOfArray(jsonFn,"function_call")
fn_idx.i = 0
While (fn_idx < numFnCalls)
CkJsonObject::setCkI(jsonFn, fn_idx)
sbFnName.i = CkStringBuilder::ckCreate()
If sbFnName.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckStringOfSb(jsonFn,"function_call[i].name",sbFnName)
callId.s = CkJsonObject::ckStringOf(jsonFn,"function_call[i].call_id")
If CkStringBuilder::ckContentsEqual(sbFnName,"get_horoscope",1) = 1
; The get_horoscope function (as defined above) has one argument named "sign".
zodiac_sign.s = CkJsonObject::ckStringOf(jsonFn,"function_call[i].args.sign")
Debug "zodiac_sign = " + zodiac_sign
; Insert application code here to call your app's get_horoscope function, passing the zodiac_sign to it..
; For this example, we'll pretend the app's get_horoscope function returned the following:
applicationFnCallResult.s = "Aquarius: Next Tuesday you will befriend a baby otter."
; Provide the tool call result as an input for the followup Ask.
CkAi::ckInputAddFnResult(ai,callId,applicationFnCallResult)
madeFunctionCalls = 1
EndIf
; Your application would add code to check for and handle each possible function call.
fn_idx = fn_idx + 1
Wend
Else
If Not CkStringBuilder::ckContentsEqual(sbEventName,"empty",1)
CkStringBuilder::ckAppendSb(sbFullResponse,sbDelta)
If CkStringBuilder::ckContentsEqual(sbEventName,"null_terminator",1)
streamingDone = 1
EndIf
EndIf
EndIf
Else
; No event arrived, so wait a short time rather than spin in a loop..
CkAi::ckSleepMs(ai,100)
EndIf
Wend
If Not madeFunctionCalls
finished = 1
EndIf
numAsks = numAsks + 1
Wend
Debug "Full Response:"
Debug CkStringBuilder::ckGetAsString(sbFullResponse)
CkJsonObject::ckDispose(jsonTools)
CkAi::ckDispose(ai)
CkStringBuilder::ckDispose(sbEventName)
CkStringBuilder::ckDispose(sbDelta)
CkStringBuilder::ckDispose(sbFullResponse)
CkJsonObject::ckDispose(jsonFn)
CkStringBuilder::ckDispose(sbFnName)
ProcedureReturn
EndProcedure