Skip to content
New Workbook
Sign up
Claude 3 Opus Deep Dive
%%capture
%pip install anthropic

Setting Up

import os
import anthropic

client = anthropic.Anthropic(
    api_key=os.environ["ANTHROPIC_API_KEY"],
)

API that Only works with Claude 2

from anthropic import HUMAN_PROMPT, AI_PROMPT

completion = client.completions.create(
    model="claude-2.1",
    max_tokens_to_sample=300,
    prompt=f"{HUMAN_PROMPT} What is Matthew effect? {AI_PROMPT}",
)
Markdown(completion.completion)

Adding System Prompt

from IPython.display import Markdown, display
Prompt = "Write the Go code for the simple data analysis."
message = client.messages.create(
    model="claude-3-opus-20240229",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": Prompt}
    ]
)
Markdown(message.content[0].text)

Adding System Prompt

Prompt = "Write a report on climate change."

message = client.messages.create(
    model="claude-3-opus-20240229",
    max_tokens=1024,
    system="Respond only in Svenska.",
    messages=[
        {"role": "user", "content": Prompt} 
    ]
)

Markdown(message.content[0].text)

Claude 3 Vision Block

  • Photo by Rachel Xiao: https://www.pexels.com/photo/brown-pendant-lamp-hanging-on-tree-near-river-772429/
  • Photo by Debayan Chakraborty: https://www.pexels.com/photo/indian-blue-jay-20433278/

import anthropic
import base64
import httpx

client = anthropic.Anthropic()

media_type = "image/jpeg"

url_1 = "https://images.pexels.com/photos/20433278/pexels-photo-20433278/free-photo-of-indian-blue-jay.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2"

image_encode_1 = base64.b64encode(httpx.get(url_1).content).decode("utf-8")

url_2 = "https://images.pexels.com/photos/772429/pexels-photo-772429.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2"

image_encode_2 = base64.b64encode(httpx.get(url_2).content).decode("utf-8")

Vision with a Single Image

message = client.messages.create(
    model="claude-3-opus-20240229",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": media_type,
                        "data": image_encode_1,
                    },
                },
                {
                    "type": "text",
                    "text": "Describe the image."
                }
            ],
        }
    ],
)
Markdown(message.content[0].text)