889 KiB
889 KiB
In [3]:
from dotenv import load_dotenv
from anthropic import Anthropic
#load environment variable
load_dotenv()
#automatically looks for an "ANTHROPIC_API_KEY" environment variable
client = Anthropic()In [2]:
truncated_response = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=10,
messages=[
{"role": "user", "content": "Write me a poem"}
]
)
print(truncated_response.content[0].text)Here is a poem for you: The
In [26]:
truncated_response.stop_reasonOut [26]:
'max_tokens'
In [27]:
longer_poem_response = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=500,
messages=[
{"role": "user", "content": "Write me a poem"}
]
)
print(longer_poem_response.content[0].text)Here is a poem for you: Whispers of the Heart In the quiet moments, When the world fades away, I hear the whispers of my heart - Gentle words that gently sway. They speak of dreams still unfurled, Of love that shines like the sun, Of passions yet to be explored, Of all that's yet to be done. These whispers, they guide my way, Reminding me to pause and feel, To listen closely to the soul, And let its truths be revealed. For in the silence, in the calm, The heart's true voice can be heard, Weaving a tapestry of hope, With every softly spoken word. So I will heed these whispers dear, And let them be my faithful friend, For in their song, I find my way, To all my heart hopes to transcend.
In [28]:
longer_poem_response.stop_reasonOut [28]:
'end_turn'
In [4]:
response = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=1000,
messages=[{"role": "user", "content": "Tell me a joke"}]
)In [6]:
print(response.content[0].text)Here's a classic dad joke for you: Why don't scientists trust atoms? Because they make up everything! How was that? I tried to keep it clean and mildly amusing. Let me know if you'd like to hear another joke.
In [7]:
print(response.usage.output_tokens)55
In [4]:
import time
def compare_num_tokens_speed():
token_counts = [100,1000,4096]
task = """
Create a long, detailed dialogue that is at least 5000 words long between two characters discussing the impact of social media on mental health.
The characters should have differing opinions and engage in a respectful thorough debate.
"""
for num_tokens in token_counts:
start_time = time.time()
response = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=num_tokens,
messages=[{"role": "user", "content": task}]
)
end_time = time.time()
execution_time = end_time - start_time
print(f"Number of tokens generated: {response.usage.output_tokens}")
print(f"Execution Time: {execution_time:.2f} seconds\n")In [5]:
compare_num_tokens_speed()Number of tokens generated: 100 Execution Time: 1.51 seconds Number of tokens generated: 1000 Execution Time: 8.33 seconds Number of tokens generated: 3433 Execution Time: 28.80 seconds
In [21]:
response = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=500,
messages=[{"role": "user", "content": "Generate a JSON object representing a person with a name, email, and phone number ."}],
)
print(response.content[0].text)Here's an example of a JSON object representing a person with a name, email, and phone number:
```json
{
"name": "John Doe",
"email": "johndoe@example.com",
"phoneNumber": "123-456-7890"
}
```
In this example, the JSON object has three key-value pairs:
1. "name": The person's name, which is a string value of "John Doe".
2. "email": The person's email address, which is a string value of "johndoe@example.com".
3. "phoneNumber": The person's phone number, which is a string value of "123-456-7890".
You can modify the values to represent a different person with their own name, email, and phone number.
In [23]:
response = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=500,
messages=[{"role": "user", "content": "Generate a JSON object representing a person with a name, email, and phone number ."}],
stop_sequences=["}"]
)
print(response.content[0].text)Here's a JSON object representing a person with a name, email, and phone number:
{
"name": "John Doe",
"email": "john.doe@example.com",
"phone": "555-1234"
In [24]:
response.stop_reasonOut [24]:
'stop_sequence'
In [25]:
response.stop_sequenceOut [25]:
'}'
In [37]:
def generate_random_letters_3_times():
for i in range(3):
response = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=500,
messages=[{"role": "user", "content": "generate a poem"}],
stop_sequences=["b", "c"]
)
print(f"Response {i+1} stopped because {response.stop_reason}. The stop sequence was {response.stop_sequence}")In [38]:
generate_random_letters_3_times()Response 1 stopped because stop_sequence. The stop sequence was c Response 2 stopped because stop_sequence. The stop sequence was b Response 3 stopped because stop_sequence. The stop sequence was b
In [47]:
def demonstrate_temperature():
temperatures = [0, 1]
for temperature in temperatures:
print(f"Prompting Claude three times with temperature of {temperature}")
print("================")
for i in range(3):
response = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=100,
messages=[{"role": "user", "content": "Come up with a name for an alien planet. Respond with a single word."}],
temperature=temperature
)
print(f"Response {i+1}: {response.content[0].text}")
In [48]:
demonstrate_temperature()Prompting Claude three times with temperature of 0 ================ Response 1: Xendor. Response 2: Xendor. Response 3: Xendor. Prompting Claude three times with temperature of 1 ================ Response 1: Xyron. Response 2: Xandar. Response 3: Zyrcon.
In [4]:
message = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=1000,
system="You are a helpful foreign language tutor that always responds in French.",
messages=[
{"role": "user", "content": "Hey there, how are you?!"}
]
)
print(message.content[0].text)Bonjour ! Je suis ravi de vous rencontrer. Comment allez-vous aujourd'hui ?
In [1]:
def generate_questions(topic, num_questions=3):
response = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=500,
system=f"You are an expert on {topic}. Generate thought-provoking questions about this topic.",
messages=[
{"role": "user", "content": f"Generate {num_questions} questions about {topic} as a numbered list."}
],
stop_sequences=[f"{num_questions+1}."]
)
print(response.content[0].text)In [5]:
generate_questions(topic="free will", num_questions=3)Here are three thought-provoking questions about free will: 1. To what extent do our decisions and actions truly originate from our own free will, rather than being shaped by factors beyond our control, such as our genes, upbringing, and societal influences? 2. If our decisions are ultimately the result of a complex interplay of biological, psychological, and environmental factors, does that mean we lack the ability to make authentic, autonomous choices, or is free will compatible with determinism? 3. What are the ethical and philosophical implications of embracing or rejecting the concept of free will? How might our views on free will impact our notions of moral responsibility, punishment, and the nature of the human condition?


