Skip to main content Skip to complementary content

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.

Information noteIf you are using the same app to complete this section as the load script section of Example 1, you must comment out, or delete, the script from Example 1 before loading the script below. If you do not do so, the data model will have issues and you might even encounter an error when loading the data.

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 Select data. This opens the Select data wizard

Select data button for an analytics connection in the Data load editor

'Add data' tab of the Data load editor, with icon for 'Select data' highlighted

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.

Select data wizard in Data load editor

'Add data' tab of the Data load editor, with icon for 'Select data' highlighted

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:

SourceTable2: NoConcatenate LOAD RowNo() as RowId, Text Inline [Text top 5 countries by population extract as JSON list ]; [openai]: LOAD * EXTENSION endpoints.ScriptEval('{"RequestType":"endpoint", "endpoint":{"connectionname":"OpenAI Connection for JSON Tables Completions (GPT-3)"}}', SourceTable2{RowId,Text});

 

Information noteFor this exact code to function correctly in your app, your OpenAI connection must also be configured with the same Name value as the connectionname specified in the load script above. For the values used for the purposes of this tutorial, see Create the connections.

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.

Information noteIf you receive an error when loading the script, it is possible that there is a mismatch between the optional, secondary Association Field in the connection configuration and the load script. If you have defined an Association Field in the configuration for your connection, ensure that it is also included in the request script.

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.

Data model created from OpenAI connection

Data model viewer showing the resulting data model, which includes two tables. OpenAI's response to the question we asked is visible when you preview the 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.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!