How I use AI models from OpenAI in my SAP CI/IS Development
- 2023/02/25, a very good high level introduction about how to give prompt to call OpenAI AI models especially Codex. Prompt Engineering (https://microsoft.github.io/prompt-engineering/)
- 2023/01/14, two examples added in the comments section. One for generation of XPath selection and another one for Groovy script.
- 2022/12/28, I encourage everyone who uses this plugin sharing your use case in the comment section. I will also leave my new discovery in the comment section.
In the first week of December, so many posts on my LinkedIn and Facebook praised how good ChatGPT is. I also had a “chat” with ChatGPT. No double, the algorithm, AI model, infrastructure behind this chatbot is one of the best AI models currently in the world.
I became aware of OpenAI two years ago when they were still working on GPT-2 and I registered my interest on their website at the time for trial access. However, it wasn’t until this year September that I received an invitation to grant me access to the website and its CodeX AI model. Since then, I read documentation many times, read posts in community, and tested different models in the playground. I began to think about how I could incorporate AI services into my daily work, specifically in relation to SAP CPI custom development and other activities such as preparing content in the specification.
Following this idea, I start exploring how to make it happen and in what way. I have been using CPI Helper Chrome Extension since its author, Dominic Beckbauer, released it to the SAP community and open-sourced the code on Github. I have also contributed some functions/UI improvements from time to time. Therefore, building a plugin on top of this extension to call the AI services was a no-brainer decision. I started working on this plugin in November, and have first version ready last week.
CPI Helper Plugin – My AI Coding Buddy
As an SAP CI/IS integration consultant, using these AI services could benefit our development work at different stage. It could be development phase or maintenance phase. The plugin I developed for the CPI Helper Chrome Extension allows you to easily leverage OpenAI API to call these AI models and incorporate their results into your work without doing it in OpenAI playground and then copy&past back to CPI. You just need to tick “activate” in the CPI Helper Plugins dialog, then you can open the below main window from clicking button in “Messages” floating box.
This is the main screen of the plugin.
- Button “Set Key” and “Load Key” allow you to set and load your OpenAI API key without having to fill it in each time you call the APIs. The key is encrypted with the AES algorithm and stored in the Chrome/Edge browser storage for added security.
- Parameter “Max Return Tokens” which determines the maximum number of tokens (i.e., words or other elements of the input text) that the model will use to generate its output. This can be useful for limiting the length of the output, for example if you only want a short code snippet or explanation.
- Parameter “Stop sequence” is a sequence of tokens that the model will use to determine when to stop generating output. For example, you could specify a stop sequence of “//end” to tell the model to stop generating code once it reaches a line that includes that sequence.
When calling OpenAI APIs, there are three main types of operations that you can perform: Complete, Insert, and Edit. These modes correspond to different AI models that are trained to perform specific tasks.
- Complete mode is used to generate text or code based on a given input. For example, you could use the mode to generate a groovy script or XSLT code based on a requirement or description. This mode is supported by several AI models, including Code Davinci 002, Text Davinci 002, and Text Davinci 003. These models are trained to generate text or code that is coherent and follows the structure and style of the input.
- Insert mode is used to insert text or code into an existing script or code. For example, you could use the mode to add a new function or code block to an existing script. This mode is supported by the Code Davinci 002, Text Davinci 002, and Text Davinci 003 models, which are trained to insert text or code in a way that is coherent and consistent with the existing script or code.
- Edit mode is used to fix errors or make changes to an existing script or code. For example, you could use the “edit” operation to correct a syntax error or rewrite a section of code to improve its efficiency. This mode is supported by the Code Davinci Edit 001 and Text Davinci Edit 001 models, which are trained to identify and fix errors in text or code, as well as to rewrite text or code to improve its structure or style.
In all of these different modes, it’s important to provide clear and unambiguous instructions to the AI model to ensure that it produces accurate and useful results.
Using Code Davinci 002 and Text Davinci 003 models to generate XSLT. In this scenario, I provide input XML, output XML, mapping rules, and first line of XSLT. Both models generate workable XSLT. Don’t forget to copy first line from request to response to form a complete XSLT.
Using Code Davinci 002 and Text Davinci 003 models to insert a check in the given XSLT. In this scenario, I provide input XML and ask models to insert a if check in the given XSLT.
Continue case 2. I give my request in Chinese and surprisingly the models gives the same and working response.
Rules of Thumb
- It’s important to provide clear and necessary information to the AI models. This will help the models understand the context and requirements of the task, and as a result produce better results.
- It can be helpful to provide the first few lines of code, or comments in the code, or input/output sample messages to the model as hints about the language and style of the code you want it to generate.
- It is important to think about the user’s perspective and what they want to achieve with the code. This will help you provide the right instructions to the AI model, and ensure that the resulting code is useful and effective.
- It’s helpful to think about how a senior developer would approach the task, and provide instructions to the AI model accordingly.
- The above AI models may not know SAP CPI/IS proprietary Groovy APIs or specific syntax but it knows Groovy language syntax and XSLT. You could specify some common SAP Groovy classes that you want use in the script in import declaration. This will give AI models hints to use right method and parameters in the generated script.
- Be mindful of the maximum number of tokens allowed in your request.
- If the response from AI models does not align with your requirements, consider utilizing another model or rephrasing your requirements.
- The Code Davinci model is capable of generating code, but it is not capable of generating comments or summaries.
Overall, using AI services for software development can be benefit in various aspect for developers and this maybe a new way of software development. However, it’s important to remember that AI models are not perfect, and their output should be carefully examined and tested after being incorporated into your integration project.
Writing code is only part of integration flow development, there are more areas to consider at different level such as synchronous/asynchronous, error handling, integration pattern…etc. Developers should still use their own judgment and expertise to evaluate the results of the AI model s to ensure code suggests by AI are accurate and fit for the purpose.
That is pretty interesting addition to the CPIhelper. I saw it can create different groovy code that can make sense in a CPI mapping context.
And yes it is not always correct but it is a good place to start.
Very nice usage of openAI in the context of CPI.
Generate XPath from given XML.
AI Coding Buddy
This is the XPath given by AI model.
And I use it in the filter step combing with => serialize() function to get string content.
Use insert mode to generate Groovy script.
In this example, first I tell AI model this is Groovy language and then provide specific class in the second line.
In the function body, I put my request in comment and add [insert] in the line below.
This is the result that I receive from AI model.
Hi Nick Yang,
Thanks a lot for sharing your insights in the SAP Community!
Would you be able to add "Artificial Intelligence" as an "SAP Managed Tag" of your blog post so AI enthusiasts in the community can easily find it?
Thanks for your suggestion. Managed to add "Artificial Intelligence" to custom tag but not SAP managed tag due to error saying not able to retrieve list of tag.