Зробімо перший запит

Вступ до моделей Claude

Nikhil Rangarajan

Data Scientist

Що таке бібліотека Anthropic?

  • Офіційний Python SDK для доступу до Claude

  • Допомагає структурувати взаємодію з Claude

  • Встановлення через pip:

    pip install anthropic
    
  • Імпорт бібліотеки:

    import anthropic
    

Схема: від підказки до Anthropic API і далі до моделі Claude

Вступ до моделей Claude

Автентифікація та налаштування

$$

import anthropic

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

$$

🔑 Ключ не потрібен у вправах курсу

Вступ до моделей Claude

Надсилаємо першу підказку

response = client.messages.create(

model="claude-sonnet-4-6",
max_tokens=100,
messages=[{"role": "user", "content": "What DataCamp course do you recommend to learn Claude?"}]
)
  • model: Claude Sonnet
  • max_tokens: Обмежує розмір виводу (прибл. 4 символи на токен англійською.)
    • Токен: найменша текстова одиниця, яку обробляє модель
  • messages: Список взаємодій, що починається з введення користувача
Вступ до моделей Claude

Отримання виводу моделі

  • response — Повна структурована відповідь API
  • response.content — Повертає список частин повідомлення
    [{'type': 'text', 
    'text': 'Claude is...'}]
    
  • response.content[0].text — Відповідь помічника
    "Claude is..."
    
Вступ до моделей Claude

Обʼєднуємо все разом

import anthropic


client = anthropic.Anthropic()
response = client.messages.create( model="claude-sonnet-4-6", max_tokens=100, messages=[{"role": "user", "content": "Summarize the benefits of Claude in one sentence."}] )
print(response.content[0].text)
Вступ до моделей Claude

Обʼєднуємо все разом

import anthropic #Імпорт

client = anthropic.Anthropic() 

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

print(response.content[0].text) 
Вступ до моделей Claude

Обʼєднуємо все разом

import anthropic #Імпорт

client = anthropic.Anthropic() #Ініціалізація

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

print(response.content[0].text) 
Вступ до моделей Claude

Обʼєднуємо все разом

import anthropic #Імпорт

client = anthropic.Anthropic() #Ініціалізація

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

print(response.content[0].text) 
Вступ до моделей Claude

Обʼєднуємо все разом

import anthropic #Імпорт

client = anthropic.Anthropic() #Ініціалізація

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

print(response.content[0].text) #Витягнення
Вступ до моделей Claude

Давайте потренуємось!

Вступ до моделей Claude

Preparing Video For Download...