Skip to content

MaxiDonkey/DelphiGenAI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

550 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Delphi GenAI - Optimized OpenAI Integration


Delphi async/await supported GitHub GetIt – Available GitHub LS Studio supported


New


Two quick examples


  • Non-streamed example:

    //uses GenAI, GenAI.Types;
    
      var API_Key := 'OPENAI_API_KEY';
      Client := TGenAIFactory.CreateInstance(API_KEY);
    
      //JSON payload
      var Payload: TResponsesParamsProc :=
        procedure(Params: TResponsesParams)
        begin
          Params
            .Model('gpt-5.5')
            .Input('What is the difference between a mathematician and a physicist?')
            .Store(False);  // Response not stored
        end;
    
      //Synchronous example
      var Value := Client.Responses.Create(Payload);
    
      try
        for var Item in Value.Output do
          for var SubItem in Item.Content do
            Memo1.Lines.Text := SubItem.Text;
      finally
        Value.Free;
      end;

  • Streamed example (SSE):

    //uses GenAI, GenAI.Types;
    
    var Client: IGenAI;
    var API_Key := 'OPENAI_API_KEY';
    Client := TGenAIFactory.CreateInstance(API_KEY);
    
    procedure TForm1.Test;
    begin
      //JSON payload
      var Payload: TResponsesParamsProc :=
        procedure(Params: TResponsesParams)
        begin
          Params
            .Model('gpt-5.5')
            .Input('What is the difference between a mathematician and a physicist?')
            .Stream
            .Store(False);  // Response not stored
        end;
    
      //Streamed callback
      var CallBack: TResponseEvent :=
        procedure(var Value: TResponseStream; IsDone: Boolean; var Cancel: Boolean)
        begin
          if not Assigned(Value) or IsDone then
            Exit;
    
          if Value.&Type = TResponseStreamType.output_text_delta then
            begin
              Memo1.Lines.Text := Memo1.Lines.Text + Value.Delta;
              Application.ProcessMessages;
            end;
        end;
    
      //Synchronous streamed example
      Client.Responses.CreateStream(Payload, CallBack);
    end;

You only ever need to reference two core units — GenAI and GenAI.Types — whatever method you use. The same client can target OpenAI, a local LM Studio server, or Google Gemini:

Note

//uses GenAI, GenAI.Types;
//  Client: IGenAI;

 // OpenAI
 Client := TGenAIFactory.CreateInstance(openAI_api_key);

 // Local model (LM Studio – OpenAI-compatible server)
 Client := TGenAIFactory.CreateLMSInstance; // default: http://127.0.0.1:1234/v1

 // Google Gemini (OpenAI-compatible surface)
 Client := TGenAIFactory.CreateGeminiInstance(gemini_api_key);


Summary



Introduction

Built with Delphi 12 Community Edition (v12.1 Patch 1) The wrapper itself is MIT-licensed. You can compile and test it free of charge with Delphi CE; any recent commercial Delphi edition works as well.


DelphiGenAI is a full OpenAI wrapper for Delphi, covering the entire platform: text, vision, audio, image generation, embeddings, conversations, containers, and the latest v1/responses agentic workflows. It offers a unified interface with sync/async/await support across major Delphi platforms, making it easy to leverage modern multimodal and tool-based AI capabilities in Delphi applications.


Important

This is an unofficial library. OpenAI does not provide any official library for Delphi. This repository contains a Delphi implementation over the OpenAI public API.


Tip

When working with asynchronous methods, declare the IGenAI client with the broadest possible scope — ideally in the application's OnCreate.



Philosophy and Scope

OpenAI exposes two complementary text surfaces:

  • the Chat Completions API (v1/chat/completions) — the established, single-call chat interface;
  • the Responses API (v1/responses) — the new agentic primitive that adds built-in tools and state management on top of the same idea, and is intended to gradually replace Chat Completions.

Around them sits the rest of the platform: audio, images, embeddings, files, vector stores, batch, fine-tuning, moderation, containers, skills, conversations and realtime.

DelphiGenAI is, by design, a strict one-to-one mapping of the OpenAI API: it does not introduce provider-specific extensions, fallbacks or behavioral adaptations on top of the vendor surface. Its goals are:

  • faithful mapping of every supported endpoint and parameter;
  • Delphi-first ergonomics (fluent builders, strongly typed results), not JSON-first usage;
  • a uniform execution model across endpoints (synchronous, asynchronous and promise-based);
  • clear boundaries with non-OpenAI vendors, which are reachable only insofar as they expose an OpenAI-compatible surface (see Provider Support).

Core execution modes

  • Standard generation — blocking or promise-based; the full response is returned at once. Suitable for background or batch workflows.
  • SSE streaming — synchronous or asynchronous, with session-level (per-chunk) or event-level (per typed event) callbacks for fine-grained interception of the response stream.
  • Tool-driven & agentic workflows — function calling and built-in tools (web search, file search, code interpreter, image generation, remote MCP, shell, apply patch, skills) with strict schema validation, plus the supporting agentic plumbing: Code Interpreter containers and automatic context compaction.
  • Multiple providers — the same IGenAI client targets OpenAI, a local LM Studio server, Google Gemini, or any OpenAI-compatible endpoint.

These distinctions are applied consistently at the API level and in the documentation.



Documentation

The documentation is organized as focused Markdown guides, each covering one capability. They are all listed, explained and ordered in a single entry point:

Each guide provides Delphi-first examples (synchronous, asynchronous and promise-based), not raw JSON.



Responses vs. Chat Completions

The v1/responses API is the new core API: an agentic primitive that combines the simplicity of chat completions with built-in tools (web search, file search, computer use, image generation, remote MCP, code interpreter, skills, containers). It is intended to gradually replace v1/chat/completions.

Note

If you're a new user, we recommend the Responses API.

Capabilities Chat Completions Responses
Text generation
Audio Coming soon
Vision
Structured Outputs
Function calling
Web search
File search
Computer use
Code interpreter
Image generation
Remote MCP
Reasoning summaries
Skills
Containers

Warning

Note from OpenAI
The Chat Completions API is an industry standard for building AI applications, and we intend to continue supporting it indefinitely. The Responses API simplifies workflows involving tool use, code execution, and state management.



Functional Demo

This repository includes a working FMX demo in the demos folder, built on top of Pythia-WebView2, used here as the host application for the wrapper.

This demo matters for users of the wrapper because it shows DelphiGenAI running inside a real application flow, not only through isolated code snippets. It demonstrates how the IGenAI client is connected to a UI-oriented conversation layer over the Responses API, with asynchronous SSE streaming, request/response JSON traceability, function / MCP / skill / agent cards, image creation and editing (Images API), file upload through the Files API, knowledge indexing into a vector store for retrieval (RAG), and microphone capture with Whisper speech-to-text.

One key part of the demo is the context reconstruction layer (Demo.OpenAI.Context.pas). It offers two continuity strategies that together cover the demo's needs without relying on the separate Conversations API: a local context rebuild, where Delphi reconstructs richer message context from the stored JSON request and streamed JSON response — text blocks, reasoning blocks, tool calls and matching tool results, MCP exchanges and web-search results — while taking the previously used tools into account; or cloud chaining via previous_response_id, where OpenAI keeps the conversation state server-side. Both are off by default, so nothing is retained on OpenAI's servers unless you explicitly opt in.

The guides keep the didactic path: each API surface is explained independently, with focused Delphi examples. The demo is the complementary reference: it shows how those capabilities cooperate end-to-end in a functional Delphi application, and it provides a practical starting point for validating your API key, runtime setup and optional MCP / skill / agent configuration.

For a full, step-by-step explanation of how the SDK is plugged into Pythia — the integration contract, the IGenAI ↔ Pythia event flow, turn routing, the async services, multi-turn context and the delphi-uses-graph custom skill — see the dedicated walkthrough: demos/docs/FMX_OpenAI.md. See also the demo-specific setup notes in demos/README.md.



Functional coverage

OpenAI endpoints supported by GenAI. Browse the matching guide for each one from the documentation index.

Endpoint Supported Status / notes
/audio/speech
updated
/audio/transcriptions
updated
/audio/translations
updated
/batches
/chat/completions
/chatkit
/completions
legacy
/containers
updated
/conversations
/embeddings
/evals
/files
/fine_tuning
/images
/models
/moderations
/organization
/realtime
/responses
updated
/skills
new
/uploads
/vector_stores


Tips and tricks

How to prevent an error when closing an application while requests are still in progress?

Starting from version 1.0.1 of GenAI, the GenAI.Monitoring unit is responsible for monitoring ongoing HTTP requests.

The Monitoring interface is accessible by including the GenAI.Monitoring unit in the uses clause. Alternatively, you can access it via the HttpMonitoring function, declared in the GenAI unit.

Usage Example

//uses GenAI;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := not HttpMonitoring.IsBusy;
  if not CanClose then
    MessageDLG(
      'Requests are still in progress. Please wait for them to complete before closing the application."',
      TMsgDlgType.mtInformation, [TMsgDlgBtn.mbOK], 0);
end;


Removed in 2.0.0

OpenAI Assistants API — deprecated since 1.3.0 (OpenAI deprecation announced August 26, 2025), now removed. Migrate to the Responses and Conversations APIs (see the migration guide). Removed units: GenAI.Assistants.pas, GenAI.Messages.pas, GenAI.Threads.pas, GenAI.Runs.pas, GenAI.RunSteps.pas.

Sora video API (v1/videos) — removed. Removed unit: GenAI.Video.pas.

The last release shipping these units is 1.4.3; their sources remain available there for reference and backward compatibility.



Contributing

Pull requests are welcome. If you're planning a major change, please open an issue first to discuss it.



License

This project is licensed under the MIT License.


About

The GenAI API wrapper for Delphi seamlessly integrates OpenAI’s latest models (gpt-5 serie), delivering robust support for agent chats/responses, text generation, vision, audio analysis, JSON configuration, web search, and asynchronous operations. Image generation with gpt-image-2. Skills and agents

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors