You can write functions to classify elements using Enums.

Here is such an example:

BAML
1enum Category {
2 Refund
3 CancelOrder @description("some description")
4 TechnicalSupport @alias("technical-help") // or alias the name
5 AccountIssue
6 Question
7}
8
9function ClassifyMessage(input: string) -> Category {
10 client GPT4Turbo
11 prompt #"
12 {#
13 This automatically injects good instructions
14 for classification since BAML knows
15 Category is an enum.
16 #}
17 {{ ctx.output_format }}
18
19 {{ _.role('user') }}
20 {{ input }}
21
22 {{ _.role('assistant') }}
23 Response:
24 "#
25}

If you use BAML Playground, you can see what we inject into the prompt, with full transparency.

The neat part about BAML is that you don’t need to parse the enums out of the answer yourself. It will just work. BAML’s fuzzy parsing detects when the LLM prints something like:

Based on the information provided, I think the answer is Refund

and will give you the actual Category.Refund when you call the function. We will add more knobs so you can make this parsing more or less strict.

Usage

1from baml_client import b
2from baml_client.types import Category
3
4...
5 result = await b.ClassifyMessage("I want to cancel my order")
6 assert result == Category.CancelOrder

Handling Dynamic Categories (e.g. user-provided, or from a database)

To handle dynamic categories you can use dynamic enums to build your enum at runtime.