paint-brush
Reimagining eCommerce with Assistants - OpenAI GPT-4o Model plus Sitecore OrderCloudby@kautilyaprasad
10,726 reads
10,726 reads

Reimagining eCommerce with Assistants - OpenAI GPT-4o Model plus Sitecore OrderCloud

by Kautilya PrasadSeptember 16th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

OpenAI launched an API that allows you to build an intelligent assistant for your headless eCommerce storefront. The assistant is embedded on your website and offers additional help to customers who need a CoPilot kind of experience on the website. The Assistant API is in Beta version right now and more functionalities are being added to it.
featured image - Reimagining eCommerce with Assistants - OpenAI GPT-4o Model plus Sitecore OrderCloud
Kautilya Prasad HackerNoon profile picture

When you shop at a physical store, you can ask questions about the product to the store associate and the associate can guide you in finding the right product. This article talks about bringing the same experience to Digital Experience. Using OpenAI, you can build an intelligent assistant for your headless eCommerce storefront. The assistant is embedded on your website and offers additional help to customers who need a CoPilot kind of experience on the website.

What is an OpenAI Assistant?

For Non-technical folks, an Assistant is a supercharged chatbot, that can answer your questions and perform actions on the website on your behalf. For technical folks, an Assistant is a retrieval-based intelligent agent that can query information outside of the model and implement vector search to answer the queries by the user and perform actions after identifying the intent of the user using Natural Language Processing.


You can configure instructions for the assistant and based on intelligence it can call different APIs. There is also an assistant playground that you can use to explore the Assistant's capabilities. It is important to know that the Assistant API is in Beta version right now and more functionalities are being added to it.

What are some use cases of Assistants in eCommerce Digital Experience?

Assistants can be used to enhance customer experience, make internal operations more productive for the catalog team, and resolve customer issues on the first contact with customer support.


Some use cases of integrating assistants are:

  1. Offer a CoPilot experience to shoppers

    1. Answer questions about products, assist in searching and narrowing down the search results based on context

    2. Learn from past orders and conversations to create a tailored customer experience


  2. Place Order using Natural Language

    1. Using any language, the customer can add a product to the cart and checkout


  3. Customer Support for Handling Returns

    1. Create RMAs

    2. Offer Coupons

    3. Real-time sentiment analysis and offer Credits


  4. Promotions Management by Catalog Team

    1. Assist in measuring the usage of a promotion

    2. Check if a promotion already exists

    3. Create a promotion using Natural Language Processing


  5. CoPilot for Catalog Team

    1. Assist in checking the price of a certain product

    2. Assist in running reports and returning quick information to the inventory team

    3. Assist in updating price for products


What is Sitecore OrderCloud

Sitecore OrderCloud is a B2B, B2C, B2X commerce and marketplace development platform, with a cloud-based, API-first, headless eCommerce architecture. We built a proof-of-concept (POC) around an AI-powered assistant that talks to your storefront. It can search products, add to cart, present shipping options, add addresses, use credit cards on file, and submit an order. Then we built another proof of concept for accepting a return as well.

How to create an Assistant using Sitecore OrderCloud and OpenAI

Step 1: Create and Configure an assistant in the OpenAI platform

Provide a name for the assistant and select a model.


Step 2: Use prompt engineering to provide instructions to the assistant

These are the instructions that the AI engine will use to determine the intent of the user. Here is a sample instruction.


You are a shopper's assistant. Help me find the best products. Call the function 'SearchProducts' to find various products. Extract relevant information and pass keywords in as the parameter to the function. 
Once you have the results, you should only say you have the results. YOU MUST NOT RESPOND WITH PRODUCTS.

From the results, only products that are matching customer's request, SAVE IN MEMORY ONLY.  For example, do not save indoor speakers when the customer is looking for outdoor speakers. Also, do not save a TV when the customer is searching for speakers

If you don't have any products, politely decline.

Once the product has been selected, guide the user through the checkout process. Our shoppers are very important, treat them with respect. 

If the customer is asking for products that are outside of these category, please mention that we only serve products from the above categories.

Look at the description column of the file provided to find answers about products. After saving product information in your memory, ask them if the customer would like to know the price of any product.
If they say yes, ask for which product they want to get price and then call the function 'GetPrice' with the product id as the parameter.

Ask them if they want to purchase. If they are interested in purchasing one, call the function 'AddToCart' with the product id and quantity as parameter.  

Once the product is added to cart, call the function 'GetCart'. This function returns cart details with and array of LineItems. Show SubTotal, ShippingCost, Tax, PromotionDiscount, CartTotal, ItemCount and from LineItems array, show following: Quantity, UnitPrice, PromotionDiscount, LineTotal, and ProductName.
Then ask the customer, if they are interested in checking out the cart.

If they say yes,  call he function 'StartCheckout'. The function returns the addresses for the checkout process. If there are multiple addresses, ask the customer which address they want to use for checkout. Once they confirm the address,  pass the selected address object to the 'AddBillingAndShipping' function.

After adding the address to the cart, retrieve the shipping methods using the function 'GetShippingMethods' and display the options with the price  to the customer. Ask them to pick the shipping method. After they confirm the shipping method, call the function 'SetShippingMethod' and use the ShipEstimates ID and ShipMethods id of the selected shipping method as the parameter.

After successful selection of shipping method, ask the customer if they would like to check for any eligible promotions. If they say yes, call the function 'ListPromotions'. This will return an array of promotions and only SAVE the ones where AutoApply is false. Furthermore, from the description of the promotion, calculate potential savings based on cart details. Show user each promotion with potential savings.

Ask if they want to apply any of the promotions, if they do, call function 'ApplyPromotion' and pass promoId as parameter. 

After this, retrive the credit card details for the user by calling the function 'GetCreditCards'. This function will return a list of credit cards. You must ALWAYS SHOW ExpirationDate, Last 4 digits of CC and Name on Credit Card. Then, ask them if they want to use one of the credit cards for the order. If they confirm and say yes, call the function 'AddCreditCard' with the selected credit card ID as the parameter. 

Then, confirm with the customer if they want to submit the order. If they say yes, call the function 'CalculateCart'. This function will return the cart totals, YOU MUST show these details to the customer, then Mention that you will now place order for user and call the function 'SubmitOrder' to create an order and you MUST ALAWYS reply with OrderID. 

At any point during the conversation, if the customer wants to remove all items from the cart and start over, call the method 'DeleteCart' and inform the customer that their cart has been reset.

At any point during the conversation, if the customer wants to see the cart and call the method 'GetCart' and inform the customer SubTotal, ShippingCost, Tax, PromotionDiscount, CartTotal, ItemCount and from LineItems array, show following: Quantity, UnitPrice, PromotionDiscount, LineTotal, and ProductName.


Step 3: Create functions for backend API calls to Sitecore OrderCloud

In the prompt engineering instructions, based on the intent of the user, different methods are being called. For example. if the customer wants to add the product to the cart, then a method is called in Sitecore OrderCloud to add the product in context to the cart. Here is a sample function:


{
  "name": "AddToCart",
  "description": "Add a single product to cart",
  "strict": false,
  "parameters": {
    "type": "object",
    "properties": {
      "productID": {
        "type": "string",
        "description": "The product ID of the identified product to buy"
      },
      "quantity": {
        "type": "string",
        "description": "Quantity of product to purchase"
      }
    },
    "required": [
      "productID",
      "quantity"
    ]
  }
}

Step 4: Write code for your assistant and handle API calls, orchestrate data flow between OpenAI and your application

First, generate an API key from the OpenAI portal. An API key is required to communicate securely with OpenAI APIs. You can then pick any programming language of your choice. Start by adding code to create a thread, add messages to the thread, wait for completion, and then return the response to the customer. Also, if you don’t want to use the portal to configure an assistant, you can do it in code as well. Here is a sample code for creating a thread. Once a thread is created, use the thread id to add any messages for the conversation.


 private static async Task<string> CreateNewThreadAsync(HttpClient httpClient)
        {
            #region Create thread            
            var newThreadResponse = await httpClient.PostAsync("v1/threads", new StringContent("",
                                                                                                Encoding.UTF8,
                                                                                               "application/json"));
            Debug.Assert(newThreadResponse != null);
            newThreadResponse.EnsureSuccessStatusCode();
            var newThread = await newThreadResponse.Content.ReadFromJsonAsync<CreateThreadResult>();
            Debug.Assert(newThread != null);
            return newThread.Id;
            #endregion
        }


Then add the implementation of every function that you have configured in the OpenAI Assistant. In this example, once the AI engine determines that the customer wants to add the product to the cart, it will call the Sitecore Ordercloud API to add the product to the cart.


case "AddToCart":
             var addToCartMessage = AddToCart(JsonSerializer.Deserialize<AddToCartArguments>(arguments));
             var message = Newtonsoft.Json.JsonConvert.SerializeObject(addToCartMessage);                                  

             HttpResponseMessage toolOutputResponseAddToCart = await PostResponse(httpClient, threadId, run, message).ConfigureAwait(false);

             Debug.Assert(toolOutputResponseAddToCart != null);
             toolOutputResponseAddToCart.EnsureSuccessStatusCode();
             isToolResponseSubmitted = true;
             max = 15;                                  
             break;


Step 5: Test and refine prompts

Use the playground to test and measure the effectiveness of the assistant before you test the assistant from the website. This eliminates multiple iterations of testing. Check the logs as well on the top right if you want to see the APIs in action, parameters and response.


Step 6: Deploy to your website

Once the assistant has been tested, you can add it to the website so that the customers can use it and experience a tailored assistant.

Challenges

Limited documentation is available right now for the assistants module by OpenAI. The program is in beta so support is driven by the community. Developers have to be good at prompt engineering. I have provided some code snippets that can be useful in your project.

Conclusion

We could integrate OpenAPI with Sitecore OrderCloud without any complexity. OpenAI provides a very powerful assistant feature that can be used to enhance the digital experience.

References