AI Chat Component — sgcAI | eSeGeCe

AI Chat

TsgcAIChat is a single chat client for seven large language model providers. Set Provider, set an API key and a model, then call Chat. Changing vendor is one assignment, the rest of your code stays the same.

TsgcAIChat

A provider-neutral chat component. It keeps the conversation history, builds the provider-specific JSON body, drives the matching HTTP API client and hands you back the assistant text, either in one call or token by token.

Component class

TsgcAIChat

Purpose

Provider-neutral LLM chat

Family

Chat & agents

Platforms

Win32 and Win64

sgcAI is self-contained. This component ships inside sgcAI together with the sgcWebSockets Core runtime it is built on, so there is no base license to buy.

Ask a model a question

Pick the provider, set the key and the model, call Chat. Streaming and async variants use the same configuration.

uses
  sgcAI, sgcAI_Chat;

var
  oChat: TsgcAIChat;
  vAnswer: string;
begin
  oChat := TsgcAIChat.Create(nil);
  oChat.Provider := aicpAnthropic;
  oChat.ChatOptions.ApiKey    := 'sk-ant-...';
  oChat.ChatOptions.Model     := 'claude-sonnet-4-5';
  oChat.ChatOptions.MaxTokens := 4096;
  oChat.SystemMessage := 'You are a Delphi expert.';
  oChat.MaxHistoryMessages := 20;

  // Blocking call, returns the assistant text.
  vAnswer := oChat.Chat('How do I keep a WebSocket connection alive?');

  // Token by token, delivered on OnChatStream.
  oChat.OnChatStream := OnChatStreamHandler;
  oChat.ChatStream('Now explain it to a beginner.');
end;

procedure TForm1.OnChatStreamHandler(Sender: TObject;
  const aChunk: string; var Cancel: Boolean);
begin
  Memo1.Text := Memo1.Text + aChunk;
  Cancel := StopRequested;   // abort a long generation
end;
// includes: sgcAI.hpp, sgcAI_Chat.hpp

TsgcAIChat *oChat = new TsgcAIChat(NULL);
oChat->Provider = aicpOpenAI;
oChat->ChatOptions->ApiKey = "sk-...";
oChat->ChatOptions->Model  = "gpt-4o";
oChat->SystemMessage = "You are a Delphi expert.";

String vAnswer = oChat->Chat("How do I keep a WebSocket connection alive?");

// Local models: same code, no data leaves the machine.
oChat->Provider = aicpOllama;
oChat->ChatOptions->BaseUrl = "http://127.0.0.1:11434";
oChat->ChatOptions->Model   = "llama3";
vAnswer = oChat->Chat("Same question, local model.");

Key properties & methods

The members you reach for most often.

Provider

Provider selects the vendor: aicpOpenAI, aicpAnthropic, aicpGemini, aicpDeepSeek, aicpOllama, aicpGrok or aicpMistral. Changing it releases the cached client for the previous provider.

ChatOptions

ApiKey, Model, MaxTokens, Temperature, TopP and BaseUrl. Leave Temperature and TopP at their default and they are omitted from the request, so the provider default applies.

Conversation

SystemMessage sets the persona for every turn. MaxHistoryMessages caps how many past messages are replayed, which bounds token cost. ClearHistory and GetHistory reset and read the transcript.

Calls

Chat blocks and returns the answer. ChatStream streams over SSE. ChatWithSystem overrides the system prompt for a single turn. ChatAsync returns a future on Delphi 2010 and later.

Events

OnChatMessage reports each completed message with its role. OnChatStream delivers every chunk and carries a Cancel flag. OnChatError surfaces provider and transport errors.

Local models

With Provider set to aicpOllama, ChatOptions.BaseUrl points the component at your own Ollama host, so prompts and answers never leave the machine.

Specifications & references

Authoritative sources for the services and protocols this component uses.

Keep exploring

Online HelpFull API reference and usage guide for the sgcAI components.
All sgcAI ComponentsBrowse the full feature matrix of all 15 components.
Download Free TrialTry all seven providers with your own API keys.
PricingSingle, Team and Site licenses, full source code, no base product required.
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 put a language model behind a Delphi form in a few minutes.