def get_results_hierarchical()

in projects/Aligned-Platform-EnergizeAI/taxonomybuilder/tbm.py [0:0]


def get_results_hierarchical(prompts, ref_labels, defns, taxonomy, verbose = False, few_shot = False):
    '''
    Given a list of prompt-category labelled pairs, definitions, and a taxonomy, runs the zero-shot training loop.
    Each classification task traverses one level deeper into the tree.

    Params:
    prompts: list of labelled pairs
    defns: definitions of categories
    taxonomy: structure of the taxonomy as a graph.
    verbose: if True, print ChatGPT outputs; if False, don't print.
    few_shot: if True, use held-out reference prompts. If False, don't.

    Returns:
    results: A list of all incorrectly classified prompts.
    '''
    results = []
    parent = 'General'

    for prompt, classification in prompts:

        categories = [x for x in taxonomy['root']]

        content = generate_classification_prompt(
            categories, 
            {x: defns[x] for x in defns if x in categories}, 
            ref_labels,
            prompt,
            few_shot = few_shot
            )
        resp = execute_llm_request(content, verbose = verbose)

        if resp != 'General':
            parent = resp

        while resp in taxonomy and len(taxonomy[resp]) > 0:
            categories = taxonomy[resp]

            content = generate_classification_prompt(
                categories, 
                {x: defns[x] for x in defns if x in categories}, 
                ref_labels,
                prompt,
                few_shot = few_shot
            )
            resp = execute_llm_request(content, verbose = verbose)
            if resp != 'General':
                parent = resp


        results.append(
            {
                'prompt': prompt,
                'expected': classification,
                'llm': resp if resp != 'General' else parent
            }
        )
    return results