Chain of Thought Prompting

Chain of thought prompting is a technique that encourages the language model to think step by step, reasoning through the problem before providing an answer. This can improve the quality of the response and make it easier to understand.

In the below example, we use chain of thought prompting to extract information from an email.

BAML will still parse the response as an OrderInfo object, even though there is additional text in the response.

1class Email {
2 subject string
3 body string
4 from_address string
5}
6
7
8class OrderInfo {
9 order_status "ORDERED" | "SHIPPED" | "DELIVERED" | "CANCELLED"
10 tracking_number string?
11 estimated_arrival_date string?
12}
13
14function GetOrderInfo(email: Email) -> OrderInfo {
15 client GPT4o
16 prompt #"
17 Extract the info from this email in the INPUT:
18
19 INPUT:
20 -------
21 from: {{email.from_address}}
22 Email Subject: {{email.subject}}
23 Email Body: {{email.body}}
24 -------
25
26 {{ ctx.output_format }}
27
28 Before you output the JSON, please explain your
29 reasoning step-by-step. Here is an example on how to do this:
30 'If we think step by step we can see that ...
31 therefore the output JSON is:
32 {
33 ... the json schema ...
34 }'
35 "#
36}
37
38test Test1 {
39 functions [GetOrderInfo]
40 args {
41 email {
42 from_address "hello@amazon.com"
43 subject "Your Amazon.com order of 'Wood Dowel Rods...' has shipped!"
44 body #"
45 Hi Sam, your package will arrive:
46 Thurs, April 4
47 Track your package:
48 www.amazon.com/gp/your-account/ship-track?ie=23&orderId123
49
50 On the way:
51 Wood Dowel Rods...
52 Order #113-7540940
53 Ship to:
54 Sam
55 SEATTLE, WA
56
57 Shipment total:
58 $0.00
59 "#
60
61 }
62 }
63}