擷取 Claude 的回應
在黑客松上,你的隊友需要精確的回覆文字,才能串接到 React 前端。示範如何切出回應物件並只印出 Claude 的答案,讓它能顯示在你的應用程式上。
本練習屬於課程
Claude 模型入門
練習說明
- 從給定的
response物件中擷取並印出 Claude 的回應。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
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(____)