# Missing reference when importing from previous python library's "utils"

**URL:** <https://community.openai.com/t/missing-reference-when-importing-from-previous-python-librarys-utils/530242>\
**Category:** API\
**Tags:** embeddings, api\
**Created:** [November 27, 2023, 8:52pm UTC](https://community.openai.com/t/missing-reference-when-importing-from-previous-python-librarys-utils/530242 "2023-11-27T20:52:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsraman85](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/vsraman85/32/255527_2.png) [@vsraman85](https://community.openai.com/u/vsraman85)\
**Post date:** [November 27, 2023, 8:52pm UTC](https://community.openai.com/t/missing-reference-when-importing-from-previous-python-librarys-utils/530242/1 "2023-11-27T20:52:25Z")

</div>

ModuleNotFoundError: No module named ‘openai.embeddings\_utils’  
with the the latest version of OpenAI; the below import statement is throwing above error.  
from openai.embeddings\_utils import get\_embedding, cosine\_similarity

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [November 27, 2023, 10:24pm UTC](https://community.openai.com/t/missing-reference-when-importing-from-previous-python-librarys-utils/530242/2 "2023-11-27T22:24:48Z")

</div>

The python library has been completely revamped.

The kludges that were available in “utils” are mostly no longer necessary, such as a function for outputting a generator as a dict.

Looks like it’s time to adapt the API reference into a more useful example…

```python
from openai import OpenAI
client = OpenAI()

def multiple_embed(embed_input): # only takes list, which can be single string
    veclist = []
    try:
        embed = client.embeddings.create(
          model="text-embedding-ada-002",
          input=embed_input, encoding_format="float"
        )
    except Exception as e:
        print(f"Embeddings failure {e}")
        raise

    firstvector = embed.model_dump().get('data')[0].get('embedding')
    for index in embed.model_dump().get('data'):
        veclist.append(index.get('embedding'))
    if not len(veclist) == len(embed_input):
        print("embeddings doesn't match number of inputs")
        raise

    cost = embed.usage.total_tokens
    print(f"embeddings: {cost} tokens, "
          f"{len(embed.model_dump().get('data'))} vectors")
    return veclist

input = [" cute kitten", " ugly cat"] # 8192 max tokens total
vectors_list = multiple_embed(input) # a list, with vector list for each input

```

---

<div class="post-metadata">

**Author:** ![vsraman85](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/vsraman85/32/255527_2.png) [@vsraman85](https://community.openai.com/u/vsraman85)\
**Post date:** [November 27, 2023, 10:32pm UTC](https://community.openai.com/t/missing-reference-when-importing-from-previous-python-librarys-utils/530242/3 "2023-11-27T22:32:15Z")

</div>

Thanks for the response. I was able to embed the text and save it as a CSV file.  
When the input comes, I would like to get do the cosine similarities matching in vector text data. I have been using OpenAI documentation for this POC, where i found the below code sample.

```auto
from openai.embeddings_utils import get_embedding, cosine_similarity

def search_reviews(df, product_description, n=3, pprint=True):
   embedding = get_embedding(product_description, model='text-embedding-ada-002')
   df['similarities'] = df.ada_embedding.apply(lambda x: cosine_similarity(x, embedding))
   res = df.sort_values('similarities', ascending=False).head(n)
   return res

res = search_reviews(df, 'delicious beans', n=3)

```

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [November 27, 2023, 10:50pm UTC](https://community.openai.com/t/missing-reference-when-importing-from-previous-python-librarys-utils/530242/4 "2023-11-27T22:50:09Z")

</div>

You can grab the utils as they were, and just adapt some of the dependencies.

> <https://github.com/openai/openai-python/blob/release-v0.28.1/openai/embeddings_utils.py>

For example:

```auto
import numpy as np
def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

```

---

<div class="post-metadata">

**Author:** ![vsraman85](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/vsraman85/32/255527_2.png) [@vsraman85](https://community.openai.com/u/vsraman85)\
**Post date:** [November 29, 2023, 7:04pm UTC](https://community.openai.com/t/missing-reference-when-importing-from-previous-python-librarys-utils/530242/5 "2023-11-29T19:04:30Z")

</div>

Thank you, but still, the highlighted libraries are throwing errors. I referred to the link that was provided in your above response.

> <https://github.com/openai/openai-python/blob/release-v0.28.1/openai/embeddings_utils.py>

 ![image](https://us1.discourse-cdn.com/openai1/original/4X/2/e/0/2e08e418a403a7c856765bcaf89d714346343f87.png)

---

<div class="post-metadata">

**Author:** ![vsraman85](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/vsraman85/32/255527_2.png) [@vsraman85](https://community.openai.com/u/vsraman85)\
**Post date:** [November 29, 2023, 7:10pm UTC](https://community.openai.com/t/missing-reference-when-importing-from-previous-python-librarys-utils/530242/6 "2023-11-29T19:10:57Z")

</div>

I tried it in anaconda notebook as well. I am getting below errors, Seems like some of the libraries are deprecated or removed in newer version ???

 ![image](https://us1.discourse-cdn.com/openai1/original/4X/6/7/f/67fcd0bbbb7748eb5f52483af9d94a84758a48f3.png)

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [November 29, 2023, 7:26pm UTC](https://community.openai.com/t/missing-reference-when-importing-from-previous-python-librarys-utils/530242/7 "2023-11-29T19:26:32Z")

</div>

You need code-writing abilities and would need to explore the code to see what methods are being used and what imports they rely on, and then write similar portable version of the function for your code.

---

<div class="post-metadata">

**Author:** ![vsraman85](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/vsraman85/32/255527_2.png) [@vsraman85](https://community.openai.com/u/vsraman85)\
**Post date:** [November 29, 2023, 10:34pm UTC](https://community.openai.com/t/missing-reference-when-importing-from-previous-python-librarys-utils/530242/8 "2023-11-29T22:34:00Z")

</div>

Got it. I am now able to build a file with only the needed functions. But as per openai documentation, I am trying to implement the text search like below  
, and getting error as  
AttributeError: ' **DataFrame’ object has no attribute ‘ada\_embedding’**  
The referral link is  
[https://platform.openai.com/docs/guides/embeddings/use-cases](https://platform.openai.com/docs/guides/embeddings/use-cases)

df = pd.read\_csv(‘filename.csv’)

df[‘ada\_embedding’] = df.ada\_embedding.apply(eval).apply(np.array)

res = search\_reviews(df, ‘Sample Text’, n=3)

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [November 30, 2023, 2:49am UTC](https://community.openai.com/t/missing-reference-when-importing-from-previous-python-librarys-utils/530242/9 "2023-11-30T02:49:32Z")

</div>

> [@vsraman85](#):
>
> AttributeError: ' **DataFrame’ object has no attribute ‘ada\_embedding’**

You’ll likely find the answer in: `openai.datalib.pandas_helper`

Another thing you can try is rather brute-force. Download the whole repo at that branch. Then, run a search-replace on “openai” and replace it with “myopenai” in every directory name, file name, and file, so you can put that “myopenai” directory in your python application’s directory and again import it.

Replace in files will replace a lot of actual API calls with broken calls, but that will tell you where you are still relying on old 0.28.1 methods that must be rewritten.

Better is to just learn what the utility function was doing and write a new one.

---

<div class="post-metadata">

**Author:** ![Foxalabs](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/foxalabs/32/738355_2.png) [@Foxalabs](https://community.openai.com/u/Foxalabs)\
**Post date:** [December 8, 2023, 3:20am UTC](https://community.openai.com/t/missing-reference-when-importing-from-previous-python-librarys-utils/530242/10 "2023-12-08T03:20:32Z")

</div>

Closed on December 8, 2023, 3:20am UTC


