Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Azure AI Search provides relevance tuning strategies for improving the relevance of search results in classic RAG solutions. Relevance tuning can be an important factor in delivering a RAG solution that meets user expectations.
Note
We now recommend agentic retrieval for RAG workflows, but classic RAG is simpler. If it meets your application requirements, it's still a good choice.
In Azure AI Search, relevance tuning includes L2 semantic ranking and scoring profiles. To implement these capabilities, you revisit the index schema to add configurations for semantic ranking and scoring profiles. You then rerun the queries using the new constructs.
In this tutorial, you modify the existing search index and queries to use:
- L2 semantic ranking
- Scoring profile for document boosting
This tutorial updates the search index created by the indexing pipeline. Updates don't affect the existing content, so no rebuild is necessary and you don't need to rerun the indexer.
Prerequisites
Visual Studio Code with the Python extension and the Jupyter package.
Azure AI Search, Basic tier or higher for managed identity and semantic ranking.
Download the sample
The sample notebook includes an updated index and query request.
Update the index for semantic ranking and scoring profiles
In a previous tutorial, you designed an index schema for RAG workloads. We purposely omitted relevance enhancements from that schema so that you could focus on the fundamentals. Deferring relevance to a separate exercise gives you a before-and-after comparison of the quality of search results after the updates are made.
Update the import statements to include classes for semantic ranking and scoring profiles.
from azure.identity import DefaultAzureCredential from azure.identity import get_bearer_token_provider from azure.search.documents.indexes import SearchIndexClient from azure.search.documents.indexes.models import ( SearchField, SearchFieldDataType, VectorSearch, HnswAlgorithmConfiguration, VectorSearchProfile, SearchIndex, SemanticConfiguration, SemanticPrioritizedFields, SemanticField, SemanticSearch, ScoringProfile, TagScoringFunction, TagScoringParameters )Add the following semantic configuration to the search index. This example can be found in the update schema step in the notebook.
# New semantic configuration semantic_config = SemanticConfiguration( name="my-semantic-config", prioritized_fields=SemanticPrioritizedFields( title_field=SemanticField(field_name="title"), keywords_fields=[SemanticField(field_name="locations")], content_fields=[SemanticField(field_name="chunk")] ) ) # Create the semantic settings with the configuration semantic_search = SemanticSearch(configurations=[semantic_config])A semantic configuration has a name and a prioritized list of fields to help optimize the inputs to semantic ranker. For more information, see Configure semantic ranking.
Next, add a scoring profile definition. As with semantic configuration, a scoring profile can be added to an index schema at any time. This example is also in the update schema step in the notebook, following the semantic configuration.
# New scoring profile scoring_profiles = [ ScoringProfile( name="my-scoring-profile", functions=[ TagScoringFunction( field_name="locations", boost=5.0, parameters=TagScoringParameters( tags_parameter="tags", ), ) ] ) ]This profile uses the tag function which boosts the scores of documents where a match was found in the locations field. Recall that the search index has a vector field, and multiple nonvector fields for title, chunks, and locations. The locations field is a string collection, and string collections can be boosted using the tags function in a scoring profile. For more information, see Add a scoring profile and Enhancing Search Relevance with Document Boosting (blog post).
Update the index definition on the search service.
# Update the search index with the semantic configuration index = SearchIndex(name=index_name, fields=fields, vector_search=vector_search, semantic_search=semantic_search, scoring_profiles=scoring_profiles) result = index_client.create_or_update_index(index) print(f"{result.name} updated")