![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi ActiveX) Streaming AI with Automatic JavaScript AI Tool Function CallingSee more AI ExamplesDemonstrates 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:
Note: This example requires Chilkat v11.4.0 or greater. For more information, see https://www.chilkatsoft.com/automatic_ai_javascript_tool_function_calling.asp
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB; ... procedure TForm1.Button1Click(Sender: TObject); var success: Integer; sbJs: TChilkatStringBuilder; ai: TChilkatAi; evalOnly: Integer; allowAllKeyword: Integer; conversation_name: WideString; sysMessage: WideString; devMessage: WideString; sbEventName: TChilkatStringBuilder; sbDelta: TChilkatStringBuilder; sbFullResponse: TChilkatStringBuilder; maxWaitMs: Integer; finished: Integer; numAsks: Integer; madeFunctionCalls: Integer; streamingDone: Integer; result: Integer; begin success := 0; // ---------------------------------------------------------------------------------- // The Javascript file loaded here is shown at the bottom of this page. // ----------------------------------------------------------------------------------- sbJs := TChilkatStringBuilder.Create(Self); success := sbJs.LoadFile('qa_data/js_tools/horoscope_tools.js','utf-8'); if (success = 0) then begin Memo1.Lines.Add(sbJs.LastErrorText); Exit; end; ai := TChilkatAi.Create(Self); // Register the tools that will be made available to the AI. evalOnly := 0; allowAllKeyword := 1; ai.RegisterJsTools(sbJs.ControlInterface,evalOnly,allowAllKeyword); // The provider can be "openai", "google", "claude", "grok", "mistral", "custom", etc. ai.Provider := 'openai'; // Use your provider's API key. ai.ApiKey := 'MY_API_KEY'; // Choose a model. ai.Model := 'gpt-5-mini'; // Tool function calling must always occur within a conversation. conversation_name := 'convo_astrology'; sysMessage := 'You are a helpful astrologer'; devMessage := 'Respond only with markdown.'; ai.NewConvo(conversation_name,sysMessage,devMessage); // Provide inputs ai.InputAddText('What is my horoscope? I am an Aquarius.'); // Get the response in streaming mode. ai.Streaming := 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 := TChilkatStringBuilder.Create(Self); sbDelta := TChilkatStringBuilder.Create(Self); sbFullResponse := TChilkatStringBuilder.Create(Self); // 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 := 5000; finished := 0; numAsks := 0; // Set a max # of followup Asks to prevent any unexpected infinite looping. while not finished and (numAsks < 10) do begin // Send the request to the AI model. success := ai.Ask('text'); if (success = 0) then begin Memo1.Lines.Add(ai.LastErrorText); Exit; end; madeFunctionCalls := 0; streamingDone := 0; while not streamingDone do begin result := ai.PollAi(0); if (result < 0) then begin Memo1.Lines.Add(ai.LastErrorText); Memo1.Lines.Add('Failed.'); Exit; end; if (result > 0) then begin // We have an event.. success := ai.NextAiEvent(maxWaitMs,sbEventName.ControlInterface,sbDelta.ControlInterface); if (success = 0) then begin Memo1.Lines.Add(ai.LastErrorText); Exit; end; // Is this an event where the AI is requesting a function call? if (sbEventName.ContentsEqual('js_function_call',1)) then begin // 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). success := ai.StreamingJsToolCall(sbDelta.ControlInterface); if (success = 0) then begin // The JS function call failed. streamingDone := 1; end else begin madeFunctionCalls := 1; end; end else begin if (not sbEventName.ContentsEqual('empty',1)) then begin sbFullResponse.AppendSb(sbDelta.ControlInterface); if (sbEventName.ContentsEqual('null_terminator',1)) then begin streamingDone := 1; end; end; end; end else begin // No event arrived, so wait a short time rather than spin in a loop.. ai.SleepMs(100); end; end; if (not madeFunctionCalls) then begin finished := 1; end; numAsks := numAsks + 1; end; Memo1.Lines.Add('Full Response:'); Memo1.Lines.Add(sbFullResponse.GetAsString()); end;
|
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.