Skip to content

Decoder

Making Decisions

Making choices and decisions is a core part of any intelligence system, and we wanted to make that as easy as possible. The decoder service (accessible via Bot().llm.decoder) can be used to make decisions or navigate choices with minimal configuration or code.

Basic Decoder

Here is a simple example of how to use the decoder to return the family of an animal. The decoder uses a prompt, keys description, and keys/values mapping to make decisions.

from dandy import Bot

bot = Bot()

keys_description = 'Animal Sounds'
keys_values = {
    'barking': 'dog',
    'meowing': 'cat',
    'quacking': 'duck'
}

animal_family = bot.llm.decoder.prompt_to_value(
    prompt='I was out on a walk and heard some barking',
    keys_description=keys_description,
    keys_values=keys_values
)

print(animal_family)
dog

Multiple Values

You can also request multiple values to be returned using prompt_to_values with max_return_values:

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.11/site-packages/markdown_exec/_internal/formatters/python.py", line 71, in _run_python
    exec_python(code, code_block_id, exec_globals)
  File "/app/.heroku/python/lib/python3.11/site-packages/markdown_exec/_internal/formatters/_exec_python.py", line 8, in exec_python
    exec(compiled, exec_globals)  # noqa: S102
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<code block: session decoder; n2>", line 13, in <module>
    activities = bot.llm.decoder.prompt_to_values(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/dandy/llm/decoder/service.py", line 45, in prompt_to_values
    return self.decoder.process(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/dandy/llm/decoder/decoder.py", line 101, in process
    for decoder_enum in self._process_decoder_prompt_to_intel(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/dandy/llm/decoder/decoder.py", line 178, in _process_decoder_prompt_to_intel
    self._validate_return_keys_intel(return_keys_intel, max_return_values)
  File "/workspace/dandy/llm/decoder/decoder.py", line 214, in _validate_return_keys_intel
    raise DecoderNoKeysRecoverableError(message)
dandy.llm.decoder.exceptions.DecoderNoKeysRecoverableError: No Activities found.

Note

The decoder's keys_values dictionary can contain any type of object as values, allowing you to map to complex objects, not just strings.