fix error OpenAIEmbeddings' object has no attibute 'embed_text' #1533
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.