Skip to content

Commit e7ab0cd

Browse files
Backport PR #1224: Update documentation for setting API keys without revealing them (#1231)
Co-authored-by: Sanjiv Das <[email protected]>
1 parent d8d2650 commit e7ab0cd

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,29 @@ To use any AI model provider within this notebook, you'll need the appropriate c
3636

3737
Obtain the necessary credentials, such as API keys, from your model provider's platform.
3838

39-
You can set your keys using environment variables or in a code cell in your notebook.
40-
In a code cell, you can use the %env magic command to set the credentials as follows:
39+
You can set your keys in a code cell in your notebook or using environment variables.
40+
In a code cell, you can set the credentials as follows without revealing your key in the notebook:
4141

4242
```python
4343
# NOTE: Replace 'PROVIDER_API_KEY' with the credential key's name,
44-
# and replace 'YOUR_API_KEY_HERE' with the key.
44+
# and enter the API key when prompted by using the code shown below.
45+
46+
import getpass
47+
48+
# Enter your key
49+
key = getpass.getpass('Enter your PROVIDER API key: ')
50+
51+
# Set the environment variable without displaying the full key
52+
os.environ['PROVIDER_API_KEY'] = key
53+
```
54+
55+
:::{note}
56+
:name: using-env-key
57+
You may also set these keys directly using the `%env` magic command, but the key value may be echoed in the cell output. If you prefer to use `%env`, be sure to not share the notebook with people you don't trust, as this may leak your API keys.
58+
```
4559
%env PROVIDER_API_KEY=YOUR_API_KEY_HERE
4660
```
61+
:::
4762

4863
For more specific instructions for each model provider, refer to [the model providers documentation](https://jupyter-ai.readthedocs.io/en/latest/users/index.html#model-providers).
4964

docs/source/users/index.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,38 @@ The location of `ipython_config.py` file is documented in [IPython configuration
992992

993993
You can use magic commands with models hosted using Amazon SageMaker.
994994

995-
First, make sure that you've set your `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables either before starting JupyterLab or using the `%env` magic command within JupyterLab. For more information about environment variables, see [Environment variables to configure the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html) in AWS's documentation.
995+
First, make sure that you've set your `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables before starting JupyterLab as follows:
996+
```
997+
os.environ['AWS_ACCESS_KEY_ID'] = <your_aws_access_key_id>
998+
os.environ['AWS_SECRET_ACCESS_KEY'] = <your_aws_secret_access_key>
999+
```
1000+
1001+
You can also set the keys interactively and securely using the following code in your notebook:
1002+
1003+
```python
1004+
# NOTE: Enter the AWS access key id and the AWS secret access key when prompted by the code below
1005+
1006+
import getpass
1007+
1008+
# Enter your keys
1009+
access_key = getpass.getpass('Enter your AWS ACCESS KEY ID: ')
1010+
secret_access_key = getpass.getpass('Enter your AWS SECRET ACCESS KEY: ')
1011+
1012+
# Set the environment variable without displaying the full key
1013+
os.environ['AWS_ACCESS_KEY_ID'] = access_key
1014+
os.environ['AWS_SECRET_ACCESS_KEY'] = secret_access_key
1015+
```
1016+
1017+
:::{note}
1018+
:name: using-env-key
1019+
You may also set these keys directly using the `%env` magic command, but the key value may be echoed in the cell output. If you prefer to use `%env`, be sure to not share the notebook with people you don't trust, as this may leak your API keys.
1020+
```
1021+
%env AWS_ACCESS_KEY_ID = <your_aws_access_key_id>
1022+
%env AWS_SECRET_ACCESS_KEY = <your_aws_secret_access_key>
1023+
```
1024+
:::
1025+
1026+
For more information about environment variables, see [Environment variables to configure the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html) in AWS's documentation.
9961027

9971028
Jupyter AI supports language models hosted on SageMaker endpoints that use JSON schemas. Authenticate with AWS via the `boto3` SDK and have the credentials stored in the `default` profile. Guidance on how to do this can be found in the [`boto3` documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html).
9981029

examples/sagemaker.ipynb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,28 @@
1111
"\n",
1212
"This demo showcases the IPython magics Jupyter AI provides out-of-the-box for Amazon SageMaker.\n",
1313
"\n",
14-
"First, make sure that you've set your `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables either before starting JupyterLab or using the `%env` magic command within JupyterLab.\n",
14+
"First, make sure that you've set your `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables before starting JupyterLab as follows:\n",
15+
"```\n",
16+
"os.environ['AWS_ACCESS_KEY_ID'] = <your_aws_access_key_id>\n",
17+
"os.environ['AWS_SECRET_ACCESS_KEY'] = <your_aws_secret_access_key>\n",
18+
"```\n",
19+
"\n",
20+
"If you prefer to set these keys interactively in this notebook, then use the following code: \n",
21+
"```python\n",
22+
"# NOTE: Enter the AWS access key id and the AWS secret access key when prompted by the code below\n",
23+
"\n",
24+
"import getpass\n",
25+
"\n",
26+
"# Enter your keys \n",
27+
"access_key = getpass.getpass('Enter your AWS ACCESS KEY ID: ')\n",
28+
"secret_access_key = getpass.getpass('Enter your AWS SECRET ACCESS KEY: ')\n",
29+
"\n",
30+
"# Set the environment variable without displaying the full key\n",
31+
"os.environ['AWS_ACCESS_KEY_ID'] = access_key\n",
32+
"os.environ['AWS_SECRET_ACCESS_KEY'] = secret_access_key\n",
33+
"```\n",
34+
"\n",
35+
"**Note**: You may also set these keys directly using the `%env` magic command, but the key value may be echoed in the cell output. If you prefer to use `%env`, be sure to not share the notebook with people you don't trust, as this may leak your API keys. \n",
1536
"\n",
1637
"Then, load the IPython extension:"
1738
]

0 commit comments

Comments
 (0)