OpenAI connect with elastic search datasource

I am trying call openAI with my custom index created on Elastic search but when I try to run it I am getting following error:

openai.BadRequestError: Error code: 400 - {'error': {'requestid': 'aaa-bbb-404d-8a12-3da23608f42c', 'code': 400, 'message': 'Validation error at #/dataSources/0/Elasticsearch/parameters: Value error, Invalid credentials configuration.  Please use either key and keyId together, or just apiKey.'}}

The code I have used for this:

client.chat.completions.create(
        messages=message_text,
        model=deployment_id,
        extra_body={
            "dataSources": [
                {
                    "type": "Elasticsearch",
                    "parameters": {
                        "apiKey": "my_apiKey",
                        "endpoint": "my_endpoint",
                        "indexName": "my_indexName",
                    },
                }
            ]
        }
    )

Hi @Saurabh_Agrawal2,

Welcome! I assume you are using the Chat Completion API from OpenAI? Which version of the library are you using, and is it for Python or JavaScript? Also are you using the legacy or latest chat completion API or another call?

Normally when I've done this myself in JavaScript I've used the Elasticsearch client to pull the required documents and pass via the prompt messages rather than pass the connection details to OpenAI. But for your error I would double check the connection details are correct and that the API key has the correct entitlements to the index as it looks like OpenAI can't access with the provided connection settings.

Hope that helps!

Hi @carly.richmond,
Thanks for looking into it.

Here I am using Python, also this client is OpenAI client. I am trying to edit this question but not able to do so.

Here are the details:
Openai python version: 1.3.2

code snippet:

import openai
client = openai.AzureOpenAI(
    base_url="my_base_url",
    api_key='my_api-key',
    api_version='2023-08-01-preview'
)
client.chat.completions.create(
        messages=message_text,
        model=deployment_id,
        extra_body={
            "dataSources": [
                {
                    "type": "Elasticsearch",
                    "parameters": {
                        "apiKey": "my_apiKey",
                        "endpoint": "my_endpoint",
                        "indexName": "my_indexName",
                    },
                }
            ]
        }
    )

If possible for you kindly update above question's code.

Hi @Saurabh_Agrawal2,

Thanks for providing the additional snippet and confirming you're using the AzureOpenAI library. Is the dataSources setting compatible with other sources aside from AzureCognitiveSearch? That's the only option I seem to find examples for when I search for this setting.

Hi @carly.richmond,

Yes I can see if update datasource type to some unsupported option then I get error:

openai.BadRequestError: Error code: 400 - {'error': {'requestid': 'abc-xyx-12bg-aa88-b7abc9a415fb', 'code': 400, 'message': "Validation error at #/dataSources/0: Input tag 'test_Elasticsearch' found using 'type' does not match any of the expected tags: 'AzureCognitiveSearch', 'Elasticsearch', 'AzureCosmosDB', 'Pinecone', 'AzureMLIndex'"}}

Which clearly states it supports Elasticsearch.

Thanks for confirming @Saurabh_Agrawal2. I would recommend double checking your key, endpoint and index name are correct, and that the API key has sufficient permissions on the index you are trying to access.

Aside from that, as it's an Azure API, it might be worth trying the Azure forums if the key looks to have sufficient permissions to see if the bad request error can be resolved.

Hope that helps!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.