TypeError: 'CreateEmbeddingResponse' object is not subscriptable

3.9k views Asked by At

I am trying to run sample code from the openai website for getting embeddings for dataset: https://platform.openai.com/docs/guides/embeddings/use-cases. However, the code returns an error that I can't resolve looking at historical posts

I tried running this code where df is a dataframe I have created with my own data that loaded succesfully.

from openai import OpenAI
client = OpenAI()

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['embedding'] = df.ITEM_DESCRIPTION.apply(lambda x: get_embedding(x, model='text-embedding-ada-002'))
df.to_csv('embedded_output.csv', index=False)
1

There are 1 answers

2
Florian On

They didn't update their documentation. It is now:

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