lima
lima

Reputation: 293

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

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??

Upvotes: 10

Views: 31497

Answers (9)

Michael Brenndoerfer
Michael Brenndoerfer

Reputation: 4076

Embedding is now called from the client object.

openai_client = Client(
    api_key="secret",
    max_retries=20,
)
openai_client.embeddings.create(
    input="Your text string goes here",
    model="text-embedding-3-small",
)

Upvotes: 0

Rok Benko
Rok Benko

Reputation: 23088

UPDATE: 6 November 2023

The embeddings_utils were removed from the OpenAI Python SDK >=v1.0.0.


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.

Upvotes: 1

yts61
yts61

Reputation: 1619

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

from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings()

Upvotes: 2

micycle
micycle

Reputation: 3820

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.

Upvotes: 5

Kamyar
Kamyar

Reputation: 18797

@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'))

Upvotes: 6

Gianmarco Guarnier
Gianmarco Guarnier

Reputation: 11

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

Upvotes: 1

jing chen
jing chen

Reputation: 96

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

Upvotes: 8

Talha Tayyab
Talha Tayyab

Reputation: 27770

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

Upvotes: 0

khushmeeet
khushmeeet

Reputation: 64

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

Upvotes: 2

Related Questions