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

fix error OpenAIEmbeddings' object has no attibute 'embed_text' #1533

Conversation

Yuri-Albuquerque
Copy link
Contributor

Compatibility Between Different Embedding Interfaces:

We use the OpenAIEmbeddings API for embedding generation. However, there are variations in the API's interface across different versions and implementations.
In some versions, the embedding function is exposed as embed_text, while in others, it might be called embed_query.
Without this try-except block, calling the incorrect method would lead to an AttributeError, causing a failure in generating the necessary embeddings for the RAGAS computation.

Error Handling:

The try block first attempts to call the embed_text method, which is the expected API in some versions or implementations of the embeddings class.
If the embed_text method does not exist, an AttributeError is caught by the except block. In this case, we gracefully handle the error by switching to the alternative method, embed_query, which is expected to work in other versions.

Future-Proofing and Flexibility:

By adding this error handling, we make the code more resilient to future changes or variations in the API without requiring major refactoring.
This also improves the flexibility of the code by allowing it to work across different versions of the embedding libraries without modification.

Finalization with finally:

Regardless of which embedding method is called (embed_text or embed_query), the finally block ensures that the output (embeddings) is processed into the expected format (NumPy arrays).
This guarantees that the rest of the RAGAS pipeline remains unaffected by the variation in the embedding call.

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Oct 18, 2024
…gCompatibility

* upstream/main:
  fix: zero division error (explodinggradients#1530)
@jjmachan
Copy link
Member

jjmachan commented Oct 18, 2024

hey @Yuri-Albuquerque how are you defining the embedding and passing it for Ragas?

are you wrapping with

class LangchainEmbeddingsWrapper(BaseRagasEmbeddings):
?

@Yuri-Albuquerque
Copy link
Contributor Author

hey @Yuri-Albuquerque how are you defining the embedding and passing it for Ragas?

from langchain_openai.embeddings import OpenAIEmbeddings
from ragas.metrics import AnswerCorrectness

embeddings = OpenAIEmbeddings(
                               model="text-embedding-ada-002",
                               max_ritries=2,
                           )
and then I'm injecting in the class answer_correctness = AnswerCorrectness( ..., embeddings = embeddings) and also in 
evaluate(..., embeddings = embeddings, metrics=[answer_correctness])```

to get the scores 

@Yuri-Albuquerque
Copy link
Contributor Author

hey @Yuri-Albuquerque how are you defining the embedding and passing it for Ragas?

are you wrapping with

class LangchainEmbeddingsWrapper(BaseRagasEmbeddings):

?

Oh! I wasn't aware of this wrapper, thanks!

@jjmachan
Copy link
Member

@Yuri-Albuquerque no worries - glad we got it fixed 🙂

I'm closing this now but thanks a lot for putting the effort to make a PR. Really appreciate that!

@jjmachan jjmachan closed this Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants