AIChat Component — sgcHTML | eSeGeCe

AIChat

TsgcHTMLComponent_AIChat — an AI assistant chat with a provider/model selector, live token streaming and RAG source citations, in Delphi, C++ Builder and .NET.

TsgcHTMLComponent_AIChat

An assistant surface that extends TsgcHTMLComponent_ChatBox with an OpenAI / Anthropic / Gemini provider header, streamed replies and collapsible RAG sources. Pick a provider, handle OnChatSend, then read the HTML property.

Component class

TsgcHTMLComponent_AIChat

Renders

Bootstrap 5 card + scoped CSS

Family

Chat & AI

Languages

Delphi, C++ Builder, .NET

Create it, wire OnChatSend, render it

Choose AIProvider and ModelName, handle OnChatSend to produce the reply, then read HTML. The event is how a browser message reaches your Delphi, C++ Builder or .NET code.

uses
  sgcHTML_Enums, sgcHTML_Component_AIChat;

var
  oAI: TsgcHTMLComponent_AIChat;
begin
  oAI := TsgcHTMLComponent_AIChat.Create(nil);
  try
    oAI.AIProvider := apOpenAI;
    oAI.ModelName := 'gpt-4o';
    oAI.AIName := 'Support Bot';
    oAI.SystemPrompt := 'You are a helpful assistant.';
    oAI.WelcomeMessage := 'Hi! Ask me anything.';
    oAI.StreamingEnabled := True;
    oAI.OnChatSend := DoChatSend;   // browser message -> your code

    WebModule.Response := oAI.HTML;   // Bootstrap card + AI header
  finally
    oAI.Free;
  end;
end;

// OnChatSend hands you the user message + the JSON history,
// you call your LLM and stream the answer back:
procedure TForm1.DoChatSend(Sender: TObject; const aUserMessage,
  aConversationHistory: string);
begin
  oAI.BeginStreaming;
  oAI.PushStreamChunk('Sure, ');
  oAI.PushStreamChunk('here is the answer...');
  oAI.EndStreaming;
  WebSocket.WriteData(oAI.GetStreamFragmentHTML);
end;
// includes: sgcHTML_Enums.hpp, sgcHTML_Component_AIChat.hpp

TsgcHTMLComponent_AIChat *oAI = new TsgcHTMLComponent_AIChat(NULL);
try
{
  oAI->AIProvider = apOpenAI;
  oAI->ModelName = "gpt-4o";
  oAI->AIName = "Support Bot";
  oAI->SystemPrompt = "You are a helpful assistant.";
  oAI->WelcomeMessage = "Hi! Ask me anything.";
  oAI->StreamingEnabled = true;
  oAI->OnChatSend = DoChatSend;   // browser message -> your code

  String html = oAI->HTML;   // Bootstrap card + AI header
}
__finally
{
  delete oAI;
}

// OnChatSend handler: call your LLM, then stream the reply:
void __fastcall TForm1::DoChatSend(TObject *Sender,
  const String aUserMessage, const String aConversationHistory)
{
  oAI->BeginStreaming();
  oAI->PushStreamChunk("Sure, ");
  oAI->PushStreamChunk("here is the answer...");
  oAI->EndStreaming();
}
using esegece.sgcWebSockets;

var ai = new TsgcHTMLComponent_AIChat();
ai.AIProvider = TsgcHTMLAIProvider.apOpenAI;
ai.ModelName = "gpt-4o";
ai.AIName = "Support Bot";
ai.SystemPrompt = "You are a helpful assistant.";
ai.WelcomeMessage = "Hi! Ask me anything.";
ai.StreamingEnabled = true;

// OnChatSend: browser message -> your code -> stream the reply
ai.OnChatSend += (sender, userMessage, conversationHistory) =>
{
    ai.BeginStreaming();
    ai.PushStreamChunk("Sure, ");
    ai.PushStreamChunk("here is the answer...");
    ai.EndStreaming();
};

string html = ai.HTML;   // Bootstrap card + AI header

Key properties & methods

The members you reach for most often.

Provider & model

AIProvider selects apOpenAI, apAnthropic, apGemini or apCustom; ModelName labels the header; ShowModelSelector toggles it; AIName names the assistant.

Conversation

SystemPrompt and WelcomeMessage seed the chat; UserColor and AIColor (both TsgcHTMLColor) tint the bubbles; GetConversationHistoryJSON returns the role/content array for your LLM call.

Send event

OnChatSend fires with the user message and JSON history when the visitor submits — the hook where you call your model. ProcessUserMessage(aMessage) drives that flow from code.

Streaming

StreamingEnabled turns it on; BeginStreaming, PushStreamChunk(aChunk) and EndStreaming grow the reply token by token; GetStreamFragmentHTML returns the htmx fragment to push.

RAG sources

RAGEnabled with OnRAGContext injects retrieved context; RAGDisplayMode (rdInline/rdCollapsible/rdFootnotes) and AddAIMessageWithSources(aText, aSourcesHTML) render the citations.

Inherited & output

From ChatBox: Messages, Title, Height, InputPlaceholder and ShowTypingIndicator. HTML returns the whole card; GetLastMessageHTML returns just the newest bubble.

Keep exploring

Online HelpFull API reference and usage guide for this component.
All sgcHTML ComponentsBrowse the full feature matrix of 80+ components.
Download Free TrialThe 30-day trial ships the 60.HTML demo projects.
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.