21 KiB
21 KiB
In [ ]:
%pip install anthropic
# Import python's built-in regular expression library
import re
import anthropic
# Retrieve the API_KEY & MODEL_NAME variables from the IPython store
%store -r API_KEY
%store -r MODEL_NAME
client = anthropic.Anthropic(api_key=API_KEY)
# Has been rewritten to take in a messages list of arbitrary length
def get_completion(messages, system_prompt=""):
message = client.messages.create(
model=MODEL_NAME,
max_tokens=2000,
temperature=0.0,
system=system_prompt,
messages=messages
)
return message.content[0].textIn [ ]:
# Initial prompt
first_user = "Name ten words that all end with the exact letters 'ab'."
# API messages array
messages = [
{
"role": "user",
"content": first_user
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print(first_response)In [ ]:
second_user = "Please find replacements for all 'words' that are not real words."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))In [ ]:
first_user = "Name ten words that all end with the exact letters 'ab'."
first_response = """Here are 10 words that end with the letters 'ab':
1. Cab
2. Dab
3. Grab
4. Gab
5. Jab
6. Lab
7. Nab
8. Slab
9. Tab
10. Blab"""
second_user = "Please find replacements for all 'words' that are not real words."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))In [ ]:
first_user = "Name ten words that all end with the exact letters 'ab'."
first_response = """Here are 10 words that end with the letters 'ab':
1. Cab
2. Dab
3. Grab
4. Gab
5. Jab
6. Lab
7. Nab
8. Slab
9. Tab
10. Blab"""
second_user = "Please find replacements for all 'words' that are not real words. If all the words are real words, return the original list."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))In [ ]:
# Initial prompt
first_user = "Write a three-sentence short story about a girl who likes to run."
# API messages array
messages = [
{
"role": "user",
"content": first_user
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print(first_response)In [ ]:
second_user = "Make the story better."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))In [ ]:
first_user = """Find all names from the below text:
"Hey, Jesse. It's me, Erin. I'm calling about the party that Joey is throwing tomorrow. Keisha said she would come and I think Mel will be there too."""
prefill = "<names>"
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": prefill
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(first_response)In [ ]:
second_user = "Alphabetize the list."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": prefill + "\n" + first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))In [ ]:
# Initial prompt
first_user = "Name ten words that all end with the exact letters 'ab'."
# API messages array
messages = [
{
"role": "user",
"content": first_user
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print(first_response)In [ ]:
second_user = "Please find replacements for all 'words' that are not real words."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))In [ ]:
first_user = "Name ten words that all end with the exact letters 'ab'."
first_response = """Here are 10 words that end with the letters 'ab':
1. Cab
2. Dab
3. Grab
4. Gab
5. Jab
6. Lab
7. Nab
8. Slab
9. Tab
10. Blab"""
second_user = "Please find replacements for all 'words' that are not real words."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))In [ ]:
first_user = "Name ten words that all end with the exact letters 'ab'."
first_response = """Here are 10 words that end with the letters 'ab':
1. Cab
2. Dab
3. Grab
4. Gab
5. Jab
6. Lab
7. Nab
8. Slab
9. Tab
10. Blab"""
second_user = "Please find replacements for all 'words' that are not real words. If all the words are real words, return the original list."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))In [ ]:
# Initial prompt
first_user = "Write a three-sentence short story about a girl who likes to run."
# API messages array
messages = [
{
"role": "user",
"content": first_user
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print(first_response)In [ ]:
second_user = "Make the story better."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))In [ ]:
first_user = """Find all names from the below text:
"Hey, Jesse. It's me, Erin. I'm calling about the party that Joey is throwing tomorrow. Keisha said she would come and I think Mel will be there too."""
prefill = "<names>"
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": prefill
}
]
# Store and print Claude's response
first_response = get_completion(messages)
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(first_response)In [ ]:
second_user = "Alphabetize the list."
# API messages array
messages = [
{
"role": "user",
"content": first_user
},
{
"role": "assistant",
"content": prefill + "\n" + first_response
},
{
"role": "user",
"content": second_user
}
]
# Print Claude's response
print("------------------------ Full messsages array with variable substutions ------------------------")
print(messages)
print("\n------------------------------------- Claude's response -------------------------------------")
print(get_completion(messages))