TsgcAIChat › Methods › ChatStream

ChatStream Method

Sends a user message and receives the response incrementally.

Syntax

function ChatStream(const aMessage: string): string;

Parameters

NameTypeDescription
aMessageconst stringUser message appended to the history and streamed to the provider.

Return Value

Accumulated assistant reply after the stream ends, equivalent to concatenating every aChunk delivered through OnChatStream. Empty when the call fails. (string)

Remarks

Same semantics as Chat but sets the provider stream flag so partial tokens are delivered through OnChatStream as they arrive. Set the Cancel parameter of that event to True to stop reading further chunks. Once the stream terminates the accumulated text is stored in history and emitted as a single OnChatMessage with role assistant. If the provider ignores streaming and returns a full response the component falls back to parsing it as a non-streaming payload. Errors are forwarded to OnChatError.

Example

oChat.OnChatStream := OnStream;
oChat.ChatStream('Tell me a story.');

procedure TForm1.OnStream(Sender: TObject; const aChunk: string;
  var Cancel: Boolean);
begin
  Memo1.Text := Memo1.Text + aChunk;
end;

Back to Methods