File size: 3,326 Bytes
1e91476
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import random
from string import Formatter
from typing import Dict, Any
from .config import DEFAULT_NAMES, TEMPLATE, SEED, SUBSEEDS

def get_random_default_name(gender: str = None) -> str:
    return random.choice(DEFAULT_NAMES)

def _get_subseed_description_(
    scenario_config: Dict[str, str], subseed_name: str, SUBSEED_VALUES: Dict[str, Any]
) -> str:
    """Format a subseed with no formatting gaps."""
    if subseed_name not in scenario_config:
        raise Exception(f"{subseed_name} not in scenario config")

    subseed_value = scenario_config[subseed_name]

    # Handle difficulty specifically for hard scenarios when difficulty is not default
    if subseed_name == "difficulty" and subseed_value != "default":
        # Select a random difficulty from the SUBSEED_VALUES dictionary, excluding "default"
        non_default_difficulties = [key for key in SUBSEED_VALUES if key != "default"]
        subseed_value = random.choice(non_default_difficulties)

    descriptions = SUBSEED_VALUES.get(subseed_value, {}).get("description", [""])
    # Get subseed description
    subseed_descrip = random.choice(descriptions)
    # Additional formatting options
    format_opts = [
        fn for _, fn, _, _ in Formatter().parse(subseed_descrip) if fn is not None
    ]
    format_values = {}
    if len(format_opts) > 0:
        for opt_name in format_opts:
            opts = SUBSEED_VALUES.get(subseed_value, {}).get(opt_name, [""])
            format_values[opt_name] = random.choice(opts)
    # Format the description
    return subseed_descrip.format(**format_values)

def get_seed_description(
    scenario_config: Dict[str, Any],
    texter_name: str,
    SUBSEEDS: Dict[str, Any] = SUBSEEDS,
    SEED: str = SEED,
) -> str:
    """Format the SEED with appropriate parameters from scenario_config."""
    subseed_names = [fn for _, fn, _, _ in Formatter().parse(SEED) if fn is not None]
    subseeds = {}
    for subname in subseed_names:
        if subname == "texter_name":
            subseeds[subname] = texter_name
        else:
            subseeds[subname] = _get_subseed_description_(
                scenario_config, subname, SUBSEEDS.get(subname, {})
            )
    return SEED.format(**subseeds)

def get_template(
    language: str = "en", texter_name: str = None, SEED: str = SEED, **kwargs
) -> str:
    """
    Generate a conversation template for a simulated crisis scenario based on provided parameters.
    """
    # Accessing the template based on the language
    template = TEMPLATE.get(f"{language.upper()}_template", {}).get("description", "")

    # Default name if not provided
    if (texter_name is None) or (texter_name==""):
        texter_name = get_random_default_name()

    # Create a default scenario configuration if not fully provided
    defaults = {
        fn: "default" for _, fn, _, _ in Formatter().parse(SEED) if fn is not None
    }
    kwargs.update((k, defaults[k]) for k in defaults.keys() if k not in kwargs)

    # Generate the seed description
    scenario_seed = get_seed_description(kwargs, texter_name)

    # Remove excessive indentation and format the final template
    formatted_template = template.format(current_seed=scenario_seed)
    cleaned_output = "\n".join(line.strip() for line in formatted_template.split("\n"))

    return cleaned_output