LangChain BasicsLesson 3

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

modelgpt-4o-mini

Which weights/API endpoint to use

temperature0 → 0.9

Low = repeats the same shape. High = more word choice.

max_tokens80

Caps how long the reply can grow.

In LangChain you set these in the constructor — not in a separate settings screen.
  • model — which name to send to the API (you already used gpt-4o-mini and llama3.2).
  • temperature — how much randomness to allow. Use 0 for notes, quizzes, and code help. Try 0.7–1.0 when 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.

model_params_demo.py
"""model_params_demo.py — temperature and max_tokens"""
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
load_dotenv()
prompt = "List three HTML tags useful on a tutorial page."
low_temp = ChatOpenAI(model="gpt-4o-mini", temperature=0, max_tokens=80)
high_temp = ChatOpenAI(model="gpt-4o-mini", temperature=0.9, max_tokens=80)
print("--- temperature=0 ---")
print(low_temp.invoke([HumanMessage(content=prompt)]).content)
… second print block for temperature=0.9 …
Two 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.py
PowerShell — (.venv) active
(.venv) PS C:\projects\langchain-course> python model_params_demo.py
--- temperature=0 ---
1. <h1> — page heading
2. <p> — paragraph text
3. <a> — hyperlink
--- temperature=0.9 ---
<header>, <nav>, and <code> — common picks when you are laying out a tutorial page.
At temperature 0 you usually get a numbered list. At 0.9 the tags and wording can shift — your lines will differ each run.

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 casetemperaturemax_tokens
Course notes / factual Q&A0100–300
Explain a concept0.2–0.4300–500
Looser wording0.8–1.0500+

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.