Skip to Content
Technical Articles
Author's profile photo Farooq Ahmed

Integration of SAP CPI(BTP IS) with ChatGPT

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:

  1. SAP BTP IS Tenant Access
  2. ChatGPT openai platform Access
  3. 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.

Image%20source%20from%20my%20account%20on%20platform.openai.com

Image source from my account on platform.openai.com

 

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.

Image%20source%20from%20my%20account%20on%20platform.openai.com

Image source from my account on platform.openai.com

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.

Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

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.

                 Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

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.

                                        Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

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

  Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

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.

Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

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.

Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

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.

                      Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

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.

  Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

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.

Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

Modify the Message body also with the selected property value.

Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

Final Integration Flow will look like:

Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

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.

Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

In order to view the traces in the CPI monitor message processing. Select the log level as Trace instead of default option Info.

Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

Testing in POSTMAN:

Image%20source%20from%20local%20installed%20Postman

Image source from local installed 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.

Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

Image%20source%20from%20my%20trail%20account%20on%20SAP%20BTP%20IS

Image source from my trail account on SAP BTP IS

Conclusion:

Any input text given to the ChatGPT API there will be cutting-edge language models working to understand and generate the response text. With the available ChatGPT APIs we can integrate them with CPI/any middleware to consume and provide more reliable response on top of AI.

If ChatGPT is connected at one side of the SAP CPI(BTP IS) the other side we can have any SAP(ECC, S4/HANA, SuccessFactor,etc.,) or non-SAP systems to achieve the end to end integration.

Disclaimer:

This article is only for informational purpose that CPI can be integrated with the ChatGPT APIs also.

Only a integration of one API has been shown as an example but no limitations in any perspective.

 

Assigned Tags

      17 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo mohan kalla
      mohan kalla

      Thank you for sharing the blog. It was a fascinating and inventive read. I'm curious, have you ever been able to successfully implement the solution? I ask because I keep encountering the same error message, "The MPL ID for the failed message is: AGP6qNuZSFQxp9a98qRrC5Vpro5y," and wonder if you've had similar experiences.

       

       

      Author's profile photo Farooq Ahmed
      Farooq Ahmed
      Blog Post Author

      Hi Mohan,

      Can you please check whether the API Key working and the certificate correctly imported into the keystore. Also make sure that Authorization and Content-Type values should be passed in the header while calling the chatGPT API.

      Thanks,

      Farooq.

      Author's profile photo Nagendra Krishna
      Nagendra Krishna

      Dear Farooq

      Very nice blog neatly presented with a simple example to learn this new topic.  Appreciate your effort in writing this blog.

       

      Thanks,

      Nagendra

       

      Author's profile photo Farooq Ahmed
      Farooq Ahmed
      Blog Post Author

      Thank you very much Nagendra.

       

      Regards,

      Farooq.

       

       

      Author's profile photo JP Hadjiandreou
      JP Hadjiandreou

      Thanks for the blog!  I had some queries around the postman settings for authorization.  Is this meant to be 'basic auth' and should it be the 'S' id that is used e.g. to login to BTP/IS.

      If you can share your email, I can show you exactly what I mean too please.

      Thanks!
      JP

      Author's profile photo Farooq Ahmed
      Farooq Ahmed
      Blog Post Author

      From Postman you can have the basic auth providing the same credentials of BTP cockpit login. If the user doesnt have enough roles then create a role collection with MessagingSend role and assign to your user. Also you can try with a service key using clientid and clientsecret.

      Regards,

      Farooq.

      Author's profile photo Ayub Ahmed
      Ayub Ahmed

      Dear Farooq,

      Thanks for the detailed blog. I try to register to chatGBT to get the API key and API, unfortunately, I am getting a message it is not available in your country(KSA).

      OpenAI's services are not available in your country.

      Regards,
      Ayub Ahmed.

      Author's profile photo Farooq Ahmed
      Farooq Ahmed
      Blog Post Author

      If ChatGPT is not available in your country, you can try the methods below to solve the problem. There should be a suitable method for you.

      Method 1: Use VPN
      Method 2: Use another Web Browser
      Method 3: Use ChatGPT in the Private Mode of Your Web Browser
      Method 4: Contact ChatGPT Support

      Regards,
      Farooq

      Author's profile photo CPI testinstane
      CPI testinstane

      Hi Farroq

       

      First of all I would like to appreciate your efforts for this blog and in detail explanation as well, I tried to replicate the exact composition in my CPI test tenant but getting the below error after performing several test cases. I have mentioned the Header data and maintained Adapter level header properties as *.

      com.sap.it.rt.adapter.http.api.exception.HttpResponseException: An internal server error occured: HTTP operation failed invoking https://api.openai.com/v1/completions with statusCode: 400. The MPL ID for the failed message is : AGP_a9AdRrlJGZ2fRoT225R4IGBW

      {"model": “text-davinci-003”, "prompt": "who is the PM of INDIA"}

      But the issue has been fixed and I am getting proper response with little change. May be this might be useful to others, when we are passing the request it should be arranged in proper JSON like below then getting successful response.

      {
      "model": "text-davinci-003",
      "prompt": "who is the PM of India"
      }

      Regards

      Ch Sudheer

      Author's profile photo JP Hadjiandreou
      JP Hadjiandreou

      Thanks!  I was also getting an error, so it could be something is 'wrong' with this body:

      {
      "model": “text-davinci-003”,
      "prompt": "${property.input}"
      }

      Note, when I ran the above (without the variable), then it was successful:

      {
      "model": "text-davinci-003",
      "prompt": "What is the capital of the moon?"
      }

      Farooq - could you please advise what I 'did wrong'. Any updates would be awesome, this is the last step to get this working 🙂

      Thanks!

      Juan

      Author's profile photo Bhaskar Sai Shankar
      Bhaskar Sai Shankar

      Hello Hadjiandreou,

      Check the quotes -

      “text-davinci-003” (this is wrong)

      "text-davinci-003" (this is correct)

      Thanks,

      Bhaskar

      Author's profile photo JP Hadjiandreou
      JP Hadjiandreou

      Thanks for the inputs!  I don't see the difference in the quotes - can you share you email please and then I can contact you on this please.

       

      Thanks!

      Jose

      Author's profile photo Bhaskar Sai Shankar
      Bhaskar Sai Shankar

      Using "straight quotes" will be considered as a string by the request body (JSON) if you're using it via postman or any testing tool and throws error if you use “inverted quotes”.

      Thanks,

      Bhaskar

      Author's profile photo JP Hadjiandreou
      JP Hadjiandreou

      Thanks so much!

      Author's profile photo Venkatesh Belagam
      Venkatesh Belagam

      Dear Farooq,

      Appreciate your efforts in making an detailed blog. Here is my concern.

      while trying to send request using postman, I am not getting the full answer.
      Ex: If I am asking "add two numbers using java" , it is not giving full answer. not even one full liner. Please let us know how to sort it out.

      I tried using model number *gpt-4* as well, but still no luck.

      Thanks in advance.

      Author's profile photo Ricardo Diaz
      Ricardo Diaz

      Thank you so much for the tutorial.

      I use this guide with the new GTP-3.5 turbo with the "chat completions" api. If someone is trying to do the same you need to make some modifications.

      These are the parts that I modified.

       

      I%20change%20the%20json%20body%20according%20to%20the%20official%20documentation%20for%20the%20completions%20api

      I change the json body according to the official documentation for the chat completions api

       

      update%20the%20address%20for%20the%20completions%20api%20url

      update the address for the chat completions api url

      the%20xml%20is%20different%20for%20the%20completions%20api%20response

      the xml is different for the completions api response

       

      so%20you%20need%20to%20change%20the%20source%20value%20in%20the%20content%20modifier

      so you need to change the source value in the content modifier

       

      That's it, hope this help.

       

      Thank you!

      Author's profile photo ravi chandra kummitha
      ravi chandra kummitha

      Hi Farooq,

      Thank you so much for the detailed post.

      I am totally new to CPI , I followe all the steps that mentined however after deploying it , I dont see the end points. Can you please help me to traoubleshoot the issue.

      Thank you so much !!