{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load env variables and create client\n",
    "import base64\n",
    "from dotenv import load_dotenv\n",
    "from anthropic import Anthropic\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "client = Anthropic()\n",
    "model = \"claude-3-7-sonnet-latest\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Helper functions\n",
    "from anthropic.types import Message\n",
    "\n",
    "\n",
    "def add_user_message(messages, message):\n",
    "    user_message = {\n",
    "        \"role\": \"user\",\n",
    "        \"content\": message.content if isinstance(message, Message) else message,\n",
    "    }\n",
    "    messages.append(user_message)\n",
    "\n",
    "\n",
    "def add_assistant_message(messages, message):\n",
    "    assistant_message = {\n",
    "        \"role\": \"assistant\",\n",
    "        \"content\": message.content if isinstance(message, Message) else message,\n",
    "    }\n",
    "    messages.append(assistant_message)\n",
    "\n",
    "\n",
    "def chat(\n",
    "    messages,\n",
    "    system=None,\n",
    "    temperature=1.0,\n",
    "    stop_sequences=[],\n",
    "    tools=None,\n",
    "    thinking=False,\n",
    "    thinking_budget=1024,\n",
    "):\n",
    "    params = {\n",
    "        \"model\": model,\n",
    "        \"max_tokens\": 4000,\n",
    "        \"messages\": messages,\n",
    "        \"temperature\": temperature,\n",
    "        \"stop_sequences\": stop_sequences,\n",
    "    }\n",
    "\n",
    "    if thinking:\n",
    "        params[\"thinking\"] = {\n",
    "            \"type\": \"enabled\",\n",
    "            \"budget_tokens\": thinking_budget,\n",
    "        }\n",
    "\n",
    "    if tools:\n",
    "        params[\"tools\"] = tools\n",
    "\n",
    "    if system:\n",
    "        params[\"system\"] = system\n",
    "\n",
    "    message = client.messages.create(**params)\n",
    "    return message\n",
    "\n",
    "\n",
    "def text_from_message(message):\n",
    "    return \"\\n\".join(\n",
    "        [block.text for block in message.content if block.type == \"text\"]\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Fire risk assessment prompt\n",
    "prompt = \"\"\"\n",
    "Analyze the attached satellite image of a property with these specific steps:\n",
    "\n",
    "1. Residence identification: Locate the primary residence on the property by looking for:\n",
    "   - The largest roofed structure \n",
    "   - Typical residential features (driveway connection, regular geometry)\n",
    "   - Distinction from other structures (garages, sheds, pools)\n",
    "   Describe the residence's location relative to property boundaries and other features.\n",
    "\n",
    "2. Tree overhang analysis: Examine all trees near the primary residence:\n",
    "   - Identify any trees whose canopy extends directly over any portion of the roof\n",
    "   - Estimate the percentage of roof covered by overhanging branches (0-25%, 25-50%, 50-75%, 75-100%)\n",
    "   - Note particularly dense areas of overhang\n",
    "\n",
    "3. Fire risk assessment: For any overhanging trees, evaluate:\n",
    "   - Potential wildfire vulnerability (ember catch points, continuous fuel paths to structure)\n",
    "   - Proximity to chimneys, vents, or other roof openings if visible\n",
    "   - Areas where branches create a \"bridge\" between wildland vegetation and the structure\n",
    "   \n",
    "4. Defensible space identification: Assess the property's overall vegetative structure:\n",
    "   - Identify if trees connect to form a continuous canopy over or near the home\n",
    "   - Note any obvious fuel ladders (vegetation that can carry fire from ground to tree to roof)\n",
    "\n",
    "5. Fire risk rating: Based on your analysis, assign a Fire Risk Rating from 1-4:\n",
    "   - Rating 1 (Low Risk): No tree branches overhanging the roof, good defensible space around the structure\n",
    "   - Rating 2 (Moderate Risk): Minimal overhang (<25% of roof), some separation between tree canopies\n",
    "   - Rating 3 (High Risk): Significant overhang (25-50% of roof), connected tree canopies, multiple points of vulnerability\n",
    "   - Rating 4 (Severe Risk): Extensive overhang (>50% of roof), dense vegetation against structure, numerous ember catch points, limited defensible space\n",
    "\n",
    "For each item above (1-5), write one sentence summarizing your findings, with your final response being the numeric Fire Risk Rating (1-4) with a brief justification.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "article_text = \"\"\"\n",
    "Earth is the third planet from the Sun and the only\n",
    "astronomical object known to harbor life. This is\n",
    "enabled by Earth being an ocean world, the only one in\n",
    "the Solar System sustaining liquid surface water. Almost\n",
    "all of Earth's water is contained in its global ocean,\n",
    "covering 70.8% of Earth's crust. The remaining 29.2% of\n",
    "Earth's crust is land, most of which is located in the\n",
    "form of continental landmasses within Earth's land\n",
    "hemisphere. Most of Earth's land is at least somewhat\n",
    "humid and covered by vegetation, while large sheets of\n",
    "ice at Earth's polar deserts retain more water than\n",
    "Earth's groundwater, lakes, rivers, and atmospheric\n",
    "water combined. Earth's crust consists of slowly moving\n",
    "tectonic plates, which interact to produce mountain\n",
    "ranges, volcanoes, and earthquakes. Earth has a liquid\n",
    "outer core that generates a magnetosphere capable of\n",
    "deflecting most of the destructive solar winds and\n",
    "cosmic radiation.\n",
    "Earth has a dynamic atmosphere, which sustains\n",
    "Earth's surface conditions and protects it from most\n",
    "meteoroids and UV-light at entry. It has a composition\n",
    "of primarily nitrogen and oxygen. Water vapor is widely\n",
    "present in the atmosphere, forming clouds that cover\n",
    "most of the planet. The water vapor acts as a greenhouse\n",
    "gas and, together with other greenhouse gases in the\n",
    "atmosphere, particularly carbon dioxide (CO2), creates\n",
    "the conditions for both liquid surface water and water\n",
    "vapor to persist via the capturing of energy from the\n",
    "Sun's light. This process maintains the current average\n",
    "surface temperature of 14.76 °C (58.57 °F), at which\n",
    "water is liquid under normal atmospheric pressure.\n",
    "Differences in the amount of captured energy between\n",
    "geographic regions (as with the equatorial region\n",
    "receiving more sunlight than the polar regions) drive\n",
    "atmospheric and ocean currents, producing a global\n",
    "climate system with different climate regions, and a\n",
    "range of weather phenomena such as precipitation,\n",
    "allowing components such as nitrogen to cycle.\n",
    "Earth\n",
    "The Blue Marble, Apollo 17, December 1972\n",
    "Designations\n",
    "Alternative\n",
    "names\n",
    "The world · The globe ·\n",
    "Terra · Tellus · Gaia ·\n",
    "Mother Earth · Sol III\n",
    "Adjectives Earthly · Terrestrial · Terran\n",
    "· Tellurian\n",
    "Symbol and\n",
    "Orbital characteristics\n",
    "Epoch J2000[n 1]\n",
    "Aphelion 152 097 597 km\n",
    "Perihelion 147 098 450 km[n 2]\n",
    "Semi-major axis 149 598 023 km[1]\n",
    "Eccentricity 0.016 7086[1]\n",
    "Orbital period\n",
    "365.256 363 004 d[2]\n",
    "(sidereal)\n",
    "(1.000 017 420 96 aj)\n",
    "29.7827 km/s[3]\n",
    "Average orbital\n",
    "speed\n",
    "Mean anomaly 358.617°\n",
    "Inclination 7.155°\n",
    "– Sun's equator;\n",
    "Earth is rounded into an ellipsoid with a circumference\n",
    "of about 40,000 kilometres (25,000 miles). It is the\n",
    "densest planet in the Solar System. Of the four rocky\n",
    "planets, it is the largest and most massive. Earth is\n",
    "about eight light-minutes away from the Sun and orbits\n",
    "it, taking a year (about 365.25 days) to complete one\n",
    "revolution. Earth rotates around its own axis in slightly\n",
    "less than a day (in about 23 hours and 56 minutes).\n",
    "Earth's axis of rotation is tilted with respect to the\n",
    "perpendicular to its orbital plane around the Sun,\n",
    "producing seasons. Earth is orbited by one permanent\n",
    "natural satellite, the Moon, which orbits Earth at\n",
    "384,400 km (238,900 mi)—1.28 light seconds—and is\n",
    "roughly a quarter as wide as Earth. The Moon's gravity\n",
    "helps stabilize Earth's axis, causes tides and gradually\n",
    "slows Earth's rotation. Tidal locking has made the Moon\n",
    "always face Earth with the same side.\n",
    "Earth, like most other bodies in the Solar System,\n",
    "formed about 4.5 billion years ago from gas and dust in\n",
    "the early Solar System. During the first billion years of\n",
    "Earth's history, the ocean formed and then life\n",
    "developed within it. Life spread globally and has been\n",
    "altering Earth's atmosphere and surface, leading to the\n",
    "Great Oxidation Event two billion years ago. Humans\n",
    "emerged 300,000 years ago in Africa and have spread\n",
    "across every continent on Earth. Humans depend on\n",
    "Earth's biosphere and natural resources for their\n",
    "survival, but have increasingly impacted the planet's\n",
    "environment. Humanity's current impact on Earth's\n",
    "climate and biosphere is unsustainable, threatening the\n",
    "livelihood of humans and many other forms of life, and\n",
    "causing widespread extinctions.\n",
    "[23]\n",
    "Etymology\n",
    "The Modern English word Earth developed, via Middle\n",
    "English, from an Old English noun most often spelled\n",
    "eorðe.\n",
    "[24] It has cognates in every Germanic language,\n",
    "from which Proto-Germanic *erþō has been\n",
    "reconstructed. In its earliest attestation, the word eorðe\n",
    "was used to translate the many senses of Latin terra and\n",
    "Greek gē: the ground, its soil, dry land, the human\n",
    "world, the surface of the world (including the sea), and\n",
    "the globe itself. As with Roman Terra (or Tellus) and\n",
    "1.578 69°\n",
    "– invariable\n",
    "plane;[4]\n",
    "0.000 05°\n",
    "– J2000 ecliptic\n",
    "Longitude of\n",
    "ascending node\n",
    "−11.260 64°\n",
    "– J2000\n",
    "ecliptic[3]\n",
    "2023-Jan-04[5]\n",
    "Time of\n",
    "perihelion\n",
    "Argument of\n",
    "perihelion\n",
    "114.207 83°[3]\n",
    "Satellites 1, the Moon\n",
    "Physical characteristics\n",
    "Mean radius 6 371.0 km[6]\n",
    "6 378.137 km[7][8]\n",
    "Equatorial\n",
    "radius\n",
    "Polar radius 6 356.752 km[9]\n",
    "Flattening 1/298.257 222 101\n",
    "(ETRS89)[10]\n",
    "Circumference 40 075.017 km equatorial[8]\n",
    "40 007.86 km\n",
    "meridional[11][n 3]\n",
    "Surface area 510 072 000 km2[12][n 4]\n",
    "Land: 148 940 000 km2\n",
    "Water: 361 132 000 km2\n",
    "Volume 1.083 21 × 1012 km3[3]\n",
    "Mass 5.972 168 × 1024 kg[13]\n",
    "Mean density 5.513 g/cm3[3]\n",
    "Surface gravity 9.806 65 m/s2[14]\n",
    "(exactly 1 g0)\n",
    "0.3307[15]\n",
    "Moment of\n",
    "inertia factor\n",
    "Escape velocity 11.186 km/s[3]\n",
    "Synodic rotation\n",
    "period\n",
    "Sidereal rotation\n",
    "period\n",
    "1.0 d\n",
    "(24h 00 m 00s)\n",
    "0.997 269 68 d[16]\n",
    "(23h 56 m 4.100s)\n",
    "0.4651 km/s[17]\n",
    "Equatorial\n",
    "rotation velocity\n",
    "Axial tilt 23.439 2811°[2]\n",
    "Albedo 0.434 geometric[3]\n",
    "0.294 Bond[3]\n",
    "Greek Gaia, Earth may have been a personified goddess\n",
    "in Germanic paganism: late Norse mythology included\n",
    "Jörð ('Earth'), a giantess often given as the mother of\n",
    "Thor.\n",
    "[25]\n",
    "Temperature 255 K (−18 °C)\n",
    "(blackbody temperature)[18]\n",
    "Surface temp. min mean max\n",
    "[n 5]−89.2 °C 14.76 °C 56.7 °C\n",
    "0.274 μSv/h[22]\n",
    "Historically, Earth has been written in lowercase.\n",
    "During the Early Middle English period, its definite\n",
    "sense as \"the globe\" began being expressed using the\n",
    "phrase the earth. By the period of Early Modern\n",
    "English, capitalization of nouns began to prevail, and\n",
    "the earth was also written the Earth, particularly when\n",
    "referenced along with other heavenly bodies. More\n",
    "recently, the name is sometimes simply given as Earth,\n",
    "by analogy with the names of the other planets, though\n",
    "earth and forms with the earth remain common.[24]\n",
    "House styles now vary: Oxford spelling recognizes the\n",
    "lowercase form as the more common, with the\n",
    "capitalized form an acceptable variant. Another\n",
    "convention capitalizes Earth when appearing as a name,\n",
    "such as a description of the \"Earth's atmosphere\", but\n",
    "employs the lowercase when it is preceded by the, such\n",
    "as \"the atmosphere of the earth\". It almost always\n",
    "appears in lowercase in colloquial expressions such as\n",
    "\"what on earth are you doing?\"[26]\n",
    "Surface\n",
    "equivalent dose\n",
    "rate\n",
    "Absolute\n",
    "magnitude (H)\n",
    "−3.99\n",
    "Atmosphere\n",
    "Surface\n",
    "101.325 kPa (at sea level)\n",
    "pressure\n",
    "Composition by\n",
    "volume\n",
    "78.08% nitrogen (dry air)\n",
    "20.95% oxygen (dry air)\n",
    "≤1% water vapor (variable)\n",
    "0.9340% argon\n",
    "0.0415% carbon dioxide\n",
    "0.00182% neon\n",
    "0.00052% helium\n",
    "0.00017% methane\n",
    "0.00011% krypton\n",
    "0.00006% hydrogen\n",
    "Source:[3]\n",
    "The name Terra /ˈtɛrə/ TERR-ə is occasionally used in\n",
    "scientific writing; it also sees use in science fiction to\n",
    "distinguish humanity's inhabited planet from others,[27] while in poetry Tellus /ˈtɛləs/ TELL-əs has\n",
    "been used to denote personification of the Earth.[28] Terra is also the name of the planet in some\n",
    "Romance languages, languages that evolved from Latin, like Italian and Portuguese, while in other\n",
    "Romance languages the word gave rise to names with slightly altered spellings, like the Spanish Tierra\n",
    "and the French Terre. The Latinate form Gaea (English: /ˈdʒiː.ə/ DJEE-ə) of the Greek poetic name\n",
    "Gaia ([ɡâi ̯ .a] or [ɡâj.ja]) is rare, though the alternative spelling Gaia has become common due to the\n",
    "Gaia hypothesis, in which case its pronunciation is /ˈ\n",
    "ɡaɪ.ə/ GYE-ə rather than the more traditional\n",
    "English /ˈ\n",
    "ɡeɪ.ə/ GAY-ə.\n",
    "[29]\n",
    "There are a number of adjectives for the planet Earth. The word earthly is derived from Earth. From\n",
    "the Latin Terra comes terran /ˈtɛrən/ TERR-ən,\n",
    "[30] terrestrial /təˈ\n",
    "rɛstriəl/ tərr-EHST-ree-əl,\n",
    "[31] and\n",
    "(via French) terrene /təˈ\n",
    "riːn/ tə-REEN,\n",
    "[32] and from the Latin Tellus comes tellurian /tɛ\n",
    "ˈlʊəriən/ teh-\n",
    "LUURR-ee-ən[33] and telluric.\n",
    "[34]\n",
    "Natural history\n",
    "Formation\n",
    "A 2012 artistic impression of the early Solar System's\n",
    "protoplanetary disk from which Earth and other Solar\n",
    "System bodies were formed\n",
    "The oldest material found in the Solar System is\n",
    "dated to 4.5682 +0.0002\n",
    "−0.0004 Ga (billion years) ago.[35]\n",
    "By 4.54 ± 0.04 Ga the primordial Earth had\n",
    "formed.[36] The bodies in the Solar System formed\n",
    "and evolved with the Sun. In theory, a solar nebula\n",
    "partitions a volume out of a molecular cloud by\n",
    "gravitational collapse, which begins to spin and\n",
    "flatten into a circumstellar disk, and then the\n",
    "planets grow out of that disk with the Sun. A\n",
    "nebula contains gas, ice grains, and dust (including\n",
    "primordial nuclides). According to nebular theory,\n",
    "planetesimals formed by accretion, with the\n",
    "primordial Earth being estimated as likely taking\n",
    "anywhere from 70 to 100 million years to form.[37]\n",
    "Estimates of the age of the Moon range from 4.5\n",
    "Ga to significantly younger.[38] A leading hypothesis is that it was formed by accretion from material\n",
    "loosed from Earth after a Mars-sized object with about 10% of Earth's mass, named Theia, collided\n",
    "with Earth.[39] It hit Earth with a glancing blow and some of its mass merged with Earth.[40][41]\n",
    "Between approximately 4.0 and 3.8 Ga, numerous asteroid impacts during the Late Heavy\n",
    "Bombardment caused significant changes to the greater surface environment of the Moon and, by\n",
    "inference, to that of Earth.[42]\n",
    "After formation\n",
    "Earth's atmosphere and oceans were formed by volcanic activity and outgassing.\n",
    "[43] Water vapor from\n",
    "these sources condensed into the oceans, augmented by water and ice from asteroids, protoplanets,\n",
    "and comets.\n",
    "[44] Sufficient water to fill the oceans may have been on Earth since it formed.[45] In this\n",
    "model, atmospheric greenhouse gases kept the oceans from freezing when the newly forming Sun had\n",
    "only 70% of its current luminosity.\n",
    "[46] By 3.5 Ga, Earth's magnetic field was established, which helped\n",
    "prevent the atmosphere from being stripped away by the solar wind.\n",
    "[47]\n",
    "As the molten outer layer of Earth cooled it formed the first solid crust, which is thought to have been\n",
    "mafic in composition. The first continental crust, which was more felsic in composition, formed by the\n",
    "partial melting of this mafic crust.[49] The presence of grains of the mineral zircon of Hadean age in\n",
    "Eoarchean sedimentary rocks suggests that at least some felsic crust existed as early as 4.4 Ga, only\n",
    "140 Ma after Earth's formation.[50] There are two main models of how this initial small volume of\n",
    "continental crust evolved to reach its current abundance:[51] (1) a relatively steady growth up to the\n",
    "present day,[52] which is supported by the radiometric dating of continental crust globally and (2) an\n",
    "initial rapid growth in the volume of continental crust during the Archean, forming the bulk of the\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"earth.pdf\", \"rb\") as f:\n",
    "    file_bytes = base64.standard_b64encode(f.read()).decode(\"utf-8\")\n",
    "\n",
    "messages = []\n",
    "\n",
    "add_user_message(\n",
    "    messages,\n",
    "    [\n",
    "        {\n",
    "            \"type\": \"document\",\n",
    "            \"source\": {\n",
    "                \"type\": \"text\",\n",
    "                \"media_type\": \"text/plain\",\n",
    "                \"data\": article_text,\n",
    "            },\n",
    "            \"title\": \"Earth Article\",\n",
    "            \"citations\": {\"enabled\": True},\n",
    "        },\n",
    "        {\n",
    "            \"type\": \"text\",\n",
    "            \"text\": \"How were Earth's atmosphere and oceans were formed?\",\n",
    "        },\n",
    "    ],\n",
    ")\n",
    "\n",
    "chat(messages)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
