%22%22%22A%20marimo%20notebook%20that%20chats%20with%20an%20agent%20that%20explains%20an%20EBM%2C%20through%20the%20real%20%60%60ebaml-chat%60%60%20command.%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%20atexit%0A%20%20%20%20import%20os%0A%20%20%20%20import%20select%0A%20%20%20%20import%20subprocess%20%20%23%20ruff%3A%20ignore%5Bsuspicious-subprocess-import%5D%20--%20the%20notebook%20runs%20the%20ebaml-chat%20command%2C%20which%20is%20the%20interface%20that%20it%20shows%0A%20%20%20%20import%20sys%0A%20%20%20%20import%20tempfile%0A%20%20%20%20import%20threading%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20import%20skops.io%0A%20%20%20%20from%20interpret.glassbox%20import%20ExplainableBoostingClassifier%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20ExplainableBoostingClassifier%2C%0A%20%20%20%20%20%20%20%20Path%2C%0A%20%20%20%20%20%20%20%20atexit%2C%0A%20%20%20%20%20%20%20%20mo%2C%0A%20%20%20%20%20%20%20%20np%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%20%20%20%20select%2C%0A%20%20%20%20%20%20%20%20skops%2C%0A%20%20%20%20%20%20%20%20subprocess%2C%0A%20%20%20%20%20%20%20%20sys%2C%0A%20%20%20%20%20%20%20%20tempfile%2C%0A%20%20%20%20%20%20%20%20threading%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%20Chat%20with%20an%20EBM%0A%0A%20%20%20%20%60ebaml-chat%60%20opens%20a%20chat%20with%20an%20agent%20that%20explains%20a%20fitted%20EBM.%20You%20ask%20about%20the%20model%2C%20or%20about%20one%20row%20of%0A%20%20%20%20your%20data%2C%20and%20you%20ask%20follow-up%20questions.%20The%20agent%20is%20a%20language%20model%20that%20reads%20the%20model%20through%20the%20tools%20of%0A%20%20%20%20the%20%5BMCP%20server%5D(mcp-notebook.md).%20This%20notebook%20runs%20the%20real%20%60ebaml-chat%60%20command%20and%20puts%20a%20chat%20box%20in%20front%0A%20%20%20%20of%20it.%20You%20learn%20what%20to%20ask%2C%20how%20the%20agent%20answers%2C%20and%20how%20to%20check%20an%20answer.%0A%0A%20%20%20%20-%20You%20need%20no%20data%20download.%20The%20notebook%20fits%20a%20small%20EBM%20on%20made-up%20data.%0A%20%20%20%20-%20The%20notebook%20starts%20the%20chat%20without%20a%20key.%20Each%20message%20that%20you%20send%20needs%20an%20LLM%2C%20so%20it%20needs%0A%20%20%20%20%20%20%60OPENROUTER_API_KEY%60%2C%20or%20a%20key%20that%20you%20type%20below.%0A%20%20%20%20-%20The%20notebook%20needs%20the%20BAML%20toolchain%20(%60baml%60%2C%20version%200.25%20or%20later)%20and%20the%20%60mcp%60%20extra%3A%0A%20%20%20%20%20%20%60pip%20install%20%22ebaml%5Bmcp%5D%22%60.%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%20Fit%20and%20save%20a%20model%0A%0A%20%20%20%20The%20chat%20reads%20a%20saved%20model%20and%2C%20if%20you%20want%20to%20ask%20about%20rows%2C%20a%20CSV%20file%20with%20the%20rows.%20The%20notebook%20fits%20a%0A%20%20%20%20binary%20classifier%20on%20three%20features%20and%20saves%20it%20with%20%60skops.io%60.%20It%20has%20no%20interaction%20terms%2C%20because%20the%0A%20%20%20%20explain%20tool%20works%20for%20a%20model%20without%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ExplainableBoostingClassifier%2C%20Path%2C%20np%2C%20pl%2C%20skops%2C%20tempfile)%3A%0A%20%20%20%20workdir%20%3D%20Path(tempfile.mkdtemp(prefix%3D%22ebaml-chat-%22))%0A%20%20%20%20random_generator%20%3D%20np.random.default_rng(0)%0A%20%20%20%20people%20%3D%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22age%22%3A%20random_generator.integers(18%2C%2080%2C%20500)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22income%22%3A%20random_generator.normal(50%2C%2015%2C%20500).round(1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22city%22%3A%20random_generator.choice(%5B%22north%22%2C%20%22south%22%5D%2C%20500)%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20)%0A%20%20%20%20score%20%3D%20(%0A%20%20%20%20%20%20%20%20(people%5B%22age%22%5D.to_numpy()%20-%2040)%20%2F%2020%0A%20%20%20%20%20%20%20%20%2B%20(people%5B%22income%22%5D.to_numpy()%20-%2050)%20%2F%2015%0A%20%20%20%20%20%20%20%20%2B%20(people%5B%22city%22%5D.to_numpy()%20%3D%3D%20%22north%22)%0A%20%20%20%20%20%20%20%20%2B%20random_generator.normal(size%3D500)%0A%20%20%20%20)%0A%20%20%20%20approval_threshold%20%3D%200.5%0A%20%20%20%20approved%20%3D%20(score%20%3E%20approval_threshold).astype(int)%0A%20%20%20%20classifier%20%3D%20ExplainableBoostingClassifier(feature_names%3Dpeople.columns%2C%20interactions%3D0%2C%20random_state%3D0)%0A%20%20%20%20classifier.fit(people%2C%20approved)%0A%20%20%20%20model_file%20%3D%20workdir%20%2F%20%22model.skops%22%0A%20%20%20%20data_file%20%3D%20workdir%20%2F%20%22train.parquet%22%0A%20%20%20%20skops.io.dump(classifier%2C%20model_file)%0A%20%20%20%20people.write_parquet(data_file)%0A%20%20%20%20return%20data_file%2C%20model_file%0A%0A%0A%40app.cell%0Adef%20_(data_file%2C%20mo%2C%20model_file)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%20The%20command%0A%0A%20%20%20%20In%20a%20terminal%2C%20the%20same%20chat%20starts%20with%20one%20command.%20The%20model%20and%20the%20data%20are%20the%20files%20of%20this%20notebook%3A%0A%0A%20%20%20%20%60%60%60bash%0A%20%20%20%20ebaml-chat%20--model%20%7Bmodel_file%7D%20--data%20%7Bdata_file%7D%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Add%20%60--llm%20PROVIDER%2FMODEL%60%20to%20choose%20another%20model%20(see%20%22Choosing%20the%20model%22%20in%20the%20documentation)%2C%20and%0A%20%20%20%20%60--max-steps%20NUMBER%60%20to%20limit%20the%20rounds%20of%20tool%20calls%20for%20one%20answer.%20An%20empty%20line%20ends%20the%20chat.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20os)%3A%0A%20%20%20%20key_from_env%20%3D%20bool(os.environ.get(%22OPENROUTER_API_KEY%22))%0A%20%20%20%20api_key_input%20%3D%20None%20if%20key_from_env%20else%20mo.ui.text(kind%3D%22password%22%2C%20label%3D%22OpenRouter%20API%20key%22%2C%20full_width%3DTrue)%0A%20%20%20%20key_status%20%3D%20(%0A%20%20%20%20%20%20%20%20mo.callout(%22Loaded%20%60OPENROUTER_API_KEY%60%20from%20the%20environment.%22%2C%20kind%3D%22success%22)%0A%20%20%20%20%20%20%20%20if%20key_from_env%0A%20%20%20%20%20%20%20%20else%20mo.vstack(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mo.callout(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%60OPENROUTER_API_KEY%60%20is%20not%20set.%20You%20can%20start%20the%20chat%2C%20but%20each%20message%20shows%20the%20error%20of%20a%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22missing%20key.%20Type%20a%20key%20and%20press%20Enter%20to%20restart%20the%20chat%20with%20it.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20kind%3D%22warn%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20api_key_input%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20key_status%0A%20%20%20%20return%20(api_key_input%2C)%0A%0A%0A%40app.cell%0Adef%20_(api_key_input%2C%20os)%3A%0A%20%20%20%20%23%20a%20typed%20key%20lives%20in%20this%20process%20only%2C%20and%20the%20chat%20that%20the%20notebook%20starts%20inherits%20it%0A%20%20%20%20api_key%20%3D%20(%0A%20%20%20%20%20%20%20%20api_key_input.value%0A%20%20%20%20%20%20%20%20if%20api_key_input%20is%20not%20None%20and%20api_key_input.value%0A%20%20%20%20%20%20%20%20else%20os.environ.get(%22OPENROUTER_API_KEY%22%2C%20%22%22)%0A%20%20%20%20)%0A%20%20%20%20return%20(api_key%2C)%0A%0A%0A%40app.cell%0Adef%20_(atexit%2C%20os%2C%20select)%3A%0A%20%20%20%20prompt_marker%20%3D%20b%22you%3E%20%22%0A%20%20%20%20reply_timeout_seconds%20%3D%20300%0A%0A%20%20%20%20def%20read_until_prompt(process%2C%20timeout%3A%20float%20%3D%20reply_timeout_seconds)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20%22%22%22Read%20the%20output%20of%20the%20chat%20up%20to%20its%20next%20prompt.%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%20str%0A%20%20%20%20%20%20%20%20%20%20%20%20The%20text%20before%20the%20prompt.%20A%20chat%20that%20ends%20before%20the%20prompt%20gives%20the%20text%20that%20it%20wrote.%0A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20buffer%20%3D%20b%22%22%0A%20%20%20%20%20%20%20%20while%20not%20buffer.endswith(prompt_marker)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ready%2C%20_%2C%20_%20%3D%20select.select(%5Bprocess.stdout%5D%2C%20%5B%5D%2C%20%5B%5D%2C%20timeout)%0A%20%20%20%20%20%20%20%20%20%20%20%20chunk%20%3D%20os.read(process.stdout.fileno()%2C%204096)%20if%20ready%20else%20b%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20not%20chunk%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20%20%20%20%20buffer%20%2B%3D%20chunk%0A%20%20%20%20%20%20%20%20return%20buffer.removesuffix(prompt_marker).decode(errors%3D%22replace%22).strip()%0A%0A%20%20%20%20def%20stop_chat(process)%20-%3E%20None%3A%0A%20%20%20%20%20%20%20%20%22%22%22Stop%20a%20chat%20process%2C%20and%20ignore%20one%20that%20has%20already%20ended.%22%22%22%0A%20%20%20%20%20%20%20%20if%20process.poll()%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20process.terminate()%0A%0A%20%20%20%20def%20register_stop(process)%20-%3E%20None%3A%0A%20%20%20%20%20%20%20%20%22%22%22Stop%20the%20chat%20process%20when%20the%20notebook%20ends.%22%22%22%0A%20%20%20%20%20%20%20%20atexit.register(stop_chat%2C%20process)%0A%0A%20%20%20%20return%20read_until_prompt%2C%20register_stop%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20api_key%2C%0A%20%20%20%20data_file%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20model_file%2C%0A%20%20%20%20os%2C%0A%20%20%20%20read_until_prompt%2C%0A%20%20%20%20register_stop%2C%0A%20%20%20%20subprocess%2C%0A%20%20%20%20sys%2C%0A%20%20%20%20threading%2C%0A)%3A%0A%20%20%20%20%23%20the%20command%20of%20the%20terminal%2C%20started%20with%20the%20Python%20of%20this%20notebook%0A%20%20%20%20chat_environment%20%3D%20%7B**os.environ%2C%20%22OPENROUTER_API_KEY%22%3A%20api_key%7D%20if%20api_key%20else%20dict(os.environ)%0A%20%20%20%20chat_process%20%3D%20subprocess.Popen(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20sys.executable%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22-c%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22from%20ebaml.mcp.chat%20import%20main%3B%20main()%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22--model%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20str(model_file)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22--data%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20str(data_file)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20stdin%3Dsubprocess.PIPE%2C%0A%20%20%20%20%20%20%20%20stdout%3Dsubprocess.PIPE%2C%0A%20%20%20%20%20%20%20%20stderr%3Dsubprocess.PIPE%2C%0A%20%20%20%20%20%20%20%20env%3Dchat_environment%2C%0A%20%20%20%20)%0A%20%20%20%20register_stop(chat_process)%0A%20%20%20%20error_lines%3A%20list%5Bstr%5D%20%3D%20%5B%5D%0A%0A%20%20%20%20def%20collect_errors()%20-%3E%20None%3A%0A%20%20%20%20%20%20%20%20%22%22%22Keep%20each%20line%20that%20the%20chat%20writes%20to%20its%20error%20stream%2C%20for%20the%20tool%20report%20of%20the%20next%20answer.%22%22%22%0A%20%20%20%20%20%20%20%20error_lines.extend(raw_line.decode(errors%3D%22replace%22).rstrip()%20for%20raw_line%20in%20chat_process.stderr)%0A%0A%20%20%20%20threading.Thread(target%3Dcollect_errors%2C%20daemon%3DTrue).start()%0A%20%20%20%20greeting%20%3D%20read_until_prompt(chat_process%2C%20timeout%3D120)%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22The%20chat%20started%20its%20server%20and%20wrote%20its%20first%20line%2C%20with%20no%20request%20to%20an%20LLM%3A%5Cn%5Cn%60%60%60text%5Cn%7Bgreeting%7D%5Cn%60%60%60%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20chat_process%2C%20error_lines%0A%0A%0A%40app.cell%0Adef%20_(chat_process%2C%20error_lines%3A%20list%5Bstr%5D%2C%20mo%2C%20read_until_prompt)%3A%0A%20%20%20%20def%20answer(messages%2C%20_config)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20%22%22%22Send%20the%20last%20message%20of%20the%20chat%20box%20to%20%60%60ebaml-chat%60%60%20and%20give%20its%20answer%2C%20with%20the%20tools%20that%20it%20called.%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%20str%0A%20%20%20%20%20%20%20%20%20%20%20%20The%20answer%20in%20Markdown.%0A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20error_lines.clear()%0A%20%20%20%20%20%20%20%20question%20%3D%20%22%20%22.join(str(messages%5B-1%5D.content).split())%0A%20%20%20%20%20%20%20%20chat_process.stdin.write(question.encode()%20%2B%20b%22%5Cn%22)%0A%20%20%20%20%20%20%20%20chat_process.stdin.flush()%0A%20%20%20%20%20%20%20%20reply%20%3D%20read_until_prompt(chat_process)%0A%20%20%20%20%20%20%20%20tools%20%3D%20%5Bline.removeprefix(%22(tools%3A%20%22).removesuffix(%22)%22)%20for%20line%20in%20error_lines%20if%20line.startswith(%22(tools%3A%20%22)%5D%0A%20%20%20%20%20%20%20%20return%20reply%20%2B%20(f%22%5Cn%5Cn*Tools%20called%3A%20%7Btools%5B-1%5D%7D*%22%20if%20tools%20else%20%22%22)%0A%0A%20%20%20%20chat_box%20%3D%20mo.ui.chat(%0A%20%20%20%20%20%20%20%20answer%2C%0A%20%20%20%20%20%20%20%20prompts%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Which%20feature%20matters%20most%2C%20and%20how%20does%20it%20change%20the%20prediction%3F%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Explain%20the%20prediction%20for%20row%203%20of%20the%20data.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22What%20would%20flip%20it%3F%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20max_height%3D600%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(chat_box%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%20Ask%0A%0A%20%20%20%20The%20chat%20box%20below%20talks%20to%20the%20command%20above.%20**Each%20message%20sends%20real%20requests**%20to%20the%20LLM%2C%20one%20for%20each%0A%20%20%20%20round%20of%20tool%20calls.%20A%20message%20about%20one%20row%20usually%20takes%20two%20or%20three%20requests.%20The%20default%20model%20is%20the%20free%0A%20%20%20%20model%20of%20OpenRouter%2C%20so%20the%20cost%20is%20zero%2C%20but%20it%20can%20be%20slow%20or%20rate%20limited.%0A%0A%20%20%20%20Try%20the%20three%20questions%20in%20this%20order.%20The%20second%20one%20explains%20a%20row.%20The%20third%20one%20is%20a%20follow-up%3A%20it%20has%20no%0A%20%20%20%20row%20in%20it%2C%20so%20the%20agent%20must%20use%20the%20earlier%20messages.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(chat_box)%3A%0A%20%20%20%20chat_box%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%20Check%20the%20answers%0A%0A%20%20%20%20-%20The%20line%20*Tools%20called*%20shows%20the%20evidence.%20A%20question%20about%20a%20row%20calls%20%60explain_prediction%60.%20A%20question%20about%20a%0A%20%20%20%20%20%20feature%20calls%20%60get_graph%60.%20An%20answer%20with%20no%20tool%20call%20rests%20on%20the%20earlier%20messages%20only.%0A%20%20%20%20-%20The%20agent%20remembers%20the%20earlier%20messages%2C%20but%20not%20the%20earlier%20tool%20results.%20It%20calls%20a%20tool%20again%20when%20it%20needs%0A%20%20%20%20%20%20a%20number.%20Because%20of%20that%2C%20a%20follow-up%20can%20take%20one%20more%20request%20than%20the%20first%20question.%0A%20%20%20%20-%20A%20turn%20that%20fails%20prints%20its%20error%2C%20for%20example%20the%20missing%20key%2C%20and%20the%20chat%20goes%20on.%20Fix%20the%20cause%20and%20ask%0A%20%20%20%20%20%20again.%0A%20%20%20%20-%20Compare%20each%20number%20of%20an%20answer%20with%20the%20output%20of%20the%20tools%20in%20the%20%5BMCP%20server%20notebook%5D(mcp-notebook.md).%0A%20%20%20%20%20%20The%20tools%20are%20exact.%20The%20model%20only%20puts%20them%20in%20words%2C%20and%20it%20can%20make%20a%20mistake%20there.%0A%20%20%20%20%22%22%22)%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
1fc2138d9ff3040677fa1ba9e569e347