OpenAI Assistants Streaming

The OpenAI Assistant Client has been improved to implement the streaming responses when calling a run thread. Before this new feature, it requires to poll the run object status till it's completed. Now you can use the new Stream events to handle the streaming messages.


Streaming Events

Instead of waiting the full response from the assistant, you can stream the response using Server-Sent Events. Just pass the param Stream = True when using the CreateRun function and the response will be using streaming.

The following events are used to handle the streaming responses

  • OnStreamRun: the event is called when there is an update in the run object.
  • OnStreamMessage: the event is called when there is an update in the message object: created, in-progess, completed...
  • OnStreamMessageDelta: Occurs when parts of a Message are being streamed.
  • OnStreamDone: Occurs when an error occurs. This can happen due to an internal server error or a timeout.
  • OnStreamError: Occurs when a stream ends.

Step 1: Create an Assistant

An Assistant represents an entity that can be configured to respond to a user's messages using several parameters like model, instructions, and tools. 

// ... create a new assistant
oAssistant := TsgcAIOpenAIAssistant.Create(nil);
// ... set your api key
oAssistant.OpenAIOptions.ApiKey := txtAPIKey.Text;
// ... assistant options
oAssistant.AssistantOptions.Name := 'Math Tutor';
oAssistant.AssistantOptions.Instructions.Text := 'You are a personal math tutor. Write and run code to answer math questions.';
oAssistant.AssistantOptions.Model := 'gpt-4o';
// ... create the assistant
oAssistant.CreateAssistant(); 

Step 2: Create a Thread

A Thread represents a conversation between a user and one or many Assistants. You can create a Thread when a user (or your AI application) starts a conversation with your Assistant. 

oThread := oAssistant.CreateThread; 

Step 3: Add a Message to the Thread and Run using Streaming

Enter your text here ...

procedure SendMessage(const oAssistant: TsgcAIOpenAIAssistant; const oThread: TsgcAIClass_Thread; const aMessage: string);
var
  i: Integer;
  oMessage: TsgcOpenAIClass_Message;
  oMessages: TsgcOpenAIClass_Response_List_Messages;
  oRun: TsgcOpenAIClass_Run;
begin
  DoLog('[user]: ' + aMessage);
  oMessage := oAssistant.CreateMessageText(oThread.Id, aMessage);
  if Assigned(oMessage) then
    oRun := oAssistant.CreateRun(oThread.Id, True);
end; 

Step 4: Handle the Response

Use the event OnStreamMessageDelta to read the server-sent stream message. 

procedure OnStreamMessageDelta(Sender: TObject; const aMessageDelta: TsgcOpenAIClass_MessageDelta; const aRaw: string);
var
  i: Integer;
  vResponse: string;
  vType: string;
begin
  for i := Low(aMessageDelta.Content) to High(aMessageDelta.Content) do
  begin
    vType := aMessageDelta.Content[i]._Type;
    if vType = 'text' then
    begin
      vResponse := TsgcOpenAIClass_MessageDeltaContent_Text
        (aMessageDelta.Content[i]).Value;
    end;
  end;
end; 

OpenAI Delphi Sample

The following compressed file contains the source code of the Assistants demo built for windows using the sgcWebSockets library. 

sgcAssistant
2.8 mb
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

BlackFriday 30% Discount

Related Posts