Skip to content

FB4D Reference IGeminiAI

Christoph Schneider edited this page Nov 20, 2024 · 4 revisions

Interface IGeminiAI

This interface provides machine-generated text capabilities based on the Gemini AI API. Questions can be posted to the AI and, if required, accompanied by images, documents, video or audio to which the questions relate.

In order to create an interface IGeminiAI use this method from the unit FB4D.GeminiAI.

constructor TGeminiAI.Create(const ApiKey: string; const Model: string = cGeminiAIDefaultModel);

If you work always with the same model you can takeover the model name already here. Otherwise you can set the model name later too.

Fetch list of models and model details

The interface method IGeminiAI.FetchListOfModelsSynchronous(ModelNames: TStrings) gets the list of model names from Gemini AI and fills it into string list ModelNames. The caller has to create and free the string list. As side effect the returned details will be stored in the class for faster access.

procedure IGeminiAI.FetchListOfModelsSynchronous(ModelNames: TStrings);

The following method allows to get the details of a model:

function FetchModelDetailsSynchronous(const ModelName: string): TGeminiModelDetails;

type
  TGeminiModelDetails = record
    ModelFullName: string;
    BaseModelId: string;
    Version: string;
    DisplayName: string;
    Description: string;
    InputTokenLimit: integer;
    OutputTokenLimit: integer;
    SupportedGenerationMethods: array of string;
    Temperature: double;
    MaxTemperature: double;
    TopP: double;
    TopK: integer;
  end;

The following method allows to set the model name for all subsequent request after get the interface IGeminiAI. You can also use this function to change between several models while life time of IGeminiAI.

procedure IGeminiAI.SetModel(const ModelName: string);

GenerateContentByPrompt

The interface method IGeminiAI.GenerateContentByPromptSynchronous generates text content as a response based on the provided simple text prompt containing the question. Use this blocking function not in the main thread of a GUI application but in threads, services or console applications. The function returns the IGeminiAIResponse interface that represents the AI's response.

function GenerateContentByPromptSynchronous(const Prompt: string): IGeminiAIResponse

The Interface method IGeminiAI.GenerateContentbyPrompt is the asynchronous and non-blocking variant of the above method and can be safely used in GUI applications within the mainform thread. The response is returned with a delay by the callback method OnResponse. For this purpose, firstly write a method OnGeminiGenContent with the following signature in your form class:

procedure OnGeminiGenContent(Response: IGeminiAIResponse);

As next, call the method GenerateContentbyPrompt and handover the prompt together with a reference to your call back method.

procedure GenerateContentbyPrompt(const Prompt: string;
   OnGeminiGenContent: TOnGeminiGenContent);

GenerateContentByRequest

For more complex requests, where media files, model parameters and security settings need to be transferred, the interface IGeminiAIRequest should be used. This interface should also be used in a chat situation where the user's last questions and the model's last answers need to be passed on for the new enquiry.

function GenerateContentByRequestSynchronous(GeminiAIRequest: IGeminiAIRequest): IGeminiAIResponse;
procedure GenerateContentByRequest(GeminiAIRequest: IGeminiAIRequest; OnRespone: TOnGeminiGenContent);

Use the frist blocking function only in background threads, services or console applications. In GUI application use the second none blocking method for calls in the main thread.

CountTokenOfPrompt

For simple text prompt the following methods calculate the number of tokens entered.

Hint: The number of tokens generated can be limited only by using model parameters in IGeminiAIRequest. There is no possibility to calculated the full cost before thge content will be generated.

type TOnGeminiCountToken = procedure(PromptToken, CachedContentToken: integer; const ErrorMsg: string) of object;
procedure CountTokenOfPrompt(const Prompt: string; OnResponse: TOnGeminiCountToken);

function CountTokenOfPromptSynchronous(const Prompt: string; out ErrorMsg: string;
  out CachedContentToken: integer): integer;

The synchronous variant returns the PromptToken number. The cachedContentToken currently always returns zero, as the caching function is not yet implemented in the IGemeinAIRequest interface. In the event of an error, the ErrorMsg informs about the reason and the number of tokens is returned as 0.

CountTokenOfRequest

For more complex requests based on the interface IGeminiAIRequest, the following methods offers to count the numbers of input token. This includes the text prompt, the media files and all former questions and answer when using the chat functionality.

procedure CountTokenOfRequest(GeminiAIRequest: IGeminiAIRequest; OnResponse: TOnGeminiCountToken);
function CountTokenOfRequestSynchronous(GeminiAIRequest: IGeminiAIRequest; out ErrorMsg: string;
  out CachedContentToken: integer): integer;

The synchronous variant returns the PromptToken number. The cachedContentToken currently always returns zero, as the caching function is not yet implemented in the IGemeinAIRequest interface. In the event of an error, the ErrorMsg informs about the reason and the number of tokens is returned as 0.

Clone this wiki locally