The JSON schema that is used as function output doesn’t directly support such lists while not including the various schema elements.
The only way to form that exact output as a function return would be as a single string, which would take just as much prompting but in the function description.
However, I have written a function to create such an iterable output that you can process with your code. Here’s the output:
"function_call": {
"name": "report_sentiment",
"arguments": "{
"word_1": "happy",
"sentiment_1": "Positive",
"word_2": "sad",
"sentiment_2": "Negative"
}"
}
and I’ve also made the function so the AI will go beyond what is directly specified:
"function_call": {
"name": "report_sentiment",
"arguments": "{
"word_1": "bereavment",
"sentiment_1": "Negative",
"word_2": "joyous",
"sentiment_2": "Positive",
"word_3": "sarcastic",
"sentiment_3": "Negative",
"word_4": "exuberant",
"sentiment_4": "Positive",
"word_5": "triumphant",
"sentiment_5": "Positive"
}"
}
How is it done? By just asking for the word and its sentiment as function parameters. The system prompt is just “Act only as sentiment classifier on input list.”
For my python, we just set this and add "functions": functions
to the API call.
functions = [
{
"name": "report_sentiment",
"description": "Required function, report sentiment of each individual input word",
"parameters": {
"type": "object",
"properties": {
"word_1": {"type": "string", "description": "first word"},
"sentiment_1": {"type": "string","description": "required", "enum": ["Positive", "Negative"]},
"word_2": {"type": "string", "description": "second word. Continue writing function if more words"},
"sentiment_2": {"type": "string","description": "required", "enum": ["Positive", "Negative"]}
}
}
}
]
Add “neutral” as an enum option also. enum limits output to only specific choices.
The language could even be minimized to just what works. No parameter descriptions were actually required except to make it automatically go beyond the two words correctly.