Model Parameters
Your scripts left temperature and max_tokens at defaults so far. This lesson sets both on purpose — the same prompt can print a short numbered list or looser wording depending on the values you pass in.
The knobs that matter first
Constructor settings
Passed when you create ChatOpenAI or ChatOllama
Which weights/API endpoint to use
Low = repeats the same shape. High = more word choice.
Caps how long the reply can grow.
model— which name to send to the API (you already usedgpt-4o-miniandllama3.2).temperature— how much randomness to allow. Use0for notes, quizzes, and code help. Try0.7–1.0when you want more varied wording.max_tokens— upper limit on reply length. Keeps output short and lowers cost on OpenAI calls.
The script
Save as model_params_demo.py. It asks for three HTML tags twice — once with temperature=0, once with 0.9. Both use max_tokens=80.
ChatOpenAI instances, same prompt — only temperature differs.Run it
Needs a valid OPENAI_API_KEY as in LangChain Basics. The script makes two API calls.
python model_params_demo.py2. <p> — paragraph text
3. <a> — hyperlink
Same idea on Ollama
ChatOllama from your Ollama script accepts temperature. Ollama uses num_predict instead of max_tokens for length:
llm = ChatOllama(
model="llama3.2",
temperature=0.2,
num_predict=80,
)Practical defaults
| Use case | temperature | max_tokens |
|---|---|---|
| Course notes / factual Q&A | 0 | 100–300 |
| Explain a concept | 0.2–0.4 | 300–500 |
| Looser wording | 0.8–1.0 | 500+ |
Full API list: OpenAI chat parameters, LangChain chat integrations.
What's Next
temperature and max_tokens are set. Next: where messages and clients sit in the LangChain stack.