ChatGPT using SAP CPI

To the beginners in CPI, for hands-on this blog would be helpful understanding how can we integrate ChatGPT using CPI basic Iflow with steps.

ChatGPT: It’s a chatbot powered by OpenAI’s GPT (Generative Pre-trained Transformer) language model. It’s capable of generating human-like responses to various questions and prompts. It can be integrated into messaging platforms, customer support systems and other apps.

Prerequisites: OpenAI account, SAP CPI, Postman

Step 1:
First, we need to create a secret key on OpenAI website
Create%20Secret%20Key
Step 2: 
To authenticate we need to download certificate provided by OpenAI and upload it into CPI system.
https://api.openai.com/v1/chat/completions – Open this URL in the browser and download the certificate into your desktop.
Download%20Certificate
Download%20Certificate
Download%20Certificate
Step 3: 
Go to CPI Monitor -> Manage Security -> Keystore and the upload the downloaded certificate
Upload%20Certificate
Step 4:
Create the Iflow using HTTPS as sender adapter as we are going to test the Iflow from Postman.
Iflow
  • Content Modifier  Preparing Request Body (Headers)
  • Authorization value is the secret key that we created in Step 1, it should be in the format Bearer {secret key}

Prepare%20Request%20Body%20-%20Headers

  • Content Modifier – Preparing Request Body (Body)

{
    "model": "gpt-3.5-turbo",
    "messages": [
        {
            "role": "user",
            "content": "${in.body}"
        }
    ]
}

Here we are building the body in JSON format to post the request content on ChatGPT API.

  • Request Reply – HTTP adapter Configurations

  • JSON To XML Converter – Converting JSON body received from the ChatGPT API to XML for extracting the content from the required field (using XML format is easier to extract the content from required field)

  • Groovy Script – Extracting the content from required field using groovy script and storing in the body

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.*;

def Message processData(Message message) {
    
    def new_body = new XmlParser().parseText(message.getBody(java.lang.String) as String);
    
    def set_body = new_body.choices.message.content.text();
    
    message.setBody(set_body);
    
    return message;
}

 

Step 5:

Testing the Iflow using Postman

This is a basic example shows how to use ChatGPT with CPI. I could find it using very useful in building Iflows real-time.

Furthermore, we can use ChatGPT for many other use cases like building XSLT mappings, groovy scripts, building UDFs which helps in building Iflows with complex logics.

Happy Learning 😊

Original Article:
https://blogs.sap.com/2023/05/30/chatgpt-using-sap-cpi/

ASK SAP EXPERTS ONLINE
Related blogs

LEAVE A REPLY

Please enter your comment!
Please enter your name here