AttributeError: module 'openai' has no attribute 'Embedding'

Question:

According to OpenAi’s documentation and a large number of demonstrations I found online, the following code should run without a problem in Python:

import openai
response = openai.Embedding.create(
  input="porcine pals say",
  model="text-embedding-ada-002"
)

However, when I run this code on my local Jupyter instance, I receive the following error:

AttributeError                            Traceback (most recent call last)
>! <ipython-input-209-e3e908b35b81> in <module>
1 import openai
2 response = openai.Embedding.create(
3   input="porcine pals say",
4   model="text-embedding-ada-002"
5 )

AttributeError: module 'openai' has no attribute 'Embedding'

This is unique only for Embedding, as other Engines (like Completion) run fine on my local machine.

I upgraded my openai library to the newest version, but the error remained. I also asked ChatGPT for help, but its response appeared to be nothing more than a work-around using Completion (not Embedding). This did not work.

My question is whether others have encountered the same problem? If so, how did you resolve it? I presently don’t have a workaround to retrieve embeddings from OpenAI’s new ‘text-embedding-ada-oo2’ model. So even if there is a workaround I could use- that would be great.

Asked By: ashap551

||

Answers:

I tested your code with:

  • Python 3.11.1 and OpenAI Python library 0.26.3
  • Python 3.11.1 and OpenAI Python library 0.26.5 (latest version)

In both cases I ran test.py and the OpenAI API returned the embedding:

[-0.02801201120018959, 0.022157097235322, -0.011196479201316833,
0.005577428266406059, 0.012320289388298988, 0.007221521344035864, 0.00034121860517188907, -0.020603187382221222, -0.011182605288922787, -0.011349095962941647, -0.007270080968737602, 0.03884775936603546, -0.016232814639806747, 7.668747275602072e-05, -0.018938282504677773, 0.040873393416404724, 0.01576109230518341, 0.032798610627651215, 0.0067047071643173695, -0.03257662057876587, -0.01071088295429945, -0.002186920726671815, 0.018535930663347244, -0.0074435085989534855, -0.0016180785605683923, -0.009108412079513073, 0.023946870118379593, -0.03690537437796593, -0.024030115455389023, -0.007582250516861677, 0.015539104118943214, -0.02534816414117813, -0.008275960572063923, -0.015261620283126831, -0.019853981211781502, 0.0053346301428973675, 0.0011671670945361257, -0.02440471760928631, 0.05225023627281189, -0.010988366790115833, -0.004113700240850449, 0.020686432719230652, …]

test.py

import openai

response = openai.Embedding.create(
  input = 'Create embedding for this text',
  model = 'text-embedding-ada-002'
)

content = response['data'][0]['embedding']

print(content)

Solution

STEP 1: Upgrade Python

See this.

STEP 2: Upgrade OpenAI Python library

pip install -U openai
Answered By: Rok Benko
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.