Tutorial: Adding OpenAI to Interoperability Production
Tags: ai, tutorial, bpl, programming
Published: 2024-05-14 17:09:22

<p>Artificial Intelligence (AI) is getting a lot of attention lately because it can change many areas of our lives. Better computer power and more data have helped AI do amazing things, like improving medical tests and making self-driving cars. AI can also help businesses make better decisions and work more efficiently, which is why it's becoming more popular and widely used. How can one integrate the OpenAI API calls into an existing IRIS Interoperability application?</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pge1j8ji7lpslwptso27.png)

<p><!--break--></p>
<h2><strong>Prerequisites</strong></h2>
<p>In this tutorial we will assume that you already have an existing interoperability production and a set of OpenAI credentials to make calls to OpenAI APIs. You can download a code we use in this tutorial from the following GitHub project branch:&nbsp;<a data-behavior="truncate" href="https://github.com/banksiaglobal/bg-openai/tree/test-app-original" rel="noreferrer" target="_blank">https://github.com/banksiaglobal/bg-openai/tree/test-app-original</a><br>To learn how to get OpenAI credentials, follow this tutorial&nbsp;<a data-behavior="truncate" href="https://allthings.how/how-to-get-your-open-ai-api-key/" rel="noreferrer" target="_blank">https://allthings.how/how-to-get-your-open-ai-api-key/</a>&nbsp;or just open OpenAI API Keys page and create one&nbsp;<a data-behavior="truncate" href="https://platform.openai.com/api-keys" rel="noreferrer" target="_blank">https://platform.openai.com/api-keys</a></p>
<h3><strong>Original Application</strong></h3>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bz331dgi884mqb3ktjv.jpg)
<p>Our application, AppExchange, emulates InterSystems OpenExchange publishing: it gets a request with a project description, project logo and GitHub URL and publishes it in the AppExchange repository.</p>
<p>&nbsp;</p>
<h2><strong>Adding a bit of Artificial Intelligence</strong></h2>
<p>Now let's assume that a person who looks after our repository noticed that some app developers are lazy and not providing either short summary or logo for the apps they are publishing. That's where our AI friend can come to the rescue!</p>
<p>The desired workflow would look like this:</p>
<ol>
<li>The application receives a URL of a repository, summary and a URL of logo as input.</li>
<li>If summary is empty, the URL is sent to a GPT-based model that parses the repository contents and generates a descriptive summary of the project. This process may involve parsing README files, code comments, and other relevant documentation within the repository to extract key information about the project's purpose, features, and usage.</li>
<li>The generated project summary is then used as an input to another GPT-based model, which is tasked with creating a logo for the project. This model uses the description to understand the project's theme, and then designs a logo that visually represents the project's essence and identity.</li>
<li>The application outputs a response that includes the original URL, the generated project summary, and the newly created logo. This response provides a comprehensive overview of the project, along with a visual identifier that can be used in branding and marketing efforts.</li>
</ol>
<p>To achieve this integration, we will use the Business Process Designer to visually design the application's workflow.</p>
<p>&nbsp;</p>
<h2><strong>Step 1: Installation</strong></h2>
<p>To start, we will download bg-openai package from Open Exchange using ZPM package manager:</p>
<pre class="codeblock-container" idlang="0" lang="ObjectScript" tabsize="4"><code class="language-cls hljs cos">zpm <span class="hljs-string">"install bg-openai"</span></code></pre>
<p><br>You can have a look at this package here&nbsp;<a data-behavior="truncate" href="https://openexchange.intersystems.com/package/bg-openai-1" rel="noreferrer" target="_blank">https://openexchange.intersystems.com/package/bg-openai-1</a>&nbsp;and check out it's source code here&nbsp;<a data-behavior="truncate" href="https://github.com/banksiaglobal/bg-openai" rel="noreferrer" target="_blank">https://github.com/banksiaglobal/bg-openai</a></p>
<p>This package is based on the great work of <a href="https://community.intersystems.com/user/kurro-lopez" target="_blank">Francisco Lopez</a> available here&nbsp;<a data-behavior="truncate" href="https://github.com/KurroLopez/iris-openai" rel="noreferrer" target="_blank">https://github.com/KurroLopez/iris-openai</a>&nbsp;with four small changes: we changed class names to be more in line with standard IRIS naming conventions, we added a new SimplePrompt request which allows users to send simple AI text prompts very easily, we changed Api Key to be a credential rather than a setting, and we changed top level package name to "Banksia" in line with company standards.</p>
<p>&nbsp;</p>
<h1><strong>Step 2: Set up OpenAI Operation</strong></h1>
<p>For further work and configuration of the products, let's move to the management portal located at the following link if you are using Docker image with our original application:</p>
<pre data-controller="syntax-highlight"><a href="http://localhost:42773/csp/sys/UtilHome.csp%20" rel="noreferrer" target="_blank">http://localhost:42773/csp/sys/UtilHome.csp</a></pre>
<p><br>Navigate to the&nbsp;<strong>Interoperability-&gt;[Namespace]-&gt;Configure-&gt;Production</strong>&nbsp;and make sure that our original production is running.</p>
<p>Add a new Operation based on class&nbsp;<strong>Banksia.OpenAi.Operation</strong>&nbsp;and name it&nbsp;<strong>OpenAiOut.</strong>&nbsp;Make it enabled. This operation will communicate with OpenAI API servers.</p>
<ul>
<li>Operation Class: Banksia.OpenAi.Operation</li>
<li>Operation Name: OpenAiOut</li>
</ul>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/axigbjhs38ooe072jrd9.png)
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Now let's make a minimal setup required to use our new Operation in Production: add an API key and SSL Configuration.</p>
<p>Navigate to&nbsp;<strong>OpenAiOut-&gt;Settings-&gt;Basic Settings-&gt;Credentials</strong>&nbsp;and click on&nbsp;<strong>magnifying glass icon</strong>&nbsp;to configure credentials.</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8wkovoo3m0ek2dj23u6.png)
<p><span>&nbsp;</span>Fill in the form data and add&nbsp;<strong>apiKey in the password&nbsp;</strong>field. Save the data by clicking on&nbsp;<strong>Save</strong>.&nbsp;<strong>ID</strong>&nbsp;and&nbsp;<strong>User Name</strong>&nbsp;fields you can fill as you like.<span>&nbsp;</span></p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tw9dcvhkbyigozn7k6jf.png)
<p><span>&nbsp;</span></p>
<p>In the&nbsp;<strong>Credentials</strong>&nbsp;field, select the&nbsp;<strong>ID</strong>&nbsp;of the credentials we saved earlier.</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gev4pylc8cdkmp0nj26.png)
<p>&nbsp;</p>
<p>Setup SSL Configuration: create a new Client SSL Configuration&nbsp;<strong>OpenAiSSL</strong>&nbsp;and select it in the dropdown.</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1icpvjim6ob1ppqkv0tz.png)
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2><strong>Step 3 - Add Summary Generation to Business Process using the Business Process Designer</strong></h2>
<p>Navigate to&nbsp;<strong>Interoperability &gt; Business Process Designer</strong>&nbsp; and open&nbsp;<strong>AppExchange.Process</strong><br>business<strong>&nbsp;</strong>process by clicking&nbsp;<strong>Open</strong>.</p>
<p>Build a flowchart of the process based on the algorithm we described above.<br>An example implementation is shown in the image below.</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/levd8oc7l9hievyicmv8.png)
<p><span>&nbsp;</span></p>
<p>Сheck the repository URL is provided and that we need to query ChatGPT to create a description if no description has been entered.</p>
<pre class="codeblock-container" idlang="0" lang="ObjectScript" tabsize="4"><code class="language-cls hljs cos">(request.Summary=<span class="hljs-string">""</span>) &amp; (request.GitHubUrl '=<span class="hljs-string">""</span>)</code></pre>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x52ox1rd2ddmapqy5nkb.png)
<p><span>&nbsp;</span>&nbsp;<span>&nbsp;</span></p>
<p>Then, add the &lt;Сall&gt; block and make a target&nbsp;<strong>OpenAiOut</strong>, which, depending on the type of request, will make a call to OpenAi api.</p>
<ul>
<li>&nbsp;Name: Generate Summary&nbsp;</li>
</ul>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1osbkfwe1317dr9xhvl.png)
<p><span>&nbsp;</span>&nbsp;<span>&nbsp;</span></p>
<p>Customize the type of request and the received response, as well as distribute variables for actions.</p>
<ul>
<li>Request Message Class: Banksia.OpenAi.Msg.SimplePrompt.Request</li>
</ul>
<blockquote>
<p>set&nbsp; callrequest.Prompt &nbsp; = "Visit the website you will be provided on the next step. Describe the main idea of the project, its objectives and key features in one paragraph."&nbsp;</p>
<p>set callrequest.UserInput = request.GitHubUrl&nbsp;</p>
<p>set callrequest.Model = "gpt-4"&nbsp;</p></blockquote>
<ul>
<li>Response Message Class: Banksia.OpenAi.Msg.SimplePrompt.Response</li>
</ul>
<blockquote>
<p>set request.Summary = callresponse.Content&nbsp;</p></blockquote>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srmvgfzl49slqgeixbo2.png)
<p><span>&nbsp;</span></p>
<p>Add a &lt;sync&gt; step to wait for a response, in the Calls field add the name of the previous &lt;call&gt;&nbsp;</p>
<ul>
<li>Calls: Generate Summary</li>
</ul>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vr0w9aystxotis7dnfwz.png)
<p><span>&nbsp;</span></p>
<h1><strong>Step 4 - Add Logo Generation to Business Process</strong></h1>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yk3hvxxjg8ipb0y5z25o.png)
<p>&nbsp;</p>
<p>After getting the repository description, let's move on to the next logical part - logo generation. Let's check that there is a description for which the image will be generated and check if there is no image URL provided. Let's set the following condition:</p>
<pre class="codeblock-container" idlang="0" lang="ObjectScript" tabsize="4"><code class="language-cls hljs cos">(request.LogoUrl=<span class="hljs-string">""</span>) &amp; (request.Summary'=<span class="hljs-string">""</span>)</code></pre>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w6jcp96axx407n0s37ob.png)
<p><span>&nbsp;</span></p>
<p>Сonfigure the next &lt;call&gt; element, make a target our&nbsp;<strong>OpenAiOut&nbsp;</strong>&nbsp;operation as well.</p>
<ul>
<li>&nbsp;Name: Generate Logo</li>
</ul>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihu9duvgtpn76yi5c0zg.png)
<p>&nbsp;</p>
<p>Customize the type of request and the received response.</p>
<ul>
<li>Request Message Class: Banksia.OpenAi.Msg.Images.Request</li>
</ul>
<blockquote>
<p>set&nbsp; callrequest.ResponseFormat&nbsp; = "url"</p>
<p>set&nbsp; callrequest.Operation&nbsp; = "generations"</p>
<p>set&nbsp; callrequest.Prompt&nbsp; = "Create a simple app icon for the following mobile application: "_request.Summary</p>
<p>set&nbsp; callrequest.Size&nbsp; = "256x256"</p></blockquote>
<ul>
<li>Response Message Class: Banksia.OpenAi.Msg.Images.Response</li>
</ul>
<blockquote>
<p>set&nbsp; request.LogoURL&nbsp; = callresponse.Data.GetAt(1).Url</p></blockquote>
<p>&nbsp;<span>&nbsp;</span>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tl88tsogg3kkr5ukc9u2.png) <span>&nbsp;</span></p>
<p>After completing the modification of our business process, click the&nbsp;<strong>compile</strong>&nbsp;button.&nbsp;</p>
<p>You can download the finished OpenAI integrated sample from the following GitHub project branch:&nbsp;<a data-behavior="truncate" href="https://github.com/banksiaglobal/bg-openai/tree/test-app" rel="noreferrer" target="_blank">https://github.com/banksiaglobal/bg-openai/tree/test-app</a></p>
<h2>&nbsp;</h2>
<h2><strong>Step 5: Test our new Business Process in Production</strong></h2>
<p>Go to the&nbsp;<strong>Interoperability-&gt;Configure-&gt;Production</strong>&nbsp;section</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w0xzy3y2ldx2bgzrf8j2.png)
<p>&nbsp;</p>
<p>First we need to restart our process to apply all the latest changes, navigate to&nbsp;<strong>AppProcess-&gt;Actions-&gt;Restart</strong>.</p>
<p>To test the process, go to&nbsp;<strong>AppProcess-&gt;Actions-&gt;Test.</strong><br>Create a test message with a GitHub URL for the OpenAI API and send it through production:</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bidxyblmnjhz685iw9u.png)
<p>&nbsp;</p>
<p>Verify that the response from the OpenAI API is received and processed correctly by the application. Go to&nbsp;<strong>Visual Trace</strong>&nbsp;to see the full application cycle and make sure that the correct data is transmitted in each process element.</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7gj9dwxk1yqiy0lzmm1z.png)
<p>&nbsp;</p>
<p>This is AI's take on our app logo:</p>
<p><img alt="" src="/sites/default/files/inline/images/logo_3.png" style="margin-left: 280px; margin-right: 280px;"></p>
<p><span>&nbsp;</span></p>
<h2><strong>Conclusion</strong></h2>
<p>By following these steps, you can integrate the OpenAI API into the interoperability production using the Business Process in InterSystems IRIS. The bg-openai module is a great resource for developers looking to incorporate AI into their applications. By simplifying the integration process, it opens up new possibilities for enhancing applications with the power of artificial intelligence.</p>
<p>&nbsp;</p>
<h3><strong>About Author</strong></h3>
<p><a href="https://community.intersystems.com/user/maria-nesterenko" target="_blank">Mariia Nesterenko</a> is&nbsp;a certified IRIS developer at Banksia Global. She is involved in application development, data structures, system interoperability, and geospatial data.</p>
<h3><strong>About Banksia Global</strong></h3>
<p><a href="https://partner.intersystems.com/company/Banksia-Global" target="_blank">Banksia Global</a> is an international boutique consultancy headquartered in Sydney, Australia, specializing in providing professional services for InterSystems technologies. With a team of dedicated and experienced professionals, we pride ourselves on being an official InterSystems Premier Partner, authorized to provide services worldwide. Our passion for excellence and innovation drives us to deliver high-quality solutions that meet the unique needs of our clients.</p>