Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Task 05: Use generative AI for recommendations

Introduction

Adatum Corporation wants to augment their recommendation capabilities by using generative AI. They plan to use Azure OpenAI Service to deliver more personalized and intelligent product recommendations to their customers.

Description

In this task, you’ll deploy and use the Azure GPT-4.1 model to generate product recommendations. You’ll deploy a GPT-4.1 model using the Azure OpenAI Service, and configure the model deployment, and set up the necessary endpoints. Then, you’ll use the deployed GPT-4.1 model to generate product recommendations based on customer queries. You’ll also invoke the language model to get the response and view the recommended products by executing the SQL statement and analyzing the return message.

Success criteria

  • You successfully deployed the Azure GPT-4.1 model using the Azure OpenAI Service.
  • You configured the model deployment and set up the necessary endpoints.

Learning resources

Key tasks

Azure OpenAI Service

Azure OpenAI Service provides REST API access to OpenAI’s powerful language models including the GPT-4, GPT-4 Turbo with Vision, GPT-3.5-Turbo, and Embeddings model series. In addition, the GPT-4 and GPT-3.5-Turbo model series have now reached general availability. These models can be easily adapted to your specific task, including, but not limited to content generation, summarization, image understanding, semantic search, and natural language to code translation. Users can access the service through REST APIs, Python SDK, or the web-based interface in the Azure OpenAI Studio.

Azure OpenAI Service is powered by a diverse set of models with different capabilities, for example:

Models Description
GPT-4o & GPT-4 Turbo The latest, most capable Azure OpenAI models with multimodal versions, which can accept both text and images as input.
GPT-4 A set of models that improve on GPT-3.5 and can understand and generate natural language and code.
GPT-3.5 A set of models that improve on GPT-3 and can understand and generate natural language and code.
Embeddings A set of models that can convert text into numerical vector form to facilitate text similarity.
DALL-E A series of models that can generate original images from natural language.
Whisper A series of models in preview that can transcribe and translate speech to text.
Text to speech (Preview) A series of models in preview that can synthesize text to speech.

Table 1: Using Azure OpenAI Models

01: Deploy GPT-4.1 Model

  1. Return to the browser that is signed into the Azure AI Foundry | Azure OpenAI Service.

  2. On the left menu, select Shared resources > Deployments.

  3. On the Model deployments tab select + Deploy model > Deploy base model.

    4b.jpg

  4. Select the Inference tasks menu, then search for and select Chat completion.

  5. From the Chat completion options, select gpt-4.1 and then select Confirm.

  6. In the Deploy gpt-4.1 window, select Deploy.

  7. On the Details tab, under Endpoint, enter the following value:

    Default Value
    Target URI: @lab.TextBox(gpt4)
    KEY: @lab.MaskedTextBox(gpt4key)

02: Use GPT-4.1

You want to help customers to find the best set of products for something, for example, to organize a high-school graduation party.

  1. Return to the Visual Studio Code workspace and enter the following in a new query:

    declare @search_text nvarchar(max) = 'help me plan a high school graduation party'
    
    -- Get the search vector for the search text
    declare @search_vector vector(1536)
    exec dbo.create_embeddings @search_text, @search_vector output;
    
    -- Get the top 50 products that are closest to the search vector
    drop table if exists #t;
    with cte as 
    (
        select         
            id, product_name, [description], product_description_vector,        
            row_number() over (partition by product_name order by id ) as rn
        from 
            [dbo].[walmart_product_details]
        where 
            available = 'TRUE'
    ), 
    cte2 as -- remove duplicates
    (
        select 
            *
        from
            cte 
        where
            rn = 1
    )
    select top(50)
        id, product_name, [description],
        vector_distance('cosine', @search_vector, product_description_vector) as distance
    into
        #t
    from 
        cte2
    order by 
        distance;
    
    -- Aggregate the search results to make them easily consumable by the LLM
    declare @search_output nvarchar(max);
    select 
        @search_output = string_agg(cast(t.[id] as varchar(10)) +'=>' + t.[product_name] + '=>' + t.[description], char(13) + char(10))
    from 
        #t as t;
    
    -- Generate the payload for the LLM
    declare @llm_payload nvarchar(max);
    set @llm_payload = 
    json_object(
        'messages': json_array(
                json_object(
                    'role':'system',
                    'content':'
                        You are an awesome AI shopping assistant  tasked with helping users find appropriate items they are looking for the occasion. 
                        You have access to a list of products, each with an ID, product name, and description, provided to you in the format of "Id=>Product=>Description". 
                        When users ask for products for specific occasions, you can leverage this information to provide creative and personalized suggestions. 
                        Your goal is to assist users in planning memorable celebrations using the available products.
                    '
                ),
                json_object(
                    'role':'user',
                    'content': '## Source ##
                        ' + @search_output + '
                        ## End ##
    
                        Your answer needs to be a json object with the following format.
                        {
                            "answer": // the answer to the question, add a source reference to the end of each sentence. Source reference is the product Id.
                            "products": // a comma-separated list of product ids that you used to come up with the answer.
                            "thoughts": // brief thoughts on how you came up with the answer, e.g. what sources you used, what you thought about, etc.
                        }'
                ),
                json_object(
                    'role':'user',
                    'content': + @search_text
                )
        ),
        'max_tokens': 800,
        'temperature': 0.3,
        'frequency_penalty': 0,
        'presence_penalty': 0,
        'top_p': 0.95,
        'stop': null
    );
    
    -- Invoke the LLM to get the response
    declare @retval int, @response nvarchar(max);
    declare @headers nvarchar(300) = N'{"api-key": "@lab.Variable(key)", "content-type": "application/json"}';
    exec @retval = sp_invoke_external_rest_endpoint
        @url = '@lab.Variable(gpt4)',
        @credential = [https://azopenai@lab.LabInstance.Id.openai.azure.com/],
        @method = 'POST',    
        @timeout = 120,
        @payload = @llm_payload,
        @response = @response output;
    select @retval as 'Return Code', @response as 'Response';
    
    -- Get the answer from the response
    select [key], [value] 
    from openjson(( 
        select t.value 
        from openjson(@response, '$.result.choices') c cross apply openjson(c.value, '$.message') t
        where t.[key] = 'content'
    ))
    
  2. Execute the SQL statement.

  3. View the return message and see what the model was able to recommend.

  4. Try different search phrases by replacing the value for the @search_text variable on the first line. For example, for the question:

     I am looking for a gift for my friend who is a foodie. She loves to cook and bake. She is always trying out new recipes and experimenting with different ingredients. I want to get her something that will inspire her creativity in the kitchen. Do you have any suggestions?
    

    Sample response:

    *For your foodie friend who loves to cook and bake, consider the ‘Borage (Borago Officinalis) Glycerite, Organic Dried Flowers and Herb Alcohol-Free Liquid Extract’ which can be a unique addition to her culinary experiments. This herbal supplement is not only a healthful ingredient but also can be used to infuse or decorate dishes, adding a touch of creativity to her kitchen adventures. Source reference: 197. *

  5. Another choice could be:

     The New Whole Foods Encyclopedia, a comprehensive resource for selecting, preparing, and using whole foods, which can inspire new culinary creations.
    

    Sample response:

    The product ‘Foxnut (Euryale Ferox) Glycerite, Organic Seeds Alcohol-Free Liquid Extract’ could be a great addition to your culinary creations inspired by ‘The New Whole Foods Encyclopedia’. This extract is made from organic Foxnut seeds and is free from alcohol, GMO, gluten, artificial colors, heavy metals, preservatives, pesticides, or fertilizers, making it a pure and natural choice for exploring new recipes. Source reference: 18.


Congratulations! You’ve successfully completed this task.