Sample code for 30+ languages & platforms
DataFlex

Demonstrates Manual Tool Function Calls

See more AI Examples

Demonstrates how to do manual tool function calling using Chilkat. This is where your application manually checks for function calls in the AI's response, makes the function calls, and returns the function call results to the AI.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vJsonTools
    Handle hoJsonTools
    Integer iToolIdx
    Handle hoAi
    String sConversation_name
    String sSysMessage
    String sDevMessage
    Variant vJsonFn
    Handle hoJsonFn
    Integer iNumFnCalls
    Integer iFn_idx
    Variant vSbFnName
    Handle hoSbFnName
    String sCallId
    String sZodiac_sign
    String sApplicationFnCallResult
    Variant vSbResponse
    Handle hoSbResponse
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    // 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 two functions 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"
    //           }
    //         }
    //       }
    //     },
    //     {
    //       "name": "get_compatibility",
    //       "description": "Returns compatibility analysis between two zodiac signs, including a score and explanation.",
    //       "parameters": {
    //         "properties": {
    //           "sign1": {
    //             "type": "string",
    //             "description": "The first zodiac sign (e.g., Aries, Taurus, Gemini)."
    //           },
    //           "sign2": {
    //             "type": "string",
    //             "description": "The second zodiac sign (e.g., Aries, Taurus, Gemini)."
    //           },
    //           "relationship_type": {
    //             "type": "string",
    //             "description": "Type of compatibility to evaluate. (e.g., romantic, friendship, professional, general)"
    //           },
    //           "detail_level": {
    //             "type": "string",
    //             "description": "Level of detail in the response. (e.g., short, medium, detailed)"
    //           }
    //         }
    //       }
    //     }
    //   ]
    // }

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonTools
    If (Not(IsComObjectCreated(hoJsonTools))) Begin
        Send CreateComObject of hoJsonTools
    End

    Move 0 To iToolIdx

    Set ComI Of hoJsonTools To iToolIdx
    Get ComUpdateString Of hoJsonTools "tools[i].name" "get_horoscope" To iSuccess
    Get ComUpdateString Of hoJsonTools "tools[i].description" "Get today's horoscope for an astrological sign." To iSuccess
    Get ComUpdateString Of hoJsonTools "tools[i].parameters.properties.sign.type" "string" To iSuccess
    Get ComUpdateString Of hoJsonTools "tools[i].parameters.properties.sign.description" "An astrological sign like Taurus or Aquarius" To iSuccess

    Move (iToolIdx + 1) To iToolIdx
    Set ComI Of hoJsonTools To iToolIdx
    Get ComUpdateString Of hoJsonTools "tools[i].name" "get_compatibility" To iSuccess
    Get ComUpdateString Of hoJsonTools "tools[i].description" "Returns compatibility analysis between two zodiac signs, including a score and explanation." To iSuccess
    Get ComUpdateString Of hoJsonTools "tools[i].parameters.properties.sign1.type" "string" To iSuccess
    Get ComUpdateString Of hoJsonTools "tools[i].parameters.properties.sign1.description" "The first zodiac sign (e.g., Aries, Taurus, Gemini)." To iSuccess
    Get ComUpdateString Of hoJsonTools "tools[i].parameters.properties.sign2.type" "string" To iSuccess
    Get ComUpdateString Of hoJsonTools "tools[i].parameters.properties.sign2.description" "The second zodiac sign (e.g., Aries, Taurus, Gemini)." To iSuccess
    Get ComUpdateString Of hoJsonTools "tools[i].parameters.properties.relationship_type.type" "string" To iSuccess
    Get ComUpdateString Of hoJsonTools "tools[i].parameters.properties.relationship_type.description" "Type of compatibility to evaluate. (e.g., romantic, friendship, professional, general)" To iSuccess
    Get ComUpdateString Of hoJsonTools "tools[i].parameters.properties.detail_level.type" "string" To iSuccess
    Get ComUpdateString Of hoJsonTools "tools[i].parameters.properties.detail_level.description" "Level of detail in the response. (e.g., short, medium, detailed)" To iSuccess

    // More tools can be added as desired..

    Set ComEmitCompact Of hoJsonTools To False
    Get ComEmit Of hoJsonTools To sTemp1
    Showln sTemp1

    Get Create (RefClass(cComChilkatAi)) To hoAi
    If (Not(IsComObjectCreated(hoAi))) Begin
        Send CreateComObject of hoAi
    End

    // Register the tools that will be made available to the AI.
    Get pvComObject of hoJsonTools to vJsonTools
    Get ComRegisterManualTools Of hoAi vJsonTools To iSuccess

    // The provider can be "openai", "google", "claude", "grok", "mistral", "custom", etc.
    Set ComProvider Of hoAi To "openai"
    // Use your provider's API key.
    Set ComApiKey Of hoAi To "MY_API_KEY"
    // Choose a model.
    Set ComModel Of hoAi To "gpt-5-mini"

    // Tool function calling must always occur within a conversation.
    Move "convo_astrology" To sConversation_name
    Move "You are a helpful astrologer" To sSysMessage
    Move "Respond only with a horoscope generated by a tool. Use the tool output as the final answer." To sDevMessage
    Get ComNewConvo Of hoAi sConversation_name sSysMessage sDevMessage To iSuccess

    // Provide inputs
    Get ComInputAddText Of hoAi "What is my horoscope? I am an Aquarius." To iSuccess

    // Send inputs, tool functions, etc. and ask for a "text" response.
    Get ComAsk Of hoAi "text" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Did the AI respond with requests for tool function calls?
    Get ComHasFunctionCalls Of hoAi To bTemp1
    If (bTemp1 = True) Begin

        Get Create (RefClass(cComChilkatJsonObject)) To hoJsonFn
        If (Not(IsComObjectCreated(hoJsonFn))) Begin
            Send CreateComObject of hoJsonFn
        End
        Set ComEmitCompact Of hoJsonFn To False

        Get pvComObject of hoJsonFn to vJsonFn
        Get ComGetFunctionCalls Of hoAi vJsonFn To iSuccess
        Get ComEmit Of hoJsonFn To sTemp1
        Showln sTemp1

        // 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"
        //       }
        //     }
        //   ]
        // }

        Get ComSizeOfArray Of hoJsonFn "function_call" To iNumFnCalls
        Move 0 To iFn_idx
        While (iFn_idx < iNumFnCalls)
            Set ComI Of hoJsonFn To iFn_idx

            Get Create (RefClass(cComChilkatStringBuilder)) To hoSbFnName
            If (Not(IsComObjectCreated(hoSbFnName))) Begin
                Send CreateComObject of hoSbFnName
            End
            Get pvComObject of hoSbFnName to vSbFnName
            Get ComStringOfSb Of hoJsonFn "function_call[i].name" vSbFnName To iSuccess
            Get ComStringOf Of hoJsonFn "function_call[i].call_id" To sCallId

            Get ComContentsEqual Of hoSbFnName "get_horoscope" True To bTemp1
            If (bTemp1 = True) Begin

                // The get_horoscope function (as defined above) has one argument named "sign".
                Get ComStringOf Of hoJsonFn "function_call[i].args.sign" To sZodiac_sign
                Showln "zodiac_sign = " sZodiac_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:
                Move "Aquarius: Next Tuesday you will befriend a baby otter." To sApplicationFnCallResult

                // Provide the tool call result as an input for the followup Ask.
                Get ComInputAddFnResult Of hoAi sCallId sApplicationFnCallResult To iSuccess
            End

            // Your application would add code to check for and handle each possible function call.

            Move (iFn_idx + 1) To iFn_idx
        Loop

        // After making the requested tool function calls, send the results back to the AI.
        Get ComAsk Of hoAi "text" To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoAi To sTemp1
            Showln sTemp1
            Procedure_Return
        End

    End

    // Get the final AI response.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponse
    If (Not(IsComObjectCreated(hoSbResponse))) Begin
        Send CreateComObject of hoSbResponse
    End
    Get pvComObject of hoSbResponse to vSbResponse
    Get ComGetOutputTextSb Of hoAi vSbResponse To iSuccess
    Get ComGetAsString Of hoSbResponse To sTemp1
    Showln sTemp1


End_Procedure