Sarim

Search Blogs...

BAML: Basically A Made-up Language

If you have been working with LLMs and trying to extract structured data from them, you have likely encountered numerous challenges.

BAML addresses many of these issues, making it significantly easier to write your LLM logic in a more intuitive and efficient manner. The integration with VS Code is particularly impressive. One of the standout features is its support for both Python and TypeScript. As stated in their documentation, "BAML works the same in EVERY language we support. No more 'it works in Python but not in TypeScript.' The type safety is exceptionally well implemented."

But enough talk about BAML. Let me show you how to use it. Since Python is the unofficial official language for AI, let's see how to use BAML with Python.

To get started quickly, I don't think I can add anything substantial beside what is mentioned in the BAML Python setup guide.

Once you follow the setup guide, you will get a folder named 'baml_src'. This is where we will be writing our BAML code. And on each save, BAML will automatically validate your code and generate baml_client in either Python or TypeScript.

clients.baml

client<llm> MyClient {
  provider openai
  options {
    base_url "https://litellm.com"
    api_key env.MY_OPENAI_KEY
    model "gpt-3.5-turbo"
    temperature 0.1
  }
}

client<llm> MyClient2 {
  provider ollama
  options {
    base_url "https://ollama.com"
    api_key env.MY_OLLAMA_KEY
    model "llama3:8b"
    temperature 0.1
  }
}

This is your llm client which will be used to generate the data. I have been using baml with litellm and it works seamlessly.

Now, let's define the schema for the data we want to extract. List of suggestions from a given context

class Suggestion {
  title string
}

function GetSuggestions(context: string) -> Suggestion[] {
  client MyClient
  prompt #"
    {{ _.role("system") }}

    Given the context below, generate three engaging question suggestions related to context
    {{ context }}

    {{ ctx.output_format }}
  "#
}

test get_suggestions {
  functions [GetSuggestions]
  args {
    context #"Some random context."#
  }
}

You can see that we have defined a class Suggestion and a function GetSuggestions. The function GetSuggestions takes a string context as input and returns a list of Suggestion objects.

It makes so much sense to create even more complex schemas and getting much better results even with models such as gpt-3.5.

ps: BAML really stands for Basically A Made-up Language.

© 2024 Sarim Ahmed