Delen via


Zelfstudie: Relevantie maximaliseren (RAG in Azure AI Search)

In deze zelfstudie leert u hoe u de relevantie van zoekresultaten die worden gebruikt in RAG-oplossingen kunt verbeteren. Relevantieafstemming kan een belangrijke factor zijn bij het leveren van een RAG-oplossing die voldoet aan de verwachtingen van de gebruiker. In Azure AI Search omvat relevantieafstemming L2 semantische classificatie- en scoreprofielen.

Als u deze mogelijkheden wilt implementeren, gaat u terug naar het indexschema om configuraties toe te voegen voor semantische classificatie- en scoreprofielen. Vervolgens voert u de query's opnieuw uit met behulp van de nieuwe constructies.

In deze zelfstudie wijzigt u de bestaande zoekindex en query's voor gebruik:

  • L2 semantische rangschikking
  • Scoreprofiel voor documentverbetering

In deze zelfstudie wordt de zoekindex bijgewerkt die is gemaakt door de indexeringspijplijn. Updates hebben geen invloed op de bestaande inhoud, dus er is geen herbouw nodig en u hoeft de indexeerfunctie niet opnieuw uit te voeren.

Notitie

Er zijn meer relevantiefuncties in preview, waaronder vectorquerygewicht en het instellen van minimale drempelwaarden, maar we laten ze weg uit deze zelfstudie omdat ze in preview zijn.

Vereisten

Het voorbeeld downloaden

Het voorbeeldnotebook bevat een bijgewerkte index en queryaanvraag.

Een basislijnquery uitvoeren voor vergelijking

Laten we beginnen met een nieuwe query: 'Zijn er wolkformaties specifiek voor oceanen en grote waterlichaampjes?'

Als u resultaten wilt vergelijken nadat u relevantiefuncties hebt toegevoegd, voert u de query uit op het bestaande indexschema voordat u semantische rangschikking of een scoreprofiel toevoegt.

Wijzig voor de Azure Government-cloud het API-eindpunt van de tokenprovider in "https://cognitiveservices.azure.us/.default".

from azure.search.documents import SearchClient
from openai import AzureOpenAI

token_provider = get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
openai_client = AzureOpenAI(
     api_version="2024-06-01",
     azure_endpoint=AZURE_OPENAI_ACCOUNT,
     azure_ad_token_provider=token_provider
 )

deployment_name = "gpt-4o"

search_client = SearchClient(
     endpoint=AZURE_SEARCH_SERVICE,
     index_name=index_name,
     credential=credential
 )

GROUNDED_PROMPT="""
You are an AI assistant that helps users learn from the information found in the source material.
Answer the query using only the sources provided below.
Use bullets if the answer has multiple points.
If the answer is longer than 3 sentences, provide a summary.
Answer ONLY with the facts listed in the list of sources below. Cite your source when you answer the question
If there isn't enough information below, say you don't know.
Do not generate answers that don't use the sources below.
Query: {query}
Sources:\n{sources}
"""

# Focused query on cloud formations and bodies of water
query="Are there any cloud formations specific to oceans and large bodies of water?"
vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields="text_vector")

search_results = search_client.search(
    search_text=query,
    vector_queries= [vector_query],
    select=["title", "chunk", "locations"],
    top=5,
)

sources_formatted = "=================\n".join([f'TITLE: {document["title"]}, CONTENT: {document["chunk"]}, LOCATIONS: {document["locations"]}' for document in search_results])

response = openai_client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": GROUNDED_PROMPT.format(query=query, sources=sources_formatted)
        }
    ],
    model=deployment_name
)

print(response.choices[0].message.content)

Uitvoer van deze aanvraag kan eruitzien als in het volgende voorbeeld.

Yes, there are cloud formations specific to oceans and large bodies of water. 
A notable example is "cloud streets," which are parallel rows of clouds that form over 
the Bering Strait in the Arctic Ocean. These cloud streets occur when wind blows from 
a cold surface like sea ice over warmer, moister air near the open ocean, leading to 
the formation of spinning air cylinders. Clouds form along the upward cycle of these cylinders, 
while skies remain clear along the downward cycle (Source: page-21.pdf).

De index voor semantische classificatie- en scoreprofielen bijwerken

In een vorige zelfstudie hebt u een indexschema ontworpen voor RAG-workloads. We hebben met opzet relevantieverbeteringen uit dat schema weggelaten, zodat u zich kunt richten op de basisprincipes. Als u de relevantie voor een afzonderlijke oefening uitstellen, krijgt u een voor-en-na-vergelijking van de kwaliteit van zoekresultaten nadat de updates zijn doorgevoerd.

  1. Werk de importinstructies bij om klassen op te nemen voor semantische classificatie- en scoreprofielen.

     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,
         AzureOpenAIVectorizer,
         AzureOpenAIVectorizerParameters,
         SearchIndex,
         SemanticConfiguration,
         SemanticPrioritizedFields,
         SemanticField,
         SemanticSearch,
         ScoringProfile,
         TagScoringFunction,
         TagScoringParameters
     )
    
  2. Voeg de volgende semantische configuratie toe aan de zoekindex. Dit voorbeeld vindt u in de stap schema bijwerken in het 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])
    

    Een semantische configuratie heeft een naam en een lijst met velden met prioriteit om de invoer te optimaliseren voor semantische rangschikking. Zie Semantische rangschikking configureren voor meer informatie.

  3. Voeg vervolgens een scoreprofieldefinitie toe. Net als bij semantische configuratie kan een scoreprofiel op elk gewenst moment worden toegevoegd aan een indexschema. Dit voorbeeld bevindt zich ook in de stap schema bijwerken in het notebook, na de semantische configuratie.

    # New scoring profile
    scoring_profiles = [  
        ScoringProfile(  
            name="my-scoring-profile",
            functions=[
                TagScoringFunction(  
                    field_name="locations",  
                    boost=5.0,  
                    parameters=TagScoringParameters(  
                        tags_parameter="tags",  
                    ),  
                ) 
            ]
        )
    ]
    

    Dit profiel maakt gebruik van de tagfunctie waarmee de scores van documenten worden verhoogd waarbij een overeenkomst is gevonden in het locatieveld. Zoals u weet, heeft de zoekindex een vectorveld en meerdere niet-vectorvelden voor titel, segmenten en locaties. Het veld Locaties is een tekenreeksverzameling en tekenreeksverzamelingen kunnen worden verhoogd met behulp van de functie Tags in een scoreprofiel. Zie Een scoreprofiel toevoegen en Relevantie voor zoeken verbeteren met Document Boosting (blogbericht) voor meer informatie.

  4. Werk de indexdefinitie voor de zoekservice bij.

    # 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")  
    

Query's bijwerken voor semantische classificatie- en scoreprofielen

In een vorige zelfstudie hebt u query's uitgevoerd die worden uitgevoerd op de zoekmachine, waarbij het antwoord en andere informatie worden doorgegeven aan een LLM voor het voltooien van de chat.

In dit voorbeeld wordt de queryaanvraag gewijzigd om de semantische configuratie en het scoreprofiel op te nemen.

Wijzig voor de Azure Government-cloud het API-eindpunt van de tokenprovider in "https://cognitiveservices.azure.us/.default".

# Import libraries
from azure.search.documents import SearchClient
from openai import AzureOpenAI

token_provider = get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
openai_client = AzureOpenAI(
     api_version="2024-06-01",
     azure_endpoint=AZURE_OPENAI_ACCOUNT,
     azure_ad_token_provider=token_provider
 )

deployment_name = "gpt-4o"

search_client = SearchClient(
     endpoint=AZURE_SEARCH_SERVICE,
     index_name=index_name,
     credential=credential
 )

# Prompt is unchanged in this update
GROUNDED_PROMPT="""
You are an AI assistant that helps users learn from the information found in the source material.
Answer the query using only the sources provided below.
Use bullets if the answer has multiple points.
If the answer is longer than 3 sentences, provide a summary.
Answer ONLY with the facts listed in the list of sources below.
If there isn't enough information below, say you don't know.
Do not generate answers that don't use the sources below.
Query: {query}
Sources:\n{sources}
"""

# Queries are unchanged in this update
query="Are there any cloud formations specific to oceans and large bodies of water?"
vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields="text_vector")

# Add query_type semantic and semantic_configuration_name
# Add scoring_profile and scoring_parameters
search_results = search_client.search(
    query_type="semantic",
    semantic_configuration_name="my-semantic-config",
    scoring_profile="my-scoring-profile",
    scoring_parameters=["tags-ocean, 'sea surface', seas, surface"],
    search_text=query,
    vector_queries= [vector_query],
    select="title, chunk, locations",
    top=5,
)
sources_formatted = "=================\n".join([f'TITLE: {document["title"]}, CONTENT: {document["chunk"]}, LOCATIONS: {document["locations"]}' for document in search_results])

response = openai_client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": GROUNDED_PROMPT.format(query=query, sources=sources_formatted)
        }
    ],
    model=deployment_name
)

print(response.choices[0].message.content)

Uitvoer van een semantisch gerangschikte en versterkte query kan eruitzien als in het volgende voorbeeld.

Yes, there are specific cloud formations influenced by oceans and large bodies of water:

- **Stratus Clouds Over Icebergs**: Low stratus clouds can frame holes over icebergs, 
such as Iceberg A-56 in the South Atlantic Ocean, likely due to thermal instability caused 
by the iceberg (source: page-39.pdf).

- **Undular Bores**: These are wave structures in the atmosphere created by the collision 
of cool, dry air from a continent with warm, moist air over the ocean, as seen off the 
coast of Mauritania (source: page-23.pdf).

- **Ship Tracks**: These are narrow clouds formed by water vapor condensing around tiny 
particles from ship exhaust. They are observed over the oceans, such as in the Pacific Ocean 
off the coast of California (source: page-31.pdf).

These specific formations are influenced by unique interactions between atmospheric conditions 
and the presence of large water bodies or objects within them.

Het toevoegen van semantische classificatie- en scoreprofielen heeft een positieve invloed op het antwoord van de LLM door het promoten van resultaten die voldoen aan scorecriteria en semantisch relevant zijn.

Nu u een beter inzicht hebt in het index- en queryontwerp, gaan we verder met optimaliseren voor snelheid en concisie. We gaan terug naar de schemadefinitie om kwantisatie en opslagreductie te implementeren, maar de rest van de pijplijn en modellen blijven intact.

Volgende stap