|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Tutorial How-To\n", |
| 8 | + "\n", |
| 9 | + "This tutorial **requires an API key** for interaction. If you don't have an API key, you can sign up for one via the [Anthropic Console](https://console.anthropic.com/) or view our [static tutorial answer key](https://docs.google.com/spreadsheets/u/0/d/1jIxjzUWG-6xBVIa2ay6yDpLyeuOh_hR_ZB75a47KX_E/edit) instead." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "## How to get started\n", |
| 17 | + "\n", |
| 18 | + "1. Clone this repository to your local machine.\n", |
| 19 | + "\n", |
| 20 | + "2. Install the required dependencies by running the following command:\n", |
| 21 | + " " |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 2, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [ |
| 29 | + { |
| 30 | + "name": "stdout", |
| 31 | + "output_type": "stream", |
| 32 | + "text": [ |
| 33 | + "Note: you may need to restart the kernel to use updated packages.\n", |
| 34 | + "Note: you may need to restart the kernel to use updated packages.\n" |
| 35 | + ] |
| 36 | + } |
| 37 | + ], |
| 38 | + "source": [ |
| 39 | + "%pip install -qU pip\n", |
| 40 | + "%pip install -qr ../utils/requirements.txt" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "markdown", |
| 45 | + "metadata": {}, |
| 46 | + "source": [ |
| 47 | + "3. Restart the kernel after installing dependencies" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": null, |
| 53 | + "metadata": {}, |
| 54 | + "outputs": [], |
| 55 | + "source": [ |
| 56 | + "# restart kernel\n", |
| 57 | + "from IPython.core.display import HTML\n", |
| 58 | + "HTML(\"<script>Jupyter.notebook.kernel.restart()</script>\")" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "markdown", |
| 63 | + "metadata": {}, |
| 64 | + "source": [ |
| 65 | + "---\n", |
| 66 | + "\n", |
| 67 | + "## Usage Notes & Tips 💡\n", |
| 68 | + "\n", |
| 69 | + "- This course uses Claude 3 Haiku with temperature 0. We will talk more about temperature later in the course. For now, it's enough to understand that these settings yield more deterministic results. All prompt engineering techniques in this course also apply to previous generation legacy Claude models such as Claude 2 and Claude Instant 1.2.\n", |
| 70 | + "\n", |
| 71 | + "- You can use `Shift + Enter` to execute the cell and move to the next one.\n", |
| 72 | + "\n", |
| 73 | + "- When you reach the bottom of a tutorial page, navigate to the next numbered file in the folder, or to the next numbered folder if you're finished with the content within that chapter file.\n", |
| 74 | + "\n", |
| 75 | + "### The Anthropic SDK & the Messages API\n", |
| 76 | + "We will be using the [Anthropic python SDK](https://docs.anthropic.com/claude/reference/claude-on-amazon-bedrock) and the [Messages API](https://docs.anthropic.com/claude/reference/messages_post) throughout this tutorial.\n", |
| 77 | + "\n", |
| 78 | + "Below is an example of what running a prompt will look like in this tutorial." |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "markdown", |
| 83 | + "metadata": {}, |
| 84 | + "source": [ |
| 85 | + "First, we set and store the model name and region." |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": null, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "import boto3\n", |
| 95 | + "session = boto3.Session() # create a boto3 session to dynamically get and set the region name\n", |
| 96 | + "AWS_REGION = session.region_name\n", |
| 97 | + "print(\"AWS Region:\", AWS_REGION)\n", |
| 98 | + "MODEL_NAME = \"anthropic.claude-3-haiku-20240307-v1:0\"\n", |
| 99 | + "\n", |
| 100 | + "%store MODEL_NAME\n", |
| 101 | + "%store AWS_REGION" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "markdown", |
| 106 | + "metadata": {}, |
| 107 | + "source": [ |
| 108 | + "Then, we create `get_completion`, which is a helper function that sends a prompt to Claude and returns Claude's generated response. Run that cell now." |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "code", |
| 113 | + "execution_count": null, |
| 114 | + "metadata": {}, |
| 115 | + "outputs": [], |
| 116 | + "source": [ |
| 117 | + "from anthropic import AnthropicBedrock\n", |
| 118 | + "\n", |
| 119 | + "client = AnthropicBedrock(aws_region=AWS_REGION)\n", |
| 120 | + "\n", |
| 121 | + "def get_completion(prompt, system=''):\n", |
| 122 | + " message = client.messages.create(\n", |
| 123 | + " model=MODEL_NAME,\n", |
| 124 | + " max_tokens=2000,\n", |
| 125 | + " temperature=0.0,\n", |
| 126 | + " messages=[\n", |
| 127 | + " {\"role\": \"user\", \"content\": prompt}\n", |
| 128 | + " ],\n", |
| 129 | + " system=system\n", |
| 130 | + " )\n", |
| 131 | + " return message.content[0].text" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "markdown", |
| 136 | + "metadata": {}, |
| 137 | + "source": [ |
| 138 | + "Now we will write out an example prompt for Claude and print Claude's output by running our `get_completion` helper function. Running the cell below will print out a response from Claude beneath it.\n", |
| 139 | + "\n", |
| 140 | + "Feel free to play around with the prompt string to elicit different responses from Claude." |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": null, |
| 146 | + "metadata": {}, |
| 147 | + "outputs": [], |
| 148 | + "source": [ |
| 149 | + "# Prompt\n", |
| 150 | + "prompt = \"Hello, Claude!\"\n", |
| 151 | + "\n", |
| 152 | + "# Get Claude's response\n", |
| 153 | + "print(get_completion(prompt))" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "markdown", |
| 158 | + "metadata": {}, |
| 159 | + "source": [ |
| 160 | + "The `MODEL_NAME` and `AWS_REGION` variables defined earlier will be used throughout the tutorial. Just make sure to run the cells for each tutorial page from top to bottom." |
| 161 | + ] |
| 162 | + } |
| 163 | + ], |
| 164 | + "metadata": { |
| 165 | + "kernelspec": { |
| 166 | + "display_name": "py310", |
| 167 | + "language": "python", |
| 168 | + "name": "python3" |
| 169 | + }, |
| 170 | + "language_info": { |
| 171 | + "codemirror_mode": { |
| 172 | + "name": "ipython", |
| 173 | + "version": 3 |
| 174 | + }, |
| 175 | + "file_extension": ".py", |
| 176 | + "mimetype": "text/x-python", |
| 177 | + "name": "python", |
| 178 | + "nbconvert_exporter": "python", |
| 179 | + "pygments_lexer": "ipython3", |
| 180 | + "version": "3.11.5" |
| 181 | + } |
| 182 | + }, |
| 183 | + "nbformat": 4, |
| 184 | + "nbformat_minor": 2 |
| 185 | +} |
0 commit comments