prompt-eng-interactive-tutorial
prompt-eng-interactive-tutorial copied to clipboard
Anthropic 1P/00_Tutorial risks insecure API key usage
Issue
Notebook Anthropic 1P/00_Tutorial_How-To.ipynb asks users to enter their Anthropic API Key in a notebook cell.
This pattern comes with elevated risk that inexperienced users will accidentally commit their API key to a public repo.
Suggested change
Here is one way to encourage a safe pattern for managing API keys:
- In the
README, instruct users to place their API key in a.envfile. - Modify each notebook to use
python-dotenvto load the environment variables each time the notebook runs.
Details
- Add
.envto.gitignoreto ensure it isn't committed - Add a
.env_templatefile to the repo root which users can copy into a.envfile. This can also include MODEL_NAME:
ANTHROPIC_API_KEY=<your-api-key-here>
MODEL_NAME=claude-3-haiku-20240307
- Add
!pip install python-dotenvto all notebooks inAnthropic 1P/ - Add the following env setup code to the import section of all notebooks in
Anthropic 1P/
import os
from dotenv import load_dotenv
load_dotenv();
API_KEY = os.getenv("ANTHROPIC_API_KEY")
MODEL_NAME = os.getenv("MODEL_NAME")
- Update
README.mdto instruct users to create.envand add their API Key, e.g.
Copy .env_template to .env:
cp .env_template .env
Within .env, replace <your-api-key-here> with your Anthropic API key (no quotes or brackets are needed).
Note:
.envis included in the.gitignoreto avoid accidentally committing your API key. The notebooks in tutorial use thepython-dotenvlibrary to load environment variables from.envin each notebook. Be sure to avoid printing and committing the actual API key inside your notebooks.