{
 "cells": [
  {
   "cell_type": "raw",
   "id": "bafbef65-ace3-42ce-83f3-553909b48685",
   "metadata": {},
   "source": [
    "---\n",
    "sidebar_position: 0\n",
    "title: Stuff\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c473378-ff18-45cf-b718-43f6f94af040",
   "metadata": {},
   "source": [
    "The stuff documents chain (\"stuff\" as in \"to stuff\" or \"to fill\") is the most straightforward of the document chains. It takes a list of documents, inserts them all into a prompt and passes that prompt to an LLM.\n",
    "\n",
    "This chain is well-suited for applications where documents are small and only a few are passed in for most calls.\n",
    "\n",
    "![stuff_diagram](../../../../static/img/stuff.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f20da9a-2a7f-4fd1-9dbc-c01f164b078b",
   "metadata": {},
   "source": [
    "## Recreating with LCEL\n",
    "\n",
    "With [LangChain Expression Language](/docs/expression_language) we can easily recreate the `StuffDocumentsChain` functionality, with the additional benefit of getting all the built-in LCEL features (batch, async, etc.) and with much more ability to customize specific parts of the chain."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0f33d9f-2cf2-4064-92f6-39fbd5a597e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatAnthropic\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.schema import StrOutputParser\n",
    "from langchain.schema.prompt_template import format_document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3e355e46-6375-4f5e-a102-fdbf72476422",
   "metadata": {},
   "outputs": [],
   "source": [
    "doc_prompt = PromptTemplate.from_template(\"{page_content}\")\n",
    "\n",
    "chain = (\n",
    "    {\n",
    "        \"content\": lambda docs: \"\\n\\n\".join(\n",
    "            format_document(doc, doc_prompt) for doc in docs\n",
    "        )\n",
    "    }\n",
    "    | PromptTemplate.from_template(\"Summarize the following content:\\n\\n{content}\")\n",
    "    | ChatAnthropic()\n",
    "    | StrOutputParser()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ece7db13-5d4b-490f-8677-387bf4eac944",
   "metadata": {},
   "source": [
    "### Example run\n",
    "\n",
    "Lets run this summarization chain on some sample data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a0ddd725-d97c-4e22-8509-7337d3a71dff",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.schema import Document\n",
    "\n",
    "text = \"\"\"Nuclear power in space is the use of nuclear power in outer space, typically either small fission systems or radioactive decay for electricity or heat. Another use is for scientific observation, as in a Mössbauer spectrometer. The most common type is a radioisotope thermoelectric generator, which has been used on many space probes and on crewed lunar missions. Small fission reactors for Earth observation satellites, such as the TOPAZ nuclear reactor, have also been flown.[1] A radioisotope heater unit is powered by radioactive decay and can keep components from becoming too cold to function, potentially over a span of decades.[2]\n",
    "\n",
    "The United States tested the SNAP-10A nuclear reactor in space for 43 days in 1965,[3] with the next test of a nuclear reactor power system intended for space use occurring on 13 September 2012 with the Demonstration Using Flattop Fission (DUFF) test of the Kilopower reactor.[4]\n",
    "\n",
    "After a ground-based test of the experimental 1965 Romashka reactor, which used uranium and direct thermoelectric conversion to electricity,[5] the USSR sent about 40 nuclear-electric satellites into space, mostly powered by the BES-5 reactor. The more powerful TOPAZ-II reactor produced 10 kilowatts of electricity.[3]\n",
    "\n",
    "Examples of concepts that use nuclear power for space propulsion systems include the nuclear electric rocket (nuclear powered ion thruster(s)), the radioisotope rocket, and radioisotope electric propulsion (REP).[6] One of the more explored concepts is the nuclear thermal rocket, which was ground tested in the NERVA program. Nuclear pulse propulsion was the subject of Project Orion.[7]\n",
    "\n",
    "Regulation and hazard prevention[edit]\n",
    "After the ban of nuclear weapons in space by the Outer Space Treaty in 1967, nuclear power has been discussed at least since 1972 as a sensitive issue by states.[8] Particularly its potential hazards to Earth's environment and thus also humans has prompted states to adopt in the U.N. General Assembly the Principles Relevant to the Use of Nuclear Power Sources in Outer Space (1992), particularly introducing safety principles for launches and to manage their traffic.[8]\n",
    "\n",
    "Benefits\n",
    "\n",
    "Both the Viking 1 and Viking 2 landers used RTGs for power on the surface of Mars. (Viking launch vehicle pictured)\n",
    "While solar power is much more commonly used, nuclear power can offer advantages in some areas. Solar cells, although efficient, can only supply energy to spacecraft in orbits where the solar flux is sufficiently high, such as low Earth orbit and interplanetary destinations close enough to the Sun. Unlike solar cells, nuclear power systems function independently of sunlight, which is necessary for deep space exploration. Nuclear-based systems can have less mass than solar cells of equivalent power, allowing more compact spacecraft that are easier to orient and direct in space. In the case of crewed spaceflight, nuclear power concepts that can power both life support and propulsion systems may reduce both cost and flight time.[9]\n",
    "\n",
    "Selected applications and/or technologies for space include:\n",
    "\n",
    "Radioisotope thermoelectric generator\n",
    "Radioisotope heater unit\n",
    "Radioisotope piezoelectric generator\n",
    "Radioisotope rocket\n",
    "Nuclear thermal rocket\n",
    "Nuclear pulse propulsion\n",
    "Nuclear electric rocket\n",
    "\"\"\"\n",
    "\n",
    "docs = [\n",
    "    Document(\n",
    "        page_content=split,\n",
    "        metadata={\"source\": \"https://en.wikipedia.org/wiki/Nuclear_power_in_space\"},\n",
    "    )\n",
    "    for split in text.split()\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bd18f647-bdb4-46ce-b01f-fe8afc208ffa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Here is a summary of the key points:\n",
      "\n",
      "- Nuclear power has been used in space for electricity, heat, and scientific observation. The most common type is a radioisotope thermoelectric generator, used on many probes and lunar missions. \n",
      "\n",
      "- Small fission reactors have been used for Earth observation satellites. Radioisotope heater units use radioactive decay to keep components warm for decades.\n",
      "\n",
      "- The US tested a nuclear reactor in space in 1965. The Soviet Union launched around 40 nuclear-powered satellites, mostly with BES-5 reactors.\n",
      "\n",
      "- Concepts for nuclear propulsion include nuclear thermal rockets, nuclear electric rockets, and nuclear pulse propulsion. The NERVA program ground tested nuclear thermal rockets.\n",
      "\n",
      "- After the 1967 Outer Space Treaty banned nuclear weapons in space, safety principles were introduced for nuclear power launch and traffic management.\n",
      "\n",
      "- Benefits of nuclear power in space include functioning independently of sunlight needed for deep space exploration, less mass than equivalent solar power, and ability to power both life support and propulsion.\n"
     ]
    }
   ],
   "source": [
    "print(chain.invoke(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.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
