TsgcAIChat › Methods › ChatAsync
Sends a user message on a background thread and returns a future that resolves to the assistant response.
function ChatAsync(const aMessage: string): IsgcFuture<string>;
| Name | Type | Description |
|---|---|---|
aMessage | const string | User message to append to the conversation history and send to the selected provider. |
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.
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.
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);