Where is the original "openai.embeddings_utils" in the latest version

hi there, i used to have the code like “from openai.embeddings_utils import distances_from_embeddings”, but after upgrading the python openai lib today, it says this code is gone, looks like it has been deleted. Any suggestions?

Thanks!

9 Likes

embeddings_utils.py is gone

alternative at https://learn.microsoft.com/en-us/azure/ai-services/openai/tutorials/embeddings?tabs=python-new%2Ccommand-line

you can use this code snippet for that.

from typing import List, Optional
from scipy import spatial

def distances_from_embeddings(
    query_embedding: List[float],
    embeddings: List[List[float]],
    distance_metric="cosine",
) -> List[List]:
    distance_metrics = {
        "cosine": spatial.distance.cosine,
        "L1": spatial.distance.cityblock,
        "L2": spatial.distance.euclidean,
        "Linf": spatial.distance.chebyshev,
    }
    distances = [
        distance_metrics[distance_metric](query_embedding, embedding)
        for embedding in embeddings
    ]
    return distances