Skip to content

LLM Map

dandy.llm.Map

Bases: BaseModel

Source code in dandy/map/map.py
def __init__(self, valid_map: Dict[str, Union[Self, Any]], **data: Any) -> None:
    super().__init__(
        valid_map=valid_map,
        **data
    )

valid_map instance-attribute

__getitem__

Source code in dandy/map/map.py
def __getitem__(self, item):
    return self.valid_map[item]

as_enum

Source code in dandy/map/map.py
def as_enum(self) -> Enum:
    enum_choices = {}
    for key, value in self._keyed_map.items():
        enum_choices[value[0]] = key

    return Enum(f'{self.__class__.__name__}Enum', enum_choices)

model_post_init

Source code in dandy/map/map.py
def model_post_init(self, __context: Any) -> None:
    self.process_map_to_keyed()

keyed_choices

Source code in dandy/map/map.py
def keyed_choices(self) -> list[str]:
    return [f'{key}. "{value[0]}"\n' for key, value in self._keyed_map.items()]

keyed_choices_str

Source code in dandy/map/map.py
def keyed_choices_str(self) -> str:
    return ''.join(self.keyed_choices())

get_selected_value

Source code in dandy/map/map.py
def get_selected_value(self, choice_key: int) -> Any:
    return self._keyed_map[choice_key][1]

process_map_to_keyed

Source code in dandy/map/map.py
def process_map_to_keyed(self):
    for i, (choice, value) in enumerate(self.valid_map.items(), start=1):
        if isinstance(value, dict):
            self._keyed_map[i] = (choice, self.process_map_to_keyed())
        else:
            self._keyed_map[i] = (choice, value)

dandy.llm.BaseLlmMap

Bases: BaseLlmProcessor[MapValuesIntel], ABC

config = 'DEFAULT' class-attribute instance-attribute

config_options = LlmConfigOptions() class-attribute instance-attribute

instructions_prompt = Prompt().text('Your job is to select a choice for the provided list of choices.').text('Make sure you select at least one choice that is the most relevant to the users input.') class-attribute instance-attribute

intel_class = MapValuesIntel class-attribute instance-attribute

map instance-attribute

__init_subclass__

Source code in dandy/llm/map/llm_map.py
def __init_subclass__(cls):
    super().__init_subclass__()

    if cls.map is None:
        raise MapCriticalException(f'{cls.__name__} map is not set.')

process classmethod

Source code in dandy/llm/map/llm_map.py
@classmethod
def process(
        cls,
        prompt: Union[Prompt, str],
        choice_count: int = 1,
) -> MapValuesIntel[Any]:
    return cls.process_map_to_intel(
        cls.map,
        prompt,
        choice_count
    )

process_map_to_intel classmethod

Source code in dandy/llm/map/llm_map.py
@classmethod
def process_map_to_intel(
        cls,
        map: Map,
        prompt: Union[Prompt, str],
        choice_count: int = 1
) -> MapValuesIntel[Any]:
    map_values_intel = MapValuesIntel()

    for map_enum in cls.process_prompt_to_intel(map, prompt, choice_count):
        map_value = map.get_selected_value(map_enum.value)

        if isinstance(map_value, type):
            if issubclass(map_value, BaseLlmMap):
                map_values_intel.extend(
                    map_value.process(
                        prompt,
                        choice_count
                    ).items
                )
            else:
                map_values_intel.append(map_value)

        elif isinstance(map_value, Map):
            map_values_intel.extend(
                cls.process_map_to_intel(
                    map_value,
                    prompt,
                    choice_count
                ).items
            )
        else:
            map_values_intel.append(map_value)

    return map_values_intel

process_prompt_to_intel classmethod

Source code in dandy/llm/map/llm_map.py
@classmethod
def process_prompt_to_intel(
        cls,
        map: Map,
        prompt: Union[Prompt, str],
        choice_count: int = 1,
) -> MapValuesIntel:

    system_prompt = (
        Prompt()
        .prompt(cls.instructions_prompt)
        .text(f'Please select {choice_count} of the following choices by number using the following rules.')
        .line_break()
        .sub_heading('Rules:')
        .list([
            'Select the choice that best matches the users input.',
            'Return at least one choice by number.'
        ])
        .line_break()
        .sub_heading('Choices:')
        .text(map.keyed_choices_str())
    )

    return llm_configs[cls.config].generate_service(
        llm_options=cls.config_options
    ).process_prompt_to_intel(
        prompt=prompt if isinstance(prompt, Prompt) else Prompt(prompt),
        intel_class=MapValuesIntel[cls.map.as_enum()],
        system_prompt=system_prompt
    )

process_to_future classmethod

Source code in dandy/llm/map/llm_map.py
@classmethod
def process_to_future(cls, *args, **kwargs) -> AsyncFuture[MapValuesIntel]:
    return AsyncFuture[MapValuesIntel](cls.process, *args, **kwargs)