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

OpenAIEmbeddings() does not work because of these bugs #12943

Closed
3 of 14 tasks
pranavgupta2603 opened this issue Nov 6, 2023 · 18 comments · Fixed by #12969
Closed
3 of 14 tasks

OpenAIEmbeddings() does not work because of these bugs #12943

pranavgupta2603 opened this issue Nov 6, 2023 · 18 comments · Fixed by #12969
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: embeddings Related to text embedding models module Ɑ: vector store Related to vector store module

Comments

@pranavgupta2603
Copy link

pranavgupta2603 commented Nov 6, 2023

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.

Copy link

dosubot bot commented Nov 6, 2023

🤖

Hello @pranavgupta2603! I'm Dosu, a bot designed to help you solve bugs, answer questions, and guide you through becoming a contributor to LangChain. While we're waiting for a human maintainer, I'm here to assist you. I'm currently reviewing your issue and will provide you with a full answer shortly.

@dosubot dosubot bot added Ɑ: embeddings Related to text embedding models module Ɑ: vector store Related to vector store module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Nov 6, 2023
Copy link

dosubot bot commented Nov 6, 2023

🤖

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.

@alexyoung23jj
Copy link

Same error

@RRaffay
Copy link

RRaffay commented Nov 6, 2023

Facing the same issue. Wonder if they announce something in the summit

@luccafabro
Copy link

Same error here, waiting solution...

@georgetok
Copy link

Same error

@tasansal
Copy link
Contributor

tasansal commented Nov 6, 2023

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.

@RRaffay
Copy link

RRaffay commented Nov 6, 2023

Docs just updated:

from openai import OpenAI
client = OpenAI()

def get_embedding(text, model="text-embedding-ada-002"):
   text = text.replace("\n", " ")
   return client.embeddings.create(input = [text], model=model)['data'][0]['embedding']

Probably need to initialize the client instead of using openai.embeddings

@beongeist
Copy link

Docs just updated:

from openai import OpenAI
client = OpenAI()

def get_embedding(text, model="text-embedding-ada-002"):
   text = text.replace("\n", " ")
   return client.embeddings.create(input = [text], model=model)['data'][0]['embedding']

Probably need to initialize the client instead of using openai.embeddings

Do you have an example of how to replace the original code: embeddings = OpenAIEmbeddings()

@rbuck-som
Copy link

Same error

same issue here

@RRaffay
Copy link

RRaffay commented Nov 6, 2023

@beongeist haven't gotten around to looking too deeply into it but I assume you'd need to override the OpenAIEmbeddings class. @dosubot's answer might be good starting point but I'm not sure if there are other dependencies that it might mess up. You could also just try using an older version of the openai library if that's still working.

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

@georgetok
Copy link

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

@valvesss
Copy link

valvesss commented Nov 6, 2023

same error! fucked my prod lol

@hwchase17
Copy link
Contributor

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

efriis added a commit that referenced this issue Nov 7, 2023
Initial PR to get OpenAIEmbeddings working with the new sdk

fyi @rlancemartin 

Fixes #12943

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
rlancemartin pushed a commit that referenced this issue Nov 7, 2023
Initial PR to get OpenAIEmbeddings working with the new sdk

fyi @rlancemartin

Fixes #12943

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
Aniloid2 added a commit to Aniloid2/personal-timeline that referenced this issue Nov 8, 2023
The openai library seems to use openai.embeddings instead of openai.Embedding (langchain-ai/langchain#12943)
@criolho
Copy link

criolho commented Nov 8, 2023

Same here, still getting the error:

(openai) # pip list | grep openai
openai 1.1.1

(openai) # python
Python 3.11.6 (main, Oct 8 2023, 05:06:43) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
vectorstore = Chroma.from_texts(["Harrison worked at Kensho"], embedding=OpenAIEmbeddings())

Traceback (most recent call last):
File "", line 1, in
File "/usr/bin/test/openai/lib/python3.11/site-packages/pydantic/v1/main.py", line 339, in init
values, fields_set, validation_error = validate_model(pydantic_self.class, data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/bin/test/openai/lib/python3.11/site-packages/pydantic/v1/main.py", line 1050, in validate_model
input_data = validator(cls_, input_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/bin/test/openai/lib/python3.11/site-packages/langchain/embeddings/openai.py", line 284, in validate_environment
values["client"] = openai.Embedding
^^^^^^^^^^^^^^^^
AttributeError: module 'openai' has no attribute 'Embedding'. Did you mean: 'embeddings'?

@efriis efriis reopened this Nov 8, 2023
@efriis
Copy link
Member

efriis commented Nov 8, 2023

I'll wait to close this until after a release is cut! This will be in 0.0.332

@efriis
Copy link
Member

efriis commented Nov 8, 2023

While release is getting worked on, you can do

pip install langchain==0.0.331rc2 chromadb==0.4.15

I included the chromadb lock on 0.4.15 because their team is also working on addressing a change in 0.4.16 that would also break your code! #13051 (comment)

@efriis
Copy link
Member

efriis commented Nov 8, 2023

Langchain 0.0.332 released with the chroma team's fix for compatibility with chromadb>=0.4.16

Can now use latest of both pip install -U langchain chromadb

@efriis efriis closed this as completed Nov 8, 2023
xieqihui pushed a commit to xieqihui/langchain that referenced this issue Nov 21, 2023
Initial PR to get OpenAIEmbeddings working with the new sdk

fyi @rlancemartin 

Fixes langchain-ai#12943

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
@dosubot dosubot bot mentioned this issue Dec 7, 2023
14 tasks
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 Ɑ: embeddings Related to text embedding models module Ɑ: vector store Related to vector store module
Projects
None yet
Development

Successfully merging a pull request may close this issue.