AzureOpenAIEmbedding issue

Why am i getting this error? and how to fix it?

I am using AzureOpenIEmbedding endpoint in Azure Databricks.
import os
from databricks.vector_search.client import VectorSearchClient
from langchain.vectorstores import DatabricksVectorSearch
from langchain.embeddings import DatabricksEmbeddings
from langchain.chains import RetrievalQA

Set environment variables

os.environ[‘DATABRICKS_TOKEN’] = dbutils.secrets.get(secret_scope, secret_key)

Ensure ‘host’ is defined here

question = “What is Apache Spark?”

# Initialize embedding model
embedding_model = DatabricksEmbeddings(endpoint=embedding_model_endpoint_name)
test_embedding = embedding_model.embed_query(question)[:20]
print(f"Test embeddings: {test_embedding}…")

def get_retriever(persist_dir: str = None):
    # Ensure 'host' variable is correctly set
    vsc = VectorSearchClient(workspace_url=host, personal_access_token=os.environ['DATABRICKS_TOKEN'], disable_notice=True)
    vs_index = vsc.get_index(endpoint_name=vector_search_endpoint_name, index_name=index_name)

    # Create the retriever
    vectorstore = DatabricksVectorSearch(vs_index, text_column="content", embedding=embedding_model, columns=["ID", "chunk"])
    return vectorstore.as_retriever(search_kwargs={'k': 4})

retriever = get_retriever()

# Define the retrieval chain
retrieve_document_chain = (
    | RunnableLambda(extract_question)  # Ensure this function is defined and works correctly
    | retriever
result = retrieve_document_chain.invoke({"messages": [{"role": "user", "content": question}]})

except Exception as e:
print(f"Error occurred: {e}")

Test embeddings: [-0.0070154425, -0.017142845, -0.00357199, -0.028359435, -0.011108347, 0.0031018131, 0.0037850917, -0.017183436, 0.0118728075, -0.041673217, 0.011886338, 0.004170704, -0.0062408345, -0.017684055, 0.0022426415, 0.010871568, 0.032526758, -0.029793642, 0.00078390975, -0.004410866]…
Error occurred: 400 Client Error: Bad Request for url: … Response text: {“error”:“Received error from openai”,“external_model_message”:{“error”:{“message”:“‘$.input’ is invalid. Please check the API reference:",“type”:“invalid_request_error”,“param”:null,"code”:null}}}

Error 400 is invalid request error.

It covers a lot of scenarios. Here’s what docs state:

An BadRequestError (formerly InvalidRequestError) indicates that your request was malformed or missing some required parameters, such as a token or an input. This could be due to a typo, a formatting error, or a logic error in your code.

If you encounter an BadRequestError, please try the following steps:

  • Read the error message carefully and identify the specific error made. The error message should advise you on what parameter was invalid or missing, and what value or format was expected.
  • Check the API Reference for the specific API method you were calling and make sure you are sending valid and complete parameters. You may need to review the parameter names, types, values, and formats, and ensure they match the documentation.
  • Check the encoding, format, or size of your request data and make sure they are compatible with our services. You may need to encode your data in UTF-8, format your data in JSON, or compress your data if it is too large.
  • Test your request using a tool like Postman or curl and make sure it works as expected. You may need to debug your code and fix any errors or inconsistencies in your request logic.
  • If the issue persists, check out our persistent errors next steps section.

It’s very difficult to point out why your code is resulting in 400 because there’s no code that’s making call to the OpenAI API here.