{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "245a954a",
   "metadata": {},
   "source": [
    "# ArXiv\n",
    "\n",
    "This notebook goes over how to use the `arxiv` tool with an agent. \n",
    "\n",
    "First, you need to install `arxiv` python package."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5a7209e",
   "metadata": {
    "tags": [],
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "!pip install arxiv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ce1a4827-ce89-4f31-a041-3246743e513a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.agents import AgentType, initialize_agent, load_tools\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(temperature=0.0)\n",
    "tools = load_tools(\n",
    "    [\"arxiv\"],\n",
    ")\n",
    "\n",
    "agent_chain = initialize_agent(\n",
    "    tools,\n",
    "    llm,\n",
    "    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
    "    verbose=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ad7dd945-5ae3-49e5-b667-6d86b15050b6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mI need to use Arxiv to search for the paper.\n",
      "Action: Arxiv\n",
      "Action Input: \"1605.08386\"\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3mPublished: 2016-05-26\n",
      "Title: Heat-bath random walks with Markov bases\n",
      "Authors: Caprice Stanley, Tobias Windisch\n",
      "Summary: Graphs on lattice points are studied whose edges come from a finite set of\n",
      "allowed moves of arbitrary length. We show that the diameter of these graphs on\n",
      "fibers of a fixed integer matrix can be bounded from above by a constant. We\n",
      "then study the mixing behaviour of heat-bath random walks on these graphs. We\n",
      "also state explicit conditions on the set of moves so that the heat-bath random\n",
      "walk, a generalization of the Glauber dynamics, is an expander in fixed\n",
      "dimension.\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mThe paper is about heat-bath random walks with Markov bases on graphs of lattice points.\n",
      "Final Answer: The paper 1605.08386 is about heat-bath random walks with Markov bases on graphs of lattice points.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'The paper 1605.08386 is about heat-bath random walks with Markov bases on graphs of lattice points.'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent_chain.run(\n",
    "    \"What's the paper 1605.08386 about?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4183343-d69a-4be0-9b2c-cc98464a6825",
   "metadata": {},
   "source": [
    "## The ArXiv API Wrapper\n",
    "\n",
    "The tool uses the `API Wrapper`. Below, we explore some of the features it provides."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8d32b39a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.utilities import ArxivAPIWrapper"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c89c110c-96ac-4fe1-ba3e-6056543d1a59",
   "metadata": {},
   "source": [
    "Run a query to get information about some `scientific article`/articles. The query text is limited to 300 characters.\n",
    "\n",
    "It returns these article fields:\n",
    "- Publishing date\n",
    "- Title\n",
    "- Authors\n",
    "- Summary\n",
    "\n",
    "Next query returns information about one article with arxiv Id equal \"1605.08386\". "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "34bb5968",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Published: 2016-05-26\\nTitle: Heat-bath random walks with Markov bases\\nAuthors: Caprice Stanley, Tobias Windisch\\nSummary: Graphs on lattice points are studied whose edges come from a finite set of\\nallowed moves of arbitrary length. We show that the diameter of these graphs on\\nfibers of a fixed integer matrix can be bounded from above by a constant. We\\nthen study the mixing behaviour of heat-bath random walks on these graphs. We\\nalso state explicit conditions on the set of moves so that the heat-bath random\\nwalk, a generalization of the Glauber dynamics, is an expander in fixed\\ndimension.'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arxiv = ArxivAPIWrapper()\n",
    "docs = arxiv.run(\"1605.08386\")\n",
    "docs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "840f70c9-8f80-4680-bb38-46198e931bcf",
   "metadata": {},
   "source": [
    "Now, we want to get information about one author, `Caprice Stanley`.\n",
    "\n",
    "This query returns information about three articles. By default, the query returns information only about three top articles."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b0867fda-e119-4b19-9ec6-e354fa821db3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Published: 2017-10-10\\nTitle: On Mixing Behavior of a Family of Random Walks Determined by a Linear Recurrence\\nAuthors: Caprice Stanley, Seth Sullivant\\nSummary: We study random walks on the integers mod $G_n$ that are determined by an\\ninteger sequence $\\\\{ G_n \\\\}_{n \\\\geq 1}$ generated by a linear recurrence\\nrelation. Fourier analysis provides explicit formulas to compute the\\neigenvalues of the transition matrices and we use this to bound the mixing time\\nof the random walks.\\n\\nPublished: 2016-05-26\\nTitle: Heat-bath random walks with Markov bases\\nAuthors: Caprice Stanley, Tobias Windisch\\nSummary: Graphs on lattice points are studied whose edges come from a finite set of\\nallowed moves of arbitrary length. We show that the diameter of these graphs on\\nfibers of a fixed integer matrix can be bounded from above by a constant. We\\nthen study the mixing behaviour of heat-bath random walks on these graphs. We\\nalso state explicit conditions on the set of moves so that the heat-bath random\\nwalk, a generalization of the Glauber dynamics, is an expander in fixed\\ndimension.\\n\\nPublished: 2003-03-18\\nTitle: Calculation of fluxes of charged particles and neutrinos from atmospheric showers\\nAuthors: V. Plyaskin\\nSummary: The results on the fluxes of charged particles and neutrinos from a\\n3-dimensional (3D) simulation of atmospheric showers are presented. An\\nagreement of calculated fluxes with data on charged particles from the AMS and\\nCAPRICE detectors is demonstrated. Predictions on neutrino fluxes at different\\nexperimental sites are compared with results from other calculations.'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "docs = arxiv.run(\"Caprice Stanley\")\n",
    "docs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d9b6292-a47d-4f99-9827-8e9f244bf887",
   "metadata": {},
   "source": [
    "Now, we are trying to find information about non-existing article. In this case, the response is \"No good Arxiv Result was found\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3580aeeb-086f-45ba-bcdc-b46f5134b3dd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'No good Arxiv Result was found'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "docs = arxiv.run(\"1605.08386WWW\")\n",
    "docs"
   ]
  }
 ],
 "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.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
