Claude Models เบื้องต้น
Nikhil Rangarajan
Data Scientist
Python SDK อย่างเป็นทางการสำหรับเข้าถึง Claude
ช่วยจัดโครงสร้างการโต้ตอบกับ Claude
ติดตั้งผ่าน pip:
pip install anthropic
นำเข้าไลบรารี:
import anthropic

$$
import anthropicclient = anthropic.Anthropic( api_key=os.getenv("ANTHROPIC_API_KEY") )
$$
🔑 ไม่จำเป็นต้องใช้คีย์ในแบบฝึกหัดของคอร์สนี้
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 Sonnetmax_tokens: จำกัดขนาดผลลัพธ์ (ประมาณ 4 ตัวอักษรต่อ token ในภาษาอังกฤษ)messages: รายการการโต้ตอบที่เริ่มจาก input ของผู้ใช้response - การตอบสนองจาก API ทั้งหมดในรูปแบบโครงสร้างresponse.content - คืนค่ารายการส่วนของข้อความ[{'type': 'text',
'text': 'Claude is...'}]
response.content[0].text - คำตอบจาก assistant"Claude is..."
import anthropicclient = 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)
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)
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)
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)
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 เบื้องต้น