RAG - How can I define orderby/sort in a chat completions create command with data source?

Nuno Rodrigues 20 Reputation points
2024-06-11T14:43:26.7233333+00:00

This is how I define my chat completions create when I add my own data source from an Azure Search Index. My question is simple. I would like to order the results from the search based on specific index fields. This is possible when I interact with Azure Search client directly but not through this? How can I do this? If this is not possible, it seems like a huge flaw in the design of this api.

When I try to add an orderby field it tells me that the api doesn't recognize it.

response = client.chat.completions.create(
    messages=[{"role": "user", "content": "What are the differences between Azure Machine Learning and Azure AI services?"}],
    model=deployment,
    extra_body={
        "dataSources": [
            {
                "type": "AzureCognitiveSearch",
                "parameters": {
                    "endpoint": os.environ["SEARCH_ENDPOINT"],
                    "key": os.environ["SEARCH_KEY"],
                    "indexName": os.environ["SEARCH_INDEX_NAME"],
                }
            }
        ]
    },
    stream=True,
)
Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
831 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,576 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 15,786 Reputation points Microsoft Employee
    2024-06-12T02:41:26.9333333+00:00

    @Nuno Rodrigues It seems you’re trying to integrate sorting functionality into the chat completions create command with an Azure Search Index as a data source. Currently, the Azure OpenAI API does not support the orderby parameter directly in the chat completions create command. This is a common feature in Azure AI Search where you can specify the orderby clause to sort the results based on specific fields in your index.

    However, if the API does not recognize the orderby field, it suggests that this functionality might not be exposed through the chat completions API. This could be by design, as the chat completions are likely intended to provide responses based on relevance to the input query rather than a sorted order.

    To achieve the desired sorting, you might need to perform a two-step process:

    Use the Azure Cognitive Search client directly to query your index with the orderby clause and retrieve the sorted results.

    Pass these results to the chat completion API to generate the completion based on the sorted data.

    This approach allows you to maintain the sorting control with Azure Cognitive Search and still leverage the conversational capabilities of the chat completions.

    0 comments No comments