TypeError: 'CreateEmbeddingResponse' object is not subscriptable
Asked Answered
N

1

8

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)
Neomineomycin answered 9/11, 2023 at 9:54 Comment(2)
Can you please edit to provide a minimal reproducible example including the full traceback and whatever input data you used.Transformism
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.Wheedle
M
31

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
Minette answered 9/11, 2023 at 22:38 Comment(3)
Thank you Florian, your answer works for me. There is a similar case with the ChatCompletion, TypeError: 'ChatCompletion' object is not subscriptable But why this error occurs in the first place, can anyone explain it please?Fishmonger
It seems create() was returning dict like object before and returning class instance currently.Tokenism
This is the best answerStrega

© 2022 - 2024 — McMap. All rights reserved.