Introduction

In the back-end development of large-model applications, it is often necessary to connect online dialogue models from different vendors, while also taking into account local offline reasoning models. There are differences in the SDKs and input parameter formats of different service providers. If each model writes a separate set of request logic, a large amount of redundant code will be generated, and the later maintenance cost will be very high.

LangChain 1.x provides three sets of standardized access solutions: manufacturer-specific packaging, OpenAI standard compatible interface, and unified initialization tool init_chat_model. Each of the three solutions has applicable scenarios. It can not only use the unique expansion capabilities of the model, but also realize a set of codes to quickly switch models. This article only explains configuration security specifications, complete runnable code, generation parameter tuning, and local offline model access from the perspective of project implementation. The full text only explains the technical implementation, and does not include any platform recommendations or traffic related content.

1. Sensitive configuration security management specifications

Model interface keys and service addresses are privacy credentials and are not allowed to be directly hard-coded in business code. Industry common solutions are through .env Files are stored uniformly and configured using python-dotenv Read environment variables; also in .gitignore Ignore the configuration in the file .env, to prevent the key from being leaked when submitted to the code repository.

1. Project dependency installation command

pip install langchain langchain-deepseek langchain-zhipuai langchain-openai langchain-openrouter python-dotenv

2. .env configuration file template

Create a new project root directory .env Files are only saved locally and are not submitted to the code repository:

# Online model service provider interface configuration
DEEPSEEK_API_KEY=sk-xxx
DEEPSEEK_BASE_URL=https://api.deepseek.com

ZHIPUAI_API_KEY=sk-xxx
ZHIPUAI_BASE_URL=https://open.bigmodel.cn/api/paas/v4/

# Model aggregation service configuration
OPENROUTER_API_KEY=sk-xxx
OPENROUTER_BASE_URL=https://openrouter.ai/api/v1

3. Load the basic code of environment variables

load_dotenv(override=True) Local configuration files will be read first and global system environment variables will be overwritten. All model calling codes need to be executed in advance:

import os
from dotenv import load_dotenv
# Load local configuration file
load_dotenv(override=True)

2. Manufacturer-specific encapsulation access method

LangChain encapsulates exclusive dialogue classes for the mainstream commercial dialogue models in the market. It natively supports the unique extension parameters of each manufacturer and is suitable for business projects that can stably connect to a single model in the long term.

2.1 Three ways to initialize ChatDeepSeek

  1. Read .env configuration (standard writing method for production environment)
import os
from dotenv import load_dotenv
from langchain_deepseek import ChatDeepSeek

load_dotenv(override=True)
api_key = os.getenv("DEEPSEEK_API_KEY")
base_url = os.getenv("DEEPSEEK_BASE_URL")

llm = ChatDeepSeek(
    model="deepseek-v4-flash",
    api_key=api_key,
    api_base=base_url,
)
res = llm.invoke("Please introduce yourself in one sentence")
print(res.content)
  1. Extremely shorthand (used when global environment variables have been configured locally)
from dotenv import load_dotenv
from langchain_deepseek import ChatDeepSeek
load_dotenv(override=True)
llm = ChatDeepSeek(model="deepseek-v4-flash")
res = llm.invoke("Please introduce yourself in one sentence")
print(res.content)
  1. Pass the credentials directly into the code (only local temporary debugging, online use is prohibited)
from langchain_deepseek import ChatDeepSeek
llm = ChatDeepSeek(
    model="deepseek-v4-flash",
    api_key="Replace personal credentials",
    api_base="https://api.deepseek.com",
)

2.2 ChatZhipuAI access example

The usage logic is exactly the same as the model above, only the imported class, environment variable identifier, and model name are replaced:

import os
from dotenv import load_dotenv
from langchain_community.chat_models import ChatZhipuAI

load_dotenv(override=True)
api_key = os.getenv("ZHIPUAI_API_KEY")
base_url = os.getenv("ZHIPUAI_BASE_URL")

llm = ChatZhipuAI(
    model="glm-5.1",
    api_key=api_key,
    api_base=base_url,
)
res = llm.invoke("Please introduce yourself in one sentence")
print(res.content)

3. OpenAI standard compatible universal access

Currently, most commercial models and model aggregation services are compatible with the OpenAI interface specification. There is no need to import various manufacturer-specific SDKs and can be used uniformly. ChatOpenAI The call can be completed, which is suitable for rapid development scenarios that require frequent switching of different models.

Example of docking:

import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

load_dotenv(override=True)
api_key = os.getenv("DEEPSEEK_API_KEY")
base_url = os.getenv("DEEPSEEK_BASE_URL")

llm = ChatOpenAI(
    model="deepseek-v4-flash",
    api_key=api_key,
    base_url=base_url,
)
res = llm.invoke("1+3equal to how much")
print(res.content)

This solution is highly versatile and can be reused with domestic mainstream commercial models and various aggregation services. It only needs to adjust the three parameters of the interface address, voucher, and model name.

4. Example of model aggregation service access

The model aggregation service can call multiple models from different manufacturers through a unified interface, and LangChain provides exclusive encapsulation classes ChatOpenRouter, the model name needs to follow the format specified by the platform "manufacturer identification/model name".

import os
from dotenv import load_dotenv
from langchain_openrouter import ChatOpenRouter

load_dotenv(override=True)
api_key = os.getenv("OPENROUTER_API_KEY")
base_url = os.getenv("OPENROUTER_BASE_URL")

llm = ChatOpenRouter(
    model="deepseek/deepseek-v4-flash",
    api_key=api_key,
    base_url=base_url,
)
res = llm.invoke("What conversation model are you?")
print(res.content)

Aggregation services also support ChatOpenAI It is compatible with the writing method and the unified initialization tool later, and the adaptation method is flexible.

5. LangChain 1.x unified initialization tool init_chat_model

init_chat_model It is a general model initialization tool built into LangChain 1.x. A set of code can switch any manufacturer's model without frequent replacement of imported classes. It is suitable for business systems with multi-model scheduling and dynamic model switching.

5.1 Example of commercial model on the docking line

Model naming format:Service provider identification:Model name, the framework automatically matches the underlying driver:

import os
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model

load_dotenv(override=True)
api_key = os.getenv("DEEPSEEK_API_KEY")
base_url = os.getenv("DEEPSEEK_BASE_URL")

llm = init_chat_model(
    model="deepseek:deepseek-v4-flash",
    api_key=api_key,
    api_base=base_url,
)
res = llm.invoke("Please introduce yourself in one sentence")
print(res.content)

5.2 How to write aggregation service adaptation

All aggregation services are used uniformly openai As the service provider identification, the model name fills in the platform standard format:

llm = init_chat_model(
    model="openai:deepseek/deepseek-v4-flash",
    api_key=os.getenv("OPENROUTER_API_KEY"),
    api_base=os.getenv("OPENROUTER_BASE_URL"),
)

6. General generation parameter tuning instructions

Both the online API model and the local offline inference model support the same set of hyperparameter configurations. Two core parameters are used to control the generated content style and text length.

6.1 Temperature randomness control (high frequency use)

The value range is 0 ~ 2. The larger the value, the more random and creative the generated content is, and the lower the logical consistency. Use specifications according to scenarios:

sheet

parameter intervalApplicable business scenarios
0.0~0.3Mathematical calculations, code generation, data extraction, and text classification require stable and unified output.
0.5~0.7Daily Q&A, intelligent customer service, balancing logical accuracy and natural expression
0.8~1.5Copywriting, plan brainstorming, creative content required
1.5~2.0Poetry, story creation, highly free and divergent texts

Comparison test code:

  1. temperature=0, multiple output contents are highly unified
model = ChatOpenRouter(
    temperature=0,
    model="deepseek/deepseek-v4-flash",
    api_key=os.getenv("OPENROUTER_API_KEY"),
    base_url=os.getenv("OPENROUTER_BASE_URL"),
)
for i in range(3):
    res = model.invoke("Write a short spring poem")
    print(f"No.{i+1}wheel: {res.content}\n")
  1. temperature=1.9, the content generated multiple times has obvious differences.
model = ChatOpenRouter(
    temperature=1.9,
    model="deepseek/deepseek-v4-flash",
    api_key=os.getenv("OPENROUTER_API_KEY"),
    base_url=os.getenv("OPENROUTER_BASE_URL"),
)
for i in range(3):
    res = model.invoke("Write a short spring poem")
    print(f"No.{i+1}wheel: {res.content}\n")

6.2 max_tokens maximum output length

Limit the upper limit of a single returned text token to avoid excessively long text consuming the call quota. Long text summaries and batch question and answer scenarios can be configured as needed.

7. Local offline Ollama model access

In addition to online paid API services, LangChain is compatible with the open source model deployed by local Ollama, does not require interface credentials, and provides two initialization methods:

  1. Exclusive class ChatOllama
from langchain_ollama import ChatOllama
llm = ChatOllama(model="deepseek-v4-flash")
  1. Unify init_chat_model initialization
from langchain.chat_models import init_chat_model
llm = init_chat_model(
    model="deepseek-v4-flash",
    model_provider="langchain-ollama"
)

8. Comparison and summary of various access solutions

sheet

Service typeAvailable access methodsApplicable development scenarios
DeepSeek official interfaceChatDeepSeek, ChatOpenAI, init_chat_modelIt is necessary to use the manufacturer's exclusive expansion capabilities and long-term stable business
Zhipu AI, Alibaba Cloud BailianVendor-specific Chat class, ChatOpenAI, init_chat_modelDomestic commercial models, internal business systems of enterprises
OpenRouter Aggregation ServiceChatOpenRouter, ChatOpenAI, init_chat_modelMulti-model rapid testing and unified billing management
Ollama local offline modelChatOllama, init_chat_modelLocal development and debugging, intranet offline privatization deployment

9. Best practices for project implementation

  1. Privacy configuration security management and controlUse uniformly .env File storage interface credentials, plain text hard coding is prohibited; project addition .gitignore Shield configuration files and regularly rotate the keys of each platform to reduce the risk of leakage.
  2. Suggestions for selecting access methods
  • Requires the manufacturer's unique expansion capabilities: select the manufacturer's exclusive dialogue class;
  • Quick debugging and temporary switching of multiple models: use universal ChatOpenAI;
  • Multi-model dynamic scheduling, unified framework projects: priority use init_chat_model.
  1. Parameter tuning specificationsFor rigorous tasks such as data calculation, coding, and classification, the temperature setting is 0~0.3; for copywriting and creative scenarios, the temperature can be raised to above 1.0.
  2. Online and offline code compatibilityOnline API and local Ollama offline model can be shared init_chat_model Initialization logic and switching environments do not require significant code modifications.
  3. Project lightweightDependency packages are installed on demand, and unused manufacturer SDKs do not need to be introduced, reducing project size.