Example 2: 'OpenAI Completions API (GPT-3) - JSON Tables' configuration
This example teaches you how to use the OpenAI analytics connector with the 'OpenAI Completions API (GPT-3) - JSON Tables' configuration. It guides you through basic exercises showing you how to incorporate the functionality into your data model.
You can also complete this example using the Azure OpenAI connector. When using that connector, you interact with the models through the Microsoft Azure platform, rather than the OpenAI platform.
Before you start
Before you can complete this example, you need to complete the following steps.
-
Create an OpenAI platform account
-
Obtain an OpenAI API key
-
Enable analytic connection functionality in Qlik Cloud
-
Create connections with the applicable connector configurations
Using the OpenAI connector in your load script
Create a new Qlik Sense app, to which you will add a load script to communicate with the OpenAI platform. The tables you load with this script will be accessible in your app's data model.
Follow the steps below. For additional details, see Select and load data from an OpenAI connection.
Creating the table of data to be sent to OpenAI
The first step is to create a table which contains the request you are sending to OpenAI. The rows of data in the request are known as "completions". The text for each item must be contained as rows in a single field (column) in the table. Each row in this column represents text to be completed by the OpenAI API.
There can be a second additional field for the Association Field, but this needs to match the field name specified in the configuration. This is a special field that won’t be sent to OpenAI, but is attached to the responses for the purpose of generating a Qlik data model.
In your app, open the Data load editor and paste the following code into a new script section:
SourceTable2:
NoConcatenate
LOAD
RowNo() as RowId,
Text
Inline
[Text
top 5 countries by population extract as JSON list];
This table contains a field called Text, which contains a single question which we are asking to OpenAI. The RowId field is the Association Field, which will be used in the next step to link the responses to the app's data model.
Creating the OpenAI load statement
The next step is to create another load statement to communicate with OpenAI. The table you load with this statement points to the names of the table and fields you are using to send the request data. In short, a second table is being loaded by the script to provide OpenAI's responses to the request.
To generate this part of the script, you can use the Select data wizard for the connection you are using. This generates a template script including the names of the properties you provide. Alternatively, you can add this table manually into the script editor without using the wizard.
Locate the connection you are using in the Data sources panel of the Data load editor, and click . This opens the Select data wizard
When using the Select data wizard, you need to provide two properties:
-
The name of the Resident Table. This is the table with the questions being sent to OpenAI. In this case, it is SourceTable2.
-
The name of the Data Field. This is the field containing the questions being sent to OpenAI. In this case, it is Text.
Under Tables, click the checkbox for the table called openai.
When you click Insert script, the new table is added to the load script.
With the OpenAI load statement now added, your load script should look like the following:
You are now ready to run the load script.
Loading the data into the app
After the script is complete, click Load data. This performs the communications with OpenAI and inserts the tables into your app's data model.
Open the Data model viewer and notice that the two tables are linked together as expected. In the Preview pane, OpenAI's response to our request item is included in a second table.
You can now generate analytics content using the data that has been loaded into the app. You will not need to refer to the OpenAI connection in any charts you create using this data.