%22%22%22A%20marimo%20notebook%20that%20sends%20the%20same%20request%20to%20different%20LLM%20providers%20with%20the%20%60%60model%60%60%20argument.%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20os%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20from%20interpret.glassbox%20import%20ExplainableBoostingClassifier%0A%20%20%20%20from%20sklearn.datasets%20import%20fetch_openml%0A%0A%20%20%20%20from%20ebaml%20import%20(%0A%20%20%20%20%20%20%20%20DescribeGraphRequest%2C%0A%20%20%20%20%20%20%20%20InvalidOptionError%2C%0A%20%20%20%20%20%20%20%20LLMRequestError%2C%0A%20%20%20%20%20%20%20%20MissingAPIKeyError%2C%0A%20%20%20%20%20%20%20%20ModelOptions%2C%0A%20%20%20%20%20%20%20%20describe_graph%2C%0A%20%20%20%20)%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20DescribeGraphRequest%2C%0A%20%20%20%20%20%20%20%20ExplainableBoostingClassifier%2C%0A%20%20%20%20%20%20%20%20InvalidOptionError%2C%0A%20%20%20%20%20%20%20%20LLMRequestError%2C%0A%20%20%20%20%20%20%20%20MissingAPIKeyError%2C%0A%20%20%20%20%20%20%20%20ModelOptions%2C%0A%20%20%20%20%20%20%20%20describe_graph%2C%0A%20%20%20%20%20%20%20%20fetch_openml%2C%0A%20%20%20%20%20%20%20%20mo%2C%0A%20%20%20%20%20%20%20%20os%2C%0A%20%20%20%20%20%20%20%20pl%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Choose%20the%20model%0A%0A%20%20%20%20Every%20function%20of%20EBaMl%20that%20calls%20an%20LLM%20takes%20a%20%60model%60%20argument.%20This%20notebook%20sends%20the%20same%20request%20to%0A%20%20%20%20several%20providers%2C%20so%20that%20you%20can%20compare%20the%20setup%20of%20each%20one%20and%20the%20text%20that%20each%20one%20writes.%0A%0A%20%20%20%20-%20Everything%20outside%20the%20buttons%20runs%20without%20a%20key%20and%20sends%20no%20request.%0A%20%20%20%20-%20A%20button%20sends%20real%20requests%20(three%20turns%20for%20one%20graph)%20to%20the%20provider%20of%20its%20section.%20Press%20it%20only%20for%20a%0A%20%20%20%20%20%20provider%20that%20you%20have%20set%20up.%20A%20section%20shows%20the%20key%20that%20it%20needs%2C%20and%20whether%20the%20environment%20has%20it.%0A%20%20%20%20-%20The%20key%20never%20goes%20into%20%60ModelOptions%60.%20The%20options%20hold%20the%20*name*%20of%20the%20environment%20variable%2C%20and%20EBaMl%20reads%0A%20%20%20%20%20%20the%20variable%20when%20it%20sends%20a%20request.%20To%20set%20a%20key%2C%20start%20the%20notebook%20from%20a%20shell%20that%20has%20the%20variable.%0A%0A%20%20%20%20The%20documentation%20page%20%22Choosing%20the%20model%22%20lists%20all%20providers%20and%20options.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20A%20small%20EBM%20to%20talk%20about%0A%0A%20%20%20%20The%20Titanic%20data%20and%20one%20feature%2C%20%60sex%60%2C%20whose%20graph%20has%20two%20bins.%20A%20short%20graph%20keeps%20each%20request%20cheap.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ExplainableBoostingClassifier%2C%20fetch_openml%2C%20pl)%3A%0A%20%20%20%20%23%20The%20liac-arff%20parser%20reads%20the%20categories%20as%20text.%20The%20pandas%20parser%20of%20scikit-learn%201.9%20fails%20on%20pandas%203.1.%0A%20%20%20%20titanic%20%3D%20pl.from_pandas(fetch_openml(%22titanic%22%2C%20version%3D1%2C%20as_frame%3DTrue%2C%20parser%3D%22liac-arff%22).frame)%0A%20%20%20%20features%20%3D%20titanic.select(%22pclass%22%2C%20%22sex%22%2C%20%22age%22%2C%20%22sibsp%22%2C%20%22parch%22%2C%20%22fare%22%2C%20%22embarked%22).with_columns(%0A%20%20%20%20%20%20%20%20pl.col(%22sex%22%2C%20%22embarked%22).cast(pl.String)%2C%0A%20%20%20%20)%0A%20%20%20%20survived%20%3D%20titanic%5B%22survived%22%5D.cast(pl.String).cast(pl.Int8)%0A%20%20%20%20%23%20no%20pairwise%20interactions%3A%20the%20LLM%20reads%20one%20graph%20per%20feature%0A%20%20%20%20ebm%20%3D%20ExplainableBoostingClassifier(feature_names%3Dfeatures.columns%2C%20interactions%3D0%2C%20random_state%3D42)%0A%20%20%20%20ebm.fit(features%2C%20survived)%0A%20%20%20%20sex_index%20%3D%20features.columns.index(%22sex%22)%0A%20%20%20%20return%20ebm%2C%20sex_index%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20os)%3A%0A%20%20%20%20def%20key_status(variable%3A%20str%20%7C%20None)%20-%3E%20mo.Html%3A%0A%20%20%20%20%20%20%20%20%22%22%22Show%20whether%20the%20environment%20has%20the%20key%20of%20a%20provider%2C%20without%20showing%20the%20key.%0A%0A%20%20%20%20%20%20%20%20Returns%0A%20%20%20%20%20%20%20%20-------%0A%20%20%20%20%20%20%20%20mo.Html%0A%20%20%20%20%20%20%20%20%20%20%20%20A%20callout%20with%20the%20status.%0A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20if%20variable%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(%22This%20provider%20needs%20no%20key.%22%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20if%20os.environ.get(variable)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(f%22%60%7Bvariable%7D%60%20is%20set.%22%2C%20kind%3D%22success%22)%0A%20%20%20%20%20%20%20%20return%20mo.callout(f%22%60%7Bvariable%7D%60%20is%20not%20set.%20Start%20the%20notebook%20from%20a%20shell%20that%20has%20it.%22%2C%20kind%3D%22warn%22)%0A%0A%20%20%20%20return%20(key_status%2C)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20DescribeGraphRequest%2C%0A%20%20%20%20LLMRequestError%2C%0A%20%20%20%20describe_graph%2C%0A%20%20%20%20ebm%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20sex_index%2C%0A)%3A%0A%20%20%20%20def%20ask(model)%20-%3E%20mo.Html%3A%0A%20%20%20%20%20%20%20%20%22%22%22Describe%20the%20%60sex%60%20graph%20with%20the%20model%2C%20or%20show%20the%20fix%20steps%20of%20a%20failed%20request.%0A%0A%20%20%20%20%20%20%20%20Returns%0A%20%20%20%20%20%20%20%20-------%0A%20%20%20%20%20%20%20%20mo.Html%0A%20%20%20%20%20%20%20%20%20%20%20%20The%20description%2C%20or%20a%20callout%20with%20the%20error.%0A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20with%20mo.status.spinner(%22Asking%20the%20LLM%20(three%20turns)...%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20description%20%3D%20describe_graph(DescribeGraphRequest(ebm%2C%20sex_index%2C%20num_sentences%3D3%2C%20model%3Dmodel))%0A%20%20%20%20%20%20%20%20except%20LLMRequestError%20as%20error%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20the%20message%20names%20the%20cause%20and%20lists%20the%20steps%20that%20fix%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22**The%20request%20failed.**%5Cn%5Cn%7Berror%7D%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20%20%20%20%20return%20mo.md(description)%0A%0A%20%20%20%20return%20(ask%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20shorthand%20and%20%60ModelOptions%60%0A%0A%20%20%20%20The%20shorthand%20%60%22provider%2Fmodel%22%60%20selects%20a%20provider%20and%20a%20model.%20It%20splits%20at%20the%20first%20%60%2F%60%20only%2C%20so%0A%20%20%20%20%60%22openrouter%2Fopenai%2Fgpt-4o-mini%22%60%20is%20the%20provider%20%60openrouter%60%20with%20the%20model%20%60openai%2Fgpt-4o-mini%60.%20%60ModelOptions%60%0A%20%20%20%20sets%20more%3A%20the%20temperature%2C%20the%20output%20limit%2C%20the%20address%20of%20the%20server%20and%20extra%20headers.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ModelOptions%2C%20mo)%3A%0A%20%20%20%20shorthand%20%3D%20ModelOptions.parse(%22openrouter%2Fopenai%2Fgpt-4o-mini%22)%0A%20%20%20%20tuned%20%3D%20ModelOptions(provider%3D%22anthropic%22%2C%20model%3D%22claude-haiku-4-5%22%2C%20temperature%3D0.2%2C%20max_output_tokens%3D8000)%0A%20%20%20%20mo.md(f%22%60%60%60%5Cn%7Bshorthand!r%7D%5Cn%5Cn%7Btuned!r%7D%5Cn%60%60%60%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20OpenRouter%0A%0A%20%20%20%20One%20key%20reaches%20many%20models.%20The%20free%20router%20%60openrouter%2Ffree%60%20is%20the%20default%20of%20EBaMl%2C%20and%20it%20picks%20a%20new%20model%20for%0A%20%20%20%20each%20request.%20A%20pinned%20model%20gives%20repeatable%20results.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(key_status%2C%20mo)%3A%0A%20%20%20%20openrouter_button%20%3D%20mo.ui.run_button(label%3D%22Describe%20with%20OpenRouter%22)%0A%20%20%20%20mo.vstack(%5Bkey_status(%22OPENROUTER_API_KEY%22)%2C%20openrouter_button%5D)%0A%20%20%20%20return%20(openrouter_button%2C)%0A%0A%0A%40app.cell%0Adef%20_(ask%2C%20mo%2C%20openrouter_button)%3A%0A%20%20%20%20mo.stop(not%20openrouter_button.value%2C%20mo.md(%22Press%20the%20button%20to%20send%20the%20request.%22))%0A%20%20%20%20ask(%22openrouter%2Fopenai%2Fgpt-4o-mini%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20OpenAI%0A%0A%20%20%20%20The%20same%20call%2C%20with%20the%20provider%20%60openai%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(key_status%2C%20mo)%3A%0A%20%20%20%20openai_button%20%3D%20mo.ui.run_button(label%3D%22Describe%20with%20OpenAI%22)%0A%20%20%20%20mo.vstack(%5Bkey_status(%22OPENAI_API_KEY%22)%2C%20openai_button%5D)%0A%20%20%20%20return%20(openai_button%2C)%0A%0A%0A%40app.cell%0Adef%20_(ask%2C%20mo%2C%20openai_button)%3A%0A%20%20%20%20mo.stop(not%20openai_button.value%2C%20mo.md(%22Press%20the%20button%20to%20send%20the%20request.%22))%0A%20%20%20%20ask(%22openai%2Fgpt-4o-mini%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Anthropic%0A%0A%20%20%20%20This%20section%20uses%20%60ModelOptions%60%20to%20set%20the%20temperature%20and%20the%20output%20limit.%20A%20reasoning%20model%20counts%20its%20hidden%0A%20%20%20%20tokens%20in%20%60max_output_tokens%60%2C%20so%20keep%20the%20limit%20large.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(key_status%2C%20mo)%3A%0A%20%20%20%20anthropic_button%20%3D%20mo.ui.run_button(label%3D%22Describe%20with%20Anthropic%22)%0A%20%20%20%20mo.vstack(%5Bkey_status(%22ANTHROPIC_API_KEY%22)%2C%20anthropic_button%5D)%0A%20%20%20%20return%20(anthropic_button%2C)%0A%0A%0A%40app.cell%0Adef%20_(ModelOptions%2C%20anthropic_button%2C%20ask%2C%20mo)%3A%0A%20%20%20%20mo.stop(not%20anthropic_button.value%2C%20mo.md(%22Press%20the%20button%20to%20send%20the%20request.%22))%0A%20%20%20%20ask(ModelOptions(provider%3D%22anthropic%22%2C%20model%3D%22claude-haiku-4-5%22%2C%20temperature%3D0.2%2C%20max_output_tokens%3D8000))%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Ollama%2C%20a%20local%20model%0A%0A%20%20%20%20The%20model%20runs%20on%20your%20machine%20and%20needs%20no%20key.%20Install%20%5BOllama%5D(https%3A%2F%2Follama.com)%2C%20then%20run%0A%20%20%20%20%60ollama%20pull%20llama3.1%60%20and%20keep%20the%20server%20running.%20%60base_url%60%20defaults%20to%20%60http%3A%2F%2Flocalhost%3A11434%2Fv1%60.%20If%20the%0A%20%20%20%20server%20is%20not%20running%2C%20the%20section%20shows%20the%20error%20with%20its%20fix%20steps.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(key_status%2C%20mo)%3A%0A%20%20%20%20ollama_button%20%3D%20mo.ui.run_button(label%3D%22Describe%20with%20Ollama%22)%0A%20%20%20%20mo.vstack(%5Bkey_status(None)%2C%20ollama_button%5D)%0A%20%20%20%20return%20(ollama_button%2C)%0A%0A%0A%40app.cell%0Adef%20_(ask%2C%20mo%2C%20ollama_button)%3A%0A%20%20%20%20mo.stop(not%20ollama_button.value%2C%20mo.md(%22Press%20the%20button%20to%20send%20the%20request.%22))%0A%20%20%20%20ask(%22ollama%2Fllama3.1%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20A%20gateway%20with%20an%20OpenAI-compatible%20API%0A%0A%20%20%20%20The%20provider%20%60openai-generic%60%20talks%20to%20any%20server%20that%20follows%20the%20OpenAI%20API%3A%20a%20company%20gateway%2C%20vLLM%2C%20LM%20Studio.%0A%20%20%20%20It%20needs%20%60base_url%60.%20%60api_key_env%60%20names%20the%20variable%20that%20holds%20the%20key%20of%20the%20gateway%2C%20and%20%60headers%60%20adds%20headers%0A%20%20%20%20to%20each%20request.%20Fill%20the%20fields%2C%20then%20press%20the%20button.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20gateway_url%20%3D%20mo.ui.text(value%3D%22https%3A%2F%2Fllm.example.com%2Fv1%22%2C%20label%3D%22base_url%22%2C%20full_width%3DTrue)%0A%20%20%20%20gateway_model%20%3D%20mo.ui.text(value%3D%22my-model%22%2C%20label%3D%22model%22)%0A%20%20%20%20gateway_key_env%20%3D%20mo.ui.text(value%3D%22MY_GATEWAY_KEY%22%2C%20label%3D%22api_key_env%22)%0A%20%20%20%20gateway_button%20%3D%20mo.ui.run_button(label%3D%22Describe%20with%20the%20gateway%22)%0A%20%20%20%20mo.vstack(%5Bgateway_url%2C%20mo.hstack(%5Bgateway_model%2C%20gateway_key_env%5D%2C%20justify%3D%22start%22)%2C%20gateway_button%5D)%0A%20%20%20%20return%20gateway_button%2C%20gateway_key_env%2C%20gateway_model%2C%20gateway_url%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20ModelOptions%2C%0A%20%20%20%20gateway_key_env%2C%0A%20%20%20%20gateway_model%2C%0A%20%20%20%20gateway_url%2C%0A%20%20%20%20key_status%2C%0A%20%20%20%20mo%2C%0A)%3A%0A%20%20%20%20gateway%20%3D%20ModelOptions(%0A%20%20%20%20%20%20%20%20provider%3D%22openai-generic%22%2C%0A%20%20%20%20%20%20%20%20model%3Dgateway_model.value%2C%0A%20%20%20%20%20%20%20%20base_url%3Dgateway_url.value%2C%0A%20%20%20%20%20%20%20%20api_key_env%3Dgateway_key_env.value%20or%20None%2C%0A%20%20%20%20%20%20%20%20headers%3D%7B%22x-team%22%3A%20%22ebm%22%7D%2C%0A%20%20%20%20)%0A%20%20%20%20mo.vstack(%5Bkey_status(gateway.api_key_env)%2C%20mo.md(f%22%60%60%60%5Cn%7Bgateway!r%7D%5Cn%60%60%60%22)%5D)%0A%20%20%20%20return%20(gateway%2C)%0A%0A%0A%40app.cell%0Adef%20_(ask%2C%20gateway%2C%20gateway_button%2C%20mo)%3A%0A%20%20%20%20mo.stop(not%20gateway_button.value%2C%20mo.md(%22Press%20the%20button%20to%20send%20the%20request.%22))%0A%20%20%20%20ask(gateway)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Other%20providers%0A%0A%20%20%20%20%60google%60%2C%20%60azure%60%2C%20%60vertex%60%20and%20%60bedrock%60%20work%20the%20same%20way.%20Their%20keys%20and%20settings%20differ%3A%20the%20table%20in%20the%0A%20%20%20%20documentation%20page%20%22Choosing%20the%20model%22%20lists%20them.%0A%0A%20%20%20%20%23%23%20When%20the%20setup%20is%20wrong%0A%0A%20%20%20%20EBaMl%20checks%20the%20provider%20before%20the%20first%20paid%20request%2C%20and%20it%20turns%20the%20failures%20that%20you%20can%20fix%20into%20errors%0A%20%20%20%20with%20fix%20steps.%20The%20two%20cells%20below%20send%20no%20request.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20DescribeGraphRequest%2C%0A%20%20%20%20InvalidOptionError%2C%0A%20%20%20%20describe_graph%2C%0A%20%20%20%20ebm%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20sex_index%2C%0A)%3A%0A%20%20%20%20provider_failure%20%3D%20None%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20describe_graph(DescribeGraphRequest(ebm%2C%20sex_index%2C%20model%3D%22not-a-provider%2Fsome-model%22))%0A%20%20%20%20except%20InvalidOptionError%20as%20error%3A%0A%20%20%20%20%20%20%20%20provider_failure%20%3D%20mo.callout(mo.md(f%22%60InvalidOptionError%60%3A%20%7Berror%7D%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20provider_failure%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20DescribeGraphRequest%2C%0A%20%20%20%20MissingAPIKeyError%2C%0A%20%20%20%20ModelOptions%2C%0A%20%20%20%20describe_graph%2C%0A%20%20%20%20ebm%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20sex_index%2C%0A)%3A%0A%20%20%20%20%23%20the%20variable%20name%20is%20made%20up%2C%20so%20the%20environment%20cannot%20have%20it%0A%20%20%20%20unset%20%3D%20ModelOptions(provider%3D%22openai%22%2C%20model%3D%22gpt-4o-mini%22%2C%20api_key_env%3D%22EBAML_NOTEBOOK_UNSET_KEY%22)%0A%20%20%20%20key_failure%20%3D%20None%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20describe_graph(DescribeGraphRequest(ebm%2C%20sex_index%2C%20model%3Dunset))%0A%20%20%20%20except%20MissingAPIKeyError%20as%20error%3A%0A%20%20%20%20%20%20%20%20key_failure%20%3D%20mo.callout(mo.md(f%22%60MissingAPIKeyError%60%3A%20%7Berror%7D%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20key_failure%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
1ae8ca3dc5f866914ae710caaf196c34