I am doing this project but it gives me errors web-qa-embeddings

Hello

I’m trying to do this tutorial:
https://platform.openai.com/docs/tutorials/web-qa-embeddings

I installed this configuration on a Ubuntu server, but they gave me the following errors.

line 245File “/opt/web-crawl-q-and-a-example/web-qa.2.py”, in df[‘embeddings’] = df.text.apply(lambda x: client. embeddings.create(input=x, engine=‘text-embedding-3-large’)[‘data’][0][‘embedding’]) File “/opt/web-crawl-q-and-a-example /env/lib/python3.10/site-packages/pandas/core/series.py”, line 4771, in apply return SeriesApply(self, func, convert_dtype, args, kwargs).apply() File “/opt/web -crawl-q-and-a-example/env/lib/python3.10/site-packages/pandas/core/apply.py”, line 1105, in apply return self.apply_standard() File “/opt/web- crawl-q-and-a-example/env/lib/python3.10/site-packages/pandas/core/apply.py”, line 1156, in apply_standard mapped = lib.map_infer( File “pandas/_libs/lib. pyx”, line 2918, in pandas._libs.lib.map_infer File “/opt/web-crawl-q-and-a-example/web-qa.2.py”, line 245, in df[’ embeddings’] = df.text.apply(lambda x: client.embeddings.create(input=x, engine=‘text-embedding-3-large’)[‘data’][0][‘embedding’]) TypeError : Embeddings.create() got an unexpected keyword argument ‘engine’

Can you guide me with these errors.

I have the latest version of Openai

It looks like the example was updated for the python openai library released Nov 2023, which is not backwards-compatible. However, they missed updating the old parameter “engine”. The new term is “model”. You also have the choice of starting with two new models, with more parameters such as dimensions available.

To learn to use and keep track of changes, the forum has an API Reference link in the sidebar.

2 Likes

I modified the word engine by model but I ran the code and it still gave me problems, now it shows me the following:

  File "/opt/web-crawl-q-and-a-example/web-qa.2.py", line 244, in <module>
    df['embeddings'] = df.text.apply(lambda x: client.embeddings.create(input=x, model='text-embedding-ada-002')['data'][0]['embedding' ])
  File "/opt/web-crawl-q-and-a-example/env/lib/python3.10/site-packages/pandas/core/series.py", line 4771, in the application
    return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
  File "/opt/web-crawl-q-and-a-example/env/lib/python3.10/site-packages/pandas/core/apply.py", line 1105, in apply
    return self.apply_standard()
  File "/opt/web-crawl-q-and-a-example/env/lib/python3.10/site-packages/pandas/core/apply.py", line 1156, in apply_standard
    mapped = lib.map_infer(
  File "pandas/_libs/lib.pyx", line 2918, in pandas._libs.lib.map_infer
  File "/opt/web-crawl-q-and-a-example/web-qa.2.py", line 244, in <lambda>
    df['embeddings'] = df.text.apply(lambda x: client.embeddings.create(input=x, model='text-embedding-ada-002')['data'][0]['embedding' ])

TypeError: ‘CreateEmbeddingResponse’ object has no subscripts

Any ideas to make it work

File “/opt/web-crawl-q-and-a-example/web-qa.2.py”, line 244, in
df[‘embeddings’] = df.text.apply(lambda x: client.embeddings.create(input=x, model=‘text-embedding-ada-002’)[‘data’][0][‘embedding’])
File “/opt/web-crawl-q-and-a-example/env/lib/python3.10/site-packages/pandas/core/series.py”, line 4771, in apply
return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
File “/opt/web-crawl-q-and-a-example/env/lib/python3.10/site-packages/pandas/core/apply.py”, line 1105, in apply
return self.apply_standard()
File “/opt/web-crawl-q-and-a-example/env/lib/python3.10/site-packages/pandas/core/apply.py”, line 1156, in apply_standard
mapped = lib.map_infer(
File “pandas/_libs/lib.pyx”, line 2918, in pandas._libs.lib.map_infer
File “/opt/web-crawl-q-and-a-example/web-qa.2.py”, line 244, in
df[‘embeddings’] = df.text.apply(lambda x: client.embeddings.create(input=x, model=‘text-embedding-ada-002’)[‘data’][0][‘embedding’])
TypeError: ‘CreateEmbeddingResponse’ object is not subscriptable

It appears you are trying to parse the response object as if it was still a dictionary. It is not.

Instead, realize that the return is a pydantic model with methods, and the return object can have different entities depending on whether you specify a JSON or base64 return.

Although I like the determined preciseness and compactness of getting base64 which has raw float32 within, here is JSON value parsing.

from openai import OpenAI
import numpy as np
client = OpenAI()

embed_input = [" Don't pet the porcupine.", "I love PCs"]
try:
    embed = client.embeddings.create(
      model="text-embedding-3-small",
      input=embed_input, dimensions=1536,
    )
except Exception as e:
    print(f"Embeddings failure {e}")
    raise

# One embeddings value list can be obtained by "embed.data[0].embedding"
# Convert the embeddings by index into a rank-2 numpy array
embeddings_array = np.array([data.embedding for data in embed.data])

And then we can see what got loaded, and demonstrate a comparison.

# Print a sample of each embeddings input
for i in range(embeddings_array.shape[0]):
    print(f"First 5 elements of embedding {i+1}: \
          {embeddings_array[i, :5]}")
print(f"Dot product of the first two embeddings: \
      {np.dot(embeddings_array[0], embeddings_array[1])}")

First 5 elements of embedding 1: [ 0.05267531 -0.03325627 -0.0161239 0.00649647 -0.08565015]
First 5 elements of embedding 2: [ 0.01257202 -0.00713852 -0.02897868 0.04020394 0.05347256]
Dot product of the first two embeddings: 0.13790034562842052

[juanjose.espinoza] obviously just walks through a tutorial (so do I): same problem.
I simply want to pass questions about a certain website (ours) to the API, not to learn Python (we usually use PHP).
What woud be the correct expression instead of the one cited by juanjose.espinoza as it ist still written in the tutorial?