The prompt in a impl<llm> is a block string. This means you can add block comments as well, and it auto dedents and trims whitespace.

impl<llm, MyFunction> some_name {
    client GPT4
    prompt #"
        Hi GPT! {// Hi works better than Hello //}

        Write me a story.
    "#
}

Every prompt has two template paramters that are available to you:

  • input: the input to the function
  • output: the output of the function (see printers below)

input

#input allows you to pass in the function’s input text (or object) to the LLM prompt

input for string/enums

Using {#input} in your prompt is the equivalent of str(arg).

input for complex types

If you add {#input} directly to your prompt when the input is a custom class, we serialize the object to JSON into your prompt. To prevent this you can inject each field individually like {#input.field}.

Printers

Printers are BAMLs standardization for serialization. Since LLMs require strings as inputs, BAML provides a standardized way to serialize and deserialize types/values to and from strings.

Instead of having ad-hoc functions scattered throughout your codebase or manually writing output formats, the type information you define in BAML can be used to automatically generate the output format for you. This has the benefit of:

  • consistent formatting across all your prompts
  • easier to read prompts (but you can always see the full prompt string in the playground)
  • When you change the type of the output, the prompt automatically updates to match the function signature
  • When you add / remove enum values, the prompt automatically updates

Instead of needing to write a prompt like this:

#"
    Please answer in this json format: { "verbs" : string[] }
"#

You can write (output is automatically substituted for the return type of the function):

#"
    Please answer in this json format: {#print_type(output)}
"#

The available printer functions are:

  • print_enum: converts an enum into a pretty string of its values.
  • print_type: prints a type as a well structured json schema Learn more

For now, BAML has built in formatting for print_enum and print_type, but soon, we’ll allow you to specify your own formatting for these functions.

print_enum by default prints the enum name and values as a list of strings.

Example Simple

enum MyEnum {
    A
    B
    C
}

print_enum(MyEnum) will print:

MyEnum
---
A
B
C

Example Advanced

@alias, @skip, and @description are explained in the Deep Dive - enums and print_enum section.

enum MyEnum {
    A @description(#"
        This is a description of A
    "#)
    B
    C @skip
    D @alias("new_name")
    @description(#"
        This is a description of category
    "#)
}

print_enum(MyEnum) will print:

MyEnum
---
A: This is a description of A
B
new_name: This is a description of category

print_type by default prints the type as a json schema.

Example Simple

class Person {
    name string
    age int
    items string[]
}

print_type(Person) will print:

{
    "name": string,
    "age": int,
    "items": string[]
}

Example Advanced 1

@alias and @description are explained in the Deep Dive - classes and print_type section.

class Person {
    name string @alias("full_name")
    age int @description(#"
        Age in years
    "#)
    items string[]
    @description(#"
        What categories of items does this person have?
    "#)
}

print_type(Person) will print:

{
    "full_name": string,
    // Age in years
    "age": int,
    // What categories of items does this person have?
    "items": string[]
}

Example Advanced 2

enum ItemCategory {
    Food
    Clothes
    Electronics

    @@alias(Category)
}

class Item {
    name string
    category ItemCategory
}

class Person {
    name string @alias("full_name")
    age int @description(#"
        Age in years
    "#)
    items Item[]
}

print_type(Person[]) will print:

{
    "full_name": string,
    // Age in years
    "age": int,
    "items": {
            "name": string,
            "category": "Category as string"
    }[]
}[]

print_type doesn’t actually print out the values of the enum, it just prints the name of the enum. If you use enums, you should additionally use print_enum to print out the values of the enum. BAML will output a warning if you use an enum in the output and don’t use print_enum in the prompt.