"Incorrect API key provided" Error when working with Azure OpenAI

Hi all,

I am trying to use Weaviate with Azure Open AI. I am aware that one needs to pass a different header when making a request like “X-Azure-Api-Key”" but I constantly get the error.

Either

API Key: no api key found

When I only pass the azure key like here:

client = weaviate.Client(
url=weaviate_url,
auth_client_secret=weaviate.auth.AuthApiKey(api_key=weaviate_secret_key),
additional_headers={
“X-Azure-Api-Key”: azure_openai_key,
},
)

or this error:

Incorrect API key provided

When I use this (as this was suggested somewhere:

client = weaviate.Client(
url=weaviate_url,
auth_client_secret=weaviate.auth.AuthApiKey(api_key=weaviate_secret_key),
additional_headers={
“X-Azure-Api-Key”: azure_openai_key,
“X-Openai-Api-Key”: azure_openai_key,
},
)

I tried so many variations (providing it in the docker compose file or not, having it in an .env file or in clear text) but I do not know how to resolve it;

This is my current set up

openai.api_type = “azure”
openai.api_version = “2023-07-01-preview”
openai.api_base = os.getenv(‘AZURE_API_ENDPOINT’)
openai.api_key = os.getenv(“AZURE_APIKEY”)
azure_openai_key = openai.api_key

client = weaviate.Client(
url=weaviate_url,
auth_client_secret=weaviate.auth.AuthApiKey(api_key=weaviate_secret_key),
additional_headers={
“X-Azure-Api-Key”: azure_openai_key,
“X-Openai-Api-Key”: azure_openai_key,
},
)

and in my schema I also define this:

            "class": "ClassName",
            "description": "A class description",
            "vectorizer": "text2vec-openai",
            "moduleConfig": {
                "text2vec-openai": {
                    "model": "ada",
                    "resourceName": "XXX",
                    "deploymentId": "text-embedding-ada-002",
                }
            },

I would highly appreciate any help as I really do not know how to solve this

Many thanks!

Hi @c-lara ! Welcome to our community :hugs:

Here is the doc for that. Your code seems correct.

Can you try reproducing it in a shared python notebook? That way we can make sure we are in the same page regarding your code.

Thanks!

its a different issue. answer for both is not in document. Can you share all steps involved in using azure opeani in weviate. Steps mentioned in doc are not sufficient.

1 Like

Would you be able to explain what’s happenign here? Like this person I think I’ve tried everything and it still is looking for an OPENAI key not an Azure OPENAI key.

1 Like

Hi both,

Fortunately, I found the solution!

You need to insert the baseURL in your schema definition like so:

                "moduleConfig": {
                    "text2vec-openai": {
                        "baseURL": "https://YOUR_RESSOURCE_NAME.openai.azure.com/",
                        "resourceName": "YOUR_RESSOURCE_NAME",
                        "deploymentId": "text-embedding-ada-002",
                    }
                },

Then it correctly recognizes it as Azure. I think it would be useful for others to update this in the Weaviate Documentation, as this is really not mentioned anywhere.

Good luck!

Basic question, this iconfig is in json format, how to provide same thing via code ? or where i can use this json ?

Hi @Shreyas_Deshpande ! Welcome to our community :hugs:

You can both provide the collection configuration as a json or use, for example, the python v4 to build it.

While using the python v4 is easier, json may require your to know all the names of properties, and because of that be a little bit more complicated.

For example

from weaviate import classes as wvc
client.collections.delete("Collection")
collection = client.collections.create(
    "Collection",
    vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_openai()
)

you can now “export” this collection definition:

collection_definition = collection.config.get().to_dict()
import json
print(json.dumps(collection_definition))
{
    "invertedIndexConfig": {
        "bm25": {
            "b": 0.75,
            "k1": 1.2
        },
        "cleanupIntervalSeconds": 60,
        "indexNullState": false,
        "indexPropertyLength": false,
        "indexTimestamps": false,
        "stopwords": {
            "preset": "en"
        }
    },
    "multiTenancyConfig": {
        "enabled": false
    },
    "properties": [],
    "replicationConfig": {
        "factor": 1
    },
    "shardingConfig": {
        "virtualPerPhysical": 128,
        "desiredCount": 1,
        "actualCount": 1,
        "desiredVirtualCount": 128,
        "actualVirtualCount": 128,
        "key": "_id",
        "strategy": "hash",
        "function": "murmur3"
    },
    "vectorIndexConfig": {
        "cleanupIntervalSeconds": 300,
        "distanceMetric": "cosine",
        "dynamicEfMin": 100,
        "dynamicEfMax": 500,
        "dynamicEfFactor": 8,
        "ef": -1,
        "efConstruction": 128,
        "flatSearchCutoff": 40000,
        "maxConnections": 64,
        "skip": false,
        "vectorCacheMaxObjects": 1000000000000
    },
    "vectorIndexType": "hnsw",
    "vectorizer": "text2vec-openai",
    "class": "Collection",
    "moduleConfig": {
        "text2vec-openai": {
            "baseURL": "https://api.openai.com",
            "model": "ada",
            "vectorizeClassName": true
        }
    }
}

and if necessary, import from json, changing only the name:

collection_definition["Class"] = "MyNewCollection"
client.collections.create_from_dict(collection_definition)

Let me know if this helps :slight_smile: