ChatGPT is a chatbot platform that is designed to engage with users in conversations. It utilizes Artificial Intelligence and Natural Language processing to understand users’ queries and respond in the most appropriate way possible. ChatGPT has been programmed to provide useful and accurate information to help users find answers to their questions. Source: ChatGPT 😅

This blog explains the prerequisites for integrating ChatGPT in SAP, testing ChatGPT in POSTMAN, and implementing a POST call to ChatGPT to send a question and get a response from ChatGPT.

Prerequisites:

  1. Sign into Open AI
  2. Create New Secret Key
  3. Uploading of Open AI SSL certificates in SAP using Tcode: STRUST

 

For testing purposes, fork the OpenAI collection in Postman. Choose ‘Create chat message completion’ from the OpenAI directory tree as shown below. This will help us get a sample code snippet that can be later converted into ABAP.

Create%20Chat%20Message%20Completion

Create Chat Message Completion

As shown in the above JSON body, role = ‘user’ is a user with a question in the content field, and role = ‘assistant’ is a ChatGPT with a response in the content field. Now let’s see the response to the question “Where was it played?”.

ChatGPT%20Response

ChatGPT Response

Now, let’s create a new program in SAP using Tcode SE38 and write the below code. Replace the secret key with the key you have generated, remaining code remains the same. Below are the steps implemented.

1. Getting questions from the selection screen.

2. Create URL using cl_http_client=>create_by_url. Here, pass the URL as : “https://api.openai.com/v1/chat/completions”.

3. Setting header fields like content-type, accept, and, most importantly, authorization, where a secret key needs to be passed.

4. Generating a payload and converting it into an Xstring.

5. Attaching a payload to the set_data method.

6. Setting the POST method using set_method.

7. Call the send method to trigger the POST call.

8. If sy-subrc = 0, call the receive method.

9. If sy-subrc = 0, deserialize the JSON and display the data.

REPORT zchatgpt.
DATA: lv_json_data TYPE string,
      lr_data      TYPE REF TO data.

FIELD-SYMBOLS:
  <data>        TYPE data,
  <data1>       TYPE data,
  <results>     TYPE any,
  <structure>   TYPE any,
  <structure1>  TYPE any,
  <table>       TYPE ANY TABLE,
  <field>       TYPE any,
  <field1>      TYPE any,
  <field_value> TYPE data.

DATA :lo_client    TYPE REF TO if_http_client,
      lv_payload   TYPE string,
      lv_payload_x TYPE xstring,
      lv_response  TYPE string.

SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-000.

  PARAMETERS: P_ques TYPE char100 OBLIGATORY.

SELECTION-SCREEN: END OF BLOCK b1.

START-OF-SELECTION.

  cl_http_client=>create_by_url(
  EXPORTING
  url = 'https://api.openai.com/v1/chat/completions'
  IMPORTING
  client = lo_client
  EXCEPTIONS
  argument_not_found = 1
  plugin_not_active = 2
  internal_error = 3
  ).

  IF sy-subrc = 0.
    lo_client->request->set_version( if_http_request=>co_protocol_version_1_0 ).

    lo_client->request->set_header_field(
    EXPORTING
    name = 'Content-Type'
    value = 'application/json' ).

    lo_client->request->set_header_field(
    EXPORTING
    name = 'Accept'
    value = 'application/json' ).

    lo_client->request->set_header_field(
    EXPORTING
    name = 'Authorization'
    value = 'Bearer 'Replace your secret key'' ).

    lv_payload = '{"model":"gpt-3.5-turbo","messages":[{"role":"user","content":"'.
    CONCATENATE lv_payload p_ques INTO lv_payload.
    CONCATENATE lv_payload
                '"}],"temperature":1,"top_p":1,"n":1,"stream":false,"max_tokens":250,"presence_penalty":0,"frequency_penalty":0}' INTO lv_payload.

    CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
      EXPORTING
        text   = lv_payload
      IMPORTING
        buffer = lv_payload_x.

    lo_client->request->set_data( lv_payload_x ).

    lo_client->request->set_method( 'POST' ).

    lo_client->send(
    EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state = 2
    http_processing_failed = 3
    http_invalid_timeout = 4

    ).

    IF sy-subrc = 0.
      lo_client->receive(
      EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state = 2
      http_processing_failed = 3 ).

      lv_json_data = lo_client->response->get_cdata( ).

      CALL METHOD /ui2/cl_json=>deserialize
        EXPORTING
          json         = lv_json_data
          pretty_name  = /ui2/cl_json=>pretty_mode-user
          assoc_arrays = abap_true
        CHANGING
          data         = lr_data.

      IF lr_data IS BOUND.
        ASSIGN lr_data->* TO <data>.
        ASSIGN COMPONENT 'CHOICES' OF STRUCTURE <data> TO <results>.
        ASSIGN <results>->* TO <table>.

        LOOP AT <table> ASSIGNING <structure>.
          ASSIGN <structure>->* TO <data>.
          ASSIGN COMPONENT 'MESSAGE' OF STRUCTURE <data> TO <field>.
          IF <field> IS ASSIGNED.
            ASSIGN <field>->* TO <data1>.
            ASSIGN COMPONENT 'CONTENT' OF STRUCTURE <data1> TO <field1>.

            lr_data = <field1>.
            ASSIGN lr_data->* TO <field_value>.

            CALL FUNCTION 'RSPLS_LONG_STRING_DISPLAY'
              EXPORTING
                i_trace_string       = <field_value>
                      .
          ENDIF.
          UNASSIGN: <field>, <field_value>.
        ENDLOOP.
      ENDIF.
    ENDIF.
  ENDIF.

Finally, we are ready to test the ChatGPT magic.

Question

Question

Answer

Let’s move on to a complex question.

Question

Question

Answer

Answer

 

In this way, we can use ChatGPT to search queries, articles,sample codes, and whatnot, which will make everyone’s life easy. I am still not sure about the overall accuracy of ChatGPT but still very useful.

I’m looking forward to hearing more about SAP-ChatGPT integration. Feel free to comment and discuss.

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x