FB4D Reference IGeminiAIRequest - SchneiderInfosystems/FB4D GitHub Wiki

Interface IGeminiAIRequest

This interface must be used for more complex requests to Gemini AI that go beyond simple text requests.

However, it can also be used to generate a simple text request (optionally including system instructions and a request ID):

function Prompt(const PromptText: string; const SystemInstructions: string = '';
  const RequestID: string = ''): IGeminiAIRequest;
function SetSystemInstruction(const SystemInstructions: string): IGeminiAIRequest;

For command prompt that referes to a media file (document, picture, video, audio) the following method can be used:

function PromptWithMediaData(const PromptText, MimeType: string; MediaStream: TStream;
  const RequestID: string = ''): IGeminiAIRequest;

For application based on Firemonkey or on FGX framework when using Delphi 11 and higher, you can simply use for images the following method which extracts the mime type automatically from the media stream. Keep in mind, this is working for images only. PDF, Video and audio need to constructed with the above method.

function PromptWithImgData(const PromptText: string; ImgStream: TStream;
  const RequestID: string = ''): IGeminiAIRequest;

Note: VCL does not offer this great functionality, that is why you need to use always the method PromptWithMediaData.

The following method sets the model parameters for the request.

function ModelParameter(Temperatur, TopP: double; MaxOutputTokens, TopK: cardinal): IGeminiAIRequest;

The Temperatur (0.0..2.0) controls the randomness of the generated text. The value of TopP (0.0..1.0) controls the diversity of the generated text. The MaxOutputTokens Limits the maximum number of tokens in the generated text. The TopK controls the number of candidate words considered during generation.

For Gemini 2.5+ thinking models, the thinking token budget and whether thought summaries are returned in the response can be configured with:

function SetThinkingConfig(ThinkingBudget: integer; IncludeThoughts: boolean = false): IGeminiAIRequest;

Pass ThinkingBudget = 0 to disable thinking for low-latency tasks, -1 for dynamic thinking, or a positive integer for a fixed token budget.

The following method offers to sets a stop word or a stop sequences. Stop sequences are used to stop the text generation in Gemini AI when such a word or sequence of words are generated.

function SetStopSequences(StopSequences: TStrings): IGeminiAIRequest;

The following method can be used to set the safety settings for one category of harm. Use this method on the same request interface for all categories that needs to handled in different way than the default model does.

function SetSafety(HarmCat: THarmCategory; LevelToBlock: TSafetyBlockLevel): IGeminiAIRequest;

To enable Google Search grounding or configure response modalities (mText, mImage, mAudio):

function SetNewGroundingByGoogleSearch: IGeminiAIRequest;
function SetGroundingByGoogleSearch(Threshold: double): IGeminiAIRequest;
function SetResponseModalities(Modalities: TModalities): IGeminiAIRequest;

For using in chats add the model answer to the next request by the following method.

function AddAnswerForNextRequest(const ResultAsMarkDown: string): IGeminiAIRequest;

In chats after adding the last answer from the model the next question from the user can be added with the following method.

function AddQuestionForNextRequest(const PromptText: string): IGeminiAIRequest;

Within a chat to calculated to number of tokens in the prompt the next function allows get a working request.

function CloneWithoutCfgAndSettings(Request: IGeminiAIRequest): IGeminiAIRequest;

You can use the following method to instruct the Gemini AI to only respond with a JSON object by supplying a defined JSON schema. This function is suitable in cases where you need structured data from queries that need to be processed further in the code.

function SetJSONResponseSchema(Schema: IGeminiSchema): IGeminiAIRequest;

For understanding how to declare the JSON schema, see this page IGeminiSchema.