-3
import openai
from langchain.embeddings.openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model_name="ada")

query_result = embeddings.embed_query("Hello world")
len(query_result)

When compiling the above code it gives the following error:

> ValidationError                           Traceback (most recent call
> last) <ipython-input-60-3e1d76e373bd> in <cell line: 4>()
>       2 from langchain.embeddings.openai import OpenAIEmbeddings
>       3 
> ----> 4 embeddings = OpenAIEmbeddings(model_name="ada")
>       5 
>       6 query_result = embeddings.embed_query("Hello world")
> 
> /usr/local/lib/python3.10/dist-packages/pydantic/main.cpython-310-x86_64-linux-gnu.so
> in pydantic.main.BaseModel.__init__()
> 
> ValidationError: 1 validation error for OpenAIEmbeddings model_name  
> extra fields not permitted (type=value_error.extra)

I have tried using this :-

import os
from openai_embed import OpenAIEmbeddings

os.environ["OPENAI_API_KEY"] = "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"
api_key = os.getenv("OPENAI_API_KEY")

embeddings = OpenAIEmbeddings(openai_api_key=api_key)

query_result = embeddings.embed_query("Hello world")
print(len(query_result))

But the error is not fixing

Helen
  • 87,344
  • 17
  • 243
  • 314

1 Answers1

2

There is no model_name parameter. The parameter used to control which model to use is called deployment, not model_name. Additionally, there is no model called ada. You probably meant text-embedding-ada-002, which is the default model for langchain. If you're satisfied with that, you don't need to specify which model you want.

Here's an example of how to use text-embedding-ada-002.

import os
from langchain.embeddings.openai import OpenAIEmbeddings

os.environ["OPENAI_API_KEY"] = "sk-xxxx"

embeddings = OpenAIEmbeddings()

print(embeddings.embed_query("Hello world"))

Here's an example of how to use a non-default model.

import os
from langchain.embeddings.openai import OpenAIEmbeddings

os.environ["OPENAI_API_KEY"] = "sk-xxxx"

# Use old version of Ada. You probably want V2 rather than this.
embeddings = OpenAIEmbeddings(deployment="text-similarity-ada-001")

print(embeddings.embed_query("Hello world"))
Nick ODell
  • 15,465
  • 3
  • 32
  • 66