DEV Community

hellovai
hellovai

Posted on

1

BAML: A new programming language for using LLMs + a VSCode Playground

All we wanted was our prompts in our codebase, not on some website. Then just to see the prompts before we ran the code. Then comments inside of prompts. Then just less strings everywhere, and more type-safety...

At some point, it became BAML: a type-safe, self-contained way to call LLMs from Python and/or TypeScript.

BAML encapsulates all the boilerplate for:

  • flexible parsing of LLM responses into your exact data model
  • streaming LLM responses as partial JSON
  • wrapping LLM calls with retries and fallback strategies

Our VSCode extension provides:

  • real-time prompt previews,
  • an LLM testing playground, and
  • syntax highlighting (of course)

Testing in vscode

Here's a short BAML snippet for extracting a resume (with syntax highlighting):

Image description

Or in code form:

// extract_resume.baml

// 1. Define the type for the output
class Resume {
  name string
  // Use an array to get multiple education histories
  education Education[]
}

// A nested class
class Education {
  university string
  start_year int
  // @description injects context into the prompt about this field
  end_year int?
  @description("unset if still in school")
}

// 2. Define the function signature
// This function takes in a single paramater
// Outputs a Resume type
function ExtractResume {
  input (resume_text: string)
  output Resume
}

// 3. Use an llm to implement ExtractResume.
// We'll name this impl 'version1'.
impl<llm, ExtractResume> version1 {
  client GPT4
  prompt #"
    Extract the resume from:
    ###
    {// This macro injects your input param //}
    {#input.resume_text}
    ###

    Output JSON Schema:
    {// This macro prints out your schema //}
    {#print_type(output)}
  "#
}
Enter fullscreen mode Exit fullscreen mode

The whole premise really just boils down to:

  • preferring to use types, not strings
  • not wanting to leave VSCode just to do some LLM testing

We also have a bunch of cool features in the works: conditionals and loops in our prompt templates, image support, and more powerful types.

We're still pretty early and would love to hear your feedback. To get started:

https://github.com/boundaryml/baml

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay