-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(langchain): add OpenAI content moderation middleware #9411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(langchain): add OpenAI content moderation middleware #9411
Conversation
|
b3cee8c to
5dc0660
Compare
473f187 to
1015dc1
Compare
hntrl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- how does streaming work with this middleware?
- I don't expect pulling that pulling the client off of
ChatOpenAIwill work like you're hoping.BaseChatOpenAIonly initializes a client when its invoked, which in the case ofChatOpenAIis never since it defers to the inner chat classes - because we're leaning on
openaiclient, I think this makes another great case for us figuring out so we don't have to do weird stuff with type inferencing
Streaming of the model response is not impacted by this middleware. The event stream "just" starts a bit delayed as the messages are being verified.
I am calling
💯 |
|
Gotcha. When I mentioned streaming I was more thinking token-by-token streaming, since in that case I can technically render/paint content that should be moderated since the moderation step hasn't kicked in. Think it's fine to ear mark that as a known feature gap |
This PR introduces a new middleware that integrates OpenAI's Moderation API into React agents, enabling automatic content policy enforcement at multiple stages of agent execution. The middleware can check user inputs, model outputs, and tool results for policy violations and handle them according to configurable behaviors.
It aligns with our implementation on the Python side introduced in langchain-ai/langchain#33492
Except for the following difference: instead of passing in an OpenAI client, we take an OpenAI model instance.
Implementation Details
moderateContentmethod fromChatOpenAI(requires related PR)beforeModelandafterModelhooksHumanMessage,AIMessage,ToolMessage)jumpTocontrol flow to end execution when violations are detectedBasic Usage
Custom Violation Message