Skip to main content

Overview

Functions are BAML-powered LLM functions that you can:
  • Create and edit with structured inputs and outputs
  • Run evaluations against test cases
  • Re-run with different models and configurations
  • Version and publish for production use

Creating a Function

  1. Navigate to Functions in the main navigation
  2. Click Create Function
  3. Enter a name for your function (e.g., ExtractName)
  4. Define your function using BAML syntax

Example Function

class NameOutput {
  firstName string
  lastName string
}

function ExtractName(text: string) -> NameOutput {
  client "openai/gpt-4o"
  prompt #"
    Extract the first and last name from the following text:

    {{ text }}

    Return the name as JSON.
  "#
}

Function Editor

The function editor provides:
  • Syntax Highlighting: BAML syntax is highlighted for readability
  • Error Checking: Syntax errors are shown inline
  • Auto-save: Changes are saved automatically as drafts
  • Version History: View and restore previous versions

Function Tabs

Each function has multiple views:
TabDescription
EditorWrite and edit your BAML code
BehaviorsRun evaluations and manage test cases
VersionsBrowse and restore previous versions

Behaviors (Evaluations)

The Behaviors tab is where you run evaluations against your function:

Test Cases

Add test cases to evaluate your function:
  1. Go to the Behaviors tab
  2. Click Add Test Cases
  3. Add inputs as JSON or import from captures
[
  { "text": "My name is John Doe" },
  { "text": "Jane Smith is a software engineer" },
  { "text": "Dr. Robert Johnson, PhD" }
]

Running Evaluations

  1. Select test cases to run
  2. Choose which function version to evaluate
  3. Click Run
  4. Review outputs and compare with expected results

Importing from Captures

Build test cases from real production data:
  1. Go to Labeling and find relevant captures
  2. Label them with appropriate tags
  3. Import labeled captures as test cases

Versioning

Every time you save a function, a new version is created:
  • Draft: Unsaved changes (not deployed)
  • Published: The active version used by the SDK
  • Previous Versions: Historical versions you can restore

Publishing Changes

  1. Make changes in the editor
  2. Click Save to create a draft
  3. Click Publish to make the version active

Restoring a Version

  1. Open the Versions tab
  2. Find the version you want to restore
  3. Click Restore
  4. Review and publish when ready

BAML Syntax

Functions are defined using BAML. Here are the key concepts:

Classes

Define structured output types:
class Person {
  firstName string
  lastName string
  age int?  // optional field
  email string?
}

Enums

Define categorical outputs:
enum Sentiment {
  POSITIVE
  NEGATIVE
  NEUTRAL
}

Functions

Define the function signature and prompt:
function ClassifySentiment(text: string) -> Sentiment {
  client "openai/gpt-4o"
  prompt #"
    Classify the sentiment of the following text as POSITIVE, NEGATIVE, or NEUTRAL:

    {{ text }}
  "#
}

Clients

Configure which LLM provider to use:
client "openai/gpt-4o"        // OpenAI GPT-4o
client "anthropic/claude-3"    // Anthropic Claude 3
client "openai/gpt-3.5-turbo" // OpenAI GPT-3.5 Turbo

Best Practices

  • Descriptive Names: Use clear, descriptive function names like ExtractInvoiceData instead of Extract
  • Typed Outputs: Always define a class for your output to ensure type safety
  • Clear Prompts: Write clear, specific prompts with examples when needed
  • Build Evaluations: Create test cases from real captures to ensure quality
  • Version Control: Test before publishing new versions