Integration of SAP CPI(BTP IS) with ChatGPT
Introduction
What is ChatGPT?
ChatGPT is a language model developed by OpenAI. It is designed to understand natural language, generate human-like responses to a wide range of questions, and carry out various language-related tasks. It has been trained on a large corpus of text data and can understand a broad range of topics. Its purpose is to assist users in their quest for knowledge and provide them with informative and useful responses.
What is CPI(BTP-IS)?
CPI(BTP-IS) is a set of services and tools provided by SAP on its cloud-based Business Technology Platform (BTP) to enable integration between different systems, applications, and data sources. The key benefit of CPI(BTP IS) is that it enables organizations to quickly and easily integrate their systems, data, and applications without the need for extensive coding or custom development. This helps to streamline business processes, reduce costs, and improve operational efficiency.
How ChatGPT can be integrated?
ChatGPT can be integrated using APIs (Application Programming Interfaces) provided by OpenAI. The API can be used to send text inputs to ChatGPT and receive responses in real-time. Chatbot Platform Integration and Custom Integration using libraries and SDK also possible.
How CPI integrates with ChatGPT?
CPI interacts ChatGPT through HTTP requests using API Keys for authentication. ChatGPT has multiple APIs depending on the usage or self learning basis like Models, Completions, Edits, Images, Create Image Variations, Embeddings, Files, Fine-tunes and Moderations APIs
Note: All the available ChatGPT APIs can be integrated with SAP CPI but for demonstration will use the Completion API.
Integration of SAP CPI(BTP IS) with ChatGPT
Prerequisites:
- SAP BTP IS Tenant Access
- ChatGPT openai platform Access
- Postman Tool for testing
Step-1: Create a new secret key on ChatGPT
After login to the account on https://platform.openai.com/account/api-keys goto API Keys and click on Create new secret key.
Step-2: Download the security certificate from respective ChatGPT API
For demo we will use the API “ https://api.openai.com/v1/completions” from other available APIs of ChatGPT.
Open the API url in any browser and click on the lock(HTTPS) icon from the address bar and select show certificate.
Export the root certificate and save to your local desktop.
Step-3: Upload the root security certificate in the SAP BTP IS(CPI)=>KeyStore
Login to BTP account and redirect to your Integration Suite home page. From the left side pane select the monitor artifacts options and open the Keystore page.
Click on Add dropdown option from right side top pane and select the Certificate option and upload the already downloaded security certificate from Step-1.
Step-4: Create a new Integration Flow under the desired package with appropriate naming convention.
From the left side pane design artifacts option select or create a package and create a new IFLOW.
We need to create a scenario of HTTPS sender adapter to HTTP receiver adapter with intermediate call on chatGPT API .
Step-5: Configure a sender HTTPS adapter as we will test it from Postman tool.
As per the requirement we can have any sender adapter which can able to provide the expected input text.
Provide the desired Address path which will be added to the deployed tenant URL.
Step-6: Configure the JSON to XML converter as the input text from Postman will be provided as JSON.
As per the requirement we can have any format input can be sent from sender system which can be modified accordingly.
Remove the Namespace Mapping if selected.
In our case JSON to XML only is used as input from POSTMAN will be JSON and for further processing of exchange properties we will use XML .
Step-7: Configure the Content Modifier to set the Message Header, Exchange Property and Message Body.
The ChatGPT HTTP API will be called with Authorization and ContentType values in the Message Header
So accordingly provide the Header constant in the Content Modifier with Content-Type as application/json
And Authorization will be the Bearer (space) API key generated in the step-1
The sample input from source will be in the below JSON format
{
“prompt”: “Input Text here”
}
In order to capture the Input “Prompt” value and prepare complete request to API the exchange property will be configured with a “input” referring to the Xpath of the JSON converted XML field value.
The original API HTTP call will be expecting the actual source input with its respective parameters. Those parameters and source input will be formulated in the Message Body.
Each ChatGPT API call will be processed based on the model name provided. In our case will provide the latest model “text-davinci-003” which is the most capable GPT-3 model. Can do any task the other models can do, often with higher quality, longer output and better instruction-following. And supports inserting completions within text. Also its trained with the internet content available upto Jun2021.
Step-8: Configure a Request-reply with HTTP adapter receiver connection to ChatGPT API.
Provide the mandatory field values like Address of the ChatGPT API, Method as POST, Authentication as Client Certificate and the Request/Response headers. The Request headers can be given as * or Authorization|Content-Type etc.
Step-9: Handling the API JSON response.
When any ChatGPT API is invoked with JSON request then there will be a JSON response which can be converted to XML to produce the desired field output.
Configure the JSON to XML converter to select the respective XML field values to be populated in the output.
Step-10: Configure the Content Modifier for converted XML payload.
Create a “output” property referring to the XML text field which will be only populated as final output.
Modify the Message body also with the selected property value.
Final Integration Flow will look like:
Once the IFLOW is saved and deployed , go to the Managed Integration Content and get the endpoint which will be used in the source system to trigger a message to CPI.
In order to view the traces in the CPI monitor message processing. Select the log level as Trace instead of default option Info.
Testing in POSTMAN:
Provide the CPI tenant endpoint URL for the created IFLOW along with the BTP account credentials in the Postman Authorization section.
For the sample input provided we have received the response accordingly from the chatGPT API.
Monitoring the IFLOW from CPI tenant with the trace enabled.