Gabay sa Integrasyon ng LangChain
Alamin kung paano isama ang LangChain sa AI Gateway para sa pinagkakaisang pag-access sa maraming modelo ng AI.
Gabay sa Integrasyon ng LangChain
Ang LangChain ay isa sa mga pinakapopular na framework para sa pagbuo ng mga aplikasyong gumagamit ng malalaking language model. Nagbibigay ang AI Gateway ng seamless na integrasyon sa LangChain, na nagbibigay sa iyo ng pinagkakaisang pag-access sa 100+ na modelo sa pamamagitan ng iisang API.
Compatibility
AI Gateway is fully compatible with LangChain's OpenAI integration. You can swap providers in just a few lines of code.
Mga Kinakailangan
- Python 3.8 o mas mataas
- LangChain 0.1.0 o mas mataas
- Isang API key ng AI Gateway
Pag-install
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.