langchain: OpenAIEmbeddings() does not work because of these bugs

System Info

Python Version: 3.11 LangChain Version: 0.0.331 OpenAI Version: 1.0.0

Who can help?

@hwchase17, @agola11, @eyurtsev

Information

  • The official example notebooks/scripts
  • My own modified scripts

Related Components

  • LLMs/Chat Models
  • Embedding Models
  • Prompts / Prompt Templates / Prompt Selectors
  • Output Parsers
  • Document Loaders
  • Vector Stores / Retrievers
  • Memory
  • Agents / Agent Executors
  • Tools / Toolkits
  • Chains
  • Callbacks/Tracing
  • Async

Reproduction

The following error has been caused due to the recent change in version of OpenAI to 1.0.0 Use OpenAI==0.28.1 to fix this error With the code: embeddings = OpenAIEmbeddings()

The error produced is: AttributeError: module 'openai' has no attribute 'Embedding'. Did you mean: 'embeddings'?

I went through the langchain/embeddings/openai.py file and then changed value["client"] = openai.Embedding to value["client"] = openai.embeddings, but then I receive this new error:

AttributeError: module 'openai' has no attribute 'error' in the same file (langchain/embeddings/openai.py)

Expected behavior

There should be no error when calling this function.

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Reactions: 4
  • Comments: 18 (5 by maintainers)

Commits related to this issue

Most upvoted comments

pip install openai==0.28.1 as a hotfix works, guys

yup we’re working on moving to support 1.x releases, but for now please pin openai==0.28.1 - sorry!

🤖

Thank you for providing the detailed information. It seems like the issue is due to the changes in the OpenAI API version 1.0.0. The AttributeError you’re encountering is likely because the ‘Embedding’ attribute has been renamed or removed in the new version of the OpenAI API.

In the langchain/embeddings/openai.py file, the OpenAIEmbeddings class is trying to access openai.Embedding, which seems to no longer exist in the new version of the OpenAI API. You’ve correctly identified and changed this to openai.embeddings, but it seems like there’s another issue with openai.error which also doesn’t exist in the new version.

To resolve this issue, you should check the OpenAI API documentation for the version you are using and update your code accordingly. If the ‘Embedding’ and ‘error’ attributes have been removed or renamed, you should use the new attribute or method that replaces them.

Here’s an example of how you might modify the OpenAIEmbeddings class:

class OpenAIEmbeddings(BaseEmbeddings):
    def __init__(self, openai_api_key, model="davinci", max_tokens=4096, max_retries=3):
        super().__init__(openai_api_key, model, max_tokens, max_retries)
        self.client = openai.embeddings  # updated attribute

    def _handle_error(self, error):
        if isinstance(error, openai.NewErrorName):  # updated attribute
            # handle error
            pass
        else:
            # handle other errors
            pass

Please replace NewErrorName with the correct error handling attribute from the OpenAI API version 1.0.0.

Remember to thoroughly test your code after making these changes to ensure everything works as expected. If you encounter any other issues, feel free to provide more information so we can assist you further.

Sources


This 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.

same error! fucked my prod lol

this only happens with the latest openai-python. Probably breaking changes on the API as a workaround at the moment, pin to openai==0.28.1. Langchain will probably need updates to support major version change.

Same error

Same error here, waiting solution…