OpenAI Embeddings API error: "AttributeError: module 'openai' has no attribute 'Embedding'"

9.4k views Asked by At

I was running a python script with the openai library. Whenever I run this function on my local machine it throws the following error

 def gpt3_embedding(content, engine='text-embedding-ada-002'):
     #delay_print('Making a vector')
     content = content.encode(encoding='ASCII',errors='ignore').decode()
     response = openai.Embedding.create(input=content,model=engine)
     vector = response['data'][0]['embedding']  # this is a normal list
     #delay_print('Vector returned')
     return vector

AttributeError: module 'openai' has no attribute 'Embedding

NB : I am using the latest openai package and python 3.11.1

If all goes well, the error shouldnt appear at all.

1

There are 1 answers

0
Rok Benko On

I tested your code with:

  • Python 3.11.1 and OpenAI Python library 0.25.0
  • Python 3.11.1 and OpenAI Python library 0.26.3 (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 the instructions.

STEP 2: Upgrade OpenAI Python library

pip install --upgrade openai