การดึงคำตอบจาก Claude
ในงาน Hackathon เพื่อนร่วมทีมต้องการข้อความคำตอบที่แน่ชัดเพื่อนำไปเชื่อมต่อกับ React front-end ลองแสดงวิธีดึงข้อมูลจาก response object และพิมพ์เฉพาะคำตอบของ Claude เพื่อนำไปแสดงผลในแอปของคุณ
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Claude Models เบื้องต้น
คำแนะนำการฝึกหัด
- ดึงและพิมพ์คำตอบของ Claude จาก
responseobject ที่กำหนดให้
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
from anthropic import Anthropic
client = Anthropic(api_key="datacamp-token", base_url=url)
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=150,
messages=[
{"role": "user", "content": "How does Claude work? Reply in one sentence."}
])
# Extract and print Claude's response
print(____)