An open API service indexing awesome lists of open source software.

https://github.com/elijas/baml-parser

BAML workaround to use only the parser functionality
https://github.com/elijas/baml-parser

Last synced: about 1 month ago
JSON representation

BAML workaround to use only the parser functionality

Awesome Lists containing this project

README

          

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Overview\n",
"\n",
"This project demonstrates how to use the [BAML](https://www.boundaryml.com/) parser by itself, without requiring the full BAML ecosystem. This approach is ideal if you want to experiment with BAML's parsing capabilities while maintaining your current infrastructure.\n",
"\n",
"### How It Works\n",
"\n",
"We start a local server to use as OpenAI API compatible mock API server. The local server receives requests and simply returns the same input it receives. This creates a \"pass-through\" mechanism that lets you:\n",
"\n",
"1. Continue using your current systems\n",
"1. Add BAML's parsing capabilities\n",
"1. Avoid a complete migration to the full BAML framework\n",
"\n",
"It serves as a temporary workaround for [BAML Issue #1403](https://github.com/BoundaryML/baml/issues/1403), which addresses the need for using BAML's parser independently from its other components.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Setup"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. [Install uv](https://docs.astral.sh/uv/getting-started/installation/), the python package manager\n",
"2. Start the OpenAI API compatible \"echo\" server:\n",
"\n",
"```uv run litellm --config echo_server/config.yaml --host 127.0.0.1 --port 8200```\n",
"\n",
"3. Run this notebook:\n",
"\n",
"```uv run python -m notebook README.ipynb```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Examples"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ[\"BAML_LOG\"] = \"warn\"\n",
"from baml_client import b\n",
"from baml_client.type_builder import TypeBuilder"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example: Static output model"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Food(count=2, items=['banana', 'cherry'])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# We have the class defined in the food.baml file:\n",
"#\n",
"# class Food {\n",
"# count int\n",
"# items string[]\n",
"# }\n",
"\n",
"llm_completion = \"\"\"\n",
" Of course, I can help you with that. Here is a list of fruits:\n",
" { count: 2.0,, \"items\": [banana, 'cherry'] }\n",
" Let me know if you need anything else!\n",
"\"\"\"\n",
"\n",
"b.ParseFood(llm_completion)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example: Dynamic output model"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Result(price=42.0, tags=['new!'])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tb = TypeBuilder()\n",
"tb.add_baml(\"\"\"\n",
" dynamic class Result {\n",
" price float\n",
" tags string[]\n",
" }\n",
"\"\"\")\n",
"\n",
"llm_completion = \"\"\"\n",
" Here is your answer:\n",
" { price: 42,, \"tags\": new! }\n",
" Let me know if you need anything else!\n",
"\"\"\"\n",
"\n",
"b.ParseCompletion(llm_completion, {\"tb\": tb})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Export README.ipynb to README.md"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"tags": [
"remove_cell"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook README.ipynb to markdown\n",
"[NbConvertApp] Writing 2333 bytes to README.md\n"
]
}
],
"source": [
"!uv run jupyter nbconvert --to markdown README.ipynb"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}