adding files for bedrock-converse

This commit is contained in:
Elie Schoppik
2024-11-26 12:58:55 -05:00
parent b4f26aedef
commit 0a2d2dcfeb
76 changed files with 3213 additions and 34088 deletions

View File

@@ -40,7 +40,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@@ -82,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@@ -94,7 +94,7 @@
" \n",
" How many legs does the animal have? Please respond with a number\"\"\"\n",
"\n",
" messages = [{'role': 'user', 'content': user_content}]\n",
" messages = [{'role': 'user', 'content': [{\"text\": user_content}]}]\n",
" return messages"
]
},
@@ -107,17 +107,17 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'role': 'user',\n",
" 'content': 'You will be provided a statement about an animal and your job is to determine how many legs that animal has.\\n \\n Here is the animal statement.\\n <animal_statement>The animal is a human.</animal_statement>\\n \\n How many legs does the animal have? Please respond with a number'}]"
" 'content': [{'text': 'You will be provided a statement about an animal and your job is to determine how many legs that animal has.\\n \\n Here is the animal statement.\\n <animal_statement>The animal is a human.</animal_statement>\\n \\n How many legs does the animal have? Please respond with a number'}]}]"
]
},
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
@@ -135,25 +135,24 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"from anthropic import Anthropic\n",
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv()\n",
"client = Anthropic()\n",
"\n",
"MODEL_NAME = \"claude-3-haiku-20240307\"\n",
"import boto3\n",
"\n",
"def get_completion(messages):\n",
" response = client.messages.create(\n",
" model=MODEL_NAME,\n",
" max_tokens=200,\n",
"\n",
" bedrock_client = boto3.client(service_name='bedrock-runtime', region_name=\"us-west-2\")\n",
" model_id = \"anthropic.claude-3-haiku-20240307-v1:0\"\n",
"\n",
" # Send the message.\n",
" response = bedrock_client.converse(\n",
" modelId=model_id,\n",
" messages=messages\n",
" )\n",
" return response.content[0].text"
"\n",
" return response[\"output\"][\"message\"][\"content\"][0][\"text\"]"
]
},
{
@@ -168,7 +167,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 25,
"metadata": {},
"outputs": [
{
@@ -177,7 +176,7 @@
"'2'"
]
},
"execution_count": 6,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
@@ -207,7 +206,7 @@
},
{
"cell_type": "code",
"execution_count": 93,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
@@ -224,27 +223,27 @@
},
{
"cell_type": "code",
"execution_count": 94,
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['2',\n",
" '0',\n",
" '5',\n",
" 'The animal described in the statement is a snake. Snakes are reptiles that do not have any legs. Therefore, the animal has 0 legs.',\n",
" '4',\n",
" '4',\n",
" '6',\n",
" '4',\n",
" 'Based on the provided animal statement, \"The animal is a bird.\", the animal has 2 legs.\\n\\nResponse: 2',\n",
" '2',\n",
" '0',\n",
" '8',\n",
" 'An octopus has 8 legs.',\n",
" '8',\n",
" '5',\n",
" '8']"
" 'The animal described in the statement has 8 legs.']"
]
},
"execution_count": 94,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
@@ -262,7 +261,7 @@
},
{
"cell_type": "code",
"execution_count": 95,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@@ -275,11 +274,11 @@
"\n",
"Animal Statement: The animal is a snake.\n",
"Golden Answer: 0\n",
"Output: 0\n",
"Output: The animal described in the statement is a snake. Snakes are reptiles that do not have any legs. Therefore, the animal has 0 legs.\n",
"\n",
"Animal Statement: The fox lost a leg, but then magically grew back the leg he lost and a mysterious extra leg on top of that.\n",
"Golden Answer: 5\n",
"Output: 5\n",
"Output: 4\n",
"\n",
"Animal Statement: The animal is a dog.\n",
"Golden Answer: 4\n",
@@ -295,9 +294,7 @@
"\n",
"Animal Statement: The animal is a bird.\n",
"Golden Answer: 2\n",
"Output: Based on the provided animal statement, \"The animal is a bird.\", the animal has 2 legs.\n",
"\n",
"Response: 2\n",
"Output: 2\n",
"\n",
"Animal Statement: The animal is a fish.\n",
"Golden Answer: 0\n",
@@ -309,7 +306,7 @@
"\n",
"Animal Statement: The animal is an octopus.\n",
"Golden Answer: 8\n",
"Output: An octopus has 8 legs.\n",
"Output: 8\n",
"\n",
"Animal Statement: The animal is an octopus that lost two legs and then regrew three legs.\n",
"Golden Answer: 9\n",
@@ -317,7 +314,7 @@
"\n",
"Animal Statement: The animal is a two-headed, eight-legged mythical creature.\n",
"Golden Answer: 8\n",
"Output: 8\n",
"Output: The animal described in the statement has 8 legs.\n",
"\n"
]
}
@@ -336,14 +333,14 @@
},
{
"cell_type": "code",
"execution_count": 97,
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Score: 66.66666666666666%\n"
"Score: 58.333333333333336%\n"
]
}
],
@@ -404,7 +401,7 @@
},
{
"cell_type": "code",
"execution_count": 98,
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
@@ -416,7 +413,7 @@
" \n",
" How many legs does the animal have? Respond only with a numeric digit, like 2 or 6, and nothing else.\"\"\"\n",
"\n",
" messages = [{'role': 'user', 'content': user_content}]\n",
" messages = [{'role': 'user', 'content': [{\"text\":user_content}]}]\n",
" return messages"
]
},
@@ -438,7 +435,7 @@
},
{
"cell_type": "code",
"execution_count": 99,
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
@@ -454,16 +451,16 @@
},
{
"cell_type": "code",
"execution_count": 101,
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['2', '0', '6', '4', '6', '4', '2', '0', '8', '8', '5', '8']"
"['2', '0', '6', '4', '6', '4', '2', '0', '8', '8', '8', '8']"
]
},
"execution_count": 101,
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
@@ -481,7 +478,7 @@
},
{
"cell_type": "code",
"execution_count": 102,
"execution_count": 34,
"metadata": {},
"outputs": [
{
@@ -530,7 +527,7 @@
"\n",
"Animal Statement: The animal is an octopus that lost two legs and then regrew three legs.\n",
"Golden Answer: 9\n",
"Output: 5\n",
"Output: 8\n",
"\n",
"Animal Statement: The animal is a two-headed, eight-legged mythical creature.\n",
"Golden Answer: 8\n",
@@ -561,7 +558,7 @@
},
{
"cell_type": "code",
"execution_count": 103,
"execution_count": 35,
"metadata": {},
"outputs": [
{
@@ -606,7 +603,7 @@
},
{
"cell_type": "code",
"execution_count": 105,
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
@@ -621,7 +618,7 @@
" Then, output your final answer inside of <answer> tags. \n",
" Inside the <answer> tags return just the number of legs as an integer and nothing else.\"\"\"\n",
"\n",
" messages = [{'role': 'user', 'content': user_content}]\n",
" messages = [{'role': 'user', 'content': [{\"text\": user_content}]}]\n",
" return messages"
]
},
@@ -634,7 +631,7 @@
},
{
"cell_type": "code",
"execution_count": 109,
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
@@ -650,7 +647,7 @@
},
{
"cell_type": "code",
"execution_count": 110,
"execution_count": 38,
"metadata": {},
"outputs": [
{
@@ -660,7 +657,7 @@
"Animal Statement: The animal is a human.\n",
"Golden Answer: 2\n",
"Output: <thinking>\n",
"The animal is a human, and based on this information, we can reasonably conclude that a human has 2 legs. Humans are bipedal, meaning they have two legs that they use for locomotion and standing upright. This is a characteristic of the human species.\n",
"The animal described in the statement is a human. Humans are a type of mammal, and typically have two legs. Therefore, the animal described in the statement has 2 legs.\n",
"</thinking>\n",
"\n",
"<answer>2</answer>\n",
@@ -668,25 +665,23 @@
"Animal Statement: The animal is a snake.\n",
"Golden Answer: 0\n",
"Output: <thinking>\n",
"The animal stated in the given statement is a snake. Snakes are known to be legless reptiles, as they do not have any legs. They move by slithering on the ground using their body and scales.\n",
"The statement says that the animal is a snake. Snakes are reptiles that do not have any legs. They move by undulating their bodies across the ground.\n",
"</thinking>\n",
"\n",
"<answer>0</answer>\n",
"\n",
"Animal Statement: The fox lost a leg, but then magically grew back the leg he lost and a mysterious extra leg on top of that.\n",
"Golden Answer: 5\n",
"Output: Here is my step-by-step reasoning:\n",
"<thinking>\n",
"1. The initial statement says the fox lost a leg.\n",
"2. But then the fox \"magically grew back the leg he lost and a mysterious extra leg on top of that.\"\n",
"3. This means the fox originally had 4 legs, lost 1 leg, and then grew back the lost leg plus an extra leg, for a total of 5 legs.\n",
"Output: <thinking>\n",
"The animal statement mentions that the fox lost a leg, but then magically grew back the leg he lost and a mysterious extra leg on top of that. This means that the fox originally had 4 legs, lost 1 leg, and then regrew the lost leg and gained an additional leg, making it a total of 5 legs.\n",
"</thinking>\n",
"\n",
"<answer>5</answer>\n",
"\n",
"Animal Statement: The animal is a dog.\n",
"Golden Answer: 4\n",
"Output: <thinking>\n",
"The animal statement says the animal is a dog. Dogs are quadrupeds, meaning they have four legs. Therefore, the number of legs the dog has is 4.\n",
"The animal statement indicates that the animal is a dog. Dogs are commonly known to be quadrupeds, meaning they have four legs. Therefore, the number of legs the animal has is 4.\n",
"</thinking>\n",
"\n",
"<answer>4</answer>\n",
@@ -694,7 +689,7 @@
"Animal Statement: The animal is a cat with two extra legs.\n",
"Golden Answer: 6\n",
"Output: <thinking>\n",
"The animal statement says that the animal is a cat with two extra legs. A typical cat has four legs, so with the two extra legs, the animal must have six legs in total.\n",
"The animal statement says that the animal is a cat with two extra legs. A cat typically has 4 legs, so with the two extra legs, the animal would have a total of 6 legs.\n",
"</thinking>\n",
"\n",
"<answer>6</answer>\n",
@@ -702,7 +697,7 @@
"Animal Statement: The animal is an elephant.\n",
"Golden Answer: 4\n",
"Output: <thinking>\n",
"An elephant is a large mammal that belongs to the order Proboscidea. Elephants are known to have four legs, one for each of their four limbs. Therefore, based on the given animal statement, the animal is an elephant, and elephants have four legs.\n",
"The animal mentioned in the statement is an elephant. Elephants are large mammals that typically have four legs. They are known to be quadrupeds, meaning they have four legs.\n",
"</thinking>\n",
"\n",
"<answer>4</answer>\n",
@@ -710,7 +705,7 @@
"Animal Statement: The animal is a bird.\n",
"Golden Answer: 2\n",
"Output: <thinking>\n",
"The statement provided indicates that the animal is a bird. Birds typically have two legs, as they are bipedal animals that walk and perch on two legs.\n",
"The animal statement says that the animal is a bird. Birds are typically known to have two legs, as they are bipedal creatures. Birds use their two legs for walking, perching, and other locomotion activities.\n",
"</thinking>\n",
"\n",
"<answer>2</answer>\n",
@@ -718,7 +713,7 @@
"Animal Statement: The animal is a fish.\n",
"Golden Answer: 0\n",
"Output: <thinking>\n",
"Based on the given animal statement, the animal is a fish. Fish are aquatic vertebrates that typically have fins and gills to help them swim and breathe in the water. Fish do not have legs, as they move through the water using their fins and tails.\n",
"The statement provided indicates that the animal is a fish. Fish are aquatic vertebrates that typically have fins and breathe through gills. They do not have legs, but instead have fins to propel themselves through the water.\n",
"</thinking>\n",
"\n",
"<answer>0</answer>\n",
@@ -726,16 +721,17 @@
"Animal Statement: The animal is a spider with two extra legs\n",
"Golden Answer: 10\n",
"Output: <thinking>\n",
"The animal statement says that the animal is a spider with two extra legs. \n",
"A spider typically has 8 legs, so with two extra legs, the total number of legs would be 8 + 2 = 10 legs.\n",
"The animal statement indicates that the animal is a spider with two extra legs. A spider typically has 8 legs, so adding two extra legs would make the total number of legs 10.\n",
"</thinking>\n",
"\n",
"<answer>10</answer>\n",
"\n",
"Animal Statement: The animal is an octopus.\n",
"Golden Answer: 8\n",
"Output: <thinking>\n",
"The animal statement says the animal is an octopus. An octopus is a marine invertebrate with eight tentacles that are often referred to as legs. Therefore, the animal has 8 legs.\n",
"Output: Here is my thinking process and answer:\n",
"\n",
"<thinking>\n",
"The animal statement says that the animal is an octopus. An octopus is a marine invertebrate that has eight tentacles or legs. Therefore, the animal has 8 legs.\n",
"</thinking>\n",
"\n",
"<answer>8</answer>\n",
@@ -743,9 +739,7 @@
"Animal Statement: The animal is an octopus that lost two legs and then regrew three legs.\n",
"Golden Answer: 9\n",
"Output: <thinking>\n",
"The animal is described as an octopus that lost two legs and then regrew three legs. Initially, an octopus has eight legs.\n",
"Since the animal lost two legs, it would have had six legs remaining.\n",
"Then, the animal regrew three legs, so the final number of legs the animal has is nine.\n",
"The animal is initially described as an octopus, which has 8 legs. The statement then says the octopus lost two legs, meaning it had 6 legs at that point. Finally, it says the octopus regrew three legs, so we need to add those back to the 6 legs it had after losing the two.\n",
"</thinking>\n",
"\n",
"<answer>9</answer>\n",
@@ -753,7 +747,11 @@
"Animal Statement: The animal is a two-headed, eight-legged mythical creature.\n",
"Golden Answer: 8\n",
"Output: <thinking>\n",
"The animal statement mentions that the animal is a two-headed, eight-legged mythical creature. This means that the animal has two heads and eight legs.\n",
"Based on the given animal statement, the animal is described as a \"two-headed, eight-legged mythical creature\". This indicates that the animal has a total of 8 legs.\n",
"\n",
"The statement does not provide any information about the number of legs per head. However, since the animal is described as a \"mythical creature\", we can assume that it is not a real animal and the number of legs per head is not necessarily the typical number for a normal animal.\n",
"\n",
"Therefore, the animal described in the statement has a total of 8 legs.\n",
"</thinking>\n",
"\n",
"<answer>8</answer>\n",
@@ -791,7 +789,7 @@
},
{
"cell_type": "code",
"execution_count": 111,
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
@@ -800,9 +798,7 @@
" pattern = r'<answer>(.*?)</answer>'\n",
" match = re.search(pattern, text)\n",
" if match:\n",
" return match.group(1)\n",
" else:\n",
" return None"
" return match.group(1)"
]
},
{
@@ -814,7 +810,7 @@
},
{
"cell_type": "code",
"execution_count": 112,
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
@@ -823,7 +819,7 @@
},
{
"cell_type": "code",
"execution_count": 113,
"execution_count": 41,
"metadata": {},
"outputs": [
{
@@ -832,7 +828,7 @@
"['2', '0', '5', '4', '6', '4', '2', '0', '10', '8', '9', '8']"
]
},
"execution_count": 113,
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
@@ -850,7 +846,7 @@
},
{
"cell_type": "code",
"execution_count": 114,
"execution_count": 42,
"metadata": {},
"outputs": [
{