Skip to Content
Technical Articles
Author's profile photo Bhaskar Sai Shankar

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 😊

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo suramouli koduri
      suramouli koduri

      Nice blog Bhaskar.keep blogging

      Author's profile photo Bhaskar Sai Shankar
      Bhaskar Sai Shankar
      Blog Post Author

      Thank you suramouli

      Author's profile photo Tushar Rajput
      Tushar Rajput

      Hi,

      Will it work for simple questioning only

      As when I asked to Analyse the code or to write an ABAP report, It is unable to do it.

       

      Thanks & Regards

      Tushar

      Author's profile photo Bhaskar Sai Shankar
      Bhaskar Sai Shankar
      Blog Post Author

      Hello Tushar,

      Linearize the request body.

      Regards,

      Bhaskar

      Author's profile photo Avinash Paul Ullamparhti
      Avinash Paul Ullamparhti

      Thanks for the Blog Bhaskar!

      Author's profile photo Richard Nagel
      Richard Nagel

      Can you explain what you fill in in Postman in order to properly execute the iFlow? The POST line must have a proper HTTPS address, as {{host}}{{url_path_HTTPS}} is not understood by Postman ...

       

      thx Richard

      Author's profile photo Bhaskar Sai Shankar
      Bhaskar Sai Shankar
      Blog Post Author

      Hi Richard,

      {{host}}{{url_path_HTTPS}} are the global variables saved within postman.

      {{host}} - holds the host of my trail account & {{url_path_HTTPS}} - holds the path /http.

      Regards,

      Bhaskar