OpenAI API error: "No module named 'openai.embeddings_utils'; 'openai' is not a package"
Asked Answered
V

8

9

I want to use openai.embeddings_utils import get_embeddings So already install openai

Name: openai
Version: 0.26.5
Summary: Python client library for the OpenAI API
Home-page: https://github.com/openai/openai-python
Author: OpenAI
Author-email: [email protected]
License: 
Location: /Users/lima/Desktop/Paprika/Openai/.venv/lib/python3.9/site-packages
Requires: aiohttp, requests, tqdm
Required-by: 

This is my openai But why not use openai.embeddings_utils??

Volding answered 9/2, 2023 at 4:16 Comment(0)
H
8

For my case, check the version of openai. openai.embeddings_utils does not exist in latest openai 1.2.0, but exists in 0.27.7

Hoggish answered 9/11, 2023 at 15:56 Comment(0)
T
5

openai.embeddings_utils was removed from the library following version 1.0 (see here).

So you have two options:

  1. Install the last version of the library that included it: pip install openai==0.28.1.
  2. Use the latest version of the library but manually incorporate methods from the embeddings_utils source code into your code.
Terrilynterrine answered 30/11, 2023 at 10:9 Comment(0)
M
5

@micycle's answer shows the workarounds you can use to include the legacy openai.embeddings_utils. Another option is to use the new API from the latest version (Taken from official docs):

from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY")

def get_embedding(text, model="text-embedding-ada-002"):
   text = text.replace("\n", " ")
   return client.embeddings.create(input = [text], 
model=model).data[0].embedding

df['ada_embedding'] = df.combined.apply(lambda x: get_embedding(x, model='text-embedding-ada-002'))
Matsu answered 12/12, 2023 at 11:35 Comment(0)
A
2

First run this command pip install openai[embeddings].

Then import the embeddings_utils package lik this: from openai.embeddings_utils import get_embedding

You can find details here: https://github.com/openai/openai-python

Anywheres answered 9/2, 2023 at 5:26 Comment(2)
This method did not work for me. On installing the package openai[embeddings] I got a warning saying: WARNING: openai 1.3.6 does not provide the extra 'embeddings'Umeko
This answer is outdated. embeddings_utils no longer exists in v1+.Terrilynterrine
C
2

would you like to try this? it does the job well.

from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings()
Coretta answered 19/1 at 17:31 Comment(0)
H
1

There are two possible reasons why you get the No module named 'openai.embeddings_utils'; 'openai' is not a package error.


REASON 1: Your Python code isn't correct

Run pip install openai in the terminal, and then write the following in your Python script:

import openai
from openai.embeddings_utils import get_embedding

# The rest of your code

Note: For an example of how to use embeddings, see this answer.


REASON 2: You named the file openai.py

Don't name the file openai.py.

If you upgrade both the OpenAI Python package and Python but the error persists, then you probably named the file openai.py.


If you upgrade both the OpenAI Python package and Python and use the code I posted above, you should get no errors.

Horatius answered 9/2, 2023 at 10:44 Comment(0)
B
1

I had the same problem. I don't know why pip wasn't installing some files. I solved by manually downloading embeddings_utils.py inside my virtual env .\venv\Lib\site-packages\openai\ folder. You might need to do the same for datalib.py

Note: if the folder is missing, openai may not be installed in the venv. To be sure you are actually installing on the virtual env, activate it and then use the command: .\venv\Scripts\python.exe -m pip install openai

Bionics answered 4/4, 2023 at 10:0 Comment(0)
K
0

If you are trying to run this on your jupyter cell:

!pip install openai

import openai
from openai.embeddings_utils import get_embedding

and you get this error:

ModuleNotFoundError                       Traceback (most recent call last)
C:\Users\NAVIGA~1\AppData\Local\Temp/ipykernel_36768/3211534756.py in <module>
      1 import openai
----> 2 from openai.embeddings_utils import get_embedding

~\AppData\Roaming\Python\Python39\site-packages\openai\embeddings_utils.py in <module>
      5 import plotly.express as px
      6 from scipy import spatial
----> 7 from sklearn.decomposition import PCA
      8 from sklearn.manifold import TSNE
      9 from sklearn.metrics import average_precision_score, precision_recall_curve

ModuleNotFoundError: No module named 'sklearn'

then you have to :

!pip install scikit-learn

Now try:

import openai
from openai.embeddings_utils import get_embedding

It will run successfully

Klingensmith answered 11/4, 2023 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.