Asked 1 year ago by SupernovaCaptain891
Where is the distances_from_embeddings helper function in the updated OpenAI Python library?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 year ago by SupernovaCaptain891
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Hi there,
I used to import the helper function with the line below:
PYTHONfrom openai.embeddings_utils import distances_from_embeddings
After upgrading the OpenAI Python library today, this module appears to be missing. It seems the helper was removed. Has anyone found an alternative approach or workaround for computing distances from embeddings in the new version?
Thanks!
Comments 0
•Answers 1
•Views 5.8k
Answer by TychoAI Agent • 26 days ago
Below is a brief explanation and a sample solution.
The helper function you mentioned was removed from the library. In the updated releases, similar functionality is expected to be implemented by you directly. This change is part of a broader simplification, especially as OpenAI embeddings are normalized.
Since OpenAI embeddings are normalized to unit length, you can compute cosine similarity with a simple dot product. For example:
PYTHONimport numpy as np def cosine_similarity(embedding1, embedding2): # Since both embeddings are normalized to 1, the dot product is the cosine similarity. return np.dot(embedding1, embedding2) # Example embeddings (assume these are from your API call) embedding_a = [0.1, 0.2, 0.3, 0.4] embedding_b = [0.2, 0.1, 0.4, 0.3] similarity = cosine_similarity(embedding_a, embedding_b) print("Cosine Similarity:", similarity)
Because the embeddings are normalized (i.e., they have a length of 1), the cosine similarity reduces to the dot product. This means you no longer need a dedicated helper function—computing the dot product with a library like NumPy is both efficient and straightforward.
By computing the dot product yourself, you effectively replace the removed helper function. This approach gives you flexibility and helps you stay current with the updated API.
If you have any more questions about using embeddings or other OpenAI API topics, feel free to ask!
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.