FB4D Reference IGeminiAIResponse - SchneiderInfosystems/FB4D GitHub Wiki
Interface IGeminiAIResponse
This interface contains the result of a content request of Gemini AI.
The following function returns the detailed Gemini AI model version which was used for generating the answer.
function ModelVersion: string;
The following method informs about the cost of the previous request. Costs are measured with number of tokens for the input (prompt) and their result (generated). Please refer to the Gemini AI page for the actual cost of tokens for a selected model in your country.
function UsageMetaData: TGeminiAIUsageMetaData;
TGeminiAIUsageMetaData = record
PromptTokenCount: integer; // The number of tokens used in the prompt.
GeneratedTokenCount: integer; // The number of tokens generated in the response (CandidatesTokenCount).
TotalTokenCount: integer; // The total number of tokens used (prompt + generated).
end;
The next two methods provide information about the status of the result, either as an enum or in text form.
function ResultStateStr: string;
function ResultState: TGeminiAIResultState;
TGeminiAIResultState = (
grsUnknown, // The result state is unknown.
grsTransmitError, // An error occurred during transmission.
grsParseError, // An error occurred while parsing the response.
grsValid, // The result is valid.
grsBlockedBySafety, // The result was blocked by the safety system.
grsBlockedbyOtherReason // The result was blocked for another reason.
);
The next two methods provide information about the reason for the termination of the text generation, either as a set of enums over all candidates or as comma separated text if several reasons were found.
function FinishReasonsCommaSepStr: string;
function FinishReasons: TGeminiAIFinishReasons;
TGeminiAIFinishReason = (
gfrUnknown, // The finish reason is unknown.
gfrStop, // The generation reached a natural stopping point.
gfrMaxToken, // The generation reached the maximum token limit.
gfrSafety, // The generation was stopped for safety reasons.
gfrRecitation, // The generation was flagged as potential recitation of existing content.
gfrOther // The generation finished for another reason.
);
TGeminiAIFinishReasons = set of TGeminiAIFinishReason;
The function IsValid
returns true, if valid text was generated.
The function FailureDetail
returns a string with the failure reason, if no valid text was generated.
The function ResultAsMarkDown
returns the generated text in Markdown formatted as string. In the future when more than one candidates will be returned, the candidates will be returned as a list in Markdown notation.
As there is currently no memo component in Firemonkey and VCL that allows Markdown formatted text to be displayed correctly, FB4D uses the delphi-markdown submodule to convert markdown to HTML for visualisation in the web browser component. The usage of this submodul is optional and
need to be configured with the conditional compiler define MARKDOWN2HTML
. When you have set this define, the follow method allows to fetch the result in HTML notation.
function ResultAsHTML: string;
In fact, the markdown to html converter does not build a complete HTML file, but only html parts converted from Markdown. The web browser component can also handle such html fragments.
When using IGeminiAIRequest.SetJSONResponseSchema
you can access the resulting JSON Object with the following method:
function ResultAsJSON: TJSONValue;
Do not confuse the above function with the following function, which outputs the JSON object generated by the web server and can be used to evaluate currently unprocessed JSON fields. The following methods allows to access the details of this resulting JSON object:
function RawJSONResult: TJSONValue;
function RawFormatedJSONResult: string
The first method returns the JSON object while the second method returns the formatted JSON string that was returned by Gemini AI Api.
In Version 1.7.0 the second method was named FormatedJSON
.
Result of candicates
function NumberOfResults: integer
The above method returns the number of candidates in the result. Currently (November 24) only one candidate is returned at a time, but this may change in the future.
function EvalResult(ResultIndex: integer): TGeminiAIResult;
The above method returns details about the result in the following record for a distinct candidate.
TGeminiAIResult = record
FinishReason: TGeminiAIFinishReason; // The reason why the generation finished
Index: integer; // The index of the result within the response
// An array of strings representing the generated text, split into parts
PartText: array of string;
// An array of safety ratings for each harm category.
SafetyRatings: array [THarmCategory] of TSafetyRating;
function ResultAsMarkDown: string; // Returns the result text formatted as Markdown
function FinishReasonAsStr: string; // Returns the finish reason as a string
end;
TGeminiAIFinishReason = (
gfrUnknown, // The finish reason is unknown.
gfrStop, // The generation reached a natural stopping point.
gfrMaxToken, // The generation reached the maximum token limit.
gfrSafety, // The generation was stopped for safety reasons.
gfrRecitation, // The generation was flagged as potential recitation of existing content.
gfrOther // The generation finished for another reason.
);
THarmCategory = (
hcUnspecific, // Unspecified harm category.
hcHateSpeech, // Hate speech.
hcHarassment, // Harassment.
hcSexuallyExplicit, // Sexually explicit content.
hcDangerousContent, // Dangerous content.
hcCivicIntegrity, // Content that undermines civic integrity.
hcDangerous, // Dangerous content.
hcMedicalAdvice, // Unsolicited medical advice.
hcSexual, // Sexual content.
hcViolence, // Violent content.
hcToxicity, // Toxic or abusive language.
hcDerogatory // Derogatory or offensive language.
);
TSafetyRating = record
// The probability level of the harm category.
Probability: TProbabilityAndSeverity;
// The probability score (typically a value between 0 and 1).
ProbabilityScore: extended;
// The severity level of the harm category.
Severity: TProbabilityAndSeverity;
// The severity score (typically a value between 0 and 1).
SeverityScore: extended;
// Returns the probability level as a string.
function ProbabilityAsStr: string;
// Returns the severity level as a string.
function SeverityAsStr: string;
end;