{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import stackprinter # type: ignore\n", "import jupyter_black # type: ignore\n", "from dotenv import load_dotenv # type: ignore\n", "import time\n", "\n", "from baml_agents import init_logging, with_model\n", "from baml_client.async_client import b\n", "\n", "init_logging()\n", "stackprinter.set_excepthook()\n", "load_dotenv()\n", "jupyter_black.load()\n", "\n", "b = with_model(b, \"gpt-4.1-nano\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using BAML in Jupyter notebokos\n", "\n", "## 1. Streaming outputs\n", "\n", "BAML offers a great developer experience, and I used some utilities to recreate some of the magic in Jupyter notebooks." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from baml_agents.jupyter import JupyterOutputBox\n", "\n", "with JupyterOutputBox(clear_after_finish=False) as output_box:\n", " s = \"\"\n", " for c in \"abcdefghijklmnopqrstuvwxyz\" * 3:\n", " s += c\n", " output_box.update(s)\n", " time.sleep(0.008)\n", "\n", "# The letters are generated and displayed as they are being generated." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
" \"genre\": \"Fantasy\",\n",
" \"characters\": [\n",
" {\n",
" \"name\": \"Lina\",\n",
" \"age\": 12,\n",
" \"occupation\": \"Apprentice Mage\"\n",
" },\n",
" {\n",
" \"name\": \"Eldon\",\n",
" \"age\": 45,\n",
" \"occupation\": \"Wizard\"\n",
" },\n",
" {\n",
" \"name\": \"Norik\",\n",
" \"age\": 17,\n",
" \"occupation\": \"Forest Ranger\"\n",
" }\n",
" ],\n",
" \"story_summary\": \"Lina seeks Eldon's help to unlock her magic potential; together, they face enchanted challenges in the Whispering Woods, discovering courage and friendship along the way.\"\n",
"}"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from baml_agents.jupyter import JupyterBamlCollector\n",
"\n",
"with JupyterOutputBox(clear_after_finish=False) as s:\n",
" c = JupyterBamlCollector(b, stream_callback=s.display(formatter=\"json\"))\n",
" await c.b.InteractiveBamlJupyter_WriteShortStory()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Inspecting prompts and completions"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
"\n",
" "
],
"text/plain": [
"{'genre': 'Fantasy',\n",
" 'characters': [{'name': 'Lina', 'age': 12, 'occupation': 'Apprentice Mage'}, {'name': 'Eldon', 'age': 45, 'occupation': 'Wizard'}, {'name': 'Norik', 'age': 17, 'occupation': 'Forest Ranger'}],\n",
" 'story_summary': \"Lina seeks Eldon's help to unlock her magic potential; together, they face enchanted challenges in the Whispering Woods, discovering courage and friendship along the way.\"}"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": "\n (function(){\n var el = document.getElementById(\"stream-8c1d5a3e-5935-4c61-a487-cd116b50a4de\");\n if (el) el.remove();\n })();\n ",
"text/plain": [
"