import%20marimo%0A%0A__generated_with%20%3D%20%220.13.7%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%20Introduction%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20os%0A%0A%20%20%20%20from%20openai%20import%20OpenAI%0A%0A%20%20%20%20client%20%3D%20OpenAI(%0A%20%20%20%20%20%20%20%20api_key%3Dos.environ.get(%22OPENAI_API_KEY%22)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20openai_response%20%3D%20client.responses.create(%0A%20%20%20%20%20%20%20%20model%3D%22gpt-4o-mini-2024-07-18%22%2C%0A%20%20%20%20%20%20%20%20instructions%3D%22Extract%20name%2C%20years%20of%20experience%2C%20and%20primary%20skill%20from%20the%20job%20applicant%20description.%22%2C%0A%20%20%20%20%20%20%20%20input%3D%22Khuyen%20Tran%20is%20a%20data%20scientist%20with%205%20years%20of%20experience%2C%20skilled%20in%20Python%20and%20machine%20learning.%22%2C%0A%20%20%20%20)%0A%0A%20%20%20%20print(openai_response.output_text)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20%23%20Core%20Workflow%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20nest_asyncio%0A%0A%20%20%20%20nest_asyncio.apply()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20typing%20import%20List%0A%0A%20%20%20%20from%20pydantic%20import%20BaseModel%0A%20%20%20%20from%20pydantic_ai%20import%20Agent%0A%0A%20%20%20%20return%20Agent%2C%20BaseModel%2C%20List%0A%0A%0A%40app.cell%0Adef%20_(BaseModel%2C%20List)%3A%0A%20%20%20%20class%20ApplicantProfile(BaseModel)%3A%0A%20%20%20%20%20%20%20%20first_name%3A%20str%0A%20%20%20%20%20%20%20%20last_name%3A%20str%0A%20%20%20%20%20%20%20%20experience_years%3A%20int%0A%20%20%20%20%20%20%20%20primary_skill%3A%20List%5Bstr%5D%0A%0A%20%20%20%20return%20(ApplicantProfile%2C)%0A%0A%0A%40app.cell%0Adef%20_(Agent%2C%20ApplicantProfile)%3A%0A%20%20%20%20agent%20%3D%20Agent(%0A%20%20%20%20%20%20%20%20%22gpt-4o-mini-2024-07-18%22%2C%0A%20%20%20%20%20%20%20%20system_prompt%3D%22Extract%20name%2C%20years%20of%20experience%2C%20and%20primary%20skill%20from%20the%20job%20applicant%20description.%22%2C%0A%20%20%20%20%20%20%20%20output_type%3DApplicantProfile%2C%0A%20%20%20%20)%0A%0A%20%20%20%20result%20%3D%20agent.run_sync(%0A%20%20%20%20%20%20%20%20%22Khuyen%20Tran%20is%20a%20data%20scientist%20with%205%20years%20of%20experience%2C%20skilled%20in%20Python%20and%20machine%20learning.%22%0A%20%20%20%20)%0A%20%20%20%20print(result.output)%0A%20%20%20%20return%20(result%2C)%0A%0A%0A%40app.cell%0Adef%20_(result)%3A%0A%20%20%20%20result.output.model_dump()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(result)%3A%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20df%20%3D%20pd.DataFrame(result.output.model_dump())%0A%20%20%20%20df%0A%20%20%20%20return%20(pd%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%20Using%20the%20DuckDuckGo%20Search%20Tool%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(BaseModel%2C%20List)%3A%0A%20%20%20%20class%20UnemploymentDataSource(BaseModel)%3A%0A%20%20%20%20%20%20%20%20title%3A%20List%5Bstr%5D%0A%20%20%20%20%20%20%20%20description%3A%20List%5Bstr%5D%0A%20%20%20%20%20%20%20%20url%3A%20List%5Bstr%5D%0A%0A%20%20%20%20return%20(UnemploymentDataSource%2C)%0A%0A%0A%40app.cell%0Adef%20_(Agent%2C%20UnemploymentDataSource)%3A%0A%20%20%20%20from%20pydantic_ai.common_tools.duckduckgo%20import%20duckduckgo_search_tool%0A%0A%20%20%20%20%23%20Define%20the%20agent%20with%20DuckDuckGo%20search%20tool%0A%20%20%20%20search_agent%20%3D%20Agent(%0A%20%20%20%20%20%20%20%20%22gpt-4o-mini-2024-07-18%22%2C%0A%20%20%20%20%20%20%20%20tools%3D%5Bduckduckgo_search_tool()%5D%2C%0A%20%20%20%20%20%20%20%20output_type%3DUnemploymentDataSource%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Run%20a%20search%20for%20unemployment%20rate%20dataset%0A%20%20%20%20unemployment_result%20%3D%20search_agent.run_sync(%0A%20%20%20%20%20%20%20%20%22Monthly%20unemployment%20rate%20dataset%20for%20US%20from%202018%20to%202024%22%0A%20%20%20%20)%0A%0A%20%20%20%20print(unemployment_result.output)%0A%20%20%20%20return%20(unemployment_result%2C)%0A%0A%0A%40app.cell%0Adef%20_(pd%2C%20unemployment_result)%3A%0A%20%20%20%20unemployment_df%20%3D%20pd.DataFrame(unemployment_result.output.model_dump())%0A%20%20%20%20unemployment_df%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%23%20Comparison%20with%20LangChain%20Structured%20Output%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(BaseModel%2C%20List)%3A%0A%20%20%20%20from%20typing%20import%20Optional%0A%0A%20%20%20%20class%20RecipeExtractor(BaseModel)%3A%0A%20%20%20%20%20%20%20%20ingredients%3A%20List%5Bstr%5D%0A%20%20%20%20%20%20%20%20instructions%3A%20str%0A%20%20%20%20%20%20%20%20cook_time%3A%20Optional%5Bstr%5D%0A%0A%20%20%20%20return%20(RecipeExtractor%2C)%0A%0A%0A%40app.cell%0Adef%20_(Agent%2C%20RecipeExtractor)%3A%0A%20%20%20%20recipe_agent%20%3D%20Agent(%0A%20%20%20%20%20%20%20%20%22gpt-4o-mini-2024-07-18%22%2C%0A%20%20%20%20%20%20%20%20system_prompt%3D%22Pull%20ingredients%2C%20instructions%2C%20and%20cook%20time.%22%2C%0A%20%20%20%20%20%20%20%20output_type%3DRecipeExtractor%2C%0A%20%20%20%20)%0A%0A%20%20%20%20recipe_result%20%3D%20recipe_agent.run_sync(%0A%20%20%20%20%20%20%20%20%22Sugar%2C%20flour%2C%20cocoa%2C%20eggs%2C%20and%20milk.%20Mix%2C%20bake%20at%20350F%20for%2030%20min.%22%0A%20%20%20%20)%0A%20%20%20%20print(recipe_result.output)%0A%20%20%20%20print(recipe_result.output.cook_time)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(RecipeExtractor)%3A%0A%20%20%20%20from%20langchain_core.messages%20import%20HumanMessage%2C%20SystemMessage%0A%20%20%20%20from%20langchain_openai%20import%20ChatOpenAI%0A%0A%20%20%20%20%23%20Initialize%20the%20chat%20model%0A%20%20%20%20model%20%3D%20ChatOpenAI(model%3D%22gpt-4o-mini-2024-07-18%22%2C%20temperature%3D0)%0A%0A%20%20%20%20%23%20Bind%20the%20response%20formatter%20schema%0A%20%20%20%20model_with_tools%20%3D%20model.bind_tools(%5BRecipeExtractor%5D)%0A%0A%20%20%20%20%23%20Create%20a%20list%20of%20messages%20to%20send%20to%20the%20model%0A%20%20%20%20messages%20%3D%20%5B%0A%20%20%20%20%20%20%20%20SystemMessage(%22Pull%20ingredients%2C%20instructions%2C%20and%20cook%20time.%22)%2C%0A%20%20%20%20%20%20%20%20HumanMessage(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Sugar%2C%20flour%2C%20cocoa%2C%20eggs%2C%20and%20milk.%20Mix%2C%20bake%20at%20350F%20for%2030%20min.%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20%23%20Invoke%20the%20model%20with%20the%20prepared%20messages%0A%20%20%20%20ai_msg%20%3D%20model_with_tools.invoke(messages)%0A%0A%20%20%20%20%23%20Access%20the%20tool%20calls%20made%20during%20the%20model%20invocation%0A%20%20%20%20print(ai_msg.tool_calls%5B0%5D)%0A%20%20%20%20print(ai_msg.tool_calls%5B0%5D%5B%22args%22%5D%5B%22cook_time%22%5D)%0A%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
11e9e04a1b8a1ffce687037dcea2f1dfe49c8cb86ab48bedf712f1da04025757