convosim-ui / models /business_logic_utils /prompt_generation.py
ivnban27-ctl's picture
fixed databricks integration
42a7266
raw
history blame
3.33 kB
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