Technical Articles
Creating a meal generator application with SAP Build, Cloud Application Programming and openAI
Scenario Overview
By integrating SAP Business Technology Platform products with openAI GPT and Google APIs I have managed to create an application that generates meal suggestions based on a list of ingredients.
The idea behind this proof of concept is to give it the list of ingredients I have in my fridge and receive meal suggestions, because you are someone like me who cooks almost daily, sometimes you run of out ideas.
The ingredients are stored in a SAP HANA dabase, which is exposed in a CAP application. I send prompts to the openAI API and based on the response I can dinamically generate an image sample, meal steps and also update the table in case I want to make that meal.
All the SAP products are being used on a trial environment, so if you do want to do anything similar to what I did make sure to read the SAP Tutorials for Developers
Solution Overview
The following picture gives an overview of the intended setup of the solution and the request flows in between:
Solution Overview
The end user can use the SAP Build mobile application, add/remove ingredients in the database and also generate meal samples.
Application Home Screen
Technical setup
CAP Application
The application created in SAP Business Application Studios and it consists of creating multiple services and functions which are being called from SAP Build.
CAP Application Services
I have exposed three services :
/catalog/Foods – which can be called with any CRUD operation to access the list of ingredients.
/Fridge – which I used in order to do the logic for the openAI API Calls
/delete – used to delete all the rows in the table.
In the catalog-service.js we can find all the service functions, like for example the one I created for the openAI integration.
Sample function from the server .js
For integration with SAP Build we also had to cope with the CORS policies :
CORS Policy script
SAP HANA Database
I am using a trial SAP HANA Database instance on Cloud Foundry, and as a prerequisite for this I had to create SAP HANA Schemas & HDI Container + SAP HANA Cloud services.
The database is a simple one, without datalake, in which I created a table in order to store my ingredients.
SAP HANA Database
For testing locally, I used a sample csv in order to generate my hdbcalculationviews and tables.
For deployment to Cloud Foundry i used the MTA Module Template to generate my mtar file, and then with mbt build and cf deploy commands I was able to deploy it to Cloud Foundry.
openAI Chat Completions API
I used the API in order to receive meal suggestions.
API Call to openAI
The function selects all the rows from the table and creates a prompt of this format :
const message = `I have in my fridge ${result}. I want you to suggest me a meal that I can make with these ingredients. I need you to reply ONLY WITH a JSON-format message with 4 nodes: MealPossible with values yes/no if there is any meal that you can suggest, SuggestedMeal where you put the suggested meal name and Quantities node that contains an array with necessary quantities(meal for one) in kg and the ingredient name. Forth node will be Steps which contains all the steps required to make the dish. If the MealPossible node value is no, do not send the other nodes.`;
I have requested a JSON format because it’s helping me build the logic in the SAP Build application, extract data and set it as variables.
SAP API Management and SAP Integration Suite
I wanted to create the functionality that in the case of liking the suggested meal and actually preparing it, I needed to have the ingredients list updated.
SAP CPI iFlow
I managed to do that by creating an iFlow in SAP Integration Suite that receives the json format payload with the ingredients, extracts the name, does a GET on the CAP service for Foods and updates the table.
In order to access SAP Integration Suite endpoints, I had to run the call through an API created with SAP API Management, because I had to cope with the CORS policies.
API Created
All the steps required to do this are listed in this tutorial, and it involves creating an API, a Product and generate an APIKey for it.
Google API and Custom Engine integration
Besides having a title and step-by-step instructions, I could also see how the meal should look, because that’s the first thing you should see and also like it before deciding to eat it.
For that, I decided to use a Programmable Search Engine and also the Custom Search API provided by Google in order to search a word and extract the picture url provided in the response.
Data Source SAP Build
For authentication, Google offers an APIKey that I’ve used in the REST call made in SAP Build, so when I press the generate button, if I succesfully get a meal suggestion from the AI, it will extract the meal name and use it to search an image for it ( of course, SAFE MODE turned on ).
API Call for image search
The app variable is then used as binded as the data source for the image container.
SAP Build Application
The application is pretty simple, but the buttons hide a lot of logic in the back.
The ingredients list page uses the /catalog/Foods service to connect to the SAP HANA Cloud database and show what’s already available. You can also add a new ingredient in the database, by checking for duplicate entries and also automatically incrementing the table key ID.
Button logic for adding new ingredient
The most important button is the ‘Generate new meal’ one, which as you can see below, does a lot of things in the backstage :
Generate new meal button logic
It calls the /Fridge service with the meal generator function and from the json response, it extracts using Javascript certain nodes and does a custom logic based on them.
After the call is succesful and I get a meal suggestion, if I don’t like it, I can do another call and get another suggestion, making sure that in the second call I have the first suggestion mentioned.
In the latest version, I also added a notification that asks if you want meat-free food or not :).
Video example
Conclusions
By using the SAP platform it was pretty easy to integrate and communicate between multiple applications. Alongside tutorials I was able to get my hands on experience in CAP, and in about a week I was able to be confident enough to add/edit and create different services and functions in the CAP application, and also deploy it to Cloud Foundry.
The integration with the openAI and Google APIs were a bonus, but it was nice to know and understand how things work on that side.
I hope you liked my article and feel free to reply with any suggestions here or on in the SAP Build Question section.
Thank you !
Love it!! How can I install it?
Hello ! Currently I did not implement any sort of authentication, will look in the near future to implement XSUAA and use that to authenticate. If you would like to use it, you can send me the PIN displayed in your SAP Appgyver preview app.
Thank you for the compliment,
Marian.
Great example, combining several capabilities of SAP Business Technology Platform + third party AI.
Well done Marian-Cornel Vatafu! 👏🏻👍🏻
Many thanks Pierre COL !
Marian.
Hi Marian-Cornel Vatafu,
Great blog post and use case, congrats!
Would you be able to add "Artificial Intelligence" as one of the SAP Managed Tags of your blog post? This will help our community members and AI enthusiasts to retrieve it through search more easily.
Thanks.
Hi Paul PINARD . Thanks for the compliment, I have added the tag ! 🙂
Hats-off !! Best blog with all the clear explanation of the each layer. It is straight saved in my Bookmark. Would love to install it.
Cheers;
Mohit Bansal