Send a prompt to a Gemini model and receive generated content. The Gemini API uses the generateContent endpoint to process text input and return model responses.
Send a Hello message to Gemini.
Gemini := TsgcHTTP_API_Gemini.Create(nil);
Gemini.GeminiOptions.ApiKey := 'API_KEY';
WriteLn(Gemini._CreateContent('gemini-2.0-flash', 'Hello!'));
Send a message with a system instruction to control Gemini's behavior.
Gemini := TsgcHTTP_API_Gemini.Create(nil);
Gemini.GeminiOptions.ApiKey := 'API_KEY';
WriteLn(Gemini._CreateContentWithSystem('gemini-2.0-flash',
'You are a helpful assistant that responds in Spanish.',
'What is the capital of France?'));
Use Server-Sent Events (SSE) to stream the response in real-time. Assign the OnHTTPAPISSE event handler to receive streaming events.
Gemini := TsgcHTTP_API_Gemini.Create(nil);
Gemini.GeminiOptions.ApiKey := 'API_KEY';
Gemini.OnHTTPAPISSE := OnSSEEvent;
Gemini._CreateContentStream('gemini-2.0-flash', 'Tell me a story.');
procedure TForm1.OnSSEEvent(Sender: TObject; const aEvent, aData: string;
var Cancel: Boolean);
begin
Memo1.Lines.Add(aData);
end;
Use the typed request/response classes for full control over content generation parameters.
Gemini := TsgcHTTP_API_Gemini.Create(nil);
Gemini.GeminiOptions.ApiKey := 'API_KEY';
oRequest := TsgcGeminiClass_Request_GenerateContent.Create;
Try
oRequest.Model := 'gemini-2.0-flash';
oRequest.MaxOutputTokens := 4096;
oRequest.Temperature := 0.7;
oRequest.SystemInstruction := 'You are a creative writer.';
SetLength(oContents, 1);
oContents[0] := TsgcGeminiClass_Request_Content.Create;
oContents[0].Role := 'user';
SetLength(oParts, 1);
oParts[0] := TsgcGeminiClass_Request_Part.Create;
oParts[0].Text := 'Write a haiku about programming.';
oContents[0].Parts := oParts;
oRequest.Contents := oContents;
oResponse := Gemini.CreateContent(oRequest);
Try
if Length(oResponse.Candidates) > 0 then
if Length(oResponse.Candidates[0].Parts) > 0 then
WriteLn(oResponse.Candidates[0].Parts[0].Text);
Finally
oResponse.Free;
End;
Finally
oParts[0].Free;
oContents[0].Free;
oRequest.Free;
End;