Tuesday, January 2, 2024

Azure AI Service: Cannot import name 'SemanticSettings' from 'azure.search.documents.indexes.models'

You may encounter the following exception while testing Semantic Ranking with the Azure AI service. I was exploring the 'Quickstart: Semantic Ranking' guide on Azure AI Search, available on Microsoft Learn, using the Python library. 

The exception occurred during the execution of the following code snippet:

%pip install azure-search-documents --pre
%pip show azure-search-documents
%pip install python-dotenv

import os
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents import SearchClient
from azure.search.documents.indexes.models import (  
    SearchIndex,  
    SearchField,  
    SearchFieldDataType,  
    SimpleField,  
    SearchableField,
    ComplexField,
    SearchIndex,  
    SemanticConfiguration,  
    PrioritizedFields,  
    SemanticField,  
    SemanticSettings,  
)

ImportError: cannot import name 'SemanticSettings' from 'azure.search.documents.indexes.models'

Additionally, you might encounter the following exception when executing the sample code from the aforementioned document.

ImportError: cannot import name 'PrioritizedFields' from 'azure.search.documents.indexes.models'

To resolve the issue, replace 'PrioritizedFields' with 'SemanticPrioritizedFields' in every occurrence. Additionally, remove 'prioritized_' from the following code snippet.

semantic_config = SemanticConfiguration(
    name="my-semantic-config",
    prioritized_fields=SemanticPrioritizedFields(
        title_field=SemanticField(field_name="HotelName"),
        prioritized_keywords_fields=[SemanticField(field_name="Category")],
        prioritized_content_fields=[SemanticField(field_name="Description")]
    )
)

Updated

semantic_config = SemanticConfiguration(
    name="my-semantic-config",
    prioritized_fields=SemanticPrioritizedFields(
        title_field=SemanticField(field_name="HotelName"),
        keywords_fields=[SemanticField(field_name="Category")],
        content_fields=[SemanticField(field_name="Description")]
    )
)

The SemanticSettings class is not supported in the latest version of the azure-search-documents library. To resolve the issue, replace SemanticSettings with SemanticSearch.

%pip install azure-search-documents --pre
%pip show azure-search-documents
%pip install python-dotenv

import os
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents import SearchClient
from azure.search.documents.indexes.models import (  
    SearchIndex,  
    SearchField,  
    SearchFieldDataType,  
    SimpleField,  
    SearchableField,
    ComplexField,
    SearchIndex,  
    SemanticConfiguration,  
    SemanticPrioritizedFields,  
    SemanticField,  
    SemanticSearch,  
)

Also, replace SemanticSettings with SemanticSearch throughout your code.

semantic_settings = SemanticSettings(configurations=[semantic_config])

Updated

semantic_settings = SemanticSearch(configurations=[semantic_config])

Also, replace semantic_settings with semantic_search in the code snippet provided below.

index = SearchIndex(
    name=name,
    fields=fields,
    semantic_settings=semantic_settings,
    scoring_profiles=scoring_profiles,
    suggesters = suggester)

Updated

index = SearchIndex(
    name=name,
    fields=fields,
    semantic_search=semantic_settings,
    scoring_profiles=scoring_profiles,
    suggesters = suggester)

Now you can execute the Semantic Search demo without any issues, you may receive a excpetion related to enabling the semantic ranking for your AI Search service, refer to Azure AI Search: Semantic search is not enabled for this service to resolve the issue.

No comments:

Post a Comment