-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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
OpenAIModerationChain with OpenAI SDK >= v1.0.0 Broken #13685
Comments
🤖 Hello @MrNickBreen, I'm Dosu, an automated helper bot here to assist you with your issues, questions, and contributions related to the LangChain repository. While we wait for a human maintainer, I'm here to provide initial support. I'm looking forward to helping you resolve your issue! Thank you for bringing this to our attention. It seems that the OpenAI SDK v1.0.0 update has indeed introduced some changes that are causing compatibility issues with the Based on the error message you provided, the issue seems to be with the deprecated To resolve this issue, you can modify the @root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
openai_api_key = get_from_dict_or_env(
values, "openai_api_key", "OPENAI_API_KEY"
)
openai_organization = get_from_dict_or_env(
values,
"openai_organization",
"OPENAI_ORGANIZATION",
default="",
)
try:
import openai
openai.api_key = openai_api_key
if openai_organization:
openai.organization = openai_organization
values["client"] = openai.ContentFilter
except ImportError:
raise ImportError(
"Could not import openai python package. "
"Please install it with `pip install openai`."
)
return values
def _call(
self,
inputs: Dict[str, str],
run_manager: Optional[CallbackManagerForChainRun] = None,
) -> Dict[str, str]:
text = inputs[self.input_key]
results = self.client.create(prompt=text)
output = this._moderate(text, results["result"])
return {self.output_key: output} In the Please note that this is a suggested solution based on the information provided and the current state of the LangChain codebase. It's always a good idea to thoroughly test the changes in your local environment to ensure everything works as expected. Let me know if this helps or if you need further assistance. Sources |
dosubot I think you have the right idea, but I believe we still want to use the moderation API and not the "content filter endpoint". |
🤖 Based on the information you've provided and the code in the LangChain repository, you can modify the In the Here's how the modified @root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
openai_api_key = get_from_dict_or_env(
values, "openai_api_key", "OPENAI_API_KEY"
)
openai_organization = get_from_dict_or_env(
values,
"openai_organization",
"OPENAI_ORGANIZATION",
default="",
)
try:
import openai
openai.api_key = openai_api_key
if openai_organization:
openai.organization = openai_organization
values["client"] = openai
except ImportError:
raise ImportError(
"Could not import openai python package. "
"Please install it with `pip install openai`."
)
return values In the Here's how the modified def _call(
self,
inputs: Dict[str, str],
run_manager: Optional[CallbackManagerForChainRun] = None,
) -> Dict[str, str]:
text = inputs[self.input_key]
results = self.client.moderations.create({"document": {"content": text}})
output = self._moderate(text, results["results"][0])
return {self.output_key: output} Please note that the structure of the Unfortunately, I couldn't find the exact structure of the I hope this helps! If you have any further questions or run into any issues, please let me know. SourcesThis response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
It will be great to modify the code so we can use Moderation with open-ai >= 1.0 without override methods |
For open-ai >= 1.0
|
Thanks mrchaos for your proposition. |
🤖 Hi, @MrNickBreen, I'm helping the LangChain team manage their backlog and am marking this issue as stale. From what I understand, the issue involves compatibility issues with the Langchain OpenAIModerationChain due to the OpenAI SDK v1.0.0 update. The suggested migration involves updating the codebase to use the 1.0.0 interface, specifically changing Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, please let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or the issue will be automatically closed in 7 days. Thank you for your understanding and cooperation. |
This is fixed by this PR which also includes an async implementation: #18537 |
…8537) Thank you for contributing to LangChain! ## PR title lancghain[patch]: fix `OpenAIModerationChain` and implement async ## PR message Description: fix `OpenAIModerationChain` and implement async Issues: - #18533 - #13685 Dependencies: none Twitter handle: mattflo ## Add tests and docs Existing documentation is broken: https://python.langchain.com/docs/guides/safety/moderation - [ x] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ --------- Co-authored-by: Emilia Katari <emilia@outpace.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Erick Friis <erickfriis@gmail.com>
…8537) Thank you for contributing to LangChain! ## PR title lancghain[patch]: fix `OpenAIModerationChain` and implement async ## PR message Description: fix `OpenAIModerationChain` and implement async Issues: - #18533 - #13685 Dependencies: none Twitter handle: mattflo ## Add tests and docs Existing documentation is broken: https://python.langchain.com/docs/guides/safety/moderation - [ x] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ --------- Co-authored-by: Emilia Katari <emilia@outpace.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Erick Friis <erickfriis@gmail.com>
Should be fixed in langchain 0.1.20. Closing but please let me know if the issue is not resolved and will re-open. |
System Info
It appears that OpenAI's SDK v1.0.0 update introduced some needed migrations.
Running the Langchain OpenAIModerationChain with OpenAI SDK >= v1.0.0 provides the following error:
After briefly reading the mitigation steps I believe the suggested migration is from
openai.Moderation.create()
->client.moderations.create()
.I believe the
validate_environment
ofOpenAIModerationChain
will want updating fromvalues["client"] = openai.Moderation
to using the recommendedclient.moderations
syntax. (https://api.python.langchain.com/en/latest/_modules/langchain/chains/moderation.html#OpenAIModerationChain)Who can help?
No response
Information
Related Components
Reproduction
With OpenAI SDK >= v1.0.0 try to use
OpenAIModerationChain
to moderate a piece of content.Error appears:
Expected behavior
When using
OpenAIModerationChain
with OpenAI SDK >= v1.0.0 I expect the chain to properly moderate content and not fail with an error.The text was updated successfully, but these errors were encountered: