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

added bedrock example notebooks #4554

Open
wants to merge 6 commits into
base: main
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
@@ -0,0 +1,215 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "310b0dbd",
"metadata": {},
"source": [
"# Invoking Claude via Amazon Bedrock - Streaming"
]
},
{
"cell_type": "markdown",
"id": "b8c55efc",
"metadata": {},
"source": [
"## Import required Libraries ##\n",
"\n",
"This cell imports the required libraries required to interact with Claude via Amazon Bedrock. **Boto3** is the Amazon SDK and how you interact with AWS services and the **json** library helps us parse json.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "bd598b75",
"metadata": {},
"outputs": [],
"source": [
"import boto3\n",
"import json"
]
},
{
"cell_type": "markdown",
"id": "9b3747f7",
"metadata": {},
"source": [
"## Create Bedrock Client\n",
"In this cell we're creating a Bedrock client in the us-west-2 region and assigning it to a variable named **brt**."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c64b1c67",
"metadata": {},
"outputs": [],
"source": [
"brt = boto3.client(service_name='bedrock-runtime', region_name='us-west-2')"
]
},
{
"cell_type": "markdown",
"id": "49e17b98",
"metadata": {},
"source": [
"## Create Prompt\n",
"In this cell we're creating a prompt and saving it as a variable named prompt."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "85e607b2",
"metadata": {},
"outputs": [],
"source": [
"prompt = \"What are the steps to make pancakes?\""
]
},
{
"cell_type": "markdown",
"id": "5e42834e",
"metadata": {},
"source": [
"## Create body\n",
"In this cell we're adding the prompt in the way Claude expects i.e. \\human and \\assistant, and including several hyperparameters that are expected"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "358ddea6",
"metadata": {},
"outputs": [],
"source": [
"body = json.dumps({\n",
" \"prompt\": \"\\n\\nHuman:\\n\" + prompt + \"\\n\\nAssistant:\",\n",
" \"max_tokens_to_sample\": 300,\n",
" \"temperature\": 0.1,\n",
" \"top_p\": 0.9,\n",
"})"
]
},
{
"cell_type": "markdown",
"id": "65f7e2c3",
"metadata": {},
"source": [
"## Keyword Arguments \n",
"Setting the model to Claude, add the body, hyperparameters, and some other required arguments"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9d577d66",
"metadata": {},
"outputs": [],
"source": [
"kwargs = {\n",
" \"modelId\": \"anthropic.claude-v2\",\n",
" \"accept\": \"*/*\",\n",
" \"contentType\": \"application/json\",\n",
" \"body\":body\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "25342116",
"metadata": {},
"source": [
"## Invoke model\n",
"Invoke the model with all of the variables set above. Notice we're using the **invoke_model_with_response_stream** action which streams the response as it's generated as opposed to waiting for the full response as is the case when using **invoke_model**"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "fd308af8",
"metadata": {},
"outputs": [],
"source": [
"response = brt.invoke_model_with_response_stream(**kwargs)"
]
},
{
"cell_type": "markdown",
"id": "5ed3bb43",
"metadata": {},
"source": [
"## Stream response\n",
"Response is streamed back as it's generated."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "bcb3e215",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Here is a basic recipe and instructions for making pancakes:\n",
"\n",
"Ingredients:\n",
"- 1 cup all-purpose flour \n",
"- 2 tablespoons sugar\n",
"- 2 teaspoons baking powder\n",
"- 1/2 teaspoon salt\n",
"- 1 cup milk\n",
"- 2 tablespoons butter, melted\n",
"- 1 egg\n",
"- 1/2 teaspoon vanilla extract\n",
"\n",
"Instructions:\n",
"\n",
"1. In a large bowl, whisk together the flour, sugar, baking powder and salt. Make a well in the center and add the milk, melted butter, egg and vanilla. Whisk together until just combined and a few lumps remain. \n",
"\n",
"2. Heat a lightly oiled griddle or skillet over medium heat. Pour or scoop the batter onto the griddle, using about 1/4 cup for each pancake. \n",
"\n",
"3. Cook until bubbles start to form on the surface and the underside is golden brown, about 2-3 minutes. Flip and cook the other side until golden brown, 1-2 minutes more.\n",
"\n",
"4. Serve the pancakes warm, with your favorite toppings like butter, maple syrup, fruit, whipped cream etc.\n",
"\n",
"5. Repeat with the remaining batter, greasing the griddle between batches if needed. Enjoy!\n",
"\n",
"Let me know if you need any clarification on the steps. Making pancakes from scratch is really easy and the results are so much better than from a boxed mix."
]
}
],
"source": [
"stream = response.get('body')\n",
"if stream:\n",
" for event in stream:\n",
" chunk=event.get('chunk')\n",
" if chunk:\n",
" print(json.loads(chunk.get('bytes')).get('completion'), end=\"\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "conda_python3",
"language": "python",
"name": "conda_python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading