TsgcAIChat › Methods › ChatAsync

ChatAsync Method

Sends a user message on a background thread and returns a future that resolves to the assistant response.

Syntax

function ChatAsync(const aMessage: string): IsgcFuture<string>;

Parameters

NameTypeDescription
aMessageconst stringUser message to append to the conversation history and send to the selected provider.

Return Value

An IsgcFuture<string> that resolves to the complete assistant reply. Call Await to block until the reply arrives, ThenProc to consume it in a continuation, OnError to handle failures, and Cancel to abort the request.

Remarks

ChatAsync is the future-style counterpart of Chat: the request runs on a background thread and the method returns immediately, so UI threads are never blocked while the provider generates the response. The conversation history, ChatOptions, SystemMessage and the OnChatMessage/OnChatError events behave exactly as with Chat. Calling Cancel aborts the underlying HTTP request to the provider, so a slow completion can be interrupted instead of waiting for it to finish. Requires Delphi 2010 or later (and C++Builder 2010 or later); it is not available in the .NET edition.

Example

oChat.Provider := aicpOpenAI;
oChat.ChatOptions.ApiKey := 'API_KEY';
oChat.ChatOptions.Model := 'gpt-4o-mini';
oChat.ChatAsync('Hello!')
  .ThenProc(procedure(const aReply: string)
    begin
      WriteLn(aReply);
    end)
  .OnError(procedure(E: Exception)
    begin
      WriteLn('Chat failed: ' + E.Message);
    end);

Back to Methods