PYTHON - ANTHROPIC - API
Simon-Pierre Boucher
2024-09-14

Python Version Jupyter Notebook Anthropic Anthropic Claude Anthropic API

In [1]:
import os
import requests
import json

def call_claude_api(
    model="claude-3-5-sonnet-20240620",
    messages=None,
    max_tokens=1024,
    anthropic_version="2023-06-01"
):
    # Get the API key from environment variables
    api_key = os.getenv("ANTHROPIC_API_KEY")
    
    if not api_key:
        raise ValueError("API key not found in environment variables. Please set ANTHROPIC_API_KEY.")
    
    url = "https://api.anthropic.com/v1/messages"
    
    headers = {
        "x-api-key": api_key,
        "anthropic-version": anthropic_version,
        "content-type": "application/json"
    }
    
    body = {
        "model": model,
        "max_tokens": max_tokens,
        "messages": messages
    }
    
    response = requests.post(url, headers=headers, data=json.dumps(body))
    
    if response.status_code != 200:
        raise ValueError(f"API request failed: {response.text}")
    
    result = response.json()
    return result
In [2]:
# Define the messages
messages = [
    {"role": "user", "content": "give me a list of gift idea for my girlfriend"}
]
In [3]:
# Call the function
response = call_claude_api(
    model="claude-3-5-sonnet-20240620",
    messages=messages,
    max_tokens=1024
)

# Display the assistant's response
print(response['content'][0]['text'])
Here's a list of gift ideas for your girlfriend:

1. Personalized jewelry (necklace, bracelet, or ring)
2. Spa day or massage gift certificate
3. Customized photo album or scrapbook
4. Tickets to a concert, theater show, or sports event
5. Subscription box tailored to her interests
6. Handmade crafts or artwork
7. Romantic weekend getaway
8. High-quality headphones or speakers
9. Cooking class for two
10. Kindle or e-reader with her favorite books preloaded
11. Stylish watch or smartwatch
12. Perfume or beauty gift set
13. Cozy blanket or throw
14. Fitness tracker or smartwatch
15. Gourmet food basket
16. Plants or a terrarium
17. Polaroid camera or instant photo printer
18. Luxury skincare or makeup products
19. Customized phone case or laptop sleeve
20. Art supplies if she's creative
21. Language learning software for a language she's interested in
22. Charitable donation in her name to a cause she cares about
23. Personalized star map of a significant date
24. Unique experience (e.g., hot air balloon ride, cooking class, wine tasting)
25. Handwritten love letter or book of reasons why you love her

Remember, the best gifts often come from knowing her interests, passions, and preferences.