始める無料で始める

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(____)
コードを編集して実行