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





- 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}
- 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/