Sometime in March of this year, I wrote a blog on using Odata and Python to demonstrate the use of Python and Developer Edition of ABAP 7.2. For those of you interested in this article please check
https://blogs.sap.com/2023/03/15/abap-developer-edition-python-interface-development/
Since then, Chat GPT has become ubiquitous. SAP released the S4HANA developer edition 1909 - The installation was much easier than the earlier version of ABAP 7.52 using Docker and this version also has Cloud Connector and ABAP RAP model. For those interested in their own installation of 1909 Developer edition please check this link
https://blogs.sap.com/2023/07/31/abap-platform-trial-1909-available-now/
My previous blog was written to demonstrate the power of Python and ABAP developer edition(ECC) the current blog shows how ABAP developers can use Natural Language Query with the Power of ChatGPT and Python. In the earlier blog I used Python Pandas to store the dataset returned by SAP odata with the introduction of Chat GPT Python Pandas has taken advantage of the power of GPT and came up with Panda AI (
https://github.com/gventuri/pandas-ai).
The following are required to run the program to use SAP ABAP odata and GPT using python
- Install Python and follow the install steps for Panda AI.
- Get API key from OpenAI https://elephas.app/blog/how-to-get-chatgpt-api-key-clh93ii2e1642073tpacu6w934j
- Install the S4HANA 1909 developer edition and follow the steps listed in the earlier URL
- Install any IDE such as Visual Code or Py Charm
The code below is exactly what was written in the previous blog - To the end of the code the following lines were added to interact with Chat GPT and Panda AI
import os
from pandasai import SmartDataframe
from pandasai.llm import OpenAI
key1 = os.environ["OPENAI_API_KEY"]
llm = OpenAI(api_token=key1)
df = SmartDataframe(df, config={"llm": llm})
df.chat("can you sum Rating and HelpfulCount for ProductId")
df.chat(
"Plot the histogram of Productid and helpfulcount", "using different colors for each bar",
)
The Complete listing is shown below
import pyodata
import pandas as pd
import requests
# Create a PyOData client instance
service_url = "http://vhcala4hci:50000/sap/opu/odata/sap/EPM_REF_APPS_SHOP_SRV/"
session = requests.Session()
session.auth = ('Developer', 'Htods70334')
client = pyodata.Client(service_url, session)
# Get the Products entity set and its entity type
entity_sets = client.entity_sets
products_entity_set=""
scalar_properties = set()
for es in client.schema.entity_sets:
if es.name == "Reviews":
proprties = es.entity_type.proprties()
for prop in proprties:
if prop.name == 'ProductId' or prop.name == 'Rating' or prop.name == 'HelpfulCount':
scalar_properties.add(prop.name)
print(scalar_properties)
reviews = client.entity_sets.Reviews.get_entities().execute()
# Create an empty list to store the dictionaries for each reviews
review_list = []
# Loop through each review entity and create a dictionary
for review in reviews:
review_dict = {}
for property_name in scalar_properties:
review_dict[property_name] = getattr(review, property_name)
review_list.append(review_dict)
# Convert the list of dictionaries to a pandas DataFrame
df = pd.DataFrame(review_list)
df=df.sort_index()
df2= df.groupby('ProductId').sum()
print(df2)
import os
from pandasai import SmartDataframe
from pandasai.llm import OpenAI
key1 = os.environ["OPENAI_API_KEY"]
llm = OpenAI(api_token=key1)
df = SmartDataframe(df, config={"llm": llm})
df.chat("can you sum Rating and HelpfulCount for ProductId")
df.chat(
"Plot the histogram of Productid and helpfulcount", "using different colors for each bar",
)
The output generated is as follows
we can modify the code so that any query can be entered by the user at the command level to give results which will make it easy for end users to analyze large datasets to answer the questions they have without much effort. However, there could be limitations on the dataset size(Max limits) and costs involved with API usage which are to be expected.
If you are interested in ABAP connectivity please follow the ABAP Connectivity environment topic page (
https://community.sap.com/topics/abap-connectivity), Post and answer questions (
https://answers.sap.com/tags/266264953119842772207986043063520), and read other posts on the topic (
https://blogs.sap.com/tags/266264953119842772207986043063520/)