File size: 2,039 Bytes
510a9b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def create_classification_prompt(row, label_descriptions, features, example_rows):
    """
    Generates system and user prompts for classification.

    Args:
        row (dict): A single row of feature values.
        label_descriptions (dict): Mapping of labels to their descriptions.
        features (list): List of features to include in the prompt.
        example_rows (list): Few-shot examples for the prompt.

    Returns:
        tuple: (system_prompt, user_prompt)
    """
    # System prompt
    system_prompt = "You are a classifier. Assign one of the following labels based on the input data:\n"
    for label, desc in label_descriptions.items():
        system_prompt += f"- {label}: {desc}\n"

    # Few-shot examples
    if example_rows:
        system_prompt += "\nExamples:\n"
        for example in example_rows:
            example_features = "; ".join(
                f"{feature}: {example['features'][feature]}" for feature in features
                #f"{feature}: {example.get('features', {}).get(feature, 'MISSING')}" for feature in features
            )
            system_prompt += f"Input: {example_features}\nLabel: {example['label']}\n"

    # User prompt for the current row
    user_features = "; ".join(f"{feature}: {row[feature]}" for feature in features)
    user_prompt = f"Input: {user_features}\nLabel:"

    return system_prompt, user_prompt


def generate_prompts(row, label_descriptions, features, example_rows):
    """
    Wrapper for create_classification_prompt to generate prompts for a row.

    Args:
        row (dict): Row of the dataset.
        label_descriptions (dict): Mapping of labels to their descriptions.
        features (list): List of features to include in the prompt.
        example_rows (list): Few-shot examples for the prompt.

    Returns:
        tuple: (system_prompt, user_prompt)
    """
    return create_classification_prompt(
        row=row,
        label_descriptions=label_descriptions,
        features=features,
        example_rows=example_rows,
    )