การส่ง request ครั้งแรก

Claude Models เบื้องต้น

Nikhil Rangarajan

Data Scientist

Anthropic library คืออะไร?

  • Python SDK อย่างเป็นทางการสำหรับเข้าถึง Claude

  • ช่วยจัดโครงสร้างการโต้ตอบกับ Claude

  • ติดตั้งผ่าน pip:

    pip install anthropic
    
  • นำเข้าไลบรารี:

    import anthropic
    

ไดอะแกรมเริ่มจาก prompt ไปยัง Anthropic API แล้วไปยังโมเดล Claude

Claude Models เบื้องต้น

การยืนยันตัวตนและการตั้งค่า

$$

import anthropic

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

$$

🔑 ไม่จำเป็นต้องใช้คีย์ในแบบฝึกหัดของคอร์สนี้

Claude Models เบื้องต้น

การส่ง prompt แรก

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 ตัวอักษรต่อ token ในภาษาอังกฤษ)
    • Token: หน่วยข้อความที่เล็กที่สุดที่โมเดลประมวลผล
  • messages: รายการการโต้ตอบที่เริ่มจาก input ของผู้ใช้
Claude Models เบื้องต้น

การดึงผลลัพธ์จากโมเดล

  • response - การตอบสนองจาก API ทั้งหมดในรูปแบบโครงสร้าง
  • response.content - คืนค่ารายการส่วนของข้อความ
    [{'type': 'text', 
    'text': 'Claude is...'}]
    
  • response.content[0].text - คำตอบจาก assistant
    "Claude is..."
    
Claude Models เบื้องต้น

รวมทุกอย่างเข้าด้วยกัน

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 Models เบื้องต้น

รวมทุกอย่างเข้าด้วยกัน

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) 
Claude Models เบื้องต้น

รวมทุกอย่างเข้าด้วยกัน

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) 
Claude Models เบื้องต้น

รวมทุกอย่างเข้าด้วยกัน

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) 
Claude Models เบื้องต้น

รวมทุกอย่างเข้าด้วยกัน

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
Claude Models เบื้องต้น

มาฝึกกันเถอะ!

Claude Models เบื้องต้น

Preparing Video For Download...