Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'Open in Colab' badge #91

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
532 changes: 271 additions & 261 deletions finetuning/finetuning_on_bedrock.ipynb

Large diffs are not rendered by default.

800 changes: 405 additions & 395 deletions misc/building_evals.ipynb

Large diffs are not rendered by default.

702 changes: 356 additions & 346 deletions misc/building_moderation_filter.ipynb

Large diffs are not rendered by default.

1,538 changes: 774 additions & 764 deletions misc/generate_test_cases.ipynb

Large diffs are not rendered by default.

1,048 changes: 529 additions & 519 deletions misc/how_to_enable_json_mode.ipynb

Large diffs are not rendered by default.

508 changes: 259 additions & 249 deletions misc/how_to_make_sql_queries.ipynb

Large diffs are not rendered by default.

1,036 changes: 523 additions & 513 deletions misc/illustrated_responses.ipynb

Large diffs are not rendered by default.

2,926 changes: 1,468 additions & 1,458 deletions misc/mc_qa.ipynb

Large diffs are not rendered by default.

1,838 changes: 924 additions & 914 deletions misc/metaprompt.ipynb

Large diffs are not rendered by default.

462 changes: 236 additions & 226 deletions misc/pdf_upload_summarization.ipynb

Large diffs are not rendered by default.

1,042 changes: 526 additions & 516 deletions misc/prompt_caching.ipynb

Large diffs are not rendered by default.

300 changes: 155 additions & 145 deletions misc/read_web_pages_with_haiku.ipynb
Original file line number Diff line number Diff line change
@@ -1,150 +1,160 @@
{
"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": [
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"<a href=\"https://colab.research.google.com/github/anthropics/anthropic-cookbook/blob/main/misc/read_web_pages_with_haiku.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"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"
]
"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"
}
],
"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
"nbformat": 4,
"nbformat_minor": 2
}
10 changes: 10 additions & 0 deletions misc/sampling_past_max_tokens.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"<a href=\"https://colab.research.google.com/github/anthropics/anthropic-cookbook/blob/main/misc/sampling_past_max_tokens.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Loading