To set up BAML with Ruby do the following:

1

Install BAML VSCode Extension

https://marketplace.visualstudio.com/items?itemName=boundary.baml-extension

  • syntax highlighting
  • testing playground
  • prompt previews
2

Install BAML

bundle
$bundle add baml sorbet-runtime
3

Add BAML to your existing project

This will give you some starter BAML code in a baml_src directory.

$bundle exec baml-cli init
4

Generate Ruby code from .baml files

$bundle exec baml-cli generate

` See What is baml_src to learn more about how this works.

As fun as writing BAML is, we want you be able to leverage BAML with existing ruby modules. This command gives you a ruby module that is a type-safe interface to every BAML function.

Our VSCode extension automatically runs this command when you save a BAML file.

5

Use a BAML function in Ruby!

If baml_client doesn’t exist, make sure to run the previous step!
main.rb
1require_relative "baml_client/client"
2
3def example(raw_resume)
4 # r is an instance of Baml::Types::Resume, defined in baml_client/types
5 r = Baml.Client.ExtractResume(resume: raw_resume)
6
7 puts "ExtractResume response:"
8 puts r.inspect
9end
10
11def example_stream(raw_resume)
12 stream = Baml.Client.stream.ExtractResume(resume: raw_resume)
13
14 stream.each do |msg|
15 # msg is an instance of Baml::PartialTypes::Resume
16 # defined in baml_client/partial_types
17 puts msg.inspect
18 end
19
20 stream.get_final_response
21end
22
23example 'Grace Hopper created COBOL'
24example_stream 'Grace Hopper created COBOL'