where is module ‘openai.embeddings_utils’ in new Python Openai library

Hi Folks,

Recently, while juggling various LLM projects and migrating from Python OpenAI version 0.28.1 to version 1.xx.xx, I encountered difficulty in locating replacements for the embedding_utils methods.

  • get_embedding and
  • cosine_similarity

I encountered an error: ModuleNotFoundError: No module named ‘openai.embeddings_utils’. The optimal resolution entails referencing the identical class from version 0.28.1, available at https://github.com/openai/openai-cookbook/blob/59019bd21ee4964dd58f6671b8dc1f3f6f3a92b3/examples/utils/embeddings_utils.py

I consider this error to be particularly vexing. Despite my efforts to search for a comprehensive solution online, I was unable to find a satisfactory answer in one location. Therefore, I decided to document my findings in this blog post.

code snippet from python OpenAI 0.28.1 module:

from openai.embeddings_utils import get_embedding, cosine_similarity

embedding = get_embedding(
            "Himalayan is the Great mountain range",
            model="text-embedding-ada-002"       
)

df["similarities"] = df.ada_v2_embedding.apply(lambda x: cosine_similarity(x, embedding))

is replaced by below code snippet from python OpenAI 1.xx.xx. version (1.0.0 or 1.14.1)

import openai

embedding = client.embeddings.create(
            model="text-embedding-ada-002",
            input="Himalayan is the Great mountain range"            
        )

 df["similarities"] = df.ada_v2_embedding.apply(lambda x: cosine_similarity(x, embedding))

OR you can use below one for cosine similarity

from scipy.spatial.distance import cosine

df["similarities"] = df.ada_v2_embedding.apply(lambda x: cosine(embedding, x))

Happy coding !!!

Leave a comment