7.9 KiB
Documentation Index
Fetch the complete documentation index at: https://docs.langchain.com/llms.txt Use this file to discover all available pages before exploring further.
OpenAI integrations
Integrate with OpenAI using LangChain JavaScript.
LangChain integrates with OpenAI and Azure OpenAI through the @langchain/openai package.
OpenAI is American artificial intelligence (AI) research laboratory consisting of the non-profit
OpenAI Incorporatedand its for-profit subsidiary corporationOpenAI Limited Partnership. OpenAI conducts AI research with the declared intention of promoting and developing a friendly AI. OpenAI systems run on anAzure-based supercomputing platform fromMicrosoft.
The OpenAI API is powered by a diverse set of models with different capabilities and price points.
ChatGPT is the Artificial Intelligence (AI) chatbot developed by
OpenAI.
Installation and setup
- Get an OpenAI api key and set it as an environment variable (
OPENAI_API_KEY)
Chat model
See a usage example.
import { ChatOpenAI } from "@langchain/openai";
LLM
See a usage example.
See [this section for general instructions on installing LangChain packages](/oss/javascript/langchain/install).npm install @langchain/openai @langchain/core
import { OpenAI } from "@langchain/openai";
Text embedding model
See a usage example
import { OpenAIEmbeddings } from "@langchain/openai";
Chain
import { OpenAIModerationChain } from "@langchain/classic/chains";
Middleware
Middleware specifically designed for OpenAI models. Learn more about middleware.
| Middleware | Description |
|---|---|
| Content moderation | Moderate agent traffic using OpenAI's moderation endpoint |
Content moderation
Moderate agent traffic (user input, model output, and tool results) using OpenAI's moderation endpoint to detect and handle unsafe content. Content moderation is useful for the following:
- Applications requiring content safety and compliance
- Filtering harmful, hateful, or inappropriate content
- Customer-facing agents that need safety guardrails
- Meeting platform moderation requirements
API reference: openAIModerationMiddleware
import { createAgent, openAIModerationMiddleware } from "langchain";
const agent = createAgent({
model: "openai:gpt-5.5",
tools: [searchTool, databaseTool],
middleware: [
openAIModerationMiddleware({
model: "openai:gpt-5.5",
moderationModel: "omni-moderation-latest",
checkInput: true,
checkOutput: true,
exitBehavior: "end",
}),
],
});
* `'end'` - End agent execution immediately with a violation message
* `'error'` - Throw `OpenAIModerationError` exception
* `'replace'` - Replace the flagged content with the violation message and continue
Custom template for violation messages. Supports template variables:
* `{categories}` - Comma-separated list of flagged categories
* `{category_scores}` - JSON string of category scores
* `{original_content}` - The original flagged content
Default: `"I'm sorry, but I can't comply with that request. It was flagged for {categories}."`
The middleware integrates OpenAI's moderation endpoint to check content at different stages:
Moderation stages:
checkInput- User messages before model callcheckOutput- AI messages after model callcheckToolResults- Tool outputs before model call
Exit behaviors:
'end'(default) - Stop execution with violation message'error'- Throw exception for application handling'replace'- Replace flagged content and continue
import { createAgent, openAIModerationMiddleware } from "langchain";
// Basic moderation
const agent = createAgent({
model: "openai:gpt-5.5",
tools: [searchTool, customerDataTool],
middleware: [
openAIModerationMiddleware({
model: "openai:gpt-5.5",
moderationModel: "omni-moderation-latest",
checkInput: true,
checkOutput: true,
}),
],
});
// Strict moderation with custom message
const agentStrict = createAgent({
model: "openai:gpt-5.5",
tools: [searchTool, customerDataTool],
middleware: [
openAIModerationMiddleware({
model: "openai:gpt-5.5",
moderationModel: "omni-moderation-latest",
checkInput: true,
checkOutput: true,
checkToolResults: true,
exitBehavior: "error",
violationMessage:
"Content policy violation detected: {categories}. " +
"Please rephrase your request.",
}),
],
});
// Moderation with replacement behavior
const agentReplace = createAgent({
model: "openai:gpt-5.5",
tools: [searchTool],
middleware: [
openAIModerationMiddleware({
model: "openai:gpt-5.5",
checkInput: true,
exitBehavior: "replace",
violationMessage: "[Content removed due to safety policies]",
}),
],
});