import%20marimo%0A%0A__generated_with%20%3D%20%220.11.0%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(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%20Building%20a%20Tool-Calling%20Agent%20with%20LangChain%0A%0A%20%20%20%20%20%20%20%20-%20Define%20simple%20math%20tools%20(addition%2C%20multiplication%2C%20exponentiation).%0A%20%20%20%20%20%20%20%20-%20Configure%20a%20language%20model%20and%20prompt%20template.%0A%20%20%20%20%20%20%20%20-%20Create%20and%20execute%20a%20tool-calling%20agent%20that%20performs%20a%20complex%20arithmetic%20operation.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20os%0A%20%20%20%20from%20dotenv%20import%20load_dotenv%2C%20find_dotenv%0A%20%20%20%20from%20langchain%20import%20hub%0A%20%20%20%20from%20langchain.agents%20import%20AgentExecutor%2C%20create_tool_calling_agent%0A%20%20%20%20from%20langchain_openai%20import%20ChatOpenAI%0A%20%20%20%20from%20langchain_core.tools%20import%20tool%0A%20%20%20%20from%20langchain.prompts%20import%20(%0A%20%20%20%20%20%20%20%20ChatPromptTemplate%2C%0A%20%20%20%20%20%20%20%20SystemMessagePromptTemplate%2C%0A%20%20%20%20%20%20%20%20HumanMessagePromptTemplate%2C%0A%20%20%20%20%20%20%20%20MessagesPlaceholder%0A%20%20%20%20)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20AgentExecutor%2C%0A%20%20%20%20%20%20%20%20ChatOpenAI%2C%0A%20%20%20%20%20%20%20%20ChatPromptTemplate%2C%0A%20%20%20%20%20%20%20%20HumanMessagePromptTemplate%2C%0A%20%20%20%20%20%20%20%20MessagesPlaceholder%2C%0A%20%20%20%20%20%20%20%20SystemMessagePromptTemplate%2C%0A%20%20%20%20%20%20%20%20create_tool_calling_agent%2C%0A%20%20%20%20%20%20%20%20find_dotenv%2C%0A%20%20%20%20%20%20%20%20hub%2C%0A%20%20%20%20%20%20%20%20load_dotenv%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%20tool%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(os)%3A%0A%20%20%20%20%23%20Delete%20or%20modify%20these%20lines%20if%20you%20are%20not%20behind%20the%20UTSA%20proxy.%0A%20%20%20%20os.environ%5B%22http_proxy%22%5D%20%3D%20%22http%3A%2F%2Fxa-proxy.utsarr.net%3A80%22%0A%20%20%20%20os.environ%5B%22https_proxy%22%5D%20%3D%20%22http%3A%2F%2Fxa-proxy.utsarr.net%3A80%22%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Load%20Environment%20Variables%0A%0A%20%20%20%20%20%20%20%20Create%20a%20%60.env%60%20file%20in%20the%20%60AgenticAISystems%2F%60%20folder%20and%20add%20your%20key%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%60%60%60OPENAI_API_KEY%3D%3Cyour-openai-key%3E%60%60%60%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(find_dotenv%2C%20load_dotenv%2C%20os)%3A%0A%20%20%20%20%23%20Ensure%20you%20have%20a%20.env%20file%20with%20your%20OPENAI_API_KEY.%0A%20%20%20%20working_dir%20%3D%20os.getcwd()%0A%20%20%20%20status%20%3D%20load_dotenv(%0A%20%20%20%20%20%20%20%20find_dotenv(%0A%20%20%20%20%20%20%20%20%20%20%20%20filename%3Df'%7Bworking_dir%7D%2FAgenticAISystems%2F.env'%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20raise_error_if_not_found%3DTrue%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20return%20status%2C%20working_dir%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Define%20Tools%0A%0A%20%20%20%20%20%20%20%20Define%20three%20simple%20math%20tools%20using%20the%20%60%40tool%60%20decorator%3A%0A%0A%20%20%20%20%20%20%20%20-%20**multiply**%3A%20Multiplies%20two%20integers.%0A%20%20%20%20%20%20%20%20-%20**add**%3A%20Adds%20two%20integers.%0A%20%20%20%20%20%20%20%20-%20**exponentiate**%3A%20Raises%20a%20base%20to%20a%20given%20exponent.%0A%0A%20%20%20%20%20%20%20%20These%20tools%20are%20later%20made%20available%20for%20the%20agent%20to%20call.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(tool)%3A%0A%20%20%20%20%40tool%0A%20%20%20%20def%20multiply(first_int%3A%20int%2C%20second_int%3A%20int)%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22%22%22Multiply%20two%20integers%20together.%22%22%22%0A%20%20%20%20%20%20%20%20return%20first_int%20*%20second_int%0A%0A%20%20%20%20%40tool%0A%20%20%20%20def%20add(first_int%3A%20int%2C%20second_int%3A%20int)%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22Add%20two%20integers.%22%0A%20%20%20%20%20%20%20%20return%20first_int%20%2B%20second_int%0A%0A%20%20%20%20%40tool%0A%20%20%20%20def%20exponentiate(base%3A%20int%2C%20exponent%3A%20int)%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22Exponentiate%20the%20base%20to%20the%20exponent%20power.%22%0A%20%20%20%20%20%20%20%20return%20base**exponent%0A%20%20%20%20return%20add%2C%20exponentiate%2C%20multiply%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Configure%20the%20Language%20Model%20and%20Prompt%20Template%0A%0A%20%20%20%20%20%20%20%20-%20Initialize%20a%20%60ChatOpenAI%60%20model%20(using%20GPT-3.5-turbo-0125).%0A%20%20%20%20%20%20%20%20-%20Create%20a%20chat%20prompt%20template%20with%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20A%20system%20message%20defining%20the%20assistant%20as%20%22helpful%22.%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20A%20human%20message%20that%20takes%20user%20input.%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20A%20placeholder%20(%60agent_scratchpad%60)%20for%20the%20intermediate%20steps%20after%20each%20tool%20is%20called.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20ChatOpenAI%2C%0A%20%20%20%20ChatPromptTemplate%2C%0A%20%20%20%20HumanMessagePromptTemplate%2C%0A%20%20%20%20MessagesPlaceholder%2C%0A%20%20%20%20SystemMessagePromptTemplate%2C%0A)%3A%0A%20%20%20%20llm%20%3D%20ChatOpenAI(model%3D%22gpt-3.5-turbo-0125%22)%0A%0A%20%20%20%20chat_prompt%20%3D%20ChatPromptTemplate(%0A%20%20%20%20%20%20%20%20messages%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20SystemMessagePromptTemplate.from_template(%22You%20are%20a%20helpful%20assistant.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20HumanMessagePromptTemplate.from_template(%22%7Binput%7D%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20MessagesPlaceholder(variable_name%3D%22agent_scratchpad%22)%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20chat_prompt.pretty_print()%0A%20%20%20%20return%20chat_prompt%2C%20llm%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Build%20and%20Execute%20the%20Tool-Calling%20Agent%0A%0A%20%20%20%20%20%20%20%20-%20Compile%20our%20defined%20math%20tools%20into%20a%20list.%0A%20%20%20%20%20%20%20%20-%20Create%20a%20tool-calling%20agent%20by%20combining%20the%20language%20model%2C%20tools%2C%20and%20prompt.%0A%20%20%20%20%20%20%20%20-%20An%20%60AgentExecutor%60%20is%20used%20to%20run%20the%20agent%20with%20verbose%20output.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20AgentExecutor%2C%0A%20%20%20%20add%2C%0A%20%20%20%20chat_prompt%2C%0A%20%20%20%20create_tool_calling_agent%2C%0A%20%20%20%20exponentiate%2C%0A%20%20%20%20llm%2C%0A%20%20%20%20multiply%2C%0A)%3A%0A%20%20%20%20_tools%20%3D%20%5Bmultiply%2C%20add%2C%20exponentiate%5D%0A%0A%20%20%20%20%23%20Construct%20the%20tool%20calling%20agent%0A%20%20%20%20_agent%20%3D%20create_tool_calling_agent(llm%2C%20_tools%2C%20chat_prompt)%0A%0A%20%20%20%20%23%20Create%20an%20agent%20executor%20by%20passing%20in%20the%20agent%20and%20tools%0A%20%20%20%20_agent_executor%20%3D%20AgentExecutor(agent%3D_agent%2C%20tools%3D_tools%2C%20verbose%3DTrue)%0A%0A%20%20%20%20_results%20%3D%20_agent_executor.invoke(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22input%22%3A%20%22Take%203%20to%20the%20fifth%20power%20and%20multiply%20that%20by%20the%20sum%20of%20twelve%20and%20three%2C%20then%20square%20the%20whole%20result%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20_results%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
bfda4f23b3df78d4174fe72c64c13ca968c98afdd3fd986caf2b7e6ba109264c