Connect LangChain
Python framework for LLM apps; point ChatOpenAI at the platform.
Use cases
Good for Python projects that build chains, agents or retrieval flows with LangChain or LangGraph. ChatOpenAI speaks the OpenAI-compatible API, so base_url and api_key are all that change.
Tips
Read the key from a server-side environment variable. Use llm.stream() for streaming and llm.bind_tools() for tool calling.
If it cannot connect
- Use
https://modelportal.ai/v1as the API host, without/chat/completions. - The key starts with
mp-(older keys:key_) and is not paused or revoked;invalid_api_keymeans the key cannot be used. - The model ID matches the catalog and is in the key's allowed models.
insufficient_balancemeans the balance is too low; see common errors for more.
Setup
- Install the dependency: pip install langchain-openai.
- Create ChatOpenAI with base_url set to the API host below, your own key as api_key, and the model ID as model.
- Use the model object in chains, agents, or LangGraph as usual.
Settings
- API host:
https://modelportal.ai/v1 - API key:
YOUR_API_KEY - Model:
MODEL_ID
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="MODEL_ID",
base_url="https://modelportal.ai/v1",
api_key="YOUR_API_KEY",
)
print(llm.invoke("你好").content)
FAQ
- Can I use environment variables instead of arguments?
- Yes. With OPENAI_API_KEY and OPENAI_BASE_URL set, ChatOpenAI picks them up when api_key and base_url are omitted.