OpenAI Assistants for Delphi

The Assistants API allows you to build AI assistants within your own applications. An Assistant has instructions and can leverage models, tools, and files to respond to user queries. The Assistants API currently supports three types of tools: Code Interpreter, File Search, and Function calling.


Overview

A typical integration of the Assistants API has the following flow:

  • Create an Assistant by defining its custom instructions and picking a model. If helpful, add files and enable tools like Code Interpreter, File Search, and Function calling.
  • Create a Thread when a user starts a conversation.
  • Add Messages to the Thread as the user asks questions.
  • Run the Assistant on the Thread to generate a response by calling the model and the tools.

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

The contents of the messages your users or applications create are added as Message objects to the Thread. Messages can contain both text and files. There is no limit to the number of Messages you can add to Threads — we smartly truncate any context that does not fit into the model's context window.

Once all the user Messages have been added to the Thread, you can Run the Thread with any Assistant. Creating a Run uses the model and tools associated with the Assistant to generate a response. These responses are added to the Thread as assistant Messages.

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
  begin
    oRun := oAssistant.CreateRunAndWait(oThread.Id);
    if Assigned(oRun) then
    begin
      oMessages := oAssistant.GetMessages(oThread.Id, oRun.Id);
      if Assigned(oMessages) and (Length(oMessages.Messages) > 0) then
      begin
        for i := 0 to Length(oMessages.Messages) - 1 do
          DoLog('[assistant]: ' + DoFormatResponse(oMessages.Messages[i].ContentText + #13#10));
      end;
    end;
  end;
end; 

Delphi Demo

Find below a compiled demo of OpenAI Assistants using sgcWebSockets Enterprise for Windows. 

sgcAIAssistant
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.

sgcWebSockets 2024.7
sgcWebSockets 2024.5