248 KiB
248 KiB
In [3]:
eval_data = [
{"animal_statement": "The animal is a human.", "golden_answer": "2"},
{"animal_statement": "The animal is a snake.", "golden_answer": "0"},
{"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.", "golden_answer": "5"},
{"animal_statement": "The animal is a dog.", "golden_answer": "4"},
{"animal_statement": "The animal is a cat with two extra legs.", "golden_answer": "6"},
{"animal_statement": "The animal is an elephant.", "golden_answer": "4"},
{"animal_statement": "The animal is a bird.", "golden_answer": "2"},
{"animal_statement": "The animal is a fish.", "golden_answer": "0"},
{"animal_statement": "The animal is a spider with two extra legs", "golden_answer": "10"},
{"animal_statement": "The animal is an octopus.", "golden_answer": "8"},
{"animal_statement": "The animal is an octopus that lost two legs and then regrew three legs.", "golden_answer": "9"},
{"animal_statement": "The animal is a two-headed, eight-legged mythical creature.", "golden_answer": "8"},
]In [1]:
def build_input_prompt(animal_statement):
user_content = f"""You will be provided a statement about an animal and your job is to determine how many legs that animal has.
Here is the animal statement.
<animal_statement>{animal_statement}</animal_statement>
How many legs does the animal have? Please respond with a number"""
messages = [{'role': 'user', 'content': [{"text": user_content}]}]
return messagesIn [4]:
build_input_prompt(eval_data[0]['animal_statement'])Out [4]:
[{'role': 'user',
'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'}]}]In [24]:
import boto3
def get_completion(messages):
bedrock_client = boto3.client(service_name='bedrock-runtime', region_name="us-west-2")
model_id = "anthropic.claude-3-haiku-20240307-v1:0"
# Send the message.
response = bedrock_client.converse(
modelId=model_id,
messages=messages
)
return response["output"]["message"]["content"][0]["text"]In [25]:
full_prompt = build_input_prompt(eval_data[0]['animal_statement'])
get_completion(full_prompt)Out [25]:
'2'
In [26]:
outputs = [get_completion(build_input_prompt(question['animal_statement'])) for question in eval_data]
In [27]:
outputsOut [27]:
['2', 'The animal described in the statement is a snake. Snakes are reptiles that do not have any legs. Therefore, the animal has 0 legs.', '4', '4', '6', '4', '2', '0', '8', '8', '5', 'The animal described in the statement has 8 legs.']
In [28]:
for output, question in zip(outputs, eval_data):
print(f"Animal Statement: {question['animal_statement']}\nGolden Answer: {question['golden_answer']}\nOutput: {output}\n")Animal Statement: The animal is a human. Golden Answer: 2 Output: 2 Animal Statement: The animal is a snake. Golden Answer: 0 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. 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. Golden Answer: 5 Output: 4 Animal Statement: The animal is a dog. Golden Answer: 4 Output: 4 Animal Statement: The animal is a cat with two extra legs. Golden Answer: 6 Output: 6 Animal Statement: The animal is an elephant. Golden Answer: 4 Output: 4 Animal Statement: The animal is a bird. Golden Answer: 2 Output: 2 Animal Statement: The animal is a fish. Golden Answer: 0 Output: 0 Animal Statement: The animal is a spider with two extra legs Golden Answer: 10 Output: 8 Animal Statement: The animal is an octopus. Golden Answer: 8 Output: 8 Animal Statement: The animal is an octopus that lost two legs and then regrew three legs. Golden Answer: 9 Output: 5 Animal Statement: The animal is a two-headed, eight-legged mythical creature. Golden Answer: 8 Output: The animal described in the statement has 8 legs.
In [29]:
def grade_completion(output, golden_answer):
return output == golden_answer
grades = [grade_completion(output, question['golden_answer']) for output, question in zip(outputs, eval_data)]
print(f"Score: {sum(grades)/len(grades)*100}%")Score: 58.333333333333336%
In [30]:
def build_input_prompt2(animal_statement):
user_content = f"""You will be provided a statement about an animal and your job is to determine how many legs that animal has.
Here is the animal statement.
<animal_statement>{animal_statement}</animal_statement>
How many legs does the animal have? Respond only with a numeric digit, like 2 or 6, and nothing else."""
messages = [{'role': 'user', 'content': [{"text":user_content}]}]
return messagesIn [31]:
outputs2 = [get_completion(build_input_prompt2(question['animal_statement'])) for question in eval_data]In [33]:
outputs2Out [33]:
['2', '0', '6', '4', '6', '4', '2', '0', '8', '8', '8', '8']
In [34]:
for output, question in zip(outputs2, eval_data):
print(f"Animal Statement: {question['animal_statement']}\nGolden Answer: {question['golden_answer']}\nOutput: {output}\n")Animal Statement: The animal is a human. Golden Answer: 2 Output: 2 Animal Statement: The animal is a snake. Golden Answer: 0 Output: 0 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. Golden Answer: 5 Output: 6 Animal Statement: The animal is a dog. Golden Answer: 4 Output: 4 Animal Statement: The animal is a cat with two extra legs. Golden Answer: 6 Output: 6 Animal Statement: The animal is an elephant. Golden Answer: 4 Output: 4 Animal Statement: The animal is a bird. Golden Answer: 2 Output: 2 Animal Statement: The animal is a fish. Golden Answer: 0 Output: 0 Animal Statement: The animal is a spider with two extra legs Golden Answer: 10 Output: 8 Animal Statement: The animal is an octopus. Golden Answer: 8 Output: 8 Animal Statement: The animal is an octopus that lost two legs and then regrew three legs. Golden Answer: 9 Output: 8 Animal Statement: The animal is a two-headed, eight-legged mythical creature. Golden Answer: 8 Output: 8
In [35]:
grades = [grade_completion(output, question['golden_answer']) for output, question in zip(outputs2, eval_data)]
print(f"Score: {sum(grades)/len(grades)*100}%")Score: 75.0%
In [36]:
def build_input_prompt3(animal_statement):
user_content = f"""You will be provided a statement about an animal and your job is to determine how many legs that animal has.
Here is the animal statement.
<animal_statement>{animal_statement}</animal_statement>
How many legs does the animal have?
Start by reasoning about the numbers of legs the animal has, thinking step by step inside of <thinking> tags.
Then, output your final answer inside of <answer> tags.
Inside the <answer> tags return just the number of legs as an integer and nothing else."""
messages = [{'role': 'user', 'content': [{"text": user_content}]}]
return messagesIn [37]:
outputs3 = [get_completion(build_input_prompt3(question['animal_statement'])) for question in eval_data]In [38]:
for output, question in zip(outputs3, eval_data):
print(f"Animal Statement: {question['animal_statement']}\nGolden Answer: {question['golden_answer']}\nOutput: {output}\n")Animal Statement: The animal is a human. Golden Answer: 2 Output: <thinking> 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. </thinking> <answer>2</answer> Animal Statement: The animal is a snake. Golden Answer: 0 Output: <thinking> 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. </thinking> <answer>0</answer> 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. Golden Answer: 5 Output: <thinking> 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. </thinking> <answer>5</answer> Animal Statement: The animal is a dog. Golden Answer: 4 Output: <thinking> 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. </thinking> <answer>4</answer> Animal Statement: The animal is a cat with two extra legs. Golden Answer: 6 Output: <thinking> 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. </thinking> <answer>6</answer> Animal Statement: The animal is an elephant. Golden Answer: 4 Output: <thinking> 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. </thinking> <answer>4</answer> Animal Statement: The animal is a bird. Golden Answer: 2 Output: <thinking> 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. </thinking> <answer>2</answer> Animal Statement: The animal is a fish. Golden Answer: 0 Output: <thinking> 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. </thinking> <answer>0</answer> Animal Statement: The animal is a spider with two extra legs Golden Answer: 10 Output: <thinking> 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. </thinking> <answer>10</answer> Animal Statement: The animal is an octopus. Golden Answer: 8 Output: Here is my thinking process and answer: <thinking> 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. </thinking> <answer>8</answer> Animal Statement: The animal is an octopus that lost two legs and then regrew three legs. Golden Answer: 9 Output: <thinking> 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. </thinking> <answer>9</answer> Animal Statement: The animal is a two-headed, eight-legged mythical creature. Golden Answer: 8 Output: <thinking> 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. 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. Therefore, the animal described in the statement has a total of 8 legs. </thinking> <answer>8</answer>
In [39]:
import re
def extract_answer(text):
pattern = r'<answer>(.*?)</answer>'
match = re.search(pattern, text)
if match:
return match.group(1)In [40]:
extracted_outputs3 = [extract_answer(output) for output in outputs3]In [41]:
extracted_outputs3Out [41]:
['2', '0', '5', '4', '6', '4', '2', '0', '10', '8', '9', '8']
In [42]:
grades3 = [grade_completion(output, question['golden_answer']) for output, question in zip(extracted_outputs3, eval_data)]
print(f"Score: {sum(grades3)/len(grades3)*100}%")Score: 100.0%
