Table of Contents
Introduction
A general idea of how to integrate ChatGPT with SAP ABAP:
- First, you would need to have access to an instance of the ChatGPT model, either through the OpenAI API or by running the model locally.
- Next, you would need to create an ABAP program that calls the ChatGPT API and passes it input text, such as a user’s question.
- The program would then need to handle the API response and parse the generated text output.
- Finally, the program would need to display the generated text in an appropriate format, such as in an SAP GUI screen or in an output file.
Here’s an example of some code that could be used in an ABAP program to call the OpenAI API and get a response from ChatGPT:
CALL FUNCTION 'HTTP_POST'
EXPORTING
url = 'https://api.openai.com/v1/engines/davinci/completions'
encoding = 'UTF-8'
IMPORTING
response = l_response
CHANGING
data = l_data
headers = l_headers
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
How to Integrate ChatGPT in SAP ABAP
As shown in below image, we will follow three simple steps to integrate ChatGPT APIs within SAP ABAP Report.

1. Get API of Open AI
We have already discussed all the steps involved regarding API creation in this article.
2. Create ABAP Report
Go to SE38, and create a new ABAP Report. Create one Input Parameter as shown below:

3. Integrate ChatGPT Call
Write the given code in your report:
REPORT ZCHATGPT.
PARAMETERS: l_ques type string.
data: l_response type string,
l_data type string,
lv_payload_x type xstring,
l_max_tokens type i.
"l_ques = 'What is the meaning of life?'. EXAMPLE
l_max_tokens = 50.
l_data = '{' && '"prompt":' && '"' && l_ques && '",' && '"max_tokens":' && l_max_tokens && '}'.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = l_data
IMPORTING
buffer = lv_payload_x.
DATA: lo_http_client TYPE REF TO if_http_client.
DATA: response TYPE string,
lv_auth TYPE string.
CONSTANTS : lv_url TYPE string VALUE 'https://api.openai.com/v1/engines/davinci/completions'.
"create HTTP client by url
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
"error handling
ENDIF.
"setting request method
lo_http_client->request->set_method('POST').
"adding headers
lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/x-www-form-urlencoded' ).
lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).
lo_http_client->request->set_header_field( name = 'Authorization' value = 'Bearer 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' ).
lo_http_client->request->set_form_field(
EXPORTING
name = 'prompt' " Name of form field
value = l_ques ).
"lo_http_client->request->set_data( lv_payload_x ).
"Available Security Schemes for productive API Endpoints
"OAuth 2.0
CALL METHOD lo_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.
IF sy-subrc = 0.
CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 5.
ENDIF.
IF sy-subrc <> 0.
"error handling
response = lo_http_client->response->get_cdata( ).
l_response = response.
ELSE.
response = lo_http_client->response->get_cdata( ).
IF response IS NOT INITIAL.
l_response = response.
ELSE.
l_response = 'Call was successful, but got no response'.
ENDIF.
ENDIF.
WRITE l_response.
Code Explanation
Here, I have added variables l_question and l_max_tokens to hold the user’s question and the maximum token value, respectively. I have then added these values to the l_data variable, which is passed as the data parameter in the HTTP_POST call. The response variable l_response will hold the generated text as a result of the API call.
Here, I have created an object of class if_http_client and added the headers using the add_header_field method. Then i have passed this object as a parameter ‘manager’ in the ‘HTTP_POST’ call.
It’s important to note that the format of the data passed to the OpenAI API must be in JSON format, so that’s why I have added the prompt and max_tokens in JSON format in the l_data variable.
Remember to replace <YOUR_API_KEY> with the API key that you have generated using the Open AI platform.
Output Screen

Tutorial Video
You can watch the video below to learn implementation:
[embedyt] https://www.youtube.com/watch?v=VvEO2MDpdAc[/embedyt]
Leave a Reply