Membuat permintaan pertama Anda

Pengantar Model Claude

Nikhil Rangarajan

Data Scientist

Apa itu pustaka Anthropic?

  • SDK Python resmi untuk mengakses Claude

  • Membantu menstrukturkan interaksi dengan Claude

  • Instal via pip:

    pip install anthropic
    
  • Mengimpor pustaka:

    import anthropic
    

Diagram mulai dari prompt, ke Anthropic API, lalu ke model Claude

Pengantar Model Claude

Autentikasi dan penyiapan

$$

import anthropic

client = anthropic.Anthropic( api_key=os.getenv("ANTHROPIC_API_KEY") )

$$

🔑 Kunci tidak diperlukan di latihan kursus

Pengantar Model Claude

Mengirim prompt pertama kita

response = client.messages.create(

model="claude-sonnet-4-20250514",
max_tokens=100,
messages=[{"role": "user", "content": "What DataCamp course do you recommend to learn Claude?"}]
)
  • model: Claude Sonnet
  • max_tokens: Membatasi ukuran output (sekitar 4 karakter per token dalam bahasa Inggris.)
    • Token: unit teks terkecil yang diproses model
  • messages: Daftar interaksi dimulai dari input pengguna
Pengantar Model Claude

Mengekstrak output model

  • response - Seluruh respons API terstruktur
  • response.content - Mengembalikan daftar bagian pesan
    [{'type': 'text', 
    'text': 'Claude is...'}]
    
  • response.content[0].text - Balasan asisten
    "Claude is..."
    
Pengantar Model Claude

Menggabungkan semuanya

import anthropic


client = anthropic.Anthropic()
response = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=100, messages=[{"role": "user", "content": "Summarize the benefits of Claude in one sentence."}] )
print(response.content[0].text)
Pengantar Model Claude

Menggabungkan semuanya

import anthropic #Mengimpor

client = anthropic.Anthropic() 

response = client.messages.create( 
    model="claude-sonnet-4-20250514",
    max_tokens=100,
    messages=[{"role": "user", 
               "content": "Summarize the benefits of Claude in one sentence."}]
)

print(response.content[0].text) 
Pengantar Model Claude

Menggabungkan semuanya

import anthropic #Mengimpor

client = anthropic.Anthropic() #Inisialisasi

response = client.messages.create( 
    model="claude-sonnet-4-20250514",
    max_tokens=100,
    messages=[{"role": "user", 
               "content": "Summarize the benefits of Claude in one sentence."}]
)

print(response.content[0].text) 
Pengantar Model Claude

Menggabungkan semuanya

import anthropic #Mengimpor

client = anthropic.Anthropic() #Inisialisasi

response = client.messages.create( #Meminta
    model="claude-sonnet-4-20250514",
    max_tokens=100,
    messages=[{"role": "user", 
               "content": "Summarize the benefits of Claude in one sentence."}]
)

print(response.content[0].text) 
Pengantar Model Claude

Menggabungkan semuanya

import anthropic #Mengimpor

client = anthropic.Anthropic() #Inisialisasi

response = client.messages.create( #Meminta
    model="claude-sonnet-4-20250514",
    max_tokens=100,
    messages=[{"role": "user", 
               "content": "Summarize the benefits of Claude in one sentence."}]
)

print(response.content[0].text) #Ekstraksi
Pengantar Model Claude

Ayo berlatih!

Pengantar Model Claude

Preparing Video For Download...