Skip to Content
Technical Articles
Author's profile photo Ashutosh Kumar

ChatGPT Integration to Solve Queries

Introduction:

ChatGPT is the new era AI tool to solve queries in an efficient way. In this blog, we will be using chatGPT to solve queries from social media API from SAP Business hub API.

ChatGPT, powered by OpenAI’s language model, is capable of generating human-like text responses. SAP Cloud Platform Integration (CPI) is a robust integration platform that connects various applications and services, enabling seamless data exchange across an enterprise. OpenAI provides chat API that works as chatGPT plugin for external connectors. We will use this chat API from OpenAI in CPI to solve queries.

Pre-requisites:

  • To use openAI API, you must have an account created in openAI.
  • Bearer token needs to be generated from openAI that will be used in CPI forauthorization. The process to generate token is given in the next section of this blog.
  • OpenAI certificate needs to be uploaded in CPI for API to work. The process ofgenerating certificate and uploading it into CPI given in the next section of this blog.

 

Scenario:

The iFlow consists of two APIs: one is from API business hub and other one is openAI chat API. The aim of the iFlow is to get a query from an API(In this case, it is social media activity API from API business hub) and send the query text to the openAI chat API and get the query solved using AI.

This iFlow will provide customers with real-time, AI-driven responses to their queries, improving user experiences. Also this will Utilize AI-generated insights for decision- making through the integration of ChatGPT’s analytical capabilities.

 

Solution:

The iFlow is divided into two parts: first we will fetch the data from business hub API and filter out the query part and; In the second part we will integrate this data with openAI chat API and get the queries solved.

In the integration flow, we will include the API key for authentication for both APIs, use message filter to extract relevant information from the response and send the data to mail via mail adapter. First we will filter query from business hub API and after filtering we will send data to openAI chat API via ProcesDirect. After that, openAI will send response and again we will filter out relevant data from the response, frame it in a beautiful format and send it to mail.

 

IFlow Design:

Iflow%20design

Image 1

 

How to generate Bearer token in OpenAI?

• Create an account in OpenAI
• Click on profile —> go to “View API keys”

 

Image%202

Image 2

 

• Click on “create new secret key” and create your API key.

Image%203

Image 3

 

• Copy and save the generated key that will be used in CPI.

 

 

How to create and upload certificate in CPI?

• Paste this link in your browser : “https://api.openai.com/v1/chat/completions”. • Click on the “lock” button on the left of the link.

Image%204

Image 4

 

Image%205

Image 5

 

Image%206

Image 6

 

Image%207

Image 7

 

• Export certificate with “.cer” extension.

• Open your CPI account. Go to monitor:

Image%208

Image 8

 

• Go to Keystore:

Image%209

Image 9

 

• Click on add —> Certificate:

• Browse and upload the exported certificate.

Image%2010

Image 10

 

 

Integrating social media query from SAP business hub API and Solving the query using OpenAI Chat API

Step 1: Create an iFlow Artifact.

• Go to Design —> Integrations —> Add —> integration Flow

Image%2011

Image 11

 

• Add Details
Give the name of flow and a short description about it.

Image%2012

Image 12

 

Step 2 : Integrating social media business hub API to get query from user and solving the query using chatGPT API.

The integration part will be divided into two parts:

  1. Getting query from social media API from Business hub API based on the name ofthe user.
  2. Solving the query using OpenAI Chat API.

Part 1:

Designing an iFlow to get query from social media business hub API based on the name of the user:

  • Go to link “https://api.sap.com/api/socialmediaactivity/overview”. You will get the idea about the API.
  • Login with your BTP account. Scroll down —> Download EDMX file.

 

Image%2013

Image 13

 

• Go to configuration details and copy sandbox Url. These will be used in SAP CPI.

Image%2014

Image 14

 

• Go to tryout section. Hit Run.

Image%2015

Image 15

 

  • You will get the response as above. The query is inside the “Text” value as highlighted in screenshot above.
  • Now click on “Show API Key”. You will get an API key that will be used in CPI.

Image%2016

Image 16

 

  • Go to the artifact that we created in first step.
  • Connect sender to start with “HTTPS” connection.

Image%2017

Image 17

 

Content Modifier: Add content modifier in the iFlow. This will add APIKey in the header for authorisation and fetch “name” of the user from sender as property.

Image%2018

Image 18

 

Image%2019

Image 19

 

Request Reply Data v2 adapter : Add a request reply and connect it with a Receiver with Data v2 connection. This will fetch social media data from business hub API.

Image%2020

Image 20

 

Image%2021

Image 21

 

Image%2022

Image 22

 

• Go to connection and paste the sandbox URL copied from business hub API.

Image%2023

Image 23

 

• Go to processing. Click “select” in resource path.

Image%2024

Image 24

• Select local EDMX file and upload the downloaded EDMX file from business hub.

Image%2024

Image 25

 

• Go to step 2 and select the highlighted entity.

Image%2026

Image 26

 

• Select “ObjectID”, “SocialMediaMessageAuthor”, “Text”

• Go to step 3 and select “SocialMediaMessageAuthor” and comparison as equal.

Image%2027

Image 27

 

Image%2028

Image 28

 

• Click on finish.

• Add the headers in the data connection as shown in image 29.

Image%2029

Image 29

 

Filter : Add filter in the path. This is to filter out the query part from the data.

Image%2030

Image 30

 

Content Modifier: Add content modifier top set the content type.

Image%2031

Image 31

 

• Now add a receiver and connect it with end message using ProcessDirect and give a unique path name.

Image%2032

Image 32

 

 

Part 2:

Integrating iFlow with chatGPT API and sending the data through adapters. In this case, we will use mail adapter to send the data.

Integration Process: Add a new Integration process in the same artifact.

Image%2033

Image 33

 

Sender: Add a “Sender” in the new Integration flow.

Image%2034

Image 34

 

Start message: Add a “Start Message” and similarly add an “End Message” in the IFlow.

Image%2035

Image 35

 

• Connect sender and start message with process direct connection and give the same path as in above iFlow ProcessDirect.

Image%2036

Image 36

 

Content Modifier: Add a content modifier in the flow with following inputs

  1. Message header to set content type as application/json as openAI API take data in json format only. Also this will authorise openAI API using Bearer token authorisation as given below.
  2. Exchange property to fetch the query from social media API
  3. Message body to frame data in standard required format as suggested in openAI documentation. Refer link for more information.

Image%2037

Image 37

 

Image%2038

Image 38

 

Image%2039

Image 39

 

Request Reply Http Connection: Add request reply and connect it with a receiver using http connection.

Paste the link in the address to use chat feature of openAI : “https://api.openai.com/v1/chat/completions

 

Image%2040

Image 40

 

Json to XML converter: Add a json to xml converter.The data we get from openAI is in json format. To process it further, it should be converted into to xml format as CPI processes data in xml format only.

Image%2041

Image 41

 

• Filter: Add a filter to get the resolution fetched from openAI.

Image%2042

Image 42

 

Content Modifier: Add a content modifier to set content type and get the resolution in beautified way.

Image%2043

Image 43

 

Image%2044

Image 44

 

Image%2045

Image 45

 

Mail Adapter: Now add a receiver and connect it with mail adapter to send the resolved query via mail.

Image%2046

Image 46

 

Image%2047

Image 47

 

Image%2048

Image 48

 

Step 3 : Save and deploy the artifact.

 

Testing the iflow using postman:

Image%2049

Image 49

 

 

Mail received:

 

Image%2050

Image 50

 

In Conclusion:

Integrating ChatGPT with SAP Cloud Platform Integration presents a remarkable opportunity to transform user interactions and business processes. By leveraging the power of AI-driven conversations, organizations can enhance customer experiences, streamline operations, and unlock new avenues of innovation.

In this blog, we explored the steps to integrate ChatGPT with SAP CPI. As technology continues to evolve, this integration showcases the endless potential of AI and integration platforms in creating a smarter, more efficient future.

 

Thanks and Regards,

Ashutosh Kumar

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Paul PINARD
      Paul PINARD

      Hi Ashutosh Kumar,

      Thanks a lot for sharing this great and well-documented tutorial to our AI community!

      Would you be able to add "Artificial Intelligence" as one of the SAP Managed Tags of your blog post (such as SAP API Business Hub) so people can retrieve it more easily through search?

      Author's profile photo Ashutosh Kumar
      Ashutosh Kumar
      Blog Post Author

      Hi Paul PINARD ,

      Thanks for the appreciation.

      I have added the tag "Artificial Intelligence" as per your suggestion. Feel free to share any more thoughts or suggestions you might have - I value your engagement and collaboration!