However, in general, one must write their own function, with JSON data types.
string
number
boolean
null/empty
object
array
Lets look at a demonstration of them all,
- how you would specify them as a “functions” API parameter;
- how the specification is rewritten into AI language by the endpoint;
- how the AI calls the function as it is specified.
Function specification sent to API
functions = [
{
"name": "data_demonstration",
"description": "This is the main function description",
"parameters": {
"type": "object",
"properties": {
"string_1": {"type": "string", "description": ""},
"number_2": {"type": "number", "description": ""},
"boolean_3": {"type": "boolean", "description": ""},
"empty_4": {"type": "null","description": "This is a description of the empty_4 null property"},
"string_5_enum": {"type": "string", "enum": ["Happy", "Sad"]},
},
"required": ["string_1","number_2","boolean_3", "empty_4", "string_5_enum"]
}
}
]
Function specification received by AI:
namespace functions {
// This is the main function description
type data_demonstration = (_: {
string_1: string,
number_2: number,
boolean_3: boolean,
// This is a description of the empty_4 null property
empty_4: any,
string_5_enum: "Happy" | "Sad",
}) => any;
} // namespace functions
Function used by AI:
{
..
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "data_demonstration",
"arguments": "{
"string_1": "Hello",
"number_2": 42,
"boolean_3": true,
"empty_4": null,
"string_5_enum": "Happy"
}"
}
},
"finish_reason": "function_call"
}
],...
(\n is rendered above for clarity)
Function specification with an inner object
functions = [
{
"name": "data_demonstration",
"description": "This is the main function description",
"parameters": {
"type": "object",
"properties": {
"object_1": {
"type": "object",
"description": "The object data type as a property",
"properties": {
"string_2a": {
"type": "string",
"enum": ["Happy", "Sad"]
},
"string_2b": {
"type": "string",
"description": "Description in a second object is lost"
},
},
},
"string_1": {"type": "string",
"description": "Not required gets a question mark"},
},
"required": ["object_1","placeholder_2"]
}
}
]
Function specification received by AI, with object:
namespace functions {
// This is the main function description
type data_demonstration = (_: {
// The object data type as a property
object_1: {
string_2a: "Happy" | "Sad",
string_2b: string,
},
// Not required gets a question mark
string_1?: string,
}) => any;
} // namespace functions
Above we see a big failure that people don’t realize, nesting objects gets you no descriptions for the inner properties. They don’t have the “not required” question mark.
Function used by AI and output by API:
{...
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "data_demonstration",
"arguments": "{
"object_1": {
"string_2a": "Happy",
"string_2b": "Hello World"
},
"string_1": "Example data"
}"
}
},
"finish_reason": "function_call"
}
],
...
I hope you take the time to follow each of these properties through their specification to the ultimate API output.
There is one more JSON data type, array
, which is equivalent to a Python list. Left as an exercise for you.
(Not explored, the method of setting the API function_call
parameter from auto to “required” or “none”.)