Skip to content
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

Moderation: migrate to new openai api #14317

Closed
wants to merge 3 commits into from
Closed

Moderation: migrate to new openai api #14317

wants to merge 3 commits into from

Conversation

CheeseDurger
Copy link

Fixes issue #13685 : OpenAIModerationChain still uses the old OpenAI API.

Description

The following snippet:

from langchain.chains import OpenAIModerationChain
from openai import moderations

print(OpenAIModerationChain(client=moderations).run("i want to kill you"))

Gives the following error :

Traceback (most recent call last):
  File "moderate.py", line 4, in <module>
    print(OpenAIModerationChain(client=moderations).run("i want to kill you"))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "\Lib\site-packages\langchain\chains\base.py", line 507, in run
    return self(args[0], callbacks=callbacks, tags=tags, metadata=metadata)[
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "\Lib\site-packages\langchain\chains\base.py", line 312, in __call__
    raise e
  File "\Lib\site-packages\langchain\chains\base.py", line 306, in __call__
    self._call(inputs, run_manager=run_manager)
  File "\Lib\site-packages\langchain\chains\moderation.py", line 94, in _call
    results = self.client.create(text)
              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "\Lib\site-packages\openai\lib\_old_api.py", line 39, in __call__
    raise APIRemovedInV1(symbol=self._symbol)
openai.lib._old_api.APIRemovedInV1:

You tried to access openai.Moderation, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Dec 5, 2023
Copy link

vercel bot commented Dec 5, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Jan 8, 2024 0:19am

@dosubot dosubot bot added Ɑ: models Related to LLMs or chat model modules 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Dec 5, 2023
Copy link
Contributor

@hwchase17 hwchase17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we try to do this in a way that is compatible with all versions of openai? see https://github.com/langchain-ai/langchain/pull/14222/files for inspiration

libs/langchain/langchain/chains/moderation.py Outdated Show resolved Hide resolved
@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. size:S This PR changes 10-29 lines, ignoring generated files. size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. size:XXL This PR changes 1000+ lines, ignoring generated files. labels Dec 7, 2023
@CheeseDurger
Copy link
Author

Should be compatible with v0 and v1, but reviews are appreciated.

@CheeseDurger
Copy link
Author

@hwchase17 is it mergeable ?

@karrtikiyer-tw
Copy link

We also need this fix urgently? Can this be merged? cc: @vaibhavbhuva

@MrNickBreen
Copy link

CheeseDurger, thanks for suggesting this fix for 13685.

I also wanted to comment to share I am eagerly awaiting a solution as well. Thanks all for coding & reviewing efforts!

@MrNickBreen
Copy link

CC @efriis, @eyurtsev, @nfcampos I see all of you have also successfully committed changes to this file and are associated with https://github.com/langchain-ai. Could you help @CheeseDurger with a review, more suggestions, or if appropriate, successfully merge this fix to continue to support OpenAI Moderation functionality?

Comment on lines +9 to 18
import openai
if is_openai_v1():
import openai
from openai import moderations as moderation
from openai.types import ModerationCreateResponse, Moderation
from openai.resources.moderations import Moderations
else:
from openai import Moderation as Moderations
moderation = Moderations()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately we can't do imports like this in langchain because of optional dependencies.

If you want to add this to the libs/partners/openai langchain_openai package, then you can rely on the v1 client there!

@baskaryan
Copy link
Collaborator

closing in favor of #17025

@baskaryan baskaryan closed this Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: models Related to LLMs or chat model modules size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants