|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "e78dc97c", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Introduction to SageMaker JumpStart - Text Generation" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "b4e567a4", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "---\n", |
| 17 | + "\n", |
| 18 | + "This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.\n", |
| 19 | + "\n", |
| 20 | + "\n", |
| 21 | + "\n", |
| 22 | + "---" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "id": "2d4e36b9", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "In this demo notebook, we demonstrate how to use the SageMaker Python SDK to deploy a SageMaker JumpStart text generation model and invoke the endpoint." |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "id": "0af77efb", |
| 36 | + "metadata": {}, |
| 37 | + "source": [ |
| 38 | + "## Setup\n", |
| 39 | + "First, upgrade to the latest sagemaker SDK to ensure all available models are deployable." |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": null, |
| 45 | + "id": "9b05b931-992e-4526-978d-f03196874a3b", |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "%pip install --quiet --upgrade sagemaker jmespath" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "id": "5dbb6b35", |
| 55 | + "metadata": {}, |
| 56 | + "source": [ |
| 57 | + "Select the desired model to deploy. The provided dropdown filters all text generation models available in SageMaker JumpStart." |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": null, |
| 63 | + "id": "f625a488", |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "from ipywidgets import Dropdown\n", |
| 68 | + "from sagemaker.jumpstart.notebook_utils import list_jumpstart_models\n", |
| 69 | + "\n", |
| 70 | + "\n", |
| 71 | + "dropdown = Dropdown(\n", |
| 72 | + " options=list_jumpstart_models(\"search_keywords includes Text Generation\"),\n", |
| 73 | + " value=\"huggingface-llm-mistral-7b-instruct\",\n", |
| 74 | + " description=\"Select a JumpStart text generation model:\",\n", |
| 75 | + " style={\"description_width\": \"initial\"},\n", |
| 76 | + " layout={\"width\": \"max-content\"},\n", |
| 77 | + ")\n", |
| 78 | + "display(dropdown)" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": null, |
| 84 | + "id": "8a40df34", |
| 85 | + "metadata": { |
| 86 | + "jumpStartAlterations": [ |
| 87 | + "modelIdOnly" |
| 88 | + ], |
| 89 | + "tags": [] |
| 90 | + }, |
| 91 | + "outputs": [], |
| 92 | + "source": [ |
| 93 | + "model_id = dropdown.value\n", |
| 94 | + "model_version = \"*\"" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "markdown", |
| 99 | + "id": "3c0a0388", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "## Deploy model\n", |
| 103 | + "\n", |
| 104 | + "Create a `JumpStartModel` object, which initializes default model configurations conditioned on the selected instance type. JumpStart already sets a default instance type, but you can deploy the model on other instance types by passing `instance_type` to the `JumpStartModel` class." |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": null, |
| 110 | + "id": "85a2a8e5-789f-4041-9927-221257126653", |
| 111 | + "metadata": { |
| 112 | + "tags": [] |
| 113 | + }, |
| 114 | + "outputs": [], |
| 115 | + "source": [ |
| 116 | + "from sagemaker.jumpstart.model import JumpStartModel\n", |
| 117 | + "\n", |
| 118 | + "\n", |
| 119 | + "model = JumpStartModel(model_id=model_id, model_version=model_version)" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "markdown", |
| 124 | + "id": "1259ad4f", |
| 125 | + "metadata": {}, |
| 126 | + "source": [ |
| 127 | + "You can now deploy the model using SageMaker JumpStart. If the selected model is gated, you will need to accept the end-user license agreement (EULA) prior to deployment. This is accomplished by providing the `accept_eula=True` argument to the `deploy` method. The deployment might take few minutes. " |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "code", |
| 132 | + "execution_count": null, |
| 133 | + "id": "5f3b42ed", |
| 134 | + "metadata": {}, |
| 135 | + "outputs": [], |
| 136 | + "source": [ |
| 137 | + "predictor = model.deploy()" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "markdown", |
| 142 | + "id": "f10cf06c", |
| 143 | + "metadata": {}, |
| 144 | + "source": [ |
| 145 | + "## Invoke the endpoint\n", |
| 146 | + "\n", |
| 147 | + "This section demonstrates how to invoke the endpoint using example payloads that are retrieved programmatically from the `JumpStartModel` object. You can replace these example payloads with your own payloads." |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": null, |
| 153 | + "id": "cbb364d8", |
| 154 | + "metadata": {}, |
| 155 | + "outputs": [], |
| 156 | + "source": [ |
| 157 | + "example_payloads = model.retrieve_all_examples()" |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "code", |
| 162 | + "execution_count": null, |
| 163 | + "id": "bf5899c8", |
| 164 | + "metadata": {}, |
| 165 | + "outputs": [], |
| 166 | + "source": [ |
| 167 | + "import jmespath\n", |
| 168 | + "\n", |
| 169 | + "\n", |
| 170 | + "for payload in example_payloads:\n", |
| 171 | + " response = predictor.predict(payload.body)\n", |
| 172 | + " generated_text = jmespath.search(payload.raw_payload[\"output_keys\"][\"generated_text\"], response)\n", |
| 173 | + " print(\"Input:\\n\", payload.body[payload.prompt_key])\n", |
| 174 | + " print(\"Output:\\n\", generated_text.strip())\n", |
| 175 | + " print(\"\\n===============\\n\")" |
| 176 | + ] |
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "markdown", |
| 180 | + "id": "f6a53e57", |
| 181 | + "metadata": {}, |
| 182 | + "source": [ |
| 183 | + "## Clean up the endpoint\n", |
| 184 | + "Don't forget to clean up resources when finished to avoid unnecessary charges." |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "code", |
| 189 | + "execution_count": null, |
| 190 | + "id": "0e19a2bb", |
| 191 | + "metadata": {}, |
| 192 | + "outputs": [], |
| 193 | + "source": [ |
| 194 | + "predictor.delete_predictor()" |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "markdown", |
| 199 | + "id": "c4998b86", |
| 200 | + "metadata": {}, |
| 201 | + "source": [ |
| 202 | + "## Notebook CI Test Results\n", |
| 203 | + "\n", |
| 204 | + "This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.\n", |
| 205 | + "\n", |
| 206 | + "\n", |
| 207 | + "\n", |
| 208 | + "\n", |
| 209 | + "\n", |
| 210 | + "\n", |
| 211 | + "\n", |
| 212 | + "\n", |
| 213 | + "\n", |
| 214 | + "\n", |
| 215 | + "\n", |
| 216 | + "\n", |
| 217 | + "\n", |
| 218 | + "\n", |
| 219 | + "\n", |
| 220 | + "\n", |
| 221 | + "\n", |
| 222 | + "\n", |
| 223 | + "\n", |
| 224 | + "\n", |
| 225 | + "\n", |
| 226 | + "\n", |
| 227 | + "\n", |
| 228 | + "\n", |
| 229 | + "\n", |
| 230 | + "\n", |
| 231 | + "\n", |
| 232 | + "\n", |
| 233 | + "\n", |
| 234 | + "\n", |
| 235 | + "\n", |
| 236 | + "\n" |
| 237 | + ] |
| 238 | + } |
| 239 | + ], |
| 240 | + "metadata": { |
| 241 | + "instance_type": "ml.t3.medium", |
| 242 | + "kernelspec": { |
| 243 | + "display_name": "Python 3 (Data Science 2.0)", |
| 244 | + "language": "python", |
| 245 | + "name": "python3" |
| 246 | + }, |
| 247 | + "language_info": { |
| 248 | + "codemirror_mode": { |
| 249 | + "name": "ipython", |
| 250 | + "version": 3 |
| 251 | + }, |
| 252 | + "file_extension": ".py", |
| 253 | + "mimetype": "text/x-python", |
| 254 | + "name": "python", |
| 255 | + "nbconvert_exporter": "python", |
| 256 | + "pygments_lexer": "ipython3", |
| 257 | + "version": "3.8.13" |
| 258 | + } |
| 259 | + }, |
| 260 | + "nbformat": 4, |
| 261 | + "nbformat_minor": 5 |
| 262 | +} |
0 commit comments