Controlling response length with tokens
To better control responses from QuickAid, users can now click either "Quick Answer", for a tweet-sized blurb, or "Deep Dive", for a fuller explanation.
Implement both calls and print the side-by-side results so the hackathon panel can see the difference at a glance.
The anthropic
library has been pre-imported and the client
has been pre-defined.
This exercise is part of the course
Introduction to Claude Models
Exercise instructions
- Set
max_tokens
as 25 for the short response to get a very concise answer (about 15-20 words). - Set
max_tokens
as 200 for the long response to get a detailed explanation (about 150 words).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
prompt = "Explain what artificial intelligence is."
# Create short response (25 tokens)
short_response = client.messages.create(model="claude-3-7-sonnet-latest", max_tokens=____, messages=[{"role": "user", "content": prompt}])
# Create long response (200 tokens)
long_response = client.messages.create(model="claude-3-7-sonnet-latest", max_tokens=____, messages=[{"role": "user", "content": prompt}])
print("Short response:", short_response.content[0].text)
print("Long response:", long_response.content[0].text)