OpenAI is American artificial intelligence (AI) research laboratory consisting of the non-profitOpenAI 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.LLM
See a usage example.npm
Text Embedding Model
See a usage exampleChain
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
Learn more about OpenAI’s moderation models and categories.
openAIModerationMiddleware
Configuration options
Configuration options
OpenAI model to use for moderation. Can be either a model name string (e.g.,
"openai:gpt-4o") or a BaseChatModel instance. The middleware will use this model’s client to access the moderation endpoint.OpenAI moderation model to use. Options:
'omni-moderation-latest', 'omni-moderation-2024-09-26', 'text-moderation-latest', 'text-moderation-stable'Whether to check user input messages before the model is called
Whether to check model output messages after the model is called
Whether to check tool result messages before the model is called
How to handle violations when content is flagged. Options:
'end'- End agent execution immediately with a violation message'error'- ThrowOpenAIModerationErrorexception'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
"I'm sorry, but I can't comply with that request. It was flagged for {categories}."Full example
Full example
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
'end'(default) - Stop execution with violation message'error'- Throw exception for application handling'replace'- Replace flagged content and continue