YouTip LogoYouTip

Langchain Model Params

\\ In the previous section, we learned the basic usage of [init_chat_model()](https://example.com/langchain/langchain-init_chat_mode.html).\\ \\ This section will introduce in detail the common control parameters when calling the model, helping you better control the model's behavior.\\ \\ * * *\\ \\ ## temperatureβ€”β€”Controlling Creativity and Determinism\\ \\ temperature is the most commonly used parameter, with a value range from 0 to 2. It controls the randomness of the model's output.\\ \\ ## Example\\ \\ from langchain.chat_models import init_chat_model\\ \\ # Same question, comparison of different temperature\\ \\ question ="Introduce the TUTORIAL platform in one sentence"\\ \\ # temperature=0: output is very deterministic, almost the same every time\\ \\ model_low = init_chat_model("deepseek:deepseek-v4-flash", temperature=0)\\ \\ resp1 = model_low.invoke(question)\\ \\ resp2 = model_low.invoke(question)\\ \\ print(f"temperature=0 Line1times: {resp1.content}")\\ \\ print(f"temperature=0 Line2times: {resp2.content}")\\ \\ print(f"Two times the result is the same: {resp1.content == resp2.content}")\\ \\ print()\\ \\ # temperature=1.5: diverse output, may differ each time\\ \\ model_high = init_chat_model("deepseek:deepseek-v4-flash", temperature=1.5)\\ \\ resp1 = model_high.invoke(question)\\ \\ resp2 = model_high.invoke(question)\\ \\ print(f"temperature=1.5 Line1times: {resp1.content}")\\ \\ print(f"temperature=1.5 Line2times: {resp2.content}")\\ \\ Run result:\\ \\ temperature=0 Line1times: TUTORIAL is a learning platform that provides free tutorials for programming beginners. temperature=0 Line2times: Tutorial (TUTORIAL) is a learning platform that provides free tutorials for programming beginners.Two times the result is the same: True temperature=1.5 Line1times: TUTORIAL is a simple and easy-to-understand introductory learning platform for programming and network technology. temperature=1.5 Line2times: TUTORIAL(is a Chinese free online technical learning platform beloved by programming novices.\\ \\ | temperature Value | Effect | Applicable Scenario |\\ | --- | --- | --- |\\ | 0 ~ 0.3 | Stable, deterministic output, almost consistent every time | Data extraction, classification, code generation, translation |\\ | 0.5 ~ 0.7 | Moderate creativity, natural output but on topic | Daily conversation, content summarization |\\ | 0.8 ~ 1.2 | Diverse output, more room for creativity | Creative writing, brainstorming |\\ | 1.3 ~ 2.0 | Very random output, may produce unexpected content | Exploratory generation (not recommended for production) |\\ \\ > temperature=0 does not equal "completely identical". Due to floating-point precision differences in the model's internal calculations, there may still be tiny differences in extreme cases. If absolute determinism is needed, some models provide a seed parameter.\\ \\ * * *\\ \\ ## max_tokensβ€”β€”Controlling Output Length and Cost\\ \\ max_tokens limits the maximum number of Tokens the model can output. One Token is approximately equivalent to 0.75 English words or 0.5 Chinese characters.\\ \\ ## Example\\ \\ from langchain.chat_models import init_chat_model\\ \\ model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0)\\ \\ # max_tokens=30: limit output to within 30 Tokens\\ \\ response_short = model.invoke(\\ \\ "Introduce the TUTORIAL platform in detail",\\ \\ max_tokens=30\\ \\ )\\ \\ print(f"Limit 30 tokens ({len(response_short.content)} Character):")\\ \\ print(response_short.content)\\ \\ print()\\ \\ # max_tokens=200: allow longer output\\ \\ response_long = model.invoke(\\ \\ "Introduce the TUTORIAL platform in detail",\\ \\ max_tokens=200\\ \\ )\\ \\ print(f"Limit 200 tokens ({len(response_long.content)} Character):")\\ \\ print(response_long.content)\\ \\ Run result:\\ \\ Limit 30 tokens (42 Character):TUTORIAL is an online learning platform for programming beginners, providing tutorials and examples in various programming languages. Limit 200 tokens (306 Character):TUTORIAL is an online learning platform for programming beginners, providing rich programming tutorials and reference materials. The platform covers HTML, CSS, JavaScript, Python, Java, C, C++, PHP, SQL and other programming languages and technologies. Features of TUTORIAL include:1. Systematic content: from basics to advanced, helping learners gradually master programming knowledge2. Rich examples: each knowledge point comes with runnable code examples3. ...\\ \\ > max_tokens is a hard limit on output length. If set too
← Langchain MessagesLangchain First Agent β†’