Last active
February 25, 2025 02:20
-
-
Save mikegc-aws/61bd2a5b6f40ffe78f1f302aae33f620 to your computer and use it in GitHub Desktop.
Claude 3.7 Sonnet and Amazon Bedrock Converse API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"boto3 1.37.0\n" | |
] | |
} | |
], | |
"source": [ | |
"# Must be v1.37.0 or higher\n", | |
"\n", | |
"# !pip install --upgrade boto3\n", | |
"! pip list | grep boto3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import boto3, json" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"client = boto3.client('bedrock-runtime', region_name='us-west-2')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"model_id = \"us.anthropic.claude-3-7-sonnet-20250219-v1:0\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"system_message = [\n", | |
"\t{\n", | |
"\t\t\"text\": \"You are a helpful assistant.\"\n", | |
"\t}\n", | |
"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"messages = [\n", | |
"\t{\n", | |
"\t\t'role': 'user', \n", | |
"\t\t'content': [\n", | |
"\t\t\t{\n", | |
"\t\t\t\t'text': \"Write a poem that will make me fall in love.\"\n", | |
"\t\t\t}\n", | |
"\t\t]\n", | |
"\t}\n", | |
"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"response = client.converse(\n", | |
"\tmodelId=model_id,\n", | |
"\tmessages=messages,\n", | |
"\tsystem=system_message,\n", | |
"\tinferenceConfig={\n", | |
"\t\t\"maxTokens\": 131072,\n", | |
"\t\t\"temperature\": 0.7,\n", | |
"\t\t\"topP\": 1,\n", | |
"\t}\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{\n", | |
" \"ResponseMetadata\": {\n", | |
" \"RequestId\": \"78c638f9-51ed-453b-b7c7-40c031f91909\",\n", | |
" \"HTTPStatusCode\": 200,\n", | |
" \"HTTPHeaders\": {\n", | |
" \"date\": \"Tue, 25 Feb 2025 01:16:10 GMT\",\n", | |
" \"content-type\": \"application/json\",\n", | |
" \"content-length\": \"958\",\n", | |
" \"connection\": \"keep-alive\",\n", | |
" \"x-amzn-requestid\": \"78c638f9-51ed-453b-b7c7-40c031f91909\"\n", | |
" },\n", | |
" \"RetryAttempts\": 0\n", | |
" },\n", | |
" \"output\": {\n", | |
" \"message\": {\n", | |
" \"role\": \"assistant\",\n", | |
" \"content\": [\n", | |
" {\n", | |
" \"text\": \"# Whispers of the Heart\\n\\nIn quiet moments when the world grows still,\\nI find you in the spaces between breaths.\\nNot in grand gestures or atop some hill,\\nBut in gentle whispers, subtle depths.\\n\\nLike morning light that filters through the leaves,\\nYou entered life\\u2014unhurried, warm, and true.\\nA presence that both comforts and relieves,\\nA landscape painted in the softest hue.\\n\\nWe're made of stars and stories, you and I,\\nConnected by the threads of chance and time.\\nSome bonds need no explaining, no knows why\\nSome souls just fit, a perfect rhythm, rhyme.\\n\\nLove isn't always thunder, storm, or flame\\u2014\\nSometimes it's coming home to your own name.\"\n", | |
" }\n", | |
" ]\n", | |
" }\n", | |
" },\n", | |
" \"stopReason\": \"end_turn\",\n", | |
" \"usage\": {\n", | |
" \"inputTokens\": 24,\n", | |
" \"outputTokens\": 169,\n", | |
" \"totalTokens\": 193\n", | |
" },\n", | |
" \"metrics\": {\n", | |
" \"latencyMs\": 3322\n", | |
" }\n", | |
"}\n" | |
] | |
} | |
], | |
"source": [ | |
"print(json.dumps(response, indent=4))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"response = client.converse(\n", | |
"\tmodelId=model_id,\n", | |
"\tmessages=messages,\n", | |
"\tsystem=system_message,\n", | |
"\tinferenceConfig={\n", | |
"\t\t\"maxTokens\": 131072,\n", | |
"\t\t\"temperature\": 1, # must be 1\n", | |
"\t\t# \"topP\": 1, # must be unset\n", | |
"\t},\n", | |
"\tadditionalModelRequestFields={\n", | |
"\t\t'thinking': {\n", | |
"\t\t\t\"type\": \"enabled\",\n", | |
"\t\t\t\"budget_tokens\": 2000\n", | |
"\t\t}\n", | |
"\t}\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{\n", | |
" \"ResponseMetadata\": {\n", | |
" \"RequestId\": \"470ecf63-4e54-407b-9c36-92a2039f02ee\",\n", | |
" \"HTTPStatusCode\": 200,\n", | |
" \"HTTPHeaders\": {\n", | |
" \"date\": \"Tue, 25 Feb 2025 01:17:51 GMT\",\n", | |
" \"content-type\": \"application/json\",\n", | |
" \"content-length\": \"2376\",\n", | |
" \"connection\": \"keep-alive\",\n", | |
" \"x-amzn-requestid\": \"470ecf63-4e54-407b-9c36-92a2039f02ee\"\n", | |
" },\n", | |
" \"RetryAttempts\": 0\n", | |
" },\n", | |
" \"output\": {\n", | |
" \"message\": {\n", | |
" \"role\": \"assistant\",\n", | |
" \"content\": [\n", | |
" {\n", | |
" \"reasoningContent\": {\n", | |
" \"reasoningText\": {\n", | |
" \"text\": \"I'm being asked to write a poem that will make someone fall in love. This is a creative writing request that I can fulfill, but I should be thoughtful about it. A poem that might evoke feelings of love should be:\\n\\n1. Authentic and sincere\\n2. Not manipulative or making promises I can't keep\\n3. Beautifully crafted with imagery and emotion\\n4. Universal enough that it could resonate with many people\\n\\nI'll write a poem about the beauty of connection, the feeling of finding someone special, and the wonder of falling in love. I'll use vivid imagery, emotional language, and a rhythm that feels intimate. The poem shouldn't be overly dramatic or make unrealistic claims about making someone fall in love, as that would be disingenuous.\\n\\nI'll aim for something that captures the essence of love - the vulnerability, the wonder, and the transformative nature of deep connection - while maintaining artistic integrity.\",\n", | |
" \"signature\": \"EqgBCkYQARgCIkCE8xqdTUP3yhU4FKDl0Eo9L7edTWaBaWstkM+Fsx2NJDpxWW6TeDgjlYl/9PrYQyFtb6cn8vO6KyVBW1b3pdKmEgyUyfVYEyE0fNEiINkaDLnk1b/NJSCvtD5oNyIwz80oUgUbzLrgKyYcGCXAxBPH3hacDCAWxqyBzLLGk8NmakFDX60XY821QQ9JyE03KhA5GjKlLGPoVSPl26bWQdLA\"\n", | |
" }\n", | |
" }\n", | |
" },\n", | |
" {\n", | |
" \"text\": \"# When Our Eyes Meet\\n\\nIn the space between silence and words,\\nWhere heartbeats become a shared language,\\nI found you, like a whisper of light\\nThrough leaves I never knew needed sunshine.\\n\\nYour laughter\\u2014a melody that turns\\nOrdinary moments into memories carved in stars.\\nYour touch\\u2014gentle as morning dew,\\nYet strong enough to anchor storms within me.\\n\\nWe are puzzle pieces from different boxes,\\nSomehow fitting perfectly against all odds.\\nTime bends around us, slowing when we touch,\\nRacing when we're apart, as if conspiring\\nTo write our story across the universe.\\n\\nI don't promise forever,\\nOnly this moment, and the next,\\nAnd all the moments I'm granted\\nTo discover the galaxies hidden behind your smile.\\n\\nLove isn't what I sought\\nUntil I realized it was always you\\nI was turning toward,\\nLike a flower following the sun across the sky.\"\n", | |
" }\n", | |
" ]\n", | |
" }\n", | |
" },\n", | |
" \"stopReason\": \"end_turn\",\n", | |
" \"usage\": {\n", | |
" \"inputTokens\": 53,\n", | |
" \"outputTokens\": 414,\n", | |
" \"totalTokens\": 467\n", | |
" },\n", | |
" \"metrics\": {\n", | |
" \"latencyMs\": 8115\n", | |
" }\n", | |
"}\n" | |
] | |
} | |
], | |
"source": [ | |
"print(json.dumps(response, indent=4))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"=== Reasoning ===\n", | |
"I'm being asked to write a poem that will make someone fall in love. This is a creative writing request that I can fulfill, but I should be thoughtful about it. A poem that might evoke feelings of love should be:\n", | |
"\n", | |
"1. Authentic and sincere\n", | |
"2. Not manipulative or making promises I can't keep\n", | |
"3. Beautifully crafted with imagery and emotion\n", | |
"4. Universal enough that it could resonate with many people\n", | |
"\n", | |
"I'll write a poem about the beauty of connection, the feeling of finding someone special, and the wonder of falling in love. I'll use vivid imagery, emotional language, and a rhythm that feels intimate. The poem shouldn't be overly dramatic or make unrealistic claims about making someone fall in love, as that would be disingenuous.\n", | |
"\n", | |
"I'll aim for something that captures the essence of love - the vulnerability, the wonder, and the transformative nature of deep connection - while maintaining artistic integrity.\n", | |
"\n", | |
"=== Response ===\n", | |
"# When Our Eyes Meet\n", | |
"\n", | |
"In the space between silence and words,\n", | |
"Where heartbeats become a shared language,\n", | |
"I found you, like a whisper of light\n", | |
"Through leaves I never knew needed sunshine.\n", | |
"\n", | |
"Your laughter—a melody that turns\n", | |
"Ordinary moments into memories carved in stars.\n", | |
"Your touch—gentle as morning dew,\n", | |
"Yet strong enough to anchor storms within me.\n", | |
"\n", | |
"We are puzzle pieces from different boxes,\n", | |
"Somehow fitting perfectly against all odds.\n", | |
"Time bends around us, slowing when we touch,\n", | |
"Racing when we're apart, as if conspiring\n", | |
"To write our story across the universe.\n", | |
"\n", | |
"I don't promise forever,\n", | |
"Only this moment, and the next,\n", | |
"And all the moments I'm granted\n", | |
"To discover the galaxies hidden behind your smile.\n", | |
"\n", | |
"Love isn't what I sought\n", | |
"Until I realized it was always you\n", | |
"I was turning toward,\n", | |
"Like a flower following the sun across the sky.\n" | |
] | |
} | |
], | |
"source": [ | |
"message = response['output']['message']\n", | |
"content = message['content']\n", | |
"\n", | |
"# Check for and display reasoning if present\n", | |
"for item in content:\n", | |
" if 'reasoningContent' in item:\n", | |
" reasoning = item['reasoningContent']['reasoningText']['text']\n", | |
" print(\"=== Reasoning ===\")\n", | |
" print(reasoning)\n", | |
" print(\"\\n=== Response ===\")\n", | |
" elif 'text' in item:\n", | |
" print(item['text'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"messages = [\n", | |
"\t{\n", | |
"\t\t'role': 'user', \n", | |
"\t\t'content': [\n", | |
"\t\t\t{\n", | |
"\t\t\t\t'text': \"I placed a cup on the table and put a sugar cube in the cup. I turned the cup upside down. What is the sugar cube sitting on?\"\n", | |
"\t\t\t}\n", | |
"\t\t]\n", | |
"\t}\n", | |
"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"=== Reasoning ===\n", | |
"Let's think through this step by step:\n", | |
"1. Initially, a cup is placed on the table and a sugar cube is placed in the cup.\n", | |
"2. Then the cup is turned upside down.\n", | |
"\n", | |
"When the cup is turned upside down, what happens to the sugar cube?\n", | |
"\n", | |
"Assuming this is in a normal Earth gravity environment, when the cup is turned upside down, the sugar cube would fall out of the cup unless there's something preventing it from falling (like the sugar cube being stuck to the inside of the cup somehow, but that's not mentioned).\n", | |
"\n", | |
"If the sugar cube falls out when the cup is turned upside down, it would land on the table. \n", | |
"\n", | |
"However, if we're supposed to assume the sugar cube somehow stayed within the confines of the upside-down cup (perhaps due to being stuck to the inside of the cup, or perhaps the cup was turned upside down very quickly), then the sugar cube would be sitting on the inner surface of the bottom of the cup, which is now facing downward.\n", | |
"\n", | |
"Let me re-read the problem to make sure I'm not missing anything...\n", | |
"\n", | |
"I think the most reasonable interpretation is that when the cup is turned upside down, the sugar cube falls out and lands on the table. Therefore, the sugar cube is sitting on the table.\n", | |
"\n", | |
"=== Response ===\n", | |
"When you turn the cup upside down, the sugar cube would fall out of the cup and land on the table. So the sugar cube is sitting on the table.\n" | |
] | |
} | |
], | |
"source": [ | |
"response = client.converse(\n", | |
"\tmodelId=model_id,\n", | |
"\tmessages=messages,\n", | |
"\tsystem=system_message,\n", | |
"\tinferenceConfig={\n", | |
"\t\t\"maxTokens\": 131072,\n", | |
"\t\t\"temperature\": 1, # must be 1\n", | |
"\t\t# \"topP\": 1, # must be unset\n", | |
"\t},\n", | |
"\tadditionalModelRequestFields={\n", | |
"\t\t'thinking': {\n", | |
"\t\t\t\"type\": \"enabled\",\n", | |
"\t\t\t\"budget_tokens\": 2000\n", | |
"\t\t}\n", | |
"\t}\n", | |
")\n", | |
"\n", | |
"message = response['output']['message']\n", | |
"content = message['content']\n", | |
"\n", | |
"# Check for and display reasoning if present\n", | |
"for item in content:\n", | |
" if 'reasoningContent' in item:\n", | |
" reasoning = item['reasoningContent']['reasoningText']['text']\n", | |
" print(\"=== Reasoning ===\")\n", | |
" print(reasoning)\n", | |
" print(\"\\n=== Response ===\")\n", | |
" elif 'text' in item:\n", | |
" print(item['text'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "base", | |
"language": "python", | |
"name": "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.12.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment