LoslegenKostenlos loslegen

Extracting Claude's response

At the hackathon, your teammate needs the exact reply text to wire into the React front-end. Show them how to slice the response object and print just Claude’s answer, so that it can be displayed on your app.

Diese Übung ist Teil des Kurses

Introduction to Claude Models

Kurs anzeigen

Anleitung zur Übung

  • Extract and print Claude's response from the given response object.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

from anthropic import Anthropic

client = Anthropic(api_key="datacamp-token", base_url=url)

response = client.messages.create(
    model="claude-3-7-sonnet-latest",
    max_tokens=150,
    messages=[
        {"role": "user", "content": "How does Claude work? Reply in one sentence."}
    ])
# Extract and print Claude's response
print(____)
Code bearbeiten und ausführen