BoundaryML / baml

BAML is a language that helps you get structured data from LLMs, with the best DX possible. Works with all languages. Check out the promptfiddle.com playground
https://docs.boundaryml.com
Apache License 2.0
1.31k stars 49 forks source link

feature request: support remapping the TS string value of an enum #935

Open ekinsdrow opened 2 months ago

ekinsdrow commented 2 months ago

Description

I am working with an enum in Dart for color values, which is used to describe text blocks in a JSON schema. After parsing using BAML, the color values are returned in PascalCase instead of lower case as expected.

The enum is defined as follows:

enum Color{
  Blue @description("always 'blue' in lower case") @alias( "blue")
  BlueBackground @description("always 'blue_background' in lower case") @alias("blue_background")
  Brown @description("always 'brown' in lower case") @alias("brown")
  BrownBackground @description("always 'brown_background' in lower case") @alias("brown_background")
  Default @description("always 'default' in lower case") @alias("default")
  Gray @description("always 'gray' in lower case") @alias("gray")
  GrayBackground @description("always 'gray_background' in lower case") @alias("gray_background")
  Green @description("always 'green' in lower case") @alias("green")
  GreenBackground @description("always 'green_background' in lower case") @alias("green_background")
  Orange @description("always 'orange' in lower case") @alias("orange")
  OrangeBackground @description("always 'orange_background' in lower case") @alias("orange_background")
  Pink @description("always 'pink' in lower case") @alias("pink")
  PinkBackground @description("always 'pink_background' in lower case") @alias("pink_background")
  Purple @description("always 'purple' in lower case") @alias("purple")
  PurpleBackground @description("always 'purple_background' in lower case") @alias("purple_background")
  Red @description("always 'red' in lower case") @alias("red")
  RedBackground @description("always 'red_background' in lower case") @alias("red_background")
  Yellow @description("always 'yellow' in lower case") @alias("yellow")
  YellowBackground @description("always 'yellow_background' in lower case") @alias("yellow_background")
}

Example of the block in the LLm response

 {
      "type": "paragraph",
      "paragraph": {
        "rich_text": [
          {
            "type": "text",
            "text": {
              "content": "This is a place to track the books I've read. Use the tabs below to navigate to different categories. "
            }
          }
        ],
        "color": "default",
        "children": []
      }
    }

Response after BAML parsing

{
      "type": "paragraph",
      "paragraph": {
        "rich_text": [
          {
            "type": "text",
            "text": {
              "content": "This is a place to track the books I've read. Use the tabs below to navigate to different categories. "
            }
          }
        ],
        "color": "Default",
        "children": []
      }
    }

As seen, the color field is returned as Default instead of default, despite the description and alias specifying that it should be lower case. This forces me to apply post-processing on the client side to convert the values to lower case, which I want to avoid. How i can fix this one without post-processing on client side

sxlijin commented 2 months ago

Howdy! This isn't something we support right now, unfortunately.

@alias has no implications on the generated code; it's entirely meant to provide a way to map a given LLM string into a concrete BAML type.

We currently don't provide a mechanism for users to control what strings back a given enum - we'll look into what we can do to enable this.

ekinsdrow commented 1 month ago

Ok, thanks for the reply, in general it's not hard to fix this after receiving the reply by processing it, but it would be handy to have this feature out of the box

Got it, thanks a lot again!