Open AI integration with IRIS
Tags: ai, framework, programming, tutorial
Published: 2024-02-22 14:46:21


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oaakbiby2o19xzo709ww.jpg)<span>&nbsp;</span></p>
<p>As you all know, the world of artificial intelligence is already here, and everyone wants to use it to their benefit.</p>
<p>There are many platforms that offer artificial intelligence services for free, by subscription or private ones. However, the one that stands out because of the amount of "noise" it made in the world of computing is Open AI, mainy thanks to its most renowned services: ChatGPT and DALL-E.</p>

<h3>What is Open AI?</h3>
<p>Open AI is a non-profit AI research laboratory launched in 2015 by Sam Altman, Ilya Sutskever, Greg Brockman, Wojciech Zaremba, Elon Musk, John Schulman and Andrej Karpathy with the aim of promoting and developing friendly artificial intelligence that would benefit humanity as a whole.</p>
<p>Since its foundation, the guys have released some fascinating products that, if used for good purposes, could be really powerful tools. Yet, like any other new technology, they pose a threat of potentially being used to commit crimes or do evil.</p>
<p>I decided to test the ChatGPT service and asked it what the definition of artificial intelligence was. The answer I received was an accumulation of notions found on the Internet and summarized in such a way a human would respond.</p>
<p>In short, an AI can only reply using the information used to train it. Employing its internal algorithms and the data fed to it during the training, it could compose articles, poems, or even pieces of computer code.</p>
<p>Artificial intelligence is going to impact the industry considerably and ultimately revolutionize everything…. Perhaps the expectations of how artificial intelligence will affect our future are being overstated, so we shall start to use it correctly for the common good.</p>
<p>We are tired of hearing that this new technology will change everything and that ChatGPT is the tool that will turn our world upside down, just like its brother GPT-4 did. Neither will these tools leave people without jobs, nor are they going to rule the world (like Skynet). What we are trying to analyze here is the trend. We start by looking at where we were before to understand what we have achieved so far and thus anticipate where we will find ourselves in the future.</p>
<p>In 2020, psychologist and cognitive scientist Gary Marcus published an article analyzing how GPT-2 worked. He conducted a methodical study of its operation revealing that that type of tool actually failed to understand what it was writing or what orders it received.</p>
<p>“Here's the problem: upon careful inspection, it becomes apparent the system has no idea what it is talking about: it cannot follow simple sequence of events nor reliably have any idea what might happen next.”</p>
<p>Follow the link below to see the entire article: https://thegradient.pub/gpt2-and-the-nature-of-intelligence/</p>
<p>You can clearly witness the evolution here! GPT-3 (2020) had to be trained using enough inputs indicating what you wanted to achieve, whereas the current GPT-4 version can use a natural language making it possible to give those inputs in an easier way. Now it "seems" to understand us as well as know what it is talking about itself.</p>
<p>Now when we use the same example designed by Gary Marcus in 2020 for GPT-2, we get the result as expected:</p>

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

<p>OpenAI can currently provide us with a set of tools that have greatly evolved amazingly fast and, if combined properly, will make it much easier for us to obtain a more efficient result compared to the past.</p>
<h3>What products does OpenAI offer?</h3>
<p>I am going to talk about the two best-known ones, such as DALL-E and Chat-GPT. However, they also have other services, such as Whisper, which transcribes audio into text and even translates it into a different language, or Embeddings, which allows us to measure the relationship of the text strings for searches, recommendations, groupings, etc…</p>
<h3>What do I need to use these services?</h3>
<p>You will have to create an OpenAI account, which is very easy to make, and you will be all set to use their services directly through their website at that point.</p>
<p>Chat: <a href="http://chat.openai.com" target="_blank">https://chat.openai.com</a></p>
<p>DALL-E: <a href="http://labs.openai.com" target="_blank">https://labs.openai.com</a></p>
<p>We want to integrate these services from IRIS, so we should use its API to access them. First, we must create an account and provide a payment method to be able to use the API. The cost is relatively small and depends on the use you want to give it. The more tokens you consume, the more you have to pay &#x1f609;</p>
<h3>What is a token?</h3>
<p>It is a way the models uses to understand and process the texts. Tokens can be words or just character fragments. For example, the word "hamburger" is divided into the tiles "ham," "bur," and "ger," while a short common word like "pear" is a single tile. Many tokens begin with a blank space, for example, " hello" and " bye".</p>
<h3>Is it complicated to use the API?</h3>
<p>Not at all. Follow the steps bellow and you will not have any problems:</p>
<p>Step 1: Create an API Key</p>
<p>Select the option "View API Key" within the menu of your user</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ap6uq8b5hc2o7avid6zl.png)
<p>Step 2: Create a new secret key</p>
<p>Press the button at the bottom of the section API Keys</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eu1cg5fz6y4qo1ge4azb.png)
<p><strong>VERY IMPORTANT: </strong>Once the secret key is created, it cannot be recovered at any time, so remember to save that information in a secure place.</p>
<p>Step 3: Define the name of your organization</p>
<p>The definition of your organization is not mandatory but recommended. It is a part of the header of API calls. You also ought to copy your organization's code for later use.</p>
<p><span>&nbsp;</span>You can modify it as many times as you want.</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pq500fb494uj5mfodrln.png)
<p>Step 4: Prepare the API call using the Secret Key and the Organization ID</p>
<p>As part of the API call, you must use a Bearer token authentication header and indicate the Secret Key.</p>
<p>It should also be indicated as a header parameter next to the Organization ID</p>
<table border="1" cellpadding="1" cellspacing="1" style="width: 500px;">
<tbody>
<tr>
<td>Header parameter</td>
<td>Value</td>
</tr>
<tr>
<td>api-key</td>
<td>sk-MyPersonalSecretKey12345</td>
</tr>
<tr>
<td>OpenAI-Organization</td>
<td>org-123ABCFakeId</td>
</tr>
</tbody></table>
<p>This would be an example of an invocation</p>
<pre class="codeblock-container" idlang="11" lang="Bash" tabsize="4"><code class="language-bash hljs">POST https://api.openai.com/v1/images/create
header <span class="hljs-string">'Authorization: Bearer sk-MyPersonalSecretKey12345'</span>
header <span class="hljs-string">'api-key: sk-MyPersonalSecretKey12345'</span>
header <span class="hljs-string">'OpenAI-Organization: org-123ABCFakeId '</span>
header <span class="hljs-string">'Content-Type: application/json'</span>
</code></pre>
<p>This configuration is common for all endpoints, so let's see how some of its best-known methods work.</p>
<h2>Models</h2>
<h3>Endpoint: GET https://api.openai.com/v1/models</h3>
<p>You can download all the models that OpenAI has defined to be used. Each of those models has different characteristics though. The latest, most up-to-date model for use in Chat is “gpt-4”. Bear in mind that all models IDs are in lowercase.</p>
<p>If the model name is not provided, it will return all existing models.</p>
<p>You can see its features and where you can use it on the OpenAI documentation page <a href="https://platform.openai.com/docs/models/overview" target="_blank">https://platform.openai.com/docs/models/overview</a></p>
<h2>Chat</h2>
<h3>Endpoint: POST https://api.openai.com/v1/chat/completions</h3>
<p>It allows you to create a conversation with the indicated model or through a notice. You can indicate how many tokens you want to use as a maximum and when you should stop the conversation.</p>
<p>The input parameters will be as follows:</p>
<ul>
<li><strong>model:</strong> Required. This is the ID of the model to use. You can use the ListModels API to see all of the available models or check our model overview for its description.</li>
<li><strong>messages:</strong> Required. Contains the type of message indicating which paper is to be used. You can define a dialog form indicating if it is the user or the assistant.
<ul>
<li><strong>role:</strong> It is the role of the person of the message.</li>
<li><strong>content:</strong> It is the content of the message.</li>
</ul></li>
<li><strong>temperature:</strong> Optional. It describes the demonstrated temperature with the value between 0 and 2. If a very high number is given, the result is more random. If a low digit is chosen, the answer will be more focused and deterministic. If it’s not defined, the default value is 1.</li>
<li><strong>stop:</strong> Optional. It is related to sequences where the API stops generating more tokens. If "none" is indicated, the tokens will be generated infinitely.</li>
<li><strong>max_tokens:</strong> Optional. It describes the maximum number of tokens to generate the content and is limited to the maximum number of tokens allowed by the model.</li>
</ul>
<p>Check out the link below for the documentation describing this method: <a href="https://platform.openai.com/docs/api-reference/chat/create " target="_blank">https://platform.openai.com/docs/api-reference/chat/create</a></p>
<h2>Image</h2>
<h3>Endpoint: POST https://api.openai.com/v1/images/generations</h3>
<p>It allows you to create an image as indicated by the prompt parameter. In addition, we can define the size and the way we want the result to be returned, be it through a link or content in Base64</p>
<p>The input parameters would be as mentioned below:</p>
<ul>
<li><strong>message:</strong> Required. It is related to a text describing the image we want to generate.</li>
<li><strong>n:</strong> Optional. It is related to the maximum number of images to generate. This value should be between 1 and 10. If it's not indicated, the default value is 1.</li>
<li><strong>size:</strong> Optional. It is related to the size of the generated image. The value must be "256x256", "512x512", or "1024x1024". if it's not indicated, the default values is "1024x1024"</li>
<li><strong>response_format:</strong> Optional. It is related to the format of how you want the generated images to be returned. Values should be "url" or "b64_json". If it’s not indicated, the default values is "url"</li>
</ul>
<p>Check out the link below for the documentation describing this method: <a href="https://platform.openai.com/docs/api-reference/images/create" target="_blank">https://platform.openai.com/docs/api-reference/images/create</a></p>
<h2>What does iris-openai offer?</h2>
<p>Link: <a href="https://openexchange.intersystems.com/package/iris-openai" target="_blank">https://openexchange.intersystems.com/package/iris-openai</a></p>
<p>This framework is designed to utilize request and response messages with the necessary properties to connect with OpenAi and use such of its methods as Chat, Models, and Images.</p>
<p>You can configure your production in a way that will allow you to use the messaging classes to call a Business Operation that connects to the OpenAI API.</p>
<p>Remember that you must configure the production to indicate the values of the Secret Key and Organization ID as stated above.</p>

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

<p>If you want to create an image, you need to produce a class instance "St.OpenAi.Msg.Images.ImagesRequest" and populate the values of the options to make a new picture.</p>
<p>Example:</p>
<pre class="codeblock-container" idlang="0" lang="ObjectScript" tabsize="4"><code class="language-cls hljs cos"><span class="hljs-keyword">Set</span> image=<span class="hljs-keyword">##class</span>(St.OpenAi.Msg.Images.ImagesRequest).<span class="hljs-built_in">%New</span>()
<span class="hljs-keyword">Set</span> image.Operation = <span class="hljs-string">"generations"</span>
<span class="hljs-keyword">Set</span> image.Prompt = <span class="hljs-string">"Two cats with a hat reading a comic"</span>
<span class="hljs-keyword">Set</span> image.NumOfImages = <span class="hljs-number">2</span>
</code></pre>
<p>When finished, call Business Operation "St.OpenAi.BO.Api.Connect"</p>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qihw37ybvbjt7xuo7bpl.png)
<p><span><strong>Note:</strong> In this case, it will retrieve the link of two created images.</span></p>
<pre class="codeblock-container" idlang="3" lang="JSON" tabsize="4"><code class="language-json hljs">{
    <span class="hljs-attr">"created"</span>: <span class="hljs-number">1683482604</span>,
    <span class="hljs-attr">"data"</span>: [
        {
            <span class="hljs-attr">"url"</span>: <span class="hljs-string">"https://link_of_image_01.png”
        },
        {
            "</span>url<span class="hljs-string">": "</span>https://link_of_image_02.png”
        }
    ]
}
</code></pre>
<p>If it has been indicated that we want to operate a Base64 instead of a link, it will retrieve the following message:</p>
<pre class="codeblock-container" idlang="3" lang="JSON" tabsize="4"><code class="language-json hljs">{
    <span class="hljs-attr">"created"</span>: <span class="hljs-number">1683482604</span>,
    <span class="hljs-attr">"data"</span>: [
        {
            <span class="hljs-attr">"b64_json"</span>: <span class="hljs-string">"iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAADwf7zUAAAAaGVYSWZNTQAqAAAACAACknwAAgAAACkAAAAmkoYAAgAAABgAAABQAAAAAE9wZW5BSS0tZjM5NTgwMmMzNTZiZjNkMDFjMzczOGM2OTAxYWJiNzUAAE1hZGUgd2l0aCBPcGVuQUkgREFMTC1FAAjcEcwAAQAASURBVHgBAAuE9HsBs74g/wHtAAL7AAP6AP8E+/z/BQYAAQH++vz+CQcH+fn+AgMBAwQAAPr++///AwD+BgYGAAIC/fz9//3+AAL7AwEF/wL+9/j9DQ0O/vz/+ff0CQUJAQQF/f/89fj4BwcD/wEAAfv//f4BAQQDAQH9AgIA/f3+AAABAgAA/wH8Af/9AQMGAQIBAvv+/////v/+/wEA/wEAAgMA//sCBAYCAQ”
        },
        {
            "</span>b64_json<span class="hljs-string">": "</span>D99vf7BwcI/v0A/vz9/wH8CQcI+vz8AQL9/vv+CAcF+wH/AwMA9/f8BwUEAwEB9fT+BAcKBAIB//<span class="hljs-number">7</span>//gX5//v8/P7+DgkO+fr6/wD8AP8B/wAC/f4CAwD+/wT+Av79BwcE/Pz7+/sBAAD+AAQE//<span class="hljs-number">8</span>BAP79AgIE///+AQABAv8BAwYA+vkB/v7/AwQE//<span class="hljs-number">7</span>+/Pr6BAYCBgkE/f0B/Pr6AQP+BAED/gMC/fr+AwEC/v/+//<span class="hljs-number">7</span>+CQcH+fz5BAYB9vf9BgQD+/n+BwYK/wD////<span class="hljs-number">9</span>/gD5AwIDAAQE+/j6BAUD//rwAC/fr6+wYEBAQAA/<span class="hljs-number">4</span>B//v6+/<span class="hljs-number">8</span>AAAUDB/L49woGAQMDCfr7+wMCAQMHBPvy+AQJBQD+/wEEAfr3+gIGBgP/Af3++gUFAvz9//<span class="hljs-number">4</span>A/wP/AQIGBPz+/QD7/wEDAgkGCPX29wMCAP4FBwX/+<span class="hljs-number">23</span><span class="hljs-string">"
        }
    ]
}
</span></code></pre>
<h2>What's next?</h2>
<p>After the release of this article, the content of iris-openai will be extended to make it possible to use for Wisper methods and image modification.</p>
<p>A further article will explain how to use these methods and how to include our images or make transcriptions of the audio content.</p>