Categorizing text into many subcategories

Hello, I am working on a movie categorizing service, that based on the summary of the movie, should output all the main categories and their respective subcategories.

I.e. if I gave it the summary for Shrek, it should output:
[ { category: “drama”, subCategories: [“romantic”, “tragic hero”] }, { category: “comedy”, subCategories: [“romantic comedy”, “buddy comedy”, “action comedy”] }, {category: “animation”, subCategories: } ].

The way I’m doing it now is by listing 4 categories and their respective subcategories per prompt, as many times as necessary to run through all categories. Then I ask it to choose 0 or more subcategories for each category.
This works decently, but due to the amount of calls it is both slower and more expensive than I would like it to be.

Do you have any ideas how I could improve this functionality?
Thanks!

Try and improve this function for function calling, not sure if it is actually making better categorization than yours.

{
        "name": "categorize_movie",
        "description": "Get all its possible categories for the movie",
        "parameters": {
            "type": "object",
            "properties": {
                "movie": {
                    "type": "string",
                    "description": "Movie title"
                },
                "categories": {
                    "type": "array",
                    "description": "All possible categories",
                    "items": {
                        "type": "object",
                        "properties": {
                            "category": {
                                "type": "string",
                                "description": "Main movie category, e.g. drama, comedy, animation"
                            },
                            "subCategories": {
                                "type": "array",
                                "description": "All possible subcategories under the specified category",
                                "items": {
                                    "type": "string",
                                    "description": "Subcategory of movie, e.g. romantic comedy, action comedy",
                                }
                            }
                        },
                        "required": ["category", "subCategories"]
                    }
                }
            },
            "required": ["movie", "categories"]
        }
    }],
}

Sample output:

{
role: ‘assistant’,
content: null,
function_call: {
name: ‘categorize_movie’,
arguments: ‘{\n’ +
’ “movie”: “shrek”,\n’ +
’ “categories”: [\n’ +
’ {\n’ +
’ “category”: “Animation”,\n’ +
’ “subCategories”: [“Comedy”, “Adventure”]\n’ +
’ },\n’ +
’ {\n’ +
’ “category”: “Fantasy”,\n’ +
’ “subCategories”: [“Family”]\n’ +
’ }\n’ +
’ ]\n’ +
‘}’
}
}

{
role: ‘assistant’,
content: null,
function_call: {
name: ‘categorize_movie’,
arguments: ‘{\n’ +
’ “movie”: “star wars”,\n’ +
’ “categories”: [\n’ +
’ {\n’ +
’ “category”: “Science Fiction”,\n’ +
’ “subCategories”: [“Action”, “Adventure”]\n’ +
’ },\n’ +
’ {\n’ +
’ “category”: “Fantasy”,\n’ +
’ “subCategories”: [“Adventure”]\n’ +
’ }\n’ +
’ ]\n’ +
‘}’
}
}

{
role: ‘assistant’,
content: null,
function_call: {
name: ‘categorize_movie’,
arguments: ‘{\n’ +
’ “movie”: “gone with the wind”,\n’ +
’ “categories”: [\n’ +
’ {\n’ +
’ “category”: “Drama”,\n’ +
’ “subCategories”: [“Romance”, “Historical”]\n’ +
’ },\n’ +
’ {\n’ +
’ “category”: “Adventure”,\n’ +
’ “subCategories”: [“Romance”]\n’ +
’ },\n’ +
’ {\n’ +
’ “category”: “War”,\n’ +
’ “subCategories”: [“Romance”, “Historical”]\n’ +
’ }\n’ +
’ ]\n’ +
‘}’
}
}

{
role: ‘assistant’,
content: null,
function_call: {
name: ‘categorize_movie’,
arguments: ‘{\n’ +
’ “movie”: “Letters from Iwo Jima”,\n’ +
’ “categories”: [\n’ +
’ {\n’ +
’ “category”: “War”,\n’ +
’ “subCategories”: [“Drama”, “Action”]\n’ +
’ },\n’ +
’ {\n’ +
’ “category”: “History”,\n’ +
’ “subCategories”: [“Biography”]\n’ +
’ },\n’ +
’ {\n’ +
’ “category”: “Foreign”,\n’ +
’ “subCategories”: [“Japanese”]\n’ +
’ }\n’ +
’ ]\n’ +
‘}’
}
}

If you want it to only select from your own categories/subcategories, you probably need to use enum.

Thanks for your reply!

The problem isn’t so much the prompt, but the speed and amount of prompts I have to make, using this form of categorizing. I have 40+ categories and each of them have between 10-50 subcategories, which forces me to make more prompts than I would like.