#How to handle required in assistant functions?

1 messages · Page 1 of 1 (latest)

wraith hemlock
#

@lethal sparrow

When making a function for assistants is the required part required? And how to handle when I have a key which has multiple other keys inside of it, does the required need to be inside of the parent key?

lethal sparrowBOT
#

Hi @wraith hemlock. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

When defining functions for an assistant, the required part is indeed necessary. It specifies which parameters are mandatory for the function to operate correctly. If a parameter is listed in the required array, it means that the function expects this parameter to be provided when it is called.

If you have a key which has multiple other keys inside of it (a nested object), the required array should be inside the parent key. This is because the required array is specific to the object it is defined in.

Here is an example from the extracts:

"function": {
 "name": "getCurrentWeather",
 "description": "Get the weather in location",
 "parameters": {
 "type": "object",
 "properties": {
 "location": {"type": "string", "description": "The city and state e.g. San Francisco, CA"},
 "unit": {"type": "string", "enum": ["c", "f"]}
 },
 "required": ["location"]
 }
}

In this example, the parameters object has a required array that includes "location". This means that the location parameter is required for the getCurrentWeather function.

For more information, you can refer to the OpenAI API documentation (https://platform.openai.com/docs/api-reference).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@wraith hemlock kapa.ai is still learning and improving, please let us know how it did by reacting below

wraith hemlock
#

@lethal sparrow give an example with nested objects where there are some keys in required which are on the same level and which are nested

lethal sparrowBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Sure, here's an example from the extracts that shows how to handle required with nested objects:

{
    "name": "get_addresses",
    "description": "Get all unique addresses from a text",
    "parameters": {
        "type": "object",
        "properties": {
            "addresses": {
                "type": "array",
                "description": "The address of a company or building",
                "items": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string",
                            "description": "The name of the company or building"
                        },
                        "street": {
                            "type": "string",
                            "description": "The street name of the building or company"
                        },
                        "postcode": {
                            "type": "string",
                            "description": "The postcode of the building or company, e.g. 65496, 3824 ME, ..."
                        },
                        "city": {
                            "type": "string",
                            "description": "The city name of the building or company, e.g. Köln, Kopenhagen, ..."
                        },
                        "country_code": {
                            "type": "string",
                            "description": "The ISO country code of the building or company, e.g. UK, DE, DK"
                        }
                    },
                    "required": ["name", "street", "postcode", "city", "country_code"]
                }
            }
        },
        "required": ["addresses"]
    }
}
#

In this example, the required array is used at two levels. At the top level, it specifies that the addresses parameter is required. Inside the addresses parameter, which is an array of objects, it specifies that the name, street, postcode, city, and country_code fields are required for each address object.

This means that when calling the get_addresses function, you must provide an addresses parameter that is an array of objects, and each object in the array must include the name, street, postcode, city, and country_code fields.

For more information, you can refer to the OpenAI API documentation (https://platform.openai.com/docs/api-reference).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@wraith hemlock kapa.ai is still learning and improving, please let us know how it did by reacting below

wraith hemlock
#

@lethal sparrow

so this is correct function format for assistants tools?

custom_analysis = (
{
"type": "analysis_tool",
"tool": {
"name": "project_insight_generator",
"description": "Generate insights and evaluations for the submitted project",
"parameters": {
"type": "object",
"attributes": {
"summary": {
"type": "string",
"description": "Brief summary about the project for the report",
},
"feedback": {
"type": "string",
"description": "Short personal feedback on the project",
},
"quality_assessment": {
"structure": {
"type": "string",
"enum": ["basic", "average", "noteworthy", "outstanding"],
"description": "Assess the structure of the project",
},
"required": [
"structure",
],
},
"required": [
"summary",
"feedback",
],
},
},
},
},
)

lethal sparrowBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The function format you've provided is almost correct, but there are a few adjustments needed to align it with the OpenAI API's expected format for assistant tools.

The type should be "function" instead of "analysis_tool". Also, the parameters object should have properties instead of attributes. The required field should be at the same level as properties, not nested inside it.

Here's the corrected version:

#
custom_analysis = (
    {
        "type": "function",
        "function": {
            "name": "project_insight_generator",
            "description": "Generate insights and evaluations for the submitted project",
            "parameters": {
                "type": "object",
                "properties": {
                    "summary": {
                        "type": "string",
                        "description": "Brief summary about the project for the report",
                    },
                    "feedback": {
                        "type": "string",
                        "description": "Short personal feedback on the project",
                    },
                    "quality_assessment": {
                        "type": "object",
                        "properties": {
                            "structure": {
                                "type": "string",
                                "enum": ["basic", "average", "noteworthy", "outstanding"],
                                "description": "Assess the structure of the project",
                            },
                        },
                        "required": [
                            "structure",
                        ],
                    },
                },
                "required": [
                    "summary",
                    "feedback",
                    "quality_assessment",
                ],
            },
        },
    },
)

In this corrected version, the required field is at the same level as properties, and it includes "quality_assessment" because it's a top-level property. Inside the quality_assessment object, there's another required field that specifies "structure" as a required property.

#

@wraith hemlock kapa.ai is still learning and improving, please let us know how it did by reacting below

wraith hemlock
#

@lethal sparrow so does the second required need to be inside of quality_assessments or above?

lethal sparrowBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The second required field should be inside the quality_assessment object. This is because it specifies which properties are required within the quality_assessment object itself.

Here's the relevant part of the corrected function:

"quality_assessment": {{
    "type": "object",
    "properties": {{
        "structure": {{
            "type": "string",
            "enum": ["basic", "average", "noteworthy", "outstanding"],
            "description": "Assess the structure of the project",
        }},
    }},
    "required": [
        "structure",
    ],
}},

In this example, the required field is inside the quality_assessment object and it specifies that the structure property is required within the quality_assessment object.

For more information, you can refer to the OpenAI API documentation (https://platform.openai.com/docs/api-reference).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@wraith hemlock kapa.ai is still learning and improving, please let us know how it did by reacting below

wraith hemlock
#

@lethal sparrow so children objects also need a type and properties?

lethal sparrowBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, nested or child objects also need a type and properties. The type field specifies that the value is an object, and the properties field describes the properties that this object can have.

Here's an example from the corrected function:

"quality_assessment": {{
    "type": "object",
    "properties": {{
        "structure": {{
            "type": "string",
            "enum": ["basic", "average", "noteworthy", "outstanding"],
            "description": "Assess the structure of the project",
        }},
    }},
    "required": [
        "structure",
    ],
}},

In this example, quality_assessment is a nested object. It has a type of "object" and properties that describe the structure property.

For more information, you can refer to the OpenAI API documentation (https://platform.openai.com/docs/api-reference).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: