LangChain / LangGraph Integration
Use ANT as your LLM provider in LangChain and LangGraph applications.
Installation
pip install langchain langchain-openaiBasic LangChain Usage
Use ChatOpenAI with ANT's endpoint:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="ant:auto",
openai_api_base="https://antbase.ai/v1",
openai_api_key="your-ant-api-key",
)
response = llm.invoke("Explain the difference between lists and tuples in Python.")
print(response.content)Using with Chains
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParser
llm = ChatOpenAI(
model="ant:auto",
openai_api_base="https://antbase.ai/v1",
openai_api_key="your-ant-api-key",
)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant that explains concepts simply."),
("user", "{topic}"),
])
chain = prompt | llm | StrOutputParser()
result = chain.invoke({"topic": "machine learning"})
print(result)Using with Agents
ANT's virtual models work great with agents. Use ant:best for complex reasoning tasks.
from langchain_openai import ChatOpenAI
from langchain.agents import create_openai_functions_agent, AgentExecutor
from langchain.tools import tool
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
@tool
def search_web(query: str) -> str:
"""Search the web for information."""
return f"Search results for: {query}"
# Use ant:best for agent reasoning
llm = ChatOpenAI(
model="ant:best",
openai_api_base="https://antbase.ai/v1",
openai_api_key="your-ant-api-key",
)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful research assistant."),
("user", "{input}"),
MessagesPlaceholder("agent_scratchpad"),
])
agent = create_openai_functions_agent(llm, [search_web], prompt)
executor = AgentExecutor(agent=agent, tools=[search_web])
result = executor.invoke({"input": "Find information about the latest AI developments"})
print(result["output"])LangGraph
The same configuration works with LangGraph:
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, START, END
from typing import TypedDict, Annotated
import operator
class State(TypedDict):
messages: Annotated[list, operator.add]
llm = ChatOpenAI(
model="ant:auto",
openai_api_base="https://antbase.ai/v1",
openai_api_key="your-ant-api-key",
)
def chat_node(state: State):
response = llm.invoke(state["messages"])
return {"messages": [response]}
builder = StateGraph(State)
builder.add_node("chat", chat_node)
builder.add_edge(START, "chat")
builder.add_edge("chat", END)
graph = builder.compile()
result = graph.invoke({"messages": [("user", "Hello!")]})
print(result["messages"][-1].content)Streaming
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="ant:auto",
openai_api_base="https://antbase.ai/v1",
openai_api_key="your-ant-api-key",
streaming=True,
)
for chunk in llm.stream("Write a poem about programming."):
print(chunk.content, end="", flush=True)Tips for LangChain + ANT
- Use
ant:fastfor embeddings lookups and simple classification - Use
ant:bestfor agent reasoning and complex chains - ANT's automatic failover means you don't need to implement retry logic in your chains
- Check the dashboard to see which models are being used for different tasks