Trouble with AI lab in Mastering Generative AI with OpenAI

Syntax error in the lab

I’m on Module 10 (Word Using Word Embedding for Dynamic Content) of the Mastering Generative AI with OpenAI where we are given the Jupyter Notebook file to run on our own. Like other labs, the openai has changed and I had to adapt, but I’m not 100% sure how to adapt the call to Open AI in this lab. The details from the github page tell me that openai.Embedding.create() call is replaced with client.embeddings.create()
As I had done in other labs, I assume that client is just openai (I’m still a bit weak in Python).
So I call openai.embeddings.create()
WHen I run that function I get this error:


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[18], line 1
----> 1 embeddings=list(map(text_embedding,phrases))

Cell In[15], line 3, in text_embedding(text)
      1 def text_embedding(text) -> None:
      2     response = openai.embeddings.create(model="text-embedding-ada-002", input=text)
----> 3     return response["data"][0]["embedding"]

TypeError: 'CreateEmbeddingResponse' object is not subscriptable

I haven’t found a way to inspect objects like you would do in Eclipse or VS Code as you step through the application so I’m not 100% sure what this error means or how to fix it.

Hi @hugh

Can you please send the lab link? I searched for ‘Word Using Word Embedding for Dynamic Content’ in the Mastering Generative AI with OpenAI course, but I couldn’t find it.

Here is the link to the lab:
https://learn.kodekloud.com/user/courses/mastering-generative-ai-with-openai/module/cf879fc5-dcc3-4470-830d-4393645105c9/lesson/3406167d-f560-401e-8c82-3ab74b9b320d

And here is the link referenced on that page:
https://kodekloud.com/kk-media/raw/upload/v1704979464/course-resource-new/Jupyter-Notebook-Similarity-Search.7z

Each lab should be updated to work with openai >=1.0.0, although up until this one, the fix was simple - replace openai…ChatCompletion.create() with openai.chat.completions.create()

Hi @hugh

I don’t see any ChatCompletion.create() in the lab. Could you please share a screenshot?

The replacement of ChatCompletion.create() is not in this lab, it is every other lab. In this lab it’s the function where you grab the embedding model. Here is a snippet from my jupyter file. I added the extra variable, data, to capture the response, but as I’m not super familiar with either python nor jupyter, I’m not sure how to inspect the variable I get back.

 "def text_embedding(text) -> None:\n",
    "    \n",
    "    response = openai.embeddings.create(model=\"text-embedding-ada-002\", input=text)\n",
    "    data = response[\"data\"]\n",
    "    return data[0][\"embedding\"]"

The original code block I think had openai.Embedding.create()

An example of the CatCompletion.create() can be found in the set up guide here:

I found the time to go looking for the answer. I changed my function as follows:

def text_embedding(text) → None:

response = openai.embeddings.create(input=text, model="text-embedding-ada-002")
data = response.data
return data[0].embedding

Alternatively you could hate just done return response.data[0].embedding

There were subtle changes to the API since the course was written. THe reference to the embedding API is here:
https://platform.openai.com/docs/guides/embeddings?lang=python