Skip to main content
You can repoint LangChain through Butter using its ChatOpenAI class, which accepts the necessary base URL and headers. See it in the example below.
import os
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, START, MessagesState

# Create an OpenAI model pointing to Butter
model = ChatOpenAI(
    base_url="https://proxy.butter.dev/v1",
    default_headers={"Butter-Auth": f"Bearer {os.getenv('BUTTER_API_KEY')}"},
    api_key=os.getenv('OPENAI_API_KEY'),
    model="gpt-4"
)

# Build graph
graph = StateGraph(MessagesState)
graph.add_node("chatbot", lambda state: {"messages": [model.invoke(state["messages"])]})
graph.add_edge(START, "chatbot")
graph = graph.compile()

# Run
for event in graph.stream({"messages": [("user", "What is the English word for mantequilla?")]}, stream_mode="values"):
    event["messages"][-1].pretty_print()