0
0
LangChainframework~5 mins

Model parameters (temperature, max tokens) in LangChain

Choose your learning style9 modes available
Introduction

Model parameters like temperature and max tokens help control how the AI responds. They let you make answers more creative or limit their length.

When you want the AI to give more creative or random answers.
When you want to keep the AI's response short and focused.
When you want to balance between creativity and accuracy in answers.
When you want to avoid very long or very short replies from the AI.
When you want to customize AI behavior for different tasks like chat or summarizing.
Syntax
LangChain
from langchain.llms import OpenAI

llm = OpenAI(temperature=0.7, max_tokens=150)

temperature controls randomness: 0 is very focused, 1 is very creative.

max_tokens limits how many words or pieces the AI can generate.

Examples
Very focused answers, limited to 100 tokens.
LangChain
llm = OpenAI(temperature=0.0, max_tokens=100)
Very creative answers, up to 200 tokens long.
LangChain
llm = OpenAI(temperature=1.0, max_tokens=200)
Balanced creativity with default max tokens.
LangChain
llm = OpenAI(temperature=0.5)
Sample Program

This example sets the AI to be somewhat creative but limits the answer length. It then asks for a fun fact about cats and prints the answer.

LangChain
from langchain.llms import OpenAI

# Create a model with medium creativity and max 50 tokens
llm = OpenAI(temperature=0.5, max_tokens=50)

# Ask a simple question
response = llm("What is a fun fact about cats?")

print(response)
OutputSuccess
Important Notes

Lower temperature values make the AI more predictable and focused.

Higher temperature values make the AI more creative but less predictable.

Setting max_tokens too low may cut off answers early.

Summary

Temperature controls how creative or random the AI's answers are.

Max tokens limit how long the AI's response can be.

Adjust these to get the style and length of answers you want.