misc/read_web_pages_with_haiku.ipynb (150 lines of code) (raw):

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Summarizing Web Page Content with Claude 3 Haiku\n", "In this recipe, we'll learn how to fetch the content of a web page given its URL and then use Anthropic's Claude API to generate a summary of the page's content.\n", "\n", "Let's start by installing the Anthropic library." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup\n", "First, let's install the necessary libraries and setup our Anthropic client with our API key." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Install the necessary libraries\n", "%pip install anthropic" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Import the required libraries\n", "from anthropic import Anthropic\n", "\n", "# Set up the Anthropic API client\n", "client = Anthropic()\n", "MODEL_NAME = \"claude-3-haiku-20240229\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Fetching the Web Page Content\n", "First, we need to fetch the content of the web page using the provided URL. We'll use the requests library for this purpose." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import requests\n", "\n", "url = \"https://en.wikipedia.org/wiki/96th_Academy_Awards\"\n", "response = requests.get(url)\n", "\n", "if response.status_code == 200:\n", " page_content = response.text\n", "else:\n", " print(f\"Failed to fetch the web page. Status code: {response.status_code}\")\n", " exit(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Preparing the Input for Claude\n", "Next, we'll prepare the input for the Claude API. We'll create a message that includes the page content and a prompt asking Claude to summarize it." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "prompt = f\"<content>{page_content}</content>Please produce a concise summary of the web page content.\"\n", "\n", "messages = [\n", " {\"role\": \"user\", \"content\": prompt}\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Generating the Summary\n", "Now, we'll call the Haiku to generate a summary of the web page content." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The 96th Academy Awards ceremony took place on March 10, 2024 at the Dolby Theatre in Los Angeles. The ceremony, hosted by Jimmy Kimmel, presented Academy Awards (Oscars) in 23 categories honoring films released in 2023. \n", "\n", "The big winner of the night was the film \"Oppenheimer,\" which won a leading 7 awards including Best Picture, Best Director for Christopher Nolan, and several technical awards. Other major winners were \"Poor Things\" with 4 awards and \"The Zone of Interest\" with 2 awards. Several notable records and milestones were set, including Steven Spielberg receiving his 13th Best Picture nomination, and Billie Eilish and Finneas O'Connell becoming the youngest two-time Oscar winners.\n", "\n", "The ceremony featured musical performances, tributes to past winners, and a touching \"In Memoriam\" segment. However, it also faced some criticism, such as the distracting and hard-to-follow \"In Memoriam\" presentation and political controversy around a director's comments about the Israel-Gaza conflict.\n" ] } ], "source": [ "response = client.messages.create(\n", " model=\"claude-3-haiku-20240307\",\n", " max_tokens=1024,\n", " messages=messages\n", ")\n", "\n", "summary = response.content[0].text\n", "print(summary)" ] } ], "metadata": { "kernelspec": { "display_name": "myenv", "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": 2 }