TsgcAIChat › Methods › Chat

Chat Method

Sends a user message and returns the full assistant response.

Syntax

function Chat(const aMessage: string): string;

Parameters

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

Return Value

Complete assistant reply, or an empty string when the request fails (the error is reported through OnChatError). (string)

Remarks

Appends aMessage to the internal history as a user turn, dispatches a non-streaming request to the underlying provider using the current ChatOptions and SystemMessage, parses the response, stores the assistant reply in history, raises OnChatMessage with role assistant, and returns the assistant text. Exceptions raised during the request are caught and forwarded to OnChatError; in that case the function returns an empty string. Use ChatStream when you need incremental chunks instead of a single blocking call.

Example

oChat.Provider := aicpOpenAI;
oChat.ChatOptions.ApiKey := 'API_KEY';
oChat.ChatOptions.Model := 'gpt-4o-mini';
WriteLn(oChat.Chat('Hello!'));

Back to Methods