Unlock free access to Gemini 2.0 from SAP applications

Introduction

The advent of large language models (LLMs) like Google's Gemini 2.0 [Launch Message] presents exciting possibilities for enhancing SAP applications/Agents with AI-powered capabilities. However, many SAP developers might be deterred by the perceived complexities and costs associated with cloud projects and service deployments. This blog post demonstrates how to invoke the Gemini models including the recent Gemini 2.0 Flash model directly from your SAP ABAP environment without needing a Google Cloud project or incurring any direct costs.

We will achieve this by leveraging Google'sGemini API (Generative Language API) throughABAP SDK for Google Cloud. 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 across AI/ML, enterprise, maps and workspace APIs.

Using API Keys for Simplified Access

Google's Gemini API offers a streamlined way to interact with Gemini models using API Keys. This approach eliminates the need for complex service account setups and project configurations, making it ideal for prototyping and proof-of-concepts.

Prerequisites

  1. SAP System with ABAP SDK for Google Cloud: Ensure your SAP system has the latest version of the ABAP SDK for Google Cloud installed and configured. Refer to the official Google Cloud documentation for installation instructions for on premise or BTP.
  2. Google Account (Free Tier): You need a Google account (Gmail ID). The free tier provides sufficient resources for generating API Keys and exploring the Gemini API.?
  3. API Key Generation: Generate an API Key for the Generative Language API. Refer to the steps here to generate an API Key. Securely store this key, as you'll need it with the SDK to invoke Gemini.

Invoking Gemini 2.0 Flash from ABAP

Once you have installed and configured the SDK, make use of the ABAP class /GOOG/CL_GENERATIVELANG_V1BETA shipped with the SDK to invoke Gemini API.

Setup authentication for invoking the API by following the steps below.

  1. Generate an API Key following the steps here if you have not already generated it.
  2. Setup authentication using the generated API key through either of below two ways,

In this blog I would try to show a simple example use case of invoking Gemini 2.0 flash by an ABAP GenAI bot or Agent to extract the customer name, issue category and issue reported from the text of customer query. In typical business scenarios, the Agent can be a SAP function module, class method or an ODATA service with well defined input output parameters. Customer query, instructions and prompt can be the input parameters to the Agent.

Customer Query -

From: John Doe

Query: Received a broken Cymbal monster truck, initially the manufacturer offered a replacement but not shifts the responsibility to the retailer to provide the replacement. I want replacement for the broken truck.

Instructions to the bot -?

“You are a Customer Service Agent. Your job is to review queries from customers and find out what is the issue that they are facing. Respond in JSON format in plain text without any backticks with fields "CustomerName", "IssueCategory" and "IssueReported". Populate the customer name in the field "CustomerName", category of the issue from texts "REPLACEMENT", "POOR QUALITY", "ORDER DELAYED" in field? "IssueCategory", and summarise the issue in the query and put in the field? "IssueReported". If there are no issues, populate text "No Issues" in the field "IssueReported" and "NO ISSUES" in the field "IssueCategory".”

Input Prompt to the model -

“Find out the issue in the below customer query”

In this blog, I will show the implementation for the Agent infused with Gemini through a simple report program.

TYPES:
  BEGIN OF ty_agent_response,
    customer_name  TYPE string,
    issue_category TYPE string,
    issue_reported TYPE string,

  END OF ty_agent_response.

DATA lv_sys_instruction TYPE string.
DATA lv_client_key      TYPE /goog/keyname.
DATA lv_model_id        TYPE string.
DATA lv_prompt          TYPE string.
DATA lv_customer_query  TYPE string.
DATA ls_input           TYPE /goog/cl_generativelang_v1beta=>ty_045.
DATA ls_agent_response  TYPE ty_agent_response.
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_err_msg         TYPE string.

lv_model_id = 'gemini-2.0-flash'.  " You can also change the Model ID of your choosing
lv_client_key = '<Client Key>'.    " Client key configured in table /GOOG/CLIENT_KEY

lv_customer_query = |{ 'From: John Doe' } | && |{ 'Query: Received a broken Cymbal monster truck,' } | &&
                    |{ 'initially the manufacturer offered a replacement but not shifts the' } | &&
                    |{ 'responsibility to the retailer to provide the replacement.' } | &&
                    |{ 'I want replacement for the broken truck.' }|.

lv_sys_instruction  = |{ 'You are a Customer Service Agent. Your job is to review queries from' } | &&
                      |{ 'customers and find out what is the issue that they are facing. Respond in' } | &&
                      |{ 'JSON format in plain text without any backticks with fields "CustomerName", "IssueCategory" and "IssueReported".' } | &&
                      |{ 'Populate the customer name in the field "CustomerName", category of the issue' } | &&
                      |{ 'from texts "REPLACEMENT", "POOR QUALITY", "ORDER DELAYED" in field  "IssueCategory",' } | &&
                      |{ 'and summarise the issue in the query and put in the field  "IssueReported".' } | &&
                      |{ 'If there are no issues, populate text "No Issues" in the field "IssueReported"' } | &&
                      |{ 'and "NO ISSUES" in the field "IssueCategory".' }|.

lv_prompt = |{ 'Find out the issue in the below customer query' } | && lv_customer_query.

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_gemini_invoker) = NEW /goog/cl_generativelang_v1beta( iv_key_name = lv_client_key ).
    lo_gemini_invoker->generate_content_models( EXPORTING iv_p_models_id = lv_model_id
                                                          is_input       = ls_input
                                                IMPORTING 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_gemini_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.
          /goog/cl_json_util=>deserialize_json( EXPORTING iv_json          = lv_response
                                                          iv_pretty_name   = /ui2/cl_json=>pretty_mode-extended
                                                IMPORTING es_data          = ls_agent_response ).
          cl_demo_output=>display( ls_agent_response ).
        ELSE.
          cl_demo_output=>display( |Model stopped generating response due to finish reason: { <ls_candidate>-finish_reason }| ).
        ENDIF.
      ENDIF.
    ELSE.
      cl_demo_output=>display( |Error occurred, reason: { lv_ret_code }:{ lv_err_text }| ).
    ENDIF.
  CATCH /goog/cx_sdk INTO DATA(lo_cx_sdk).
    lv_err_msg = lo_cx_sdk->get_text( ).
    cl_demo_output=>display( |Exception occurred, reason: { lv_err_msg }| ).
ENDTRY.        

When run, the program outputs the response from Gemini, serialized in ABAP types, ready to be used in follow-on business logic.

Explore Gemini models through a demo program shipped with the SDK

Additionally you can also play around with the demo program that we ship with the SDK. Follow steps from our public documentation here to run the demo program for your prompts. It also supports multimodal invocation with images, documents and videos.

Important Considerations

  • API Key Security: Protect your API Key and avoid embedding it directly in production code. Use secure configuration mechanisms.
  • Rate Limits: Be aware of the Generative Language API's rate limits and implement appropriate error handling.
  • Prompt Engineering: The quality of the model's output depends heavily on the prompt. Experiment with different prompt formats to achieve the desired results.
  • Cost Management: While API Keys offer a cost-free entry point for exploration, monitor your API usage to ensure you stay within the free tier limits.

Implementing production use cases with Gemini

While Gemini API is great for quick use cases and prototyping, we recommend you use Vertex AI API to implement your heavy production use cases to realise the true potential of Gemini models. Checkout our Vertex AI SDK for ABAP (embedded in the latest version of ABAP SDK) to build your production SAP ABAP GenAI Applications/Agents. The SDK let’s you invoke Gemini, leverage SAP function calling, generate embeddings and perform vector search natively from your ABAP environment with two to four lines of ABAP code.

Conclusion

By leveraging the Generative Language API and API Keys, SAP ABAP developers can seamlessly integrate the power of Gemini 2.0 Flash into their applications without the need for complex cloud project setups or direct costs. This approach empowers developers to explore AI-driven functionalities and enhance their SAP solutions with intelligent GenAI Agents. Remember to consult the latest Google Cloud documentation for the most up-to-date information and best practices.?

Share your experiences and feedback with us by starting a conversation on our Google Cloud Channel.

要查看或添加评论,请登录

Devesh Singh的更多文章

社区洞察

其他会员也浏览了