Model interfaces
ChatOpenAI
OpenAI chat models.
AzureChatOpenAI
Wrapper for OpenAI chat models hosted on Azure.
OpenAI
(Legacy) OpenAI text completion models.
AzureOpenAI
Wrapper for (legacy) OpenAI text completion models hosted on Azure.
OpenAIEmbeddings
OpenAI embedding models.
AzureOpenAIEmbeddings
Wrapper for OpenAI embedding models hosted on Azure.
Tools and toolkits
Dall-E Image Generator
Text-to-image generation using OpenAI’s Dall-E models.
Retrievers
ChatGPTPluginRetriever
Retrieve real-time information; e.g., sports scores, stock prices, the latest news, etc.
Document loaders
ChatGPTLoader
Load
conversations.json from your ChatGPT data export folder.Middleware
Middleware specifically designed for OpenAI models. Learn more about middleware.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
Learn more about OpenAI’s moderation models and categories.
OpenAIModerationMiddleware
Configuration options
Configuration options
ModerationModel
default:"omni-moderation-latest"
OpenAI moderation model to use. Options:
'omni-moderation-latest', 'omni-moderation-2024-09-26', 'text-moderation-latest', 'text-moderation-stable'bool
default:"True"
Whether to check user input messages before the model is called
bool
default:"True"
Whether to check model output messages after the model is called
bool
default:"False"
Whether to check tool result messages before the model is called
string
default:"end"
How to handle violations when content is flagged. Options:
'end'- End agent execution immediately with a violation message'error'- RaiseOpenAIModerationErrorexception'replace'- Replace the flagged content with the violation message and continue
str | None
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
"I'm sorry, but I can't comply with that request. It was flagged for {categories}."OpenAI | None
Optional pre-configured OpenAI client to reuse. If not provided, a new client will be created.
AsyncOpenAI | None
Optional pre-configured AsyncOpenAI client to reuse. If not provided, a new async client will be created.
Full example
Full example
The middleware integrates OpenAI’s moderation endpoint to check content at different stages:Moderation stages:
check_input- User messages before model callcheck_output- AI messages after model callcheck_tool_results- Tool outputs before model call
'end'(default) - Stop execution with violation message'error'- Raise exception for application handling'replace'- Replace flagged content and continue
Other
Adapter
Adapt LangChain models to OpenAI APIs.
OpenAIModerationChain
Detect text that could be hateful, violent, etc.