Wir verwenden Cookies. Cookies und Browserspeicher halten Sie angemeldet und merken sich Ihre Einstellungen. Mit Ihrer Zustimmung messen wir außerdem, wie die Website genutzt wird. Sie können jederzeit . Siehe unsere Cookie-Richtlinie.

LangChain

SDK-Leitfäden

LangChain

Nutzen Sie api14-Modelle in LangChain für Python und JavaScript: Base URL und Schlüssel setzen, streamen, Tools, strukturierte Ausgabe und Embeddings.

Die OpenAI-Integration von LangChain funktioniert mit api14 unverändert: Setzen Sie Base URL und Schlüssel und verwenden Sie eine beliebige Modell-ID (zum Beispiel openai/gpt-5-mini). Die Beispiele gelten für Python (langchain-openai) und JavaScript (@langchain/openai).

Installation

pip install langchain-openai
# or
npm install @langchain/openai @langchain/core
export API14_API_KEY="sk-api14-..."

Konfiguration (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)

Konfiguration (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 und strukturierte Ausgabe

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

Die meisten Embedding-Modelle bei api14 sind keine OpenAI-Modelle, schalten Sie daher die clientseitige tiktoken-Längenprüfung ab:

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-Parameter

Übergeben Sie Gateway-spezifische Felder über extra_body (Python) oder 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"}},
)

Fehler

LangChain reicht die Ausnahmen des OpenAI-SDK durch. Ein 402 insufficient_balance löst openai.APIStatusError aus; 429 rate_limited löst nach den eingebauten Wiederholungen (max_retries= bei ChatOpenAI) openai.RateLimitError aus. Siehe Fehler.