Skip to content

FB4D Reference IGeminiSchema

Christoph Schneider edited this page Nov 11, 2024 · 1 revision

Interface IGeminiSchema

This interface is used to declare a JSON schema for requesting a JSON object using IGeminiAIRequest.SetJSONResponseSchema.

For elementary data types, use one of the following methods:

function SetStringType: IGeminiSchema;
function SetFloatType: IGeminiSchema;
function SetIntegerType: IGeminiSchema;
function SetBooleanType: IGeminiSchema;
function SetEnumType(EnumValues: TStringDynArray): IGeminiSchema;

To define an array, use the following method, which in turn transfers an IGeminiSchema as the array element. This makes it easy to create nested types. The number of elements in the array can be defined with limits using the optional parameters.

function SetArrayType(ArrayElement: IGeminiSchema; MaxItems: integer = -1; MinItems: integer = -1): IGeminiSchema;

The following method is used to compile a JSON object, whereby a list of named fields must be transferred together with their data type. The type TSchemaItems, which is defined as TDictonary, is used for this. Two parameters of this type are passed to SetObjectType: The first with the list of all required fields and the second parameter with the list of all optional fields. Note that the SetObjectType function processes the transferred TDictionary objects after processing and immediately releases them for easy use with Fluid-Design.

function SetObjectType(RequiredItems: TSchemaItems; OptionalItems: TSchemaItems = nil): IGeminiSchema;
TSchemaItems = TDictionary<string, IGeminiSchema>;

In this context, it is also helpful to know the following helper class function from the FB4D.helpers unit:

class function TSchemaItemsHelper.CreateItem(const FieldName: string; Schema: IGeminiSchema): TPair<string, IGeminiSchema>;

Example

The following code demonstrates the usage within the unit test GeminiAI.

var Request: IGeminiAIRequest;
Request := TGeminiAIRequest.Create.Prompt('Create a JSON Object for the following math formula: A * A = A^2');
Request.SetJSONResponseSchema(TGeminiSchema.Create.SetObjectType(TSchemaItems.Create(
  [TSchemaItems.CreateItem('FirstFactor', TGeminiSchema.StringType),
   TSchemaItems.CreateItem('SecondFactor', TGeminiSchema.StringType),
   TSchemaItems.CreateItem('ResultingTerm', TGeminiSchema.StringType),
   TSchemaItems.CreateItem('NumberOfFactors', TGeminiSchema.IntegerType),
   TSchemaItems.CreateItem('IsFormulaCorrect', TGeminiSchema.BooleanType)])));
Clone this wiki locally