Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
vinod_singh
Participant

Introduction


As we all know, over the past few months, there has been a lot of buzz around OpenAI and ChatGPT. With their advanced natural language processing capabilities, they have the potential to transform the way we do eCommerce. One exciting development is the integration of OpenAI models with SAP Commerce. With the help of OpenAI models, you can automatically generate product descriptions, product summary, images, and videos without the need for additional PIM, CMS, or DAM systems.

This innovative solution not only increases efficiency and reduces costs but also improves customer experiences.

In this blog, we'll explore how OpenAI models can be integrated with SAP Commerce. We'll also discuss the benefits and potential of this integration, including how it can replace traditional PIM, CMS, and DAM tools. Join us on this journey to discover the power of OpenAI integration with SAP Commerce.

 

Prerequisites


Before diving into the details of OpenAI integration with SAP Commerce, readers are requested to have an account created with OpenAI and generate an API key from https://platform.openai.com/account/api-keys link. This API key (openai.api.key) would be required for all the developers to talk to OpenAI.

NOTE: It is crucial to remember that this API key should not be shared with anyone.

Additionally, it is important to note that this blog is not only specific to SAP Commerce, but it can also be applied to any Java-based e-commerce tool or even your own microservices.

I would highly recommend that readers should familiarize themselves with the basics of OpenAI and the available API's.

The OpenAI documentation provides an excellent overview of the platform and the various models and tools available for developers to leverage. To get started, you can visit the Introduction and Overview page and the API Reference page. This will provide you with a solid foundation to explore the full potential of OpenAI's capabilities.

 

Integrate OpenAI with eCommerce Application


As part of Open AI integration you would need this three parameters: API_KEY, API_URL, and PROMPT :

  1. OpenAI API Key is the API key required to access the OpenAI API (openai.api.key),

  2. OpenAI API URL is the URL of the OpenAI API (openai.api.url),

  3. Prompt is the text that will be used as a prompt for the AI model.



Configuration files


project.properties
#OpenAI Key
openai.api.key = <YOUR_GENERATED_OPEN_API_KEY>

#OpenAI Completion API
openai.api.completions.url = https://api.openai.com/v1/completions
openai.api.model = text-davinci-003
openai.api.maxtoken = 600
openai.api.temperature = 0.8

#OpenAI Image API
openai.api.image.url = https://api.openai.com/v1/images/generations
openai.api.number = 2
openai.api.size = 1024x1024

#OpenAI Prompt
openai.prompt.product.description = Write the Product description for
openai.prompt.product.summary = Write the Product Summary for

 

definition.xml


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved.
-->
<action-definition id="com.openai.backoffice.actions.openaieventaction"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.hybris.com/schema/cockpitng/action-definition.xsd">

<name>OpenAI Generate Product Information and Images</name>
<description>Action to trigger OpenAI API to generate Product Details</description>
<author>hybris</author>
<version>1.0</version>

<actionClassName>com.openai.backoffice.actions.OpenAIEventAction</actionClassName>

<inputType>de.hybris.platform.core.model.product.ProductModel</inputType>
<outputType>java.lang.String</outputType>

<iconUri>icons/openai.png</iconUri>

</action-definition>


Open AI Communication


Lets look at the some of examples of Open AI Prompt.


UseCase 1: Replace PIM


Generate Product Summary and Product Description based on Name of the Product using completions API
curl https://api.openai.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "text-davinci-003",
"prompt": "Write the Product description for NP03ZL Middle Zoom Lens",
"max_tokens": 600,
"temperature": 0.8
}'

Corresponding Request Body for Product Information :
String requestBody = String.format("{\"model\": \"%s\", \"prompt\": \"%s\", \"max_tokens\": %d, \"temperature\": %f}", model, prompt, maxTokens, temperature);

 

UseCase 2: Replace DAM


Generate Product Images based on Product Descriptions using Open API Image generation API.
curl https://api.openai.com/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"prompt": "The NP03ZL Middle Zoom Lens is the perfect choice for capturing stunning photos and videos in any environment. This lens has an impressive focal length of 16-50mm and an aperture range of f/3.5-5.6, allowing it to capture sharp, clear images even in low light. Its advanced optical design includes two aspherical elements and two extra-low dispersion elements for excellent sharpness, clarity and color accuracy. Its advanced image stabilization system helps to minimize blur and camera shake, while the silent and fast autofocus system keeps the subject in focus in any situation. With its durable construction and weather-resistant design, the NP03ZL Middle Zoom Lens is a great choice for professionals who demand quality and performance.",
"n": 2,
"size": "1024x1024"
}'

Corresponding Request Body for Image :
String requestBody = String.format("{\"prompt\": \"%s\", \"n\": %d, \"size\": \"%s\"}", prompt, n, size);

 

The request body contains the prompt and other parameters such as the model to be used, max_tokens, and temperature.

Once you have constructed the request body, pass the same to sendOpenAIRequest method as shown below. This method returns the JsonObject response.
private JsonObject sendOpenAIRequest(String apiKey, String url, String requestBody) {
JsonObject result = null;
try {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "Bearer " + apiKey);
byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8);
con.setDoOutput(true);
con.getOutputStream().write(postData);

int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
result = JsonParser.parseString(response.toString()).getAsJsonObject();
}
} else {
LOG.error("OpenAI API returned an error: " + responseCode);
}
} catch (Exception e) {
LOG.error("Error calling OpenAI API", e);
}
return result;
}

Finally, from this JSON Object retrieve the Product Information or Image URL and save it in ProductModel.

 

As you can see OpenAI button on SAP Commerce backoffice is integrated and which can generate Product Description from OpenAI API.

 


SAP Commerce Backoffice with OpenAI Integration


 

Conclusion


Finally, let us look at the demo of the solution explained in this blog. You can see how OpenAI models can be seamlessly integrated with SAP Commerce to generate product descriptions and summaries for given product.



In this blog, we have explored the process of integrating OpenAI with SAP Commerce and the benefits it can bring to the table.

If you have any feedback, ideas, or suggestions, please feel free to leave a comment on this blog. I would love to hear your thoughts and ideas. Moreover, if you need any support in integrating OpenAI into your solution, don't hesitate to contact me.

Thank you for reading and stay tuned for more exciting blogs on the integration of cutting-edge technologies into e-commerce solutions.
3 Comments
Labels in this area