sgcAI in five minutes

One component, one provider property, one call. This page gets a Delphi application talking to a large language model, shows you how to stream the answer instead of waiting for it, and is clear about the one platform limit that catches people out.

OpenAI, Claude, Gemini, DeepSeek, Ollama, Grok, Mistral
TsgcAIChat is Windows only
Enterprise edition, or the standalone sgcAI package

What the first call needs

A component, a key, a model name, and a prompt. No JSON to build and no HTTP client to configure.

Component

TsgcAIChat on the SGC AI palette page. It is a thin published wrapper around TsgcAI_Chat, which is what the shipped demos create in code.

Unit

sgcAI_Chat.pas for the class, or the barrel unit sgcAI.pas for the palette component. The demos write uses sgcAI_Chat;.

The one property that switches vendor

Provider, of type TsgcAIChatProvider. The seven members are aicpOpenAI, aicpAnthropic, aicpGemini, aicpDeepSeek, aicpOllama, aicpGrok and aicpMistral. Nothing else in your code changes.

Platform, read this one

TsgcAIChat is compiled for Windows only, Win32 and Win64. The per-vendor REST clients and the MCP client and server are not restricted. See the table below for why.

Requirements and editions

The edition column is the define that gates the code, with the line it sits on in Source/sgcVer.inc.

What Value
IDE Delphi 7 through RAD Studio 13, and the matching C++Builder versions. ChatAsync is the one member with a narrower floor, because it is wrapped in {$IFDEF D2010}.
Uses clause sgcAI_Chat for TsgcAI_Chat, TsgcAIChatProvider and the event types.
Edition, inside sgcWebSockets SGC_AI is defined on line 798, inside the {$IFDEF SGC_EDT_ENT} block that runs from line 760 to line 839. So Enterprise and up, not Standard and not Professional.
Edition, standalone package The sgcAI product defines SGC_PACK_AI on line 854, and its own block on lines 943 to 957 defines SGC_AI on line 945. Same components, without the rest of the library.
Why Windows only Both definitions of SGC_AI sit inside a {$IFDEF MSWINDOWS}, at line 797 and at line 944. Off Windows the define never appears, so sgcAI_Chat.pas compiles to an empty unit. The component also carries ComponentPlatforms(pidWin32 or pidWin64).
What is cross-platform SGC_AI_MCP, on line 800 and line 954, carries no platform guard. So the MCP client and server run on Linux, macOS, iOS and Android as well. The provider defines SGC_OPENAI, SGC_ANTHROPIC, SGC_GEMINI, SGC_DEEPSEEK, SGC_OLLAMA, SGC_GROK and SGC_MISTRAL, lines 787 to 793, are not platform gated either.

Building a Linux service or a mobile app? Skip TsgcAIChat and call the vendor REST client directly, for example TsgcHTTP_API_OpenAI or TsgcHTTP_API_Anthropic. The call an LLM from Delphi guide shows both routes side by side.

Install and find the palette page

sgcAI ships inside the sgcWebSockets installer and also as its own package. Either way the install is the same shape.

1. Unzip

Unzip the download to a folder, called {$DIR} below.

2. Library path

Tools, Options, Library. Add {$DIR}\source and the lib folder for your IDE, for example {$DIR}\libD13\$(Platform).

3. Build the packages

Open the package group for your IDE version under {$DIR}\Packages\. Compile the runtime .dpk first, then install the dcl design-time one.

4. Check the palette

A page called SGC AI appears with fifteen components. TsgcAIChat is the first. If the page is missing on a non-Windows target that is expected, because SGC_AI is Windows gated.

5. Get an API key

For a hosted provider you bring your own key from that vendor and assign it to ChatOptions.ApiKey. Ollama needs no key at all, because the model runs on your machine.

Your first answer, in about ten lines

Create the component, set a provider, a key and a model, then call Chat and read the string it returns.

fUnifiedChat.pas
uses
  Classes, SysUtils,
  // sgc
  sgcAI_Chat;

procedure TfrmUnifiedChat.btnChatClick(Sender: TObject);
var
  oChat: TsgcAI_Chat;
begin
  oChat := TsgcAI_Chat.Create(nil);
  try
    oChat.Provider := aicpOpenAI;
    oChat.ChatOptions.ApiKey := GetApiKey;
    oChat.ChatOptions.Model := 'gpt-4o-mini';
    oChat.ChatOptions.MaxTokens := 1024;
    oChat.SystemMessage := 'You are a concise assistant inside a Delphi ERP.';

    memoResponse.Lines.Text := oChat.Chat(memoPrompt.Text);
  finally
    oChat.Free;
  end;
end;

Changing vendor is one line. Provider accepts aicpOpenAI, aicpAnthropic, aicpGemini, aicpDeepSeek, aicpOllama, aicpGrok and aicpMistral. Everything else stays where it is.

fUnifiedChat.pas
procedure TfrmUnifiedChat.OnChatStream(Sender: TObject; const aChunk: string;
  var Cancel: Boolean);
begin
  memoResponse.Text := memoResponse.Text + aChunk;
  // set Cancel to stop the generation early
end;

procedure TfrmUnifiedChat.OnChatError(Sender: TObject; const aError: string);
begin
  memoResponse.Text := 'error: ' + aError;
end;

procedure TfrmUnifiedChat.btnChatStreamClick(Sender: TObject);
begin
  GetChat.Provider := aicpAnthropic;
  GetChat.ChatOptions.ApiKey := GetApiKey;
  GetChat.ChatOptions.Model := 'claude-sonnet-4-20250514';
  GetChat.SystemMessage := memoSystem.Text;

  memoResponse.Lines.Clear;
  GetChat.ChatStream(memoPrompt.Text);
end;

The demo builds its component once in GetChat and assigns OnChatStream and OnChatError there. ChatStream returns the full answer as well, so you can ignore the return value and use only the event, or use both.

uLocal.pas
uses
  Classes, SysUtils,
  // sgc
  sgcAI_Chat;

var
  oChat: TsgcAI_Chat;
begin
  oChat := TsgcAI_Chat.Create(nil);
  try
    // no API key at all: the model runs on this machine
    oChat.Provider := aicpOllama;
    oChat.ChatOptions.Model := 'llama3';

    // BaseUrl is read only for the Ollama provider, and only when
    // the server is not on the default address. Internally it is
    // forwarded to the Ollama client's Host property.
    oChat.ChatOptions.BaseUrl := 'http://localhost:11434';

    Writeln(oChat.Chat('Summarise this invoice in one line.'));
  finally
    oChat.Free;
  end;
end;

There is no OllamaOptions.BaseUrl. At the raw API level the property is OllamaOptions.Host on TsgcHTTP_API_Ollama, and ChatOptions.BaseUrl is what feeds it from this layer.

The first two tabs are the shipped demo Demos\15.AI\02.Applications\06.UnifiedChat\fUnifiedChat.pas, with the form controls replaced by literals. The third tab is the same component pointed at a local Ollama server. A second demo, 07.ConversationHistory, shows the history API in the same folder.

Check it worked, and see the failure when it does not

Chat does not raise on a failed call, which surprises people. It returns an empty string and fires an event instead.

The return value

Chat returns the assistant text as a string. An empty string means the call failed, because a failure does not raise.

OnChatError

procedure(Sender: TObject; const aError: string). Both Chat and ChatStream catch the exception and route it here. This is the first event to wire, before anything else.

OnChatStream

procedure(Sender: TObject; const aChunk: string; var Cancel: Boolean). Text appearing in the memo while the model is still writing is the proof that streaming is working rather than buffering.

The conversation

GetHistory returns what the component is replaying on the next call. MaxHistoryMessages caps it and ClearHistory starts over.

What usually goes wrong the first time

Six problems account for nearly every failed first call.

The unit will not compile off Windows

That is by design. SGC_AI is defined inside a {$IFDEF MSWINDOWS} at line 797 and again at line 944, so on Linux, macOS, iOS and Android sgcAI_Chat.pas is an empty unit. Use the vendor REST clients on those targets.

Chat returns an empty string and nothing is raised

Chat and ChatStream swallow the exception and route it to OnChatError, whose signature is procedure(Sender: TObject; const aError: string). Wire that event before you debug anything else.

A local Ollama model is ignored

The Ollama base address goes on ChatOptions.BaseUrl, not on the vendor options object. It is forwarded to OllamaOptions.Host internally, and it is the only provider for which BaseUrl is read at all.

The form freezes during a long answer

Chat and ChatStream are synchronous. On Delphi 2010 and later use ChatAsync, which returns an IsgcFuture<string>. On older compilers run the call on your own thread.

The model name is rejected

Model names belong to the vendor, not to the component, and they change. ChatOptions.Model is passed straight through, so a name that works in the vendor console works here.

The bill grows on a long conversation

The component replays the history on every call, which is what makes a follow-up question work. Cap it with MaxHistoryMessages and start over with ClearHistory.

What people build after the first answer

A prompt box is the beginning. All four of these are already in the package.

Answer from your own documents

Turn your content into vectors with TsgcAIOpenAIEmbeddings, store them in TsgcAIDatabaseVectorFile or TsgcAIDatabaseVectorPinecone, and retrieve the closest passages to put in the prompt.

Embeddings reference and vector database reference

Talk to it out loud

TsgcAIOpenAIChatBot wires a recorder, transcription, the chat call and text to speech into one component. TsgcAIOpenAITranslator does the same for live translation.

ChatBot reference and translator reference

Expose your app to an agent

The MCP server component turns your application into a tool an assistant can call, and the MCP client consumes other servers. Neither is Windows gated, so an MCP server written in Delphi runs on Linux.

MCP server reference and MCP client reference

Use the whole vendor API

Vision, documents, extended thinking, batches, image generation, transcription and moderation live on the per-vendor REST clients rather than on the neutral chat layer.

OpenAI reference and Anthropic reference

Reference, demos and documentation

The reference pages document every option and event. Demo projects ship inside the download, under Demos\15.AI.

Guide, call an LLM from Delphi The long form walkthrough: streaming, tool calling, and hosted against local.
Reference, OpenAI client Every method, option and event on TsgcHTTP_API_OpenAI.
Reference, Anthropic client Messages, tools, vision, batches and token counting on TsgcHTTP_API_Anthropic.
Reference, MCP server The component that lets an assistant call into your application.
TsgcAIChat component page Every property and event on the chat component, with the other fourteen linked from it.
Online help The generated reference, always in step with the current release.

Related reading: building AI powered Delphi apps, comparing the providers from Delphi and writing an MCP server. Every product has its own quick start, listed on the getting started page.

sgcAI quick start questions

Drop TsgcAIChat from the SGC AI palette page. It is declared in sgcAI.pas as a published wrapper around TsgcAI_Chat, which is declared in sgcAI_Chat.pas. Code that creates the object at runtime, as the shipped demos do, uses TsgcAI_Chat and uses sgcAI_Chat;. Both give you the same properties.
Inside sgcWebSockets the gate is SGC_AI, defined on line 798 of sgcVer.inc, inside the SGC_EDT_ENT block that runs from line 760 to line 839. That is the Enterprise edition and above. Standard, lines 675 to 724, and Professional, lines 727 to 758, do not define it. If you do not want the rest of the library, the standalone sgcAI package defines SGC_PACK_AI on line 854, whose own block on lines 943 to 957 turns the same components on.
Because SGC_AI is only ever defined inside a {$IFDEF MSWINDOWS}, at line 797 in the Enterprise block and at line 944 in the standalone pack block. With the define absent, sgcAI_Chat.pas and the TsgcAIChat section of sgcAI.pas compile to nothing. Design time mirrors that with ComponentPlatforms(pidWin32 or pidWin64). On those targets call the vendor REST clients instead, or use the MCP components, which carry no platform guard.
Change Provider. It accepts aicpOpenAI, aicpAnthropic, aicpGemini, aicpDeepSeek, aicpOllama, aicpGrok and aicpMistral. Then set the key and model for that vendor. For aicpOllama there is no key, and if the server is not on the default address you set ChatOptions.BaseUrl, which the component forwards to the Ollama client internally.
Call ChatStream rather than Chat, and handle OnChatStream. Its signature is procedure(Sender: TObject; const aChunk: string; var Cancel: Boolean): append aChunk to your memo as it arrives, and set Cancel to True to stop the generation early.
Chat and ChatStream do not raise. They catch the exception, fire OnChatError with the message, and return an empty string. So an unhandled OnChatError looks exactly like a model that answered with nothing. Wire it first.
On Delphi 2010 and later call ChatAsync, which is wrapped in {$IFDEF D2010} and returns an IsgcFuture<string>. On Delphi 7 through 2009 that method does not exist, so run Chat on a thread you create yourself.
Yes. It keeps the exchange and replays it on the next call, which is what makes a follow-up question work. MaxHistoryMessages caps how much is replayed, ClearHistory starts a new conversation and GetHistory returns the stored messages.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to put a model inside your application?

Download the trial and run the unified chat demo against your own key.