{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3c284df8",
   "metadata": {},
   "source": [
    "# XML Agent\n",
    "\n",
    "Some language models (like Anthropic's Claude) are particularly good at reasoning/writing XML. This goes over how to use an agent that uses XML when prompting. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f9d2ead2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents import XMLAgent, tool, AgentExecutor\n",
    "from langchain.chat_models import ChatAnthropic\n",
    "from langchain.chains import LLMChain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ebadf04f",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ChatAnthropic(model=\"claude-2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6ce9f9a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "@tool\n",
    "def search(query: str) -> str:\n",
    "    \"\"\"Search things about current events.\"\"\"\n",
    "    return \"32 degrees\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c589944e",
   "metadata": {},
   "outputs": [],
   "source": [
    "tool_list = [search]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2d8454be",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = LLMChain(\n",
    "    llm=model,\n",
    "    prompt=XMLAgent.get_default_prompt(),\n",
    "    output_parser=XMLAgent.get_default_output_parser()\n",
    ")\n",
    "agent = XMLAgent(tools=tool_list, llm_chain=chain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bca6096f",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_executor = AgentExecutor(agent=agent, tools=tool_list, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "71b872b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m <tool>search</tool>\n",
      "<tool_input>weather in New York\u001b[0m\u001b[36;1m\u001b[1;3m32 degrees\u001b[0m\u001b[32;1m\u001b[1;3m\n",
      "\n",
      "<final_answer>The weather in New York is 32 degrees\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'The weather in New York is 32 degrees'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_executor.run(\"whats the weather in New york?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cca87246",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
