LangChain 集成指南
了解如何将 LangChain 与 AI Gateway 集成,统一访问多个 AI 模型。
LangChain 集成指南
LangChain 是构建大语言模型应用最流行的框架之一。AI Gateway 与 LangChain 无缝集成,让您能够通过单个 API 统一访问 100 多个模型。
Compatibility
AI Gateway is fully compatible with LangChain's OpenAI integration. You can swap providers in just a few lines of code.
前提条件
- Python 3.8 或更高版本
- LangChain 0.1.0 或更高版本
- AI Gateway API 密钥
安装
Install LangChain and OpenAI packages
bashpip install langchain-openai langchain-coreSet up your API key
Set your AI Gateway API key as an environment variable:
pythonimport os os.environ["OPENAI_API_KEY"] = "your-ai-gateway-key" os.environ["OPENAI_API_BASE"] = "https://api.ecaix.com/v1"Initialize the chat model
Now you can use any model with LangChain:
pythonfrom langchain_openai import ChatOpenAI # Use any model - just change the model name llm = ChatOpenAI( model="gpt-4o", temperature=0.7, ) response = llm.invoke("Hello, world!") print(response.content)
Using Multiple Models
One of the key benefits of using AI Gateway with LangChain is easy model switching:
from langchain_openai import ChatOpenAI
# Define models to compare
models = ["gpt-4o", "claude-3-5-sonnet", "deepseek-v3"]
for model_name in models:
llm = ChatOpenAI(model=model_name, temperature=0.7)
response = llm.invoke("Explain quantum computing in one sentence.")
print(f"{model_name}: {response.content}\n")
Using with LangChain Expression Language (LCEL)
AI Gateway works perfectly with LangChain's powerful chaining capabilities:
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
# Create a simple chain
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant that speaks {language}."),
("human", "{question}")
])
model = ChatOpenAI(model="gpt-4o")
output_parser = StrOutputParser()
chain = prompt | model | output_parser
# Invoke the chain
result = chain.invoke({
"language": "Spanish",
"question": "What is AI?"
})
print(result)
Streaming Responses
For better user experience, use streaming:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o", streaming=True)
for chunk in llm.stream("Write a haiku about AI:"):
print(chunk.content, end="", flush=True)
Using with Agents
AI Gateway works with LangChain Agents for complex task automation:
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_react_agent
from langchain_core.tools import tool
from langchain import hub
# Define a simple tool
@tool
def get_weather(city: str) -> str:
"""Get the weather for a city."""
return f"The weather in {city} is sunny."
# Create the agent
llm = ChatOpenAI(model="gpt-4o")
tools = [get_weather]
agent = create_react_agent(llm, tools, hub.pull("hwchase17/react"))
# Run the agent
agent_executor = AgentExecutor(agent=agent, tools=tools)
result = agent_executor.invoke({
"input": "What's the weather in San Francisco?"
})
print(result["output"])
Advanced: Custom Model Selection
Use AI Gateway's automatic routing to select the best model for each task:
from langchain_openai import ChatOpenAI
# AI Gateway can automatically route requests based on task complexity
# Simple tasks → Fast/cheap models
# Complex tasks → Powerful models
llm = ChatOpenAI(
model="auto", # AI Gateway handles routing automatically
temperature=0.7,
)
# The model parameter "auto" tells AI Gateway to:
# - Analyze the request complexity
# - Select the optimal model
# - Balance cost and performance
response = llm.invoke("Write a complex Python decorator.")
print(response.content)
Best Practice
Use the "auto" model routing for production applications to optimize cost-performance balance automatically.