Lesson 5 of 6
Add parameters and read a file
Add parameters for temperature and maximum output length, and read the text to summarise from a file path given on the command line.
6 minClaude, Gemini and ChatGPT
After this lesson you will be able to control how creative Claude is with the temperature parameter, set the maximum output length, and read the text to summarise from a file path given on the command line.
Add a temperature parameter
In the previous lesson you called the API with a fixed prompt and a maximum output length of 100 tokens. Now you will make the script more flexible. The first change is to add a temperature parameter. Temperature controls how varied the response is. A low temperature, such as 0.2, makes the output more focused and predictable. A high temperature, such as 0.8, makes it more creative but less reliable. For a summary, you usually want a low temperature so the result stays close to the source text.
Open api_call.py from the previous lesson. It should look like this:
import os
from anthropic import Anthropic
from anthropic import AuthenticationError, RateLimitError
# Replace with a current model from the Anthropic model list
MODEL_NAME = "claude-3-5-sonnet-20241022"
client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
prompt = "Explain what an API is in one sentence."
try:
message = client.messages.create(
model=MODEL_NAME,
max_tokens=100,
messages=[
{"role": "user", "content": prompt}
]
)
print(message.content[0].text)
except AuthenticationError:
print("Authentication failed. Check your ANTHROPIC_API_KEY.")
except RateLimitError:
print("Rate limit exceeded. Wait a moment and try again.")Add a line that sets a temperature value, and pass it to the API call. For a summary, use 0.3. The changed part of the script is:
temperature = 0.3
message = client.messages.create(
model=MODEL_NAME,
max_tokens=100,
temperature=temperature,
messages=[
{"role": "user", "content": prompt}
]
)The temperature must be between 0 and 1. If you do not set it, the API uses a default value, which is often 1.0. For tasks where you need a consistent answer, set it explicitly.
Set the maximum output length
The max_tokens parameter limits how many tokens the model can generate. A token is roughly a piece of a word. For a short summary, 100 tokens may be enough, but for a longer text you might need more. Instead of hard-coding the value, you can make it a variable that you can change easily.
Add a line for max_tokens and use it in the call. For now, set it to 200. The changed part is:
max_tokens = 200
message = client.messages.create(
model=MODEL_NAME,
max_tokens=max_tokens,
temperature=temperature,
messages=[
{"role": "user", "content": prompt}
]
)If the model needs more tokens than you allow, the output will be cut off. If you set it too high, you may pay for tokens you do not use. For a summary, 200 tokens is a reasonable starting point.
Read the text from a file
So far the prompt is a fixed string. In the final tool, you will pass a file path on the command line and the script will read that file and summarise its content. To prepare for that, you will now read a text file and use its content in the prompt.
Create a new file called pandas.txt in the same directory as api_call.py. Put the Pandas text from Lesson 2 into it:
Pandas is a Python library for working with data. It provides two main data structures: Series for one-dimensional data and DataFrame for two-dimensional data. You can use Pandas to read data from CSV files, clean it, and analyse it.Now change the script to read this file. You need to import the sys module to get the command-line argument, and then read the file. The full script after these changes is:
import os
import sys
from anthropic import Anthropic
from anthropic import AuthenticationError, RateLimitError
# Replace with a current model from the Anthropic model list
MODEL_NAME = "claude-3-5-sonnet-20241022"
client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
temperature = 0.3
max_tokens = 200
if len(sys.argv) != 2:
print("Usage: python api_call.py <file_path>")
sys.exit(1)
file_path = sys.argv[1]
try:
with open(file_path, "r", encoding="utf-8") as file:
text = file.read()
except FileNotFoundError:
print(f"File not found: {file_path}")
sys.exit(1)
prompt = f"""You are a patient teacher who explains technical ideas to beginners.
Explain the text below in simple words.
Write three bullet points. Each bullet point must be one sentence.
Text:
{text}"""
try:
message = client.messages.create(
model=MODEL_NAME,
max_tokens=max_tokens,
temperature=temperature,
messages=[
{"role": "user", "content": prompt}
]
)
print(message.content[0].text)
except AuthenticationError:
print("Authentication failed. Check your ANTHROPIC_API_KEY.")
except RateLimitError:
print("Rate limit exceeded. Wait a moment and try again.")The script now expects a file path as a command-line argument. It reads the file content and inserts it into the prompt. The prompt uses the same structure as in Lesson 2: role, task, output format, and text.
Run the script with the file path:
python api_call.py pandas.txtThe output should be three bullet points that explain the Pandas text in simple words. The exact wording may vary, but the structure should match the output format you specified.
If the file does not exist, the script prints an error message and stops. If the API key is wrong, it prints an authentication error. If you hit a rate limit, it tells you to wait.
Summary
You have added two parameters to the API call: temperature controls creativity, and max_tokens limits the output length. You have also changed the script to read the text from a file given on the command line. In the next lesson, you will combine these pieces into a single summariser tool.
Key takeaways
- Temperature controls how varied the response is; a low value gives more focused output.
- max_tokens limits the number of tokens the model can generate.
- Read the text to summarise from a file path passed as a command-line argument.
- Handle file errors and API errors so the script fails with a clear message.
Try it
Modify api_call.py so that the temperature and max_tokens values can be set as command-line arguments. The script should accept an optional second argument for temperature (a float between 0 and 1) and an optional third argument for max_tokens (an integer). If they are not provided, use the defaults 0.3 and 200. Run the script with python api_call.py pandas.txt 0.5 150. Check that the output is three bullet points and that it is not cut off. Then run it with python api_call.py pandas.txt and confirm the defaults are used. A good result is a script that handles both cases without errors and produces a valid summary.
Official documentation
Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.