Google's Generative Language API enables seamless prototyping using the Gemini models. It lets you test and explore the capabilities of Gemini models without needing to set up complex infrastructure or manage model deployment. Google's browser based prototyping tool Google AI Studio uses the Generative Language API to enable browser-based prototyping with generative models.
With Vertex AI SDK for ABAP, you can use the Generative Language API—Gemini API to start your prototyping journey, directly from within your ABAP environment. You need an API key generated from Google AI Studio to access the Gemini models. You need to generate this API key only once.
Generate an API key
The API key that you generate from Google AI Studio provides a convenient way to authenticate to the Gemini API.
To generate an API key, perform the following steps:
In Google AI Studio, open the API keys page.
Click Create API key.
If populated, then read and acknowledge the legal notice, and then click Continue.
If populated, then read and acknowledge the safety setting reminder.
In the Create API key dialog, select one of the following options, as appropriate:
- Create API key in new project
- Create API key in existing project
Copy your key string and keep it secure. You need this API key to configure authentication to the Gemini API.
Copy the project number of the Google Cloud project where the API key is generated. You need this in a later step.
Set up authentication
To authenticate to Gemini API by using an API key, use one of the following methods:
- For the on-premises or any cloud edition of ABAP SDK for Google Cloud:
- For the SAP BTP edition of ABAP SDK for Google Cloud:
Authenticate by using an API key stored in SAP SSF
To keep your API key secure, you can store the generated API key in SAP SSF and configure a client key with the SSF authentication class. You can use this client key to access the Gemini API. For more information, see:
Authenticate by using an API key through a custom authentication class
Alternatively, you can create a custom authentication class to keep your API key and configure a client key with the custom authentication class.
On-premises or any cloud edition
In your SAP system, create a
Z
class by implementing the interface/GOOG/IF_AUTH
that is shipped with the ABAP SDK for Google Cloud:- In SAP GUI, execute the transaction code
SE24
. - In the Object type field, type a name such as
Z_TEST_GEMINI_API
, and then click Create. - Make sure that the Object type is Class, and then click OK.
- In the Description field, provide a description of the class
such as
Class for authentication to Google AI Studio
, and then save the class as a Local Object or in a Transport Request. - Open the Interfaces tab, add the interface
/GOOG/IF_AUTH
, and then press enter. - Open the Methods tab. Methods of the interface
/GOOG/IF_AUTH
are automatically implemented into theZ
class. - Double-click the method
/GOOG/IF_AUTH~GET_ACCESS_TOKEN
. In the method
/GOOG/IF_AUTH~GET_ACCESS_TOKEN
, add the following code:ev_auth_type = /goog/if_auth=>c_auth_type_apik_ssf. cv_token = 'API_KEY'. GET TIME STAMP FIELD ev_logtime.
Replace
API_KEY
with the API key that you generated in Google AI Studio.Activate your
Z
class.
- In SAP GUI, execute the transaction code
In the SAP system, create a client key configuration:
In SAP GUI, execute the transaction code
/GOOG/SDK_IMG
.Alternatively, execute the transaction code
SPRO
, and then click SAP Reference IMG.Click ABAP SDK for Google Cloud > Basic Settings > Configure Client Key.
Click New Entries.
Enter values for the following fields:
Field Description Google Cloud Key Name Specify a name of the client key configuration, such as TEST_GEMINI_API
.Google Cloud Service Account Name Leave this field blank. Google Cloud Scope Leave this field blank. Google Cloud Project Identifier Specify the project number of the Google Cloud project where the API key is stored. Command name Leave this field blank. Authorization Class Specify the Z
class that you created in the preceding step, such asZ_TEST_GEMINI_API
.Token Caching Leave this field blank.
Token Refresh Seconds Leave this field blank. Authorization Parameter 1 Leave this field blank. Authorization Parameter 2 Leave this field blank. - Save the new entry.
Make a note of the client key. You use this client key for authentication.
SAP BTP edition
In your SAP BTP, ABAP environment, create a new class by inheriting the
/GOOG/CL_AUTH_BASE
class that is shipped with the ABAP SDK for Google Cloud:- In the Eclipse platform, right-click the package where you want to create a class, and then click New > ABAP Class.
- In the Name field, enter
ZCL_TEST_GEMINI_API
. - In the Description field, enter
Authentication Class for testing Gemini API
. - In the Superclass field, enter
/GOOG/CL_AUTH_BASE
. - Click Next.
- Select a Transport Request or create a new one.
- Click Finish.
In the editor, replace the existing code with the following code:
CLASS zcl_test_gemini_api DEFINITION PUBLIC INHERITING FROM /goog/cl_auth_base FINAL CREATE PUBLIC. PUBLIC SECTION. CONSTANTS: BEGIN OF c_com_scn_sdk, wif TYPE /goog/comm_scenario VALUE '/GOOG/COM_SDK_WIF', apik_sm TYPE /goog/comm_scenario VALUE '/GOOG/COM_SDK_APIKSM', END OF c_com_scn_sdk. METHODS get_access_token REDEFINITION. PROTECTED SECTION. PRIVATE SECTION. ENDCLASS. CLASS zcl_test_gemini_api IMPLEMENTATION. METHOD get_access_token. ev_auth_type = CONV #( apikey_sm ). ev_token = API_KEY. ENDMETHOD. ENDCLASS.
Replace
API_KEY
with the API key that you generated in Google AI Studio.Activate your
Z
class.Go to the properties of this class, go to the API State tab, and then release the API with the contract state
C1
.For more information about API state, see Releasing Development Objects.
Access the SAP Fiori launchpad of the BTP ABAP system where the ABAP SDK for Google Cloud is installed.
Access the Google SDK: Main Configuration app, and then perform the following steps:
- Click Create.
In the Create new entry dialog, enter values for the following fields:
Field Description Google Cloud Key Name Specify a name of the client key configuration, such as GEMINI_QUICK_TEST
.Google Cloud Service Account Name Leave this field blank. Google Cloud Scope Leave this field blank. Google Cloud Project Identifier Specify the project ID of the Google Cloud project where you created the API key. Google Cloud Project Number Specify the project number of the Google Cloud project where you created the API key. Authentication Type Select Z
.Authentication Class Select the ZCL_TEST_GEMINI_API
class that you created in a previous step.Save the new entry.
Make a note of the client key. You use this client key for authentication.
Run sample code
On-premises or any cloud edition
You can explore the Gemini API capabilities by using the AI Studio demo shipped with the Vertex AI SDK for ABAP.
To run the AI Studio demo, perform the following steps:
In SAP GUI, execute the transaction code
/GOOG/SDK_IMG
.Alternatively, execute the transaction code
SPRO
, and then click SAP Reference IMG.Go to ABAP SDK for Google Cloud > Demos.
Open Vertex AI SDK: Demo: Prototyping with Generative Language API.
In the Client key field, enter the client key that you've created in the Set up authentication section.
In the Model ID field, enter the ID of the LLM model that you want to use, such as
gemini-1.5-flash
. For information about the available models, see Model variants.Optionally, provide the following generation parameters as appropriate:
- Max output tokens
- Response Mime type
- Temperature
- TopK
- TopP
For information about these model generation parameters, see Experiment with parameter values.
In the Enter system instructions field, provide a system instruction.
In the Enter prompt field, provide your input prompt.
Run the program. The Response from LLM area shows the generated output.
SAP BTP edition
You can explore the Gemini API capabilities by running the following sample code:
In the Eclipse platform, right-click the package where you want to create a class, and then click New > ABAP Class.
In the Name field, enter
ZCL_INVOKE_LLM
.In the Description field, enter
Sample class for testing Gemini API
.Click Next.
Select a transport request or create a new one.
Click Finish.
In the editor, replace the existing code with the following code:
CLASS zcl_invoke_llm DEFINITION PUBLIC FINAL CREATE PUBLIC. PUBLIC SECTION. INTERFACES if_oo_adt_classrun. ENDCLASS. CLASS zcl_invoke_llm IMPLEMENTATION. METHOD if_oo_adt_classrun~main. DATA lv_sys_instruction TYPE string. DATA lv_client_key TYPE /goog/keyname. DATA lv_model_id TYPE string. DATA lv_max_tokens TYPE int4. DATA lv_resp_mime_type TYPE string. DATA lv_prompt TYPE string. DATA lv_temperature TYPE /goog/num_float. DATA lv_top_k TYPE int4. DATA lv_top_p TYPE /goog/num_float. DATA ls_input TYPE /goog/cl_generativelang_v1beta=>ty_045. DATA ls_output TYPE /goog/cl_generativelang_v1beta=>ty_046. DATA ls_part TYPE /goog/cl_generativelang_v1beta=>ty_068. DATA lt_parts TYPE /goog/cl_generativelang_v1beta=>ty_t_068. DATA ls_content TYPE /goog/cl_generativelang_v1beta=>ty_018. DATA lv_msg TYPE string. DATA ls_raw TYPE string. DATA lv_err_msg TYPE string. DATA lo_cx_sdk TYPE REF TO /goog/cx_sdk. " TODO Developer: Set values for Mandatory Fields lv_model_id = 'gemini-1.5-flash'. " You can also change the Model ID of your choosing lv_client_key = 'GEMINI_QUICK_TEST'. " Client key configured " TODO Developer: Set values for Optional Fields, if required " lv_temperature = " lv_top_k = " lv_top_p = " lv_max_tokens = " TODO Developer: (Optional) Set the System Instruction lv_sys_instruction = 'You are an Italian Chef in a 5 Star Restaurant'. " TODO Developer: (Mandatory) Set the Prompt lv_prompt = 'Give me a recipe to make Margarita Pizza'. IF lv_temperature IS NOT INITIAL. ls_input-generation_config-temperature = lv_temperature. ENDIF. IF lv_top_k IS NOT INITIAL. ls_input-generation_config-top_k = lv_top_k. ENDIF. IF lv_top_p IS NOT INITIAL. ls_input-generation_config-top_p = lv_top_p. ENDIF. IF lv_max_tokens IS NOT INITIAL. ls_input-generation_config-max_output_tokens = lv_max_tokens. ENDIF. IF lv_sys_instruction IS NOT INITIAL. ls_input-system_instruction-role = 'system'. ls_part-text = lv_sys_instruction. APPEND ls_part TO ls_input-system_instruction-parts. CLEAR ls_part. ENDIF. ls_content-role = 'user'. IF lv_prompt IS NOT INITIAL. ls_part-text = lv_prompt. APPEND ls_part TO ls_content-parts. CLEAR ls_part. ENDIF. APPEND ls_content TO ls_input-contents. CLEAR ls_content. TRY. DATA(lo_llm_invoker) = NEW /goog/cl_generativelang_v1beta( iv_key_name = lv_client_key ). CATCH /goog/cx_sdk INTO lo_cx_sdk. lv_err_msg = lo_cx_sdk->get_text( ). out->write( |Exception occurred, reason: { lv_err_msg }| ). ENDTRY. TRY. lo_llm_invoker->generate_content_models( EXPORTING iv_p_models_id = lv_model_id is_input = ls_input IMPORTING es_raw = ls_raw es_output = ls_output ev_ret_code = DATA(lv_ret_code) ev_err_text = DATA(lv_err_text) es_err_resp = DATA(ls_err_resp) ). CATCH /goog/cx_sdk INTO lo_cx_sdk. lv_err_msg = lo_cx_sdk->get_text( ). out->write( |Exception occurred, reason: { lv_err_msg }| ). RETURN. ENDTRY. IF lo_llm_invoker->is_success( lv_ret_code ) = abap_true. ASSIGN ls_output-candidates[ 1 ] TO FIELD-SYMBOL(<ls_candidate>). IF sy-subrc = 0. lt_parts = <ls_candidate>-content-parts. ASSIGN lt_parts[ 1 ] TO FIELD-SYMBOL(<ls_part>). IF sy-subrc = 0. DATA(lv_response) = <ls_part>-text. out->write( lv_response ). ELSE. out->write( |Model stopped generating response due to finish reason: { <ls_candidate>-finish_reason }| ). out->write( |Read more about finish reason in the below link:| ). out->write( |https://cloud.google.com/vertex-ai/docs/reference/rest/v1/GenerateContentResponse#FinishReason| ). ENDIF. ENDIF. ELSE. out->write( |Error occurred, reason: { lv_ret_code }:{ lv_err_text }| ). ENDIF. ENDMETHOD. ENDCLASS.
Activate your class.
Run the ABAP code.
Prototype AI use cases using ABAP
You can build prototype SAP applications using the /GOOG/CL_GENERATIVELANGUAGE_V1
and /GOOG/CL_GENERATIVELANG_V1BETA
classes shipped with the SDK. You can invoke these
classes from your ABAP environment using the client Key that
you've created in the
Set up authentication section.
What's next
- Learn how to install and configure the Vertex AI SDK for ABAP.
- Explore the built-in Generative AI demo programs for SAP.
- Learn about Generative AI on Vertex AI for SAP.
- Ask your questions and discuss the Vertex AI SDK for ABAP with the community on Cloud Forums.