Tạo yêu cầu đầu tiên

Nhập môn các mô hình Claude

Nikhil Rangarajan

Data Scientist

Thư viện Anthropic là gì?

  • SDK Python chính thức để truy cập Claude

  • Hỗ trợ cấu trúc tương tác với Claude

  • Cài bằng pip:

    pip install anthropic
    
  • Import thư viện:

    import anthropic
    

Sơ đồ từ prompt đến Anthropic API, rồi đến mô hình Claude

Nhập môn các mô hình Claude

Xác thực và thiết lập

$$

import anthropic

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

$$

🔑 Không cần khóa trong bài tập

Nhập môn các mô hình Claude

Gửi prompt đầu tiên

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: Giới hạn độ dài đầu ra (khoảng 4 ký tự/một token tiếng Anh.)
    • Token: đơn vị văn bản nhỏ nhất mà mô hình xử lý
  • messages: Danh sách tương tác bắt đầu từ đầu vào của người dùng
Nhập môn các mô hình Claude

Trích xuất đầu ra của mô hình

  • response - Toàn bộ phản hồi API có cấu trúc
  • response.content - Danh sách phần của thông điệp
    [{'type': 'text', 
    'text': 'Claude is...'}]
    
  • response.content[0].text - Phản hồi của trợ lý
    "Claude is..."
    
Nhập môn các mô hình Claude

Tổng hợp mọi thứ

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)
Nhập môn các mô hình Claude

Tổng hợp mọi thứ

import anthropic #Importing

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) 
Nhập môn các mô hình Claude

Tổng hợp mọi thứ

import anthropic #Importing

client = anthropic.Anthropic() #Initializing

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) 
Nhập môn các mô hình Claude

Tổng hợp mọi thứ

import anthropic #Importing

client = anthropic.Anthropic() #Initializing

response = client.messages.create( #Requesting
    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) 
Nhập môn các mô hình Claude

Tổng hợp mọi thứ

import anthropic #Importing

client = anthropic.Anthropic() #Initializing

response = client.messages.create( #Requesting
    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) #Extracting
Nhập môn các mô hình Claude

¡Vamos a practicar!

Nhập môn các mô hình Claude

Preparing Video For Download...