LangChain
Python LLM application development framework, supports chain calling, Agents, RAG and other advanced features, compatible with OpenAI APIs.
Installation
Mac / Linux
bash
pip install langchain langchain-openaiWindows
cmd
pip install langchain langchain-openaiRequires Python 3.9+
Configuration
Directly specify api_key and base_url in your code:
python
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="gpt-4o-mini",
api_key="sk-xxx",
base_url="https://www.fast-api.org/v1"
)Replace sk-xxx with the API Key you obtained from Fast-API.
Usage Example
Basic Chat
python
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="gpt-4o-mini",
api_key="sk-yourAPIKey",
base_url="https://www.fast-api.org/v1"
)
response = llm.invoke("Hello!")
print(response.content)Multi-turn Conversation
python
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
llm = ChatOpenAI(
model="gpt-4o-mini",
api_key="sk-yourAPIKey",
base_url="https://www.fast-api.org/v1"
)
messages = [
SystemMessage(content="You are a helpful assistant"),
HumanMessage(content="What is the GIL in Python?")
]
response = llm.invoke(messages)
print(response.content)FAQ
AuthenticationError Prompt
Make sure the api_key parameter is filled in correctly, the key starts with sk-.
Connection Timeout
Check if base_url is https://www.fast-api.org/v1, and verify network accessibility.
Model Not Found
Check if the model parameter is set to a model name supported by Fast-API.
Notes
base_urlmust end with/v1:https://www.fast-api.org/v1- You can also configure via environment variables
OPENAI_API_KEYandOPENAI_API_BASEto avoid hardcoding - Supports models such as
gpt-4o,gpt-4o-mini,claude-sonnet-4-20250514, etc.