Skip to content

Middleware for the Azure Cognitive Search endpoint to enable OpenAI answering functionality

Notifications You must be signed in to change notification settings

sammydeprez/openai-cognitive-search-middleware

Repository files navigation

Azure Cognitive Search Middleware

This middleware extends the default endpoint of Azure Cognitive Search, which includes a feature to extract answers based on semantic reranking BM25. With the addition of this middleware, the response to the user remains the same, but the answer is generated by an Azure Open AI Model.

By utilizing this middleware function, there is no need to modify any code in your application if you are already using the semantic answering function.

Configuration

All necessary parameters can be configured as environment variables in the Azure Function Configuration.

The following table outlines the variables, their corresponding data types, and descriptions:

Variable Name Data Type Description
SEARCHSERVICE_NAME String The name of your Azure Cognitive Search resource
SEARCHSERVICE_SCORE_THRESHOLD Decimal The middleware filters content based on the Semantic Reranking Score to remove irrelevant context sent to OpenAI. The middleware will use only the results with a higher score than the highest score multiplied by this parameter.
SEARCHSERVICE_MAX_NO_RESULTS Integer The maximum number of results that will be sent to OpenAI Completion API
SEARCHSERVICE_FIELD_CONTENT String The field in your index containing the content to be sent to OpenAI as context
SEARCHSERVICE_FIELD_KEY String The field in your index that contains a unique key to your content. This allows sources to be cited in the answer depending on your system message.
OPENAI_API_KEY String The API Key to your OpenAI service
OPENAI_API_BASE String The API endpoint to your OpenAI service
OPENAI_API_VERSION String The API version to be used for the OpenAI service
OPENAI_API_TYPE String "azure" for Azure OpenAI or "openai" for non-Azure OpenAI endpoints
OPENAI_API_DEFAULT_MODEL String The default OpenAI model to be used, supporting gpt-35-turbo and gpt-4
OPENAI_API_SYSTEM_MESSAGE String The system message

Usage

There are two options for using the API endpoint.

Option 1

You can make a call without changing anything in your requests. This will only work if your content and key field are the same in every index and are configured in your environment variables. Below is an example call. For more detailed information about the document search API, click here.

POST https://<<YOUR ENDPOINT>>/indexes/index-all-data/docs/search?api-version=2021-04-30-Preview
api-key: <YOUR KEY>
Content-Type: application/json

{
   "search": "<The question that needs to be answered>",
   "queryType": "semantic",
   "semanticConfiguration": "<semantic configuration name>",
   "queryLanguage": "<language in iso format like nl-BE>",
   "searchFields": "content",
   "select": "id,content",
   "captions": "extractive",
   "answers": "extractive",
   "top": 10
}

Option 2

By adding two extra fields (contentField & keyField) to the body, you can define which fields in your index need to be used. The downside is that this means you will need to update your application.

POST http://<<YOUR ENDPOINT>>/indexes/index-all-data/docs/search?api-version=2021-04-30-Preview
api-key: <YOUR KEY>
Content-Type: application/json

{
    "search": "<The question that needs to be answered>",
    "queryType": "semantic",
    "semanticConfiguration": "<semantic configuration name>",
    "queryLanguage": "<language in iso format like nl-BE>",
    "searchFields": "content",
    "select": "id,content",
    "captions": "extractive",
    "answers": "extractive",
    "top": 10,
    "contentField": "content",
    "keyField": "id"
}

About

Middleware for the Azure Cognitive Search endpoint to enable OpenAI answering functionality

Resources

Stars

Watchers

Forks

Languages