Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Invoke Imagen models from SAP ABAP applications

2025-04-14_23-03-15.jpg

 Introduction

The world of SAP ABAP development is often associated with robust business logic, data processing, and enterprise resource planning. However, the increasing demand for richer user experiences and automated content generation opens up exciting new possibilities. One such frontier is the integration of generative AI models.

At Google Cloud Next 2025 we saw how Google’s Imagen models on Vertex AI can transform a user's imagination into high quality visual assets using AI generation, in seconds. In this blog post, we'll delve into the technical aspects of invoking Google Cloud's powerful Imagen models directly from your SAP ABAP applications to realise AI powered image generation and visualisation use cases for SAP customers.

Why Integrate Imagen with ABAP?

While ABAP excels in transactional processing, it traditionally lacks native capabilities for generating sophisticated visual content. Integrating with Imagen unlocks a plethora of use cases, including:

  • Dynamic Product Image Generation: Automatically create visuals for new product listings based on master data.
  • Personalized Marketing Materials: Generate tailored images for customer communications directly within SAP CRM.
  • Enhanced Reporting and Dashboards: Incorporate visually appealing charts and diagrams generated on-the-fly.
  • Streamlined Content Creation: Automate the production of images for training materials, documentation, and internal communications.
  • Improved User Experience: Embed relevant and engaging visuals directly within SAP Fiori applications.

Prerequisites

To successfully invoke Imagen models from ABAP, make sure:

  1. You have a Google Cloud account and project. Please keep the Project Id with you which is available in Google Cloud Dashboard.
  2. Billing is enabled for your project. See how to confirm that billing is enabled for your project.
  3. Vertex AI API is enabled for your GCP project.
  4. You have the latest version of ABAP SDK for Google Cloud installed and configured to invoke Vertex AI API in your SAP system (installation and configuration guide).

ABAP SDK for Google Cloud brings the power of Google Cloud to SAP developers in the programming language of their choice - ABAP. The SDK is available as a set of client libraries in the form of ABAP classes. Using these classes, ABAP developers can connect to and use Google Cloud APIs.

Invoke Imagen models from ABAP applications

We would be using class /GOOG/CL_AIPLATFORM_V1 of the SDK to invoke Imagen models on Vertex AI platform. You can invoke Imagen with a prompt to,

  • get the generated image (as prompted) in raw file format, or 
  • place the generated image in your choice of GCS folder.

Below example places the generated image in a GCS folder.

TYPES:
  BEGIN OF ty_instance,
    prompt TYPE string,

  END OF ty_instance,

  tt_instances TYPE STANDARD TABLE OF ty_instance,

  BEGIN OF ty_parameters,
    sample_count TYPE i,
    aspect_ratio TYPE string,
    storage_uri  TYPE string,

  END OF ty_parameters.

DATA:
  ls_input      TYPE /goog/cl_aiplatform_v1=>ty_001,
  ls_instance   TYPE ty_instance,
  lt_instances  TYPE tt_instances,
  ls_parameters TYPE ty_parameters,
  lv_raw        TYPE string.

TRY.

* Open HTTP Connection
    DATA(lo_client) = NEW /goog/cl_aiplatform_v1( iv_key_name = '<CLIENT_KEY>' ).

    ls_instance-prompt = '<INPUT_PROMPT>'.
    APPEND ls_instance TO lt_instances.
    CLEAR ls_instance.

    GET REFERENCE OF lt_instances INTO ls_input-instances.

    ls_parameters-sample_count = <SAMPLE_COUNT>.
    ls_parameters-storage_uri = '<GCS_URI>'.
    GET REFERENCE OF ls_parameters INTO ls_input-parameters.

* Call API method: aiplatform.projects.locations.publishers.models.predict
    CALL METHOD lo_client->predict_models
      EXPORTING
        iv_p_projects_id   = '<GCP_PROJECT_NAME>
        iv_p_locations_id  = '<LOCATION_ID>'
        iv_p_publishers_id = 'google'
        iv_p_models_id     = '<IMAGEN_MODEL_ID>'
        is_input           = ls_input
      IMPORTING
        es_raw             = lv_raw
        es_output          = DATA(ls_output)
        ev_ret_code        = DATA(lv_ret_code)
        ev_err_text        = DATA(lv_err_text)
        es_err_resp        = DATA(ls_err_resp).
    IF lo_client->is_success( lv_ret_code ).
      cl_demo_output=>display( ls_output-predictions ).
    ELSE.
      MESSAGE lv_err_text TYPE 'E'.
    ENDIF.

* Close HTTP Connection
    lo_client->close( ).

  CATCH /goog/cx_sdk INTO DATA(lo_exception).
    MESSAGE lo_exception->get_text( ) TYPE 'E'.

ENDTRY.

Replace,

  • CLIENT_KEY with the client key configured in ABAP SDK configuration table /GOOG/CLIENT_KEY as per instructions here,
  • INPUT_PROMPT with the text based prompt to instruct Imagen model to generate the image,
  • SAMPLE_COUNT with the number of images that you would like to generate for the prompt,
  • GCS_URI with the target GCS folder URI where you want the generated images to be placed,
  • GCP_PROJECT_NAME with your Google Cloud Project name where Vertex AI API is enabled,
  • LOCATION_ID with the region of your Google Cloud project,
  • IMAGEN_MODEL_ID with the Imagen model ID that you would like to invoke, select one from the list here.

An example invocation

Below is an example invocation of the code snippet when run with below parameter values,

  • INPUT_PROMPT: ‘Generate an image of a toy car with Google Cloud logo’,
  • SAMPLE_COUNT: 2,
  • GCS_URI: ‘gs://imagen-images/,’
  • IMAGEN_MODEL_ID: ‘imagen-3.0-generate-002’.

CLIENT_KEY, GCP_PROJECT_NAME and LOCATION_ID can be entered based on your configurations and Google Cloud project setup.

Once executed, the generated images are placed at the GCS folder location.2025-04-11_21-53-18.jpg

 

 Here is one of the images generated for reference.

2025-04-11_21-53-57.jpg

Bingo….with few configurations and minimum lines of code, we were able to invoke Imagen models in SAP ABAP using ABAP SDK for Google Cloud.

There are other input parameters that you can pass along with the prompt and "sample count". Refer to the link to the list of parameters that you can define under TY_PARAMETERS and pass in LS_INPUT of the code snippet. 

You can also achieve these features along with image generation using text prompts with Imagen.

Conclusion

ABAP SDK for Google Cloud provides comprehensive tools to integrate Google’s AI with your SAP ABAP applications or ABAP AI Agents. Refer to the blog linked here to show how to invoke Gemini models from your ABAP applications. You can also refer to the blog linked here to invoke Gemini models in free tier without the need of any Google Cloud project.

Let us know your use cases that you would like to explore with Imagen and Gemini by starting a conversation on our Google Cloud Channel.

1 0 160
0 REPLIES 0