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

Refactor to follow convention of saving API_KEY in .env file #53

Open
wants to merge 2 commits into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,114 @@
},
{
"cell_type": "markdown",
"metadata": {},
"source": [],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"3. Set up your API key and model name. Replace `\"your_api_key_here\"` with your actual Anthropic API key."
]
"## Getting an API key\n",
"\n",
"To authenticate your requests to the Claude API, you'll need an API key.\n",
"\n",
"Follow these steps to obtain your API key:\n",
"\n",
"1. If you haven't already, sign up for an Anthropic account by visiting https://console.anthropic.com\n",
"2. Once you've created your account and logged in, navigate to the API settings page. You can find this page by clicking on your profile icon in the top-right corner and selecting \"API Keys\" from the dropdown menu, or by navigating to the \"API Keys\" menu in the Settings tab.\n",
"3. On the API settings page, click on the \"Create Key\" button. A modal window will appear, prompting you to give your key a descriptive name. Choose a name that reflects the purpose or project you'll be using the key for. You can create as many keys as you want within your account (note that rate and message limits apply at the account level, not the API key level).\n",
"4. After entering a name, click on the \"Create\" button. Your new API key will be generated and displayed on the screen.\n",
"> Make sure to copy this key, as you won't be able to view it again once you navigate away from this page.\n",
"\n",
"![signup.png](attachment:signup.png)\n",
"\n",
"Remember, your API key is a sensitive piece of information that grants access to your Anthropic account. Treat it like a password and never share it publicly or commit it to version control systems like Git.\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Safely storing your API key\n",
"\n",
"While you can hardcode your API key directly in your Python scripts, it's generally considered best practice to keep sensitive information, like API keys, separate from your codebase. One common approach is to store the API key in a `.env` file and load it using the `python-dotenv package`. Here's how you can set it up:\n",
"\n",
"Create a new file called `.env` in the same directory as your notebook."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Add your API key to the newly created `.env` file using the following format:\n",
"\n",
"```\n",
"ANTHROPIC_API_KEY=put-your-api-key-here\n",
"```\n",
"\n",
"Make sure to save the `.env` file"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Install the python-dotenv package by running the following command in your terminal or notebook:"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"API_KEY = \"your_api_key_here\"\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"#To install the package from a notebook:\n",
"%pip install python-dotenv\n",
"\n",
"# Stores the API_KEY & MODEL_NAME variables for use across notebooks within the IPython store\n",
"%store API_KEY\n",
"%store MODEL_NAME"
]
"#To install the package from your terminal:\n",
"# pip install python-dotenv"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"We can now load the API key from the .env file using the `load_dotenv()` function from the dotenv module with the following code:"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"from dotenv import load_dotenv\n",
"import os\n",
"\n",
"load_dotenv()\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Run the notebook cells in order, following the instructions provided."
"Run the notebook cells in order, following the instructions provided."
]
},
{
Expand Down Expand Up @@ -84,10 +168,10 @@
"metadata": {},
"outputs": [],
"source": [
"import anthropic\n",
"\n",
"client = anthropic.Anthropic(api_key=API_KEY)\n",
"from anthropic import Anthropic\n",
"\n",
"client = Anthropic()\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"def get_completion(prompt: str):\n",
" message = client.messages.create(\n",
" model=MODEL_NAME,\n",
Expand Down Expand Up @@ -121,13 +205,6 @@
"# Get Claude's response\n",
"print(get_completion(prompt))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `API_KEY` and `MODEL_NAME` variables defined earlier will be used throughout the tutorial. Just make sure to run the cells for each tutorial page from top to bottom."
]
}
],
"metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
"\n",
"# Import python's built-in regular expression library\n",
"import re\n",
"import anthropic\n",
"from dotenv import load_dotenv\n",
"from anthropic import Anthropic\n",
"\n",
"# Retrieve the API_KEY & MODEL_NAME variables from the IPython store\n",
"%store -r API_KEY\n",
"%store -r MODEL_NAME\n",
"\n",
"client = anthropic.Anthropic(api_key=API_KEY)\n",
"load_dotenv() # ensure you have a `.env` file with your API key. follow the prompts in 00_Tutorial to set up\n",
"client = Anthropic()\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"\n",
"def get_completion(prompt: str, system_prompt=\"\"):\n",
" message = client.messages.create(\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
"\n",
"# Import python's built-in regular expression library\n",
"import re\n",
"import anthropic\n",
"from dotenv import load_dotenv\n",
"from anthropic import Anthropic\n",
"\n",
"# Retrieve the API_KEY & MODEL_NAME variables from the IPython store\n",
"%store -r API_KEY\n",
"%store -r MODEL_NAME\n",
"\n",
"client = anthropic.Anthropic(api_key=API_KEY)\n",
"load_dotenv() # ensure you have a `.env` file with your API key. follow the prompts in 00_Tutorial to set up\n",
"client = Anthropic()\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"\n",
"# Note that we changed max_tokens to 4K just for this lesson to allow for longer completions in the exercises\n",
"def get_completion(prompt: str, system_prompt=\"\"):\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
"\n",
"# Import python's built-in regular expression library\n",
"import re\n",
"import anthropic\n",
"from dotenv import load_dotenv\n",
"from anthropic import Anthropic\n",
"\n",
"# Retrieve the API_KEY & MODEL_NAME variables from the IPython store\n",
"%store -r API_KEY\n",
"%store -r MODEL_NAME\n",
"\n",
"client = anthropic.Anthropic(api_key=API_KEY)\n",
"load_dotenv() # ensure you have a `.env` file with your API key. follow the prompts in 00_Tutorial to set up\n",
"client = Anthropic()\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"\n",
"def get_completion(prompt: str, system_prompt=\"\"):\n",
" message = client.messages.create(\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
"\n",
"# Import python's built-in regular expression library\n",
"import re\n",
"import anthropic\n",
"from dotenv import load_dotenv\n",
"from anthropic import Anthropic\n",
"\n",
"# Retrieve the API_KEY & MODEL_NAME variables from the IPython store\n",
"%store -r API_KEY\n",
"%store -r MODEL_NAME\n",
"\n",
"client = anthropic.Anthropic(api_key=API_KEY)\n",
"load_dotenv() # ensure you have a `.env` file with your API key. follow the prompts in 00_Tutorial to set up\n",
"client = Anthropic()\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"\n",
"def get_completion(prompt: str, system_prompt=\"\"):\n",
" message = client.messages.create(\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
"\n",
"# Import python's built-in regular expression library\n",
"import re\n",
"import anthropic\n",
"from dotenv import load_dotenv\n",
"from anthropic import Anthropic\n",
"\n",
"# Retrieve the API_KEY & MODEL_NAME variables from the IPython store\n",
"%store -r API_KEY\n",
"%store -r MODEL_NAME\n",
"\n",
"client = anthropic.Anthropic(api_key=API_KEY)\n",
"load_dotenv() # ensure you have a `.env` file with your API key. follow the prompts in 00_Tutorial to set up\n",
"client = Anthropic()\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"\n",
"# New argument added for prefill text, with a default value of an empty string\n",
"def get_completion(prompt: str, system_prompt=\"\", prefill=\"\"):\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
"\n",
"# Import python's built-in regular expression library\n",
"import re\n",
"import anthropic\n",
"from dotenv import load_dotenv\n",
"from anthropic import Anthropic\n",
"\n",
"# Retrieve the API_KEY & MODEL_NAME variables from the IPython store\n",
"%store -r API_KEY\n",
"%store -r MODEL_NAME\n",
"\n",
"client = anthropic.Anthropic(api_key=API_KEY)\n",
"load_dotenv() # ensure you have a `.env` file with your API key. follow the prompts in 00_Tutorial to set up\n",
"client = Anthropic()\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"\n",
"def get_completion(prompt: str, system_prompt=\"\", prefill=\"\"):\n",
" message = client.messages.create(\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
"\n",
"# Import python's built-in regular expression library\n",
"import re\n",
"import anthropic\n",
"from dotenv import load_dotenv\n",
"from anthropic import Anthropic\n",
"\n",
"# Retrieve the API_KEY & MODEL_NAME variables from the IPython store\n",
"%store -r API_KEY\n",
"%store -r MODEL_NAME\n",
"\n",
"client = anthropic.Anthropic(api_key=API_KEY)\n",
"load_dotenv() # ensure you have a `.env` file with your API key. follow the prompts in 00_Tutorial to set up\n",
"client = Anthropic()\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"\n",
"def get_completion(prompt: str, system_prompt=\"\", prefill=\"\"):\n",
" message = client.messages.create(\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
"\n",
"# Import python's built-in regular expression library\n",
"import re\n",
"import anthropic\n",
"from dotenv import load_dotenv\n",
"from anthropic import Anthropic\n",
"\n",
"# Retrieve the API_KEY & MODEL_NAME variables from the IPython store\n",
"%store -r API_KEY\n",
"%store -r MODEL_NAME\n",
"\n",
"client = anthropic.Anthropic(api_key=API_KEY)\n",
"load_dotenv() # ensure you have a `.env` file with your API key. follow the prompts in 00_Tutorial to set up\n",
"client = Anthropic()\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"\n",
"def get_completion(prompt: str, system_prompt=\"\", prefill=\"\"):\n",
" message = client.messages.create(\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
"\n",
"# Import python's built-in regular expression library\n",
"import re\n",
"import anthropic\n",
"from dotenv import load_dotenv\n",
"from anthropic import Anthropic\n",
"\n",
"# Retrieve the API_KEY & MODEL_NAME variables from the IPython store\n",
"%store -r API_KEY\n",
"%store -r MODEL_NAME\n",
"\n",
"client = anthropic.Anthropic(api_key=API_KEY)\n",
"load_dotenv() # ensure you have a `.env` file with your API key. follow the prompts in 00_Tutorial to set up\n",
"client = Anthropic()\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"\n",
"def get_completion(prompt: str, system_prompt=\"\", prefill=\"\"):\n",
" message = client.messages.create(\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
"\n",
"# Import python's built-in regular expression library\n",
"import re\n",
"import anthropic\n",
"from dotenv import load_dotenv\n",
"from anthropic import Anthropic\n",
"\n",
"# Retrieve the API_KEY & MODEL_NAME variables from the IPython store\n",
"%store -r API_KEY\n",
"%store -r MODEL_NAME\n",
"\n",
"client = anthropic.Anthropic(api_key=API_KEY)\n",
"load_dotenv() # ensure you have a `.env` file with your API key. follow the prompts in 00_Tutorial to set up\n",
"client = Anthropic()\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"\n",
"# Has been rewritten to take in a messages list of arbitrary length\n",
"def get_completion(messages, system_prompt=\"\"):\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@
"\n",
"# Import python's built-in regular expression library\n",
"import re\n",
"import anthropic\n",
"from dotenv import load_dotenv\n",
"from anthropic import Anthropic\n",
"\n",
"# Retrieve the API_KEY variable from the IPython store\n",
"%store -r API_KEY\n",
"load_dotenv() # ensure you have a `.env` file with your API key. follow the prompts in 00_Tutorial to set up\n",
"client = Anthropic()\n",
"# Rewrittten to call Claude 3 Sonnet, which is generally better at tool use, and include stop_sequences\n",
"MODEL_NAME = \"claude-3-sonnet-20240229\"\n",
"\n",
"client = anthropic.Anthropic(api_key=API_KEY)\n",
"\n",
"# Rewrittten to call Claude 3 Sonnet, which is generally better at tool use, and include stop_sequences\n",
"def get_completion(messages, system_prompt=\"\", prefill=\"\",stop_sequences=None):\n",
" message = client.messages.create(\n",
" model=\"claude-3-sonnet-20240229\",\n",
" model=MODEL_NAME,\n",
" max_tokens=2000,\n",
" temperature=0.0,\n",
" system=system_prompt,\n",
Expand Down