InlineAIPrompt

TsgcHTMLComponent_InlineAIPrompt: a popover that asks the assistant right where the user works, next to a memo, a rich editor or in the header of a grid column, in Delphi, C++ Builder and .NET. A person decides what to keep.

TsgcHTMLComponent_InlineAIPrompt

A human in the loop control. The prompt is typed in the popover, the answer is shown in it, and only Accept reports the answer to your application through OnAIAccept. The component never writes into a dataset, a field or the page. It answers through its AIAssistant property, a TsgcHTMLAIAssistant.

Component class

TsgcHTMLComponent_InlineAIPrompt (unit sgcHTML_Component_InlineAIPrompt)

Renders

A trigger button and a popover card with the prompt box, the answer, Accept and Discard

Family

Chat & AI

Languages

Delphi, C++ Builder, .NET

Assign the assistant, route four actions, accept the answer

Give the popover a PromptID and a TargetName, assign the AIAssistant, fill Context with the rows the assistant may see, and route the four actions it posts into Open, ProcessPrompt, Accept and Discard. What was accepted reaches you in OnAIAccept.

uses
  sgcHTML_AI, sgcHTML_Component_InlineAIPrompt;

FAI := TsgcHTMLAIAssistant.Create(Self);
FAI.Provider := aiprOpenAI;
FAI.APIKey := 'API_KEY';
FAI.AllowRowDataToProvider := True;   // the answer is drafted over Context

FPrompt := TsgcHTMLComponent_InlineAIPrompt.Create(Self);
FPrompt.PromptID := 'notePrompt';
FPrompt.TargetName := 'Note';
FPrompt.Title := 'Draft the note';
FPrompt.Placeholder := 'e.g. write a short delivery note';
FPrompt.AIAssistant := FAI;
FPrompt.Context.Add('Customer=Northwind');   // the rows the assistant may see
FPrompt.OnAIAccept := PromptAccept;

Response := FPrompt.HTML;   // the trigger, and the popover while IsOpen

// only when a human presses Accept, aValue is the answer that was on screen
procedure TMain.PromptAccept(Sender: TObject; const aValue: string);
begin
  SaveNote(aValue);   // your application writes it where it belongs
end;

// in the message handler, one call for each action the popover posts
if vAction = 'inlineAIOpen' then
  FPrompt.Open
else if vAction = 'inlineAIAsk' then
  FPrompt.ProcessPrompt(vText)
else if vAction = 'inlineAIAccept' then
  FPrompt.Accept
else if vAction = 'inlineAIDiscard' then
  FPrompt.Discard;
oHTMX.PushFragment(vGuid, FPrompt.HTML);
// includes: sgcHTML_AI.hpp, sgcHTML_Component_InlineAIPrompt.hpp

FAI = new TsgcHTMLAIAssistant(this);
FAI->Provider = aiprOpenAI;
FAI->APIKey = "API_KEY";
FAI->AllowRowDataToProvider = true;   // the answer is drafted over Context

FPrompt = new TsgcHTMLComponent_InlineAIPrompt(this);
FPrompt->PromptID = "notePrompt";
FPrompt->TargetName = "Note";
FPrompt->Title = "Draft the note";
FPrompt->Placeholder = "e.g. write a short delivery note";
FPrompt->AIAssistant = FAI;
FPrompt->Context->Add("Customer=Northwind");   // the rows the assistant may see
FPrompt->OnAIAccept = PromptAccept;

String html = FPrompt->HTML;   // the trigger, and the popover while IsOpen

// only when a human presses Accept, aValue is the answer that was on screen
void __fastcall TMain::PromptAccept(TObject *Sender, const String aValue)
{
  SaveNote(aValue);   // your application writes it where it belongs
}

// in the message handler, one call for each action the popover posts
if (vAction == "inlineAIOpen")
  FPrompt->Open();
else if (vAction == "inlineAIAsk")
  FPrompt->ProcessPrompt(vText);
else if (vAction == "inlineAIAccept")
  FPrompt->Accept();
else if (vAction == "inlineAIDiscard")
  FPrompt->Discard();
oHTMX->PushFragment(vGuid, FPrompt->HTML);
using esegece.sgcWebSockets;

var ai = new TsgcHTMLAIAssistant();
ai.Provider = TsgcHTMLAIAssistant.TsgcHTMLAIProvider.aiprOpenAI;
ai.APIKey = "API_KEY";
ai.AllowRowDataToProvider = true;   // the answer is drafted over Context

var prompt = new TsgcHTMLComponent_InlineAIPrompt();
prompt.PromptID = "notePrompt";
prompt.TargetName = "Note";
prompt.Title = "Draft the note";
prompt.Placeholder = "e.g. write a short delivery note";
prompt.AIAssistant = ai;
prompt.Context.Add("Customer=Northwind");   // the rows the assistant may see
prompt.OnAIAccept += PromptAccept;

string html = prompt.HTML;   // the trigger, and the popover while IsOpen

// only when a human presses Accept, value is the answer that was on screen
void PromptAccept(object sender, string value)
{
    SaveNote(value);   // your application writes it where it belongs
}

// in the message handler, one call for each action the popover posts
if (action == "inlineAIOpen")
    prompt.Open();
else if (action == "inlineAIAsk")
    prompt.ProcessPrompt(text);
else if (action == "inlineAIAccept")
    prompt.Accept();
else if (action == "inlineAIDiscard")
    prompt.Discard();
htmx.PushFragment(guid, prompt.HTML);

Key properties & methods

The members you reach for most often.

Human in the loop

The prompt is typed in the popover, the answer is shown in it, and only Accept reports the answer to your application, through OnAIAccept. Discard closes the popover and throws the answer away. The component never writes into a dataset, a field or the page: your application decides what to do with what comes back. Model text is written through the node layer, so it is escaped and can never become markup.

Methods

Open opens the popover with no prompt typed yet. ProcessPrompt(aPrompt) asks OnAIPrompt first and, when that did not handle it, the assistant over Context, and leaves the popover open with the answer in ResultText. Accept reports ResultText through OnAIAccept and closes the popover, and reports nothing when there is no answer. Discard closes it.

Events

OnAIPrompt(Sender, aPrompt, aResult, aHandled) lets your application answer itself: set aResult and aHandled. Leave aHandled False and the assistant is asked instead. OnAIAccept(Sender, aValue) fires when a person presses Accept, and aValue is the answer that was on screen.

The assistant and Context

Assign a TsgcHTMLAIAssistant to AIAssistant, see AIAssistant. The popover calls its SuggestColumnValues over Context, a TStringList with one line for each row. Those are row data: nothing is sent while the assistant has AllowRowDataToProvider False, and nothing is asked at all when Context is empty and OnAIPrompt did not answer.

Properties

PromptID (default sgcInlineAI) is the id of the popover and the value posted in the prompt field. TargetName names the control the accepted answer belongs to and is posted in the target field, so you can route it. Title, Placeholder and ButtonText set the texts, IsOpen shows the panel, and PromptText and ResultText hold what was asked and answered.

Actions it posts

There is no HTTP route. Every control is a data-sgc-ws-send form: inlineAIOpen (fields prompt, target), inlineAIAsk (plus text), inlineAIAccept and inlineAIDiscard. Dispatch on action, check prompt to know which popover sent it, and push the re-rendered component back. A host that streams an answer re-renders it with more ResultText.

Output

The trigger is always rendered. With IsOpen on, a card is rendered next to it with the title, the prompt box, the ask button and, once there is an answer, the answer with an Accept and a Discard control. The texts come from the localized strings AIAsk, AIAccept and AIDiscard.

One user per instance

The component keeps the question and the pending answer between two messages, so one instance is one user: key it per session in a multi user application. Messages are not CSRF-checked, like any other htmx WebSocket message, so keep the authorization of what OnAIAccept does on the server.

Editions and platforms

The unit compiles when SGC_HTML is defined, which sgcVer.inc does not do for Android and iOS. A provider needs the AI units of the Enterprise or All-Access edition, or the sgcAI pack, and they are Windows only. This component has no rule-based fallback, so on other platforms, or with no provider, answer in OnAIPrompt yourself.

Keep exploring

Online HelpFull API reference and usage guide for this component.
AI Data Pack guideHow the assistant, the operation model and the data components fit together, and what leaves the machine.
All sgcHTML ComponentsBrowse the full feature matrix of 80+ components.
Download Free TrialThe 30-day trial ships the 60.HTML demo projects, including 18.AIData, where a popover drafts the note of a form.
PricingSingle, Team and Site licenses with full source code.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Get Started?

Download the free trial and start building web UIs in Delphi, C++ Builder and .NET.