Google Gemini is one of the most powerful multimodal AI model families available today, excelling at text generation, image understanding, structured outputs, and function calling across a wide range of use cases. For Delphi developers looking to integrate Gemini into their applications, sgcWebSockets provides TsgcHTTP_API_Gemini — a comprehensive, native component that wraps the entire Google Gemini API surface.
Whether you are building conversational assistants, generating structured data, processing images, creating embeddings for semantic search, or orchestrating tool-augmented workflows, this component gives you direct access to every Gemini feature through clean, type-safe Delphi code. No REST boilerplate. No JSON wrangling. Just drop the component, set your API key, and start building.
Complete API Coverage
Every major feature of the Google Gemini API is supported out of the box.
|
Content Generation Generate text from prompts with optional system instructions. Fine-tune output with temperature, top-p, top-k, and stop sequences. |
Real-Time Streaming Stream responses token-by-token using Server-Sent Events. Build responsive UIs that display answers as they are generated. |
Vision Analyze images by sending base64-encoded data with a text prompt. Gemini describes, interprets, and reasons about visual content. |
|
Structured JSON Output Force Gemini to return valid JSON conforming to your schema. Set a response MIME type and schema for guaranteed parseable results. |
Function Calling Define custom functions with JSON Schema. Gemini decides when to invoke them, enabling agentic, multi-step workflows. |
Embeddings Generate vector embeddings for text. Power semantic search, clustering, classification, and recommendation systems. |
|
Token Counting Count tokens before sending requests. Accurately estimate costs and manage context window budgets. |
Model Management List all available Gemini models or retrieve details for a specific model, including input/output token limits. |
Built-in Retry & Logging Automatic retry on transient failures with configurable attempts and wait intervals. Full request/response logging for debugging. |
Getting Started
Integrate Google Gemini into your Delphi project in under a minute. Drop the component, configure your API key, and send your first message.
// Create the component and configure the API key
var
Gemini: TsgcHTTP_API_Gemini;
vResponse: string;
begin
Gemini := TsgcHTTP_API_Gemini.Create(nil);
Try
Gemini.GeminiOptions.ApiKey := 'YOUR_API_KEY';
// Send a simple message to Gemini
vResponse := Gemini._CreateContent(
'gemini-2.5-flash', 'Hello, Gemini!');
ShowMessage(vResponse);
Finally
Gemini.Free;
End;
end;
Two API styles. Every feature offers both convenience methods (string-based, minimal code) and typed request/response classes (full control, type safety). Choose the approach that best suits your use case.
Content Generation & Streaming
The GenerateContent API is the foundation of every Gemini interaction. Send text with optional system instructions, and receive responses synchronously or streamed in real-time.
System Instructions
Control Gemini's behavior by providing a system instruction that sets the context, personality, or constraints for the conversation.
vResponse := Gemini._CreateContentWithSystem(
'gemini-2.5-flash',
'You are a helpful assistant that responds in Spanish.',
'What is the capital of France?');
// Returns: "La capital de Francia es París."
Real-Time Streaming
For responsive user interfaces, stream Gemini's response token-by-token using Server-Sent Events. Assign the OnHTTPAPISSE event handler and call _CreateContentStream.
// Enable streaming via SSE
Gemini.OnHTTPAPISSE := OnSSEEvent;
Gemini._CreateContentStream('gemini-2.5-flash',
'Tell me a story about a brave explorer.');
procedure TForm1.OnSSEEvent(Sender: TObject;
const aEvent, aData: string; var Cancel: Boolean);
begin
// aData: JSON payload with generated content
Memo1.Lines.Add(aData);
end;
Advanced Typed API
For full control over request parameters — temperature, top-p, top-k, stop sequences, max output tokens — use the typed request and response classes.
var
oRequest: TsgcGeminiClass_Request_GenerateContent;
oResponse: TsgcGeminiClass_Response_GenerateContent;
begin
oRequest := TsgcGeminiClass_Request_GenerateContent.Create;
Try
oRequest.Model := 'gemini-2.5-flash';
oRequest.SystemInstruction := 'You are a helpful assistant.';
oRequest.MaxOutputTokens := 2048;
oRequest.Temperature := 0.7;
oRequest.TopP := 0.9;
oRequest.TopK := 40;
oRequest.Contents.Add('user', 'Explain quantum computing in simple terms.');
oResponse := Gemini.CreateContent(oRequest);
Try
if Length(oResponse.Candidates) > 0 then
ShowMessage(oResponse.Candidates[0].Parts[0].Text);
Finally
oResponse.Free;
End;
Finally
oRequest.Free;
End;
end;
Vision — Image Understanding
Gemini is natively multimodal. Send photographs, screenshots, diagrams, or charts alongside a text prompt and receive detailed descriptions, data extraction, or visual Q&A.
// Load an image and ask Gemini to describe it
var
vBase64: string;
begin
vBase64 := sgcBase64Encode(LoadFileToBytes('product-photo.png'));
ShowMessage(Gemini._CreateVisionContent(
'gemini-2.5-flash',
'Describe this product image for an e-commerce listing.',
vBase64, 'image/png'));
end;
Use case. Automate quality inspection, generate alt-text for accessibility, extract data from charts, or build visual search features — all from native Delphi code.
Structured JSON Output
Guarantee that Gemini returns valid, parseable JSON conforming to your exact schema. Set a response MIME type and schema to eliminate post-processing — the response is ready to deserialize directly into your Delphi records.
var
vSchema, vResponse: string;
begin
vSchema :=
'{"type":"object","properties":{"name":{"type":"string"},' +
'"age":{"type":"integer"}},"required":["name","age"]}';
vResponse := Gemini._CreateContentJSON(
'gemini-2.5-flash',
'Extract the name and age: John is 30 years old.',
vSchema);
// Returns: {"name": "John", "age": 30}
end;
Schema enforcement. By setting ResponseMimeType to 'application/json' and providing a ResponseSchema, Gemini guarantees structurally valid output every time.
Function Calling
Define custom functions with JSON Schema, and Gemini will decide when and how to invoke them. This is the foundation for building agentic, multi-step workflows that connect the AI to your business logic.
var
oRequest: TsgcGeminiClass_Request_GenerateContent;
oFunc: TsgcGeminiClass_Request_FunctionDeclaration;
oResponse: TsgcGeminiClass_Response_GenerateContent;
begin
oRequest := TsgcGeminiClass_Request_GenerateContent.Create;
Try
oRequest.Model := 'gemini-2.5-flash';
// Define a function
oFunc := TsgcGeminiClass_Request_FunctionDeclaration.Create;
oFunc.Name := 'get_weather';
oFunc.Description := 'Get the current weather in a location';
oFunc.Parameters :=
'{"type":"object","properties":{"location":{"type":"string"}}}';
oRequest.FunctionDeclarations.Add(oFunc);
oRequest.Contents.Add('user',
'What is the weather in Madrid?');
oResponse := Gemini.CreateContent(oRequest);
Try
// Check if Gemini wants to call a function
if oResponse.Candidates[0].Parts[0].FunctionCallName '' then
begin
ShowMessage('Function: ' + oResponse.Candidates[0].Parts[0].FunctionCallName);
ShowMessage('Args: ' + oResponse.Candidates[0].Parts[0].FunctionCallArgs);
end;
Finally
oResponse.Free;
End;
Finally
oRequest.Free;
End;
end;
Embeddings
Generate high-quality vector embeddings for text. Embeddings power semantic search, document clustering, recommendation engines, and classification tasks.
// Generate embeddings for a text
var
vEmbedding: string;
begin
vEmbedding := Gemini._EmbedContent(
'text-embedding-004',
'Delphi is a powerful programming language.');
ShowMessage(vEmbedding);
end;
For full control, use the typed API to access the raw embedding values array.
var
oResponse: TsgcGeminiClass_Response_Embedding;
i: Integer;
begin
oResponse := Gemini.EmbedContent(
'text-embedding-004',
'Delphi is a powerful programming language.');
Try
for i := 0 to Length(oResponse.Values) - 1 do
Memo1.Lines.Add(FloatToStr(oResponse.Values[i]));
Finally
oResponse.Free;
End;
end;
Token Counting
Estimate costs and manage context window budgets accurately before sending requests.
var
vTokens: string;
begin
vTokens := Gemini._CountTokens(
'gemini-2.5-flash',
'How many tokens does this message consume?');
ShowMessage(vTokens);
end;
Model Management
Query available Gemini models programmatically. List all models or retrieve details for a specific model ID, including display name, description, and token limits.
// List all available Gemini models
vModels := Gemini._GetModels;
// Get details for a specific model
vModel := Gemini._GetModel('gemini-2.5-flash');
// Typed API: access model properties directly
var
oModel: TsgcGeminiClass_Response_Model;
begin
oModel := Gemini.GetModel('gemini-2.5-flash');
Try
ShowMessage('Name: ' + oModel.DisplayName);
ShowMessage('Input limit: ' + IntToStr(oModel.InputTokenLimit));
ShowMessage('Output limit: ' + IntToStr(oModel.OutputTokenLimit));
Finally
oModel.Free;
End;
end;
Configuration & Options
Fine-tune the component behavior with comprehensive configuration options.
| Property | Description |
|---|---|
GeminiOptions.ApiKey |
Your Google Gemini API key (required) |
HttpOptions.ReadTimeout |
HTTP read timeout in milliseconds (default: 60000) |
LogOptions.Enabled |
Enable request/response logging |
LogOptions.FileName |
Log file path for request/response capture |
RetryOptions.Enabled |
Automatic retry on transient failures (429, 503) |
RetryOptions.Retries |
Maximum number of retry attempts (default: 3) |
RetryOptions.Wait |
Wait time between retries in milliseconds (default: 3000) |
Request Parameters
| Parameter | Description |
|---|---|
Temperature |
Sampling temperature (0.0–2.0). Lower values = more deterministic. |
TopP |
Nucleus sampling (0.0–1.0). Controls cumulative probability cutoff. |
TopK |
Top-K sampling. Limits token selection to top K candidates. |
MaxOutputTokens |
Maximum number of tokens in the response (default: 4096). |
StopSequences |
Custom sequences that stop generation when encountered. |
ResponseMimeType |
Output format: 'application/json' for JSON, 'text/plain' for text. |
ToolChoice |
Control how the model selects functions to call. |