SDK-gidsen
LangChain
Gebruik api14-modellen in LangChain voor Python en JavaScript: ChatOpenAI, streaming, tools, structured output en embeddings met een eigen base URL.
De OpenAI-integratie van LangChain werkt ongewijzigd met api14: stel de base URL en key in en gebruik elk model-id (bijvoorbeeld openai/gpt-5-mini). De voorbeelden zijn voor Python (langchain-openai) en JavaScript (@langchain/openai).
Installeren
pip install langchain-openai
# or
npm install @langchain/openai @langchain/core
export API14_API_KEY="sk-api14-..."
Configureren (Python)
import os
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="anthropic/claude-sonnet-4.5",
base_url="https://api14.com/api/v1",
api_key=os.environ["API14_API_KEY"],
)
print(llm.invoke("Explain vector databases in two sentences.").content)
Configureren (JavaScript)
import { ChatOpenAI } from "@langchain/openai";
const llm = new ChatOpenAI({
model: "openai/gpt-5-mini",
apiKey: process.env.API14_API_KEY,
configuration: { baseURL: "https://api14.com/api/v1" },
});
const res = await llm.invoke("Explain vector databases in two sentences.");
console.log(res.content);
Streaming
for chunk in llm.stream("Write a haiku about APIs."):
print(chunk.content, end="", flush=True)
for await (const chunk of await llm.stream("Write a haiku about APIs.")) {
process.stdout.write(String(chunk.content));
}
Tools en gestructureerde uitvoer
from pydantic import BaseModel
from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
return f"21°C in {city}"
msg = llm.bind_tools([get_weather]).invoke("What is the weather in Istanbul?")
print(msg.tool_calls)
class Place(BaseModel):
city: str
country: str
place = llm.with_structured_output(Place).invoke('I live in Lisbon, Portugal.')
print(place)
Embeddings
De meeste embeddings-modellen op api14 zijn geen OpenAI-modellen, dus schakel de tiktoken-lengtecontrole aan de clientzijde uit:
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(
model="openai/text-embedding-3-small",
base_url="https://api14.com/api/v1",
api_key=os.environ["API14_API_KEY"],
check_embedding_ctx_length=False,
)
vectors = embeddings.embed_documents(["The quick brown fox", "Lorem ipsum"])
Gateway-parameters
Geef gateway-specifieke velden mee via extra_body (Python) of modelKwargs (JS):
llm = ChatOpenAI(
model="openai/gpt-5-mini",
base_url="https://api14.com/api/v1",
api_key=os.environ["API14_API_KEY"],
extra_body={"models": ["anthropic/claude-sonnet-4.5"], "provider": {"sort": "price"}},
)
Fouten
LangChain geeft de uitzonderingen van de OpenAI-SDK door. Een 402 insufficient_balance gooit openai.APIStatusError; 429 rate_limited gooit openai.RateLimitError na de ingebouwde nieuwe pogingen (max_retries= op ChatOpenAI). Zie Fouten.