Introduction to BAML
BAML is an innovative domain-specific language designed to facilitate the writing and testing of Language Model (LM) functions. In essence, it provides a robust framework for creating prompt templates that incorporate defined input variables and specific output types, such as classes, enums, and optional strings.
One of BAML's core strengths lies in its versatility, as it seamlessly integrates with popular programming languages like Python and TypeScript. This integration allows developers to focus more on engineering tasks and less on the intricacies of prompt crafting.
Notably, BAML has proven to outperform existing methods for obtaining structured data, even when used with sophisticated models like GPT3.5. Equipped with the capability to surpass models fine-tuned for tool usage, BAML stands out, as evidenced in the Berkeley Function Calling Benchmark results.
Key Features
- Support for Python and Typescript: BAML offers the capability to be easily incorporated into projects using these popular languages.
- Type Validation: It provides robust type validation, offering greater resilience to common LM errors compared to alternatives like Pydantic or Zod.
- Comprehensive Model Support: BAML is compatible with various models, including Ollama, OpenAI, and Anthropic, and is tested on smaller models like Llama2.
- Streaming Capabilities: It can stream structured and partial outputs.
- Realtime Prompt Previews: Developers can view full prompts dynamically, accommodating loops and conditionals.
- Testing Support: Testing functions within the playground requires just a single click.
- Resilience and Fallback Mechanisms: Features like retries and redundancy ensure reliable LM calls.
- Observability Platform: Utilize Boundary Studio to visualize functions and replay production requests effortlessly.
Getting Started with BAML
Installation
For Python:
- Use the command
pip install baml-py
to install BAML.
For TypeScript:
- Execute
npm install @boundaryml/baml
to integrate BAML with your project.
Additionally, a Visual Studio Code (VSCode) extension can be installed by searching "BAML" or visiting the extension marketplace. The extension offers syntax highlighting and real-time prompt previews for BAML files.
Starter Projects
There are several starter projects available to help you integrate BAML quickly:
Usage Examples
In Python
# Import the autogenerated BAML client
from baml_client import baml as b
from baml_client.baml_types import Resume
async def main():
resume_text = "Sample resume content here"
# Call a BAML defined function
resume = await b.ExtractResume(resume_text)
# Verify the type
assert isinstance(resume, Resume)
In TypeScript
import baml as b from "@/baml_client";
import { Resume } from "@/baml_client/types";
function getResume(resumeUrl: string): Promise<Resume> {
const resume_text = loadResume(resumeUrl);
return b.ExtractResume({ resumeText: content });
}
BAML ensures improved output parsing beyond what Pydantic or Zod can offer, promoting cleaner code and providing robust type guarantees.
BAML Ecosystem
BAML's ecosystem includes a VSCode extension for syntax assistance and a comprehensive observability platform, Boundary Studio, where developers can analyze and trace each request.
FAQ
- Why was BAML created? BAML was designed as a type-safe and declarative language for more efficient prompt scripting, such as Jinja but with additional capabilities.
- Is BAML secure? BAML-generated code interacts directly with your chosen LLMs and not through Boundary's servers unless explicitly allowed for observability purposes.
- How does BAML generate revenue? While BAML and its tools are free, income is generated through Boundary Studio, which offers advanced monitoring and feedback collection features.
BAML is a dedicated platform maximizing efficiency in writing Language Model functions, providing developers with a smooth and integrated experience for both simple and complex tasks.