add jupyter notebook: Invoke LLM modelfrom AWS Bedrock
This commit is contained in:
@@ -0,0 +1,190 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "9138adfe-71b0-4db2-a08f-dd9e472fdd63",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import boto3"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "15d71dd6-cc03-485e-8a34-7a33ed5dee0e",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "1358921d-173b-4d5d-828c-b6c3726a5eb3",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"#### Connect to bedrock models"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "b3827087-182f-48be-8b59-b2741f8ded44",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import json"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "94c11534-6847-4e4a-b8e4-8066e0cc6aca",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Use the Conversation API to send a text message to Amazon Nova.\n",
|
||||||
|
"\n",
|
||||||
|
"import boto3\n",
|
||||||
|
"from botocore.exceptions import ClientError\n",
|
||||||
|
"\n",
|
||||||
|
"# Create a Bedrock Runtime client in the AWS Region you want to use.\n",
|
||||||
|
"client = boto3.client(\"bedrock-runtime\", region_name=\"us-east-1\")\n",
|
||||||
|
"\n",
|
||||||
|
"# Set the model ID, e.g., Amazon Nova Lite.\n",
|
||||||
|
"model_id = \"amazon.nova-lite-v1:0\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "9a8ad65f-abaa-475c-892c-2e2b4e668f5d",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "ac20bb00-e93f-4a95-a1de-dd2688bce591",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Start a conversation with the user message.\n",
|
||||||
|
"user_message = \"\"\"\n",
|
||||||
|
"List the best parks to see in London with number of google ratings and value ie. 4.5 out of 5 etc. \n",
|
||||||
|
"Give number of ratings and give output in table form\n",
|
||||||
|
"\"\"\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"id": "a29f0055-48c4-4f25-b33f-cde1eaf755c5",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"conversation = [\n",
|
||||||
|
" {\n",
|
||||||
|
" \"role\": \"user\",\n",
|
||||||
|
" \"content\": [{\"text\": user_message}],\n",
|
||||||
|
" }\n",
|
||||||
|
"]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"id": "0e68b2d5-4d43-4b80-8574-d3c847b33661",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Certainly! Here's a table of some of the best parks in London, along with their Google ratings and the number of ratings they have received. Note that ratings can fluctuate over time, so it's always a good idea to check the latest reviews on Google before planning your visit.\n",
|
||||||
|
"\n",
|
||||||
|
"| Park Name | Google Rating | Number of Ratings |\n",
|
||||||
|
"| --- | --- | --- |\n",
|
||||||
|
"| Regent's Park | 4.7 | 10,000+ |\n",
|
||||||
|
"| Hyde Park | 4.6 | 20,000+ |\n",
|
||||||
|
"| Greenwich Park | 4.6 | 5,000+ |\n",
|
||||||
|
"| Kensington Gardens | 4.6 | 5,000+ |\n",
|
||||||
|
"| St. James's Park | 4.5 | 3,000+ |\n",
|
||||||
|
"| Victoria Park | 4.5 | 2,000+ |\n",
|
||||||
|
"| Richmond Park | 4.6 | 3,000+ |\n",
|
||||||
|
"| Hampstead Heath | 4.6 | 4,000+ |\n",
|
||||||
|
"| Holland Park | 4.5 | 1,000+ |\n",
|
||||||
|
"| Battersea Park | 4.4 | 1,500+ |\n",
|
||||||
|
"\n",
|
||||||
|
"These ratings are based on the latest information available as of 2023. Always check the most recent reviews for the most up-to-date information.\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"try:\n",
|
||||||
|
" # Send the message to the model, using a basic inference configuration.\n",
|
||||||
|
" response = client.converse(\n",
|
||||||
|
" modelId=model_id,\n",
|
||||||
|
" messages=conversation,\n",
|
||||||
|
" inferenceConfig={\"maxTokens\": 512, \"temperature\": 0.5, \"topP\": 0.9},\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
" # Extract and print the response text.\n",
|
||||||
|
" response_text = response[\"output\"][\"message\"][\"content\"][0][\"text\"]\n",
|
||||||
|
" print(response_text)\n",
|
||||||
|
"\n",
|
||||||
|
"except (ClientError, Exception) as e:\n",
|
||||||
|
" print(f\"ERROR: Can't invoke '{model_id}'. Reason: {e}\")\n",
|
||||||
|
" exit(1)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "8ed16ee7-3f09-4780-8dfc-d1c5f3cffdbe",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "7f8c7a18-0907-430d-bfe7-86ecb8933bfd",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "2183994b-cde5-45b0-b18b-37be3277d73b",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"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.11.13"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user