FB4D Reference IGeminiSchema - SchneiderInfosystems/FB4D GitHub Wiki
Interface IGeminiSchema
This interface is used to declare a JSON schema for requesting a JSON object using IGeminiAIRequest.SetJSONResponseSchema.
For elementary data types and property attributes, use one of the following methods (or the corresponding class helper methods on TGeminiSchema such as TGeminiSchema.StringType, TGeminiSchema.IntegerType, TGeminiSchema.BooleanType, etc.):
function SetStringType: IGeminiSchema;
function SetFloatType: IGeminiSchema;
function SetDoubleType: IGeminiSchema;
function SetIntegerType: IGeminiSchema;
function SetInt64Type: IGeminiSchema;
function SetBooleanType: IGeminiSchema;
function SetEnumType(EnumValues: TStringDynArray): IGeminiSchema;
function SetDateTimeType: IGeminiSchema;
function SetDescription(const Description: string): IGeminiSchema;
function SetNullable(IsNullable: boolean): IGeminiSchema;
To define an array, use the following method (or TGeminiSchema.ArrayType), 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; MinItems: integer = -1; MaxItems: integer = -1): IGeminiSchema;
The following method is used to compile a JSON object (or TGeminiSchema.ObjectType), whereby a list of named fields must be transferred together with their data type. The type TSchemaItems, which is defined as TDictionary, 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)])));