Skip to content

Commit

Permalink
refactor: Simplify M2 config, improve setup nb
Browse files Browse the repository at this point in the history
Rename get_bedrock_client() `url_override` param to `endpoint_url` to
match the underlying boto3 equivalent, and swap the kwargs to
alphabetical order. Streamline connectivity customizations to reduce
the number of places M2 preview users in SageMaker need to edit.

Setup notebook updates: Lint the code (max line length, consistent
intendations & quotes, etc). Add imports to sample snippet for
rendering the SDXL image so it can be successfully copy/pasted into
SageMaker Studio and work. Add a 'next steps' cell mostly so running
the nb end-to-end doesn't always generate an additional cell.

Replicate the setup/connectivity updates to all other lab notebooks.
Add Studio kernel spec to intro of all lab notebooks.
  • Loading branch information
athewsey committed Jul 27, 2023
1 parent e736f21 commit c1c193f
Show file tree
Hide file tree
Showing 14 changed files with 2,570 additions and 1,086 deletions.
532 changes: 267 additions & 265 deletions 00_Intro/bedrock_boto3_setup.ipynb

Large diffs are not rendered by default.

93 changes: 42 additions & 51 deletions 01_Generation/00_generate_w_bedrock.ipynb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "dc40c48b-0c95-4757-a067-563cfccd51a5",
"metadata": {
"tags": []
},
"source": [
"# Invoke Bedrock model for text generation using zero-shot prompt"
"# Invoke Bedrock model for text generation using zero-shot prompt\n",
"\n",
"> *This notebook should work well with the **`Data Science 2.0`** kernel in SageMaker Studio*"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c9a413e2-3c34-4073-9000-d8556537bb6a",
"metadata": {},
Expand Down Expand Up @@ -46,23 +46,15 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "64baae27-2660-4a1e-b2e5-3de49d069362",
"metadata": {},
"source": [
"## Setup"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "37115f13",
"metadata": {},
"source": [
"#### ⚠️⚠️⚠️ Execute the following cells before running this notebook ⚠️⚠️⚠️\n",
"## Setup\n",
"\n",
"Before running the rest of this notebook, you'll need to run the cells below to (ensure necessary libraries are installed and) connect to Bedrock.\n",
"\n",
"For a detailed description on what the following cells do refer to [Bedrock boto3 setup](../00_Intro/bedrock_boto3_setup.ipynb) notebook."
"For more details on how the setup works and ⚠️ **whether you might need to make any changes**, refer to the [Bedrock boto3 setup notebook](../00_Intro/bedrock_boto3_setup.ipynb) notebook."
]
},
{
Expand All @@ -74,51 +66,51 @@
},
"outputs": [],
"source": [
"# Make sure you run `download-dependencies.sh` from the root of the repository to download the dependencies before running this cell\n",
"%pip install ../dependencies/botocore-1.29.162-py3-none-any.whl ../dependencies/boto3-1.26.162-py3-none-any.whl ../dependencies/awscli-1.27.162-py3-none-any.whl --force-reinstall\n",
"%pip install langchain==0.0.190 --quiet"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "776fd083",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"#### Un comment the following lines to run from your local environment outside of the AWS account with Bedrock access\n",
"# Make sure you ran `download-dependencies.sh` from the root of the repository first!\n",
"%pip install --force-reinstall \\\n",
" ../dependencies/awscli-1.27.162-py3-none-any.whl \\\n",
" ../dependencies/boto3-1.26.162-py3-none-any.whl \\\n",
" ../dependencies/botocore-1.29.162-py3-none-any.whl\n",
"\n",
"#import os\n",
"#os.environ['BEDROCK_ASSUME_ROLE'] = '<YOUR_VALUES>'\n",
"#os.environ['AWS_PROFILE'] = '<YOUR_VALUES>'"
"%pip install --quiet langchain==0.0.190"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "568a6d26-e3ee-4b0c-a1eb-efc4bff99994",
"id": "776fd083",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import boto3\n",
"import json\n",
"import os\n",
"import sys\n",
"\n",
"import boto3\n",
"\n",
"module_path = \"..\"\n",
"sys.path.append(os.path.abspath(module_path))\n",
"from utils import bedrock, print_ww\n",
"\n",
"os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'\n",
"boto3_bedrock = bedrock.get_bedrock_client(os.environ.get('BEDROCK_ASSUME_ROLE', None))"
"\n",
"# ---- ⚠️ Un-comment and edit the below lines as needed for your AWS setup ⚠️ ----\n",
"\n",
"# os.environ[\"AWS_DEFAULT_REGION\"] = \"<REGION_NAME>\" # E.g. \"us-east-1\"\n",
"# os.environ[\"AWS_PROFILE\"] = \"<YOUR_PROFILE>\"\n",
"# os.environ[\"BEDROCK_ASSUME_ROLE\"] = \"<YOUR_ROLE_ARN>\" # E.g. \"arn:aws:...\"\n",
"# os.environ[\"BEDROCK_ENDPOINT_URL\"] = \"<YOUR_ENDPOINT_URL>\" # E.g. \"https://...\"\n",
"\n",
"\n",
"boto3_bedrock = bedrock.get_bedrock_client(\n",
" assumed_role=os.environ.get(\"BEDROCK_ASSUME_ROLE\", None),\n",
" endpoint_url=os.environ.get(\"BEDROCK_ENDPOINT_URL\", None),\n",
" region=os.environ.get(\"AWS_DEFAULT_REGION\", None),\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "4f634211-3de1-4390-8c3f-367af5554c39",
"metadata": {},
Expand All @@ -145,7 +137,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "cc9784e5-5e9d-472d-8ef1-34108ee4968b",
"metadata": {},
Expand Down Expand Up @@ -176,7 +167,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c4ca6751",
"metadata": {},
Expand All @@ -196,7 +186,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "088cf6bf-dd73-4710-a0cc-6c11d220c431",
"metadata": {},
Expand All @@ -205,7 +194,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "379498f2",
"metadata": {},
Expand Down Expand Up @@ -238,7 +226,9 @@
"cell_type": "code",
"execution_count": null,
"id": "3748383a-c140-407f-a7f6-8f140ad57680",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# The relevant portion of the response begins after the first newline character\n",
Expand All @@ -249,7 +239,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "2d69e1a0",
"metadata": {},
Expand All @@ -264,7 +253,9 @@
"cell_type": "code",
"execution_count": null,
"id": "ad073290",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"response = boto3_bedrock.invoke_model_with_response_stream(body=body, modelId=modelId, accept=accept, contentType=contentType)\n",
Expand All @@ -283,7 +274,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "9a788be5",
"metadata": {},
Expand All @@ -295,7 +285,9 @@
"cell_type": "code",
"execution_count": null,
"id": "02d48c73",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"print('\\t\\t\\x1b[31m**COMPLETE OUTPUT**\\x1b[0m\\n')\n",
Expand All @@ -304,7 +296,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "64b08b3b",
"metadata": {},
Expand Down Expand Up @@ -900,9 +891,9 @@
],
"instance_type": "ml.t3.medium",
"kernelspec": {
"display_name": "tmp-bedrock",
"display_name": "Python 3 (Data Science 2.0)",
"language": "python",
"name": "python3"
"name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-west-2:236514542706:image/sagemaker-data-science-38"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -914,7 +905,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.8.13"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit c1c193f

Please sign in to comment.