OpenAI analytics connector – Interactive example
This example allows you to import a pre-configured app into Qlik Cloud, where you can learn how to create chart expressions to control usage of OpenAI data, and to generate responses as users make app selections.
In this example, it is assumed you have a basic understanding of analytics connectors in Qlik Cloud. For clarification on any of the key concepts, refer to the earlier tutorial examples and the remaining OpenAI connector documentation.
This example uses the 'OpenAI Chat Completions API (GPT-3.5, GPT-4) - Rows' configuration of the connector.
Before you start
Considerations
OpenAI applies rate limits to use of its APIs. This means that depending on the connector configuration and OpenAI model that you use, you might be restricted in how many requests you can make within certain time intervals (for example, a minute). For this reason, each chart expression has been commented out until you need to use it, and only one chart expression can likely be evaluated at a time. Refer to OpenAI's documentation on rate limits for more information.
Preparations
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 a separate connection for this example (Interactive_Chat_Completions_GPT_3.5_4_Rows)
For full details, see: Getting started with the OpenAI platform and Create the connections.
Download and upload the app
Download the example app here:
OpenAI Demo App - Complaints Analysis and Handling
Upload the app into the Analytics activity center.
What's in the app?
The app contains a small inline dataset with customer comments on a website for a company offering various products. We will use OpenAI to generate sample responses and severity analysis based on which customer comment we select in the app.
Chart expression exercises
After you have uploaded the app, open it and enable editing of its sheets if needed. You might need to make the sheets private.
The example sheets use the Server side extensions syntax to use analytics connectors in chart expressions. The chart expressions reference fields called ComplaintText and ProductDescription, which appear in the data model.
The goal of these exercise is to show how you can use if statements, in an interactive setting where the chart references fields in your data model, to control how often the chart is calculated. This reduces the costs you incur from OpenAI, since in this case the charts are only calculated when a single selection in a single field is made.
These examples are set up so that only one chart expression can be evaluated at a time. This is due to possible rate limits that might apply to your OpenAI services, preventing evaluation of more than one chart expression simultaneously.
Example 1
The first sheet, Example 1, contains a basic layout of charts showing metrics of a series of customer complaints. The source data includes individual customer complaints, along with dimensions such as the type of product they are complaining about, and a severity score out of 5. There are chart objects to show a selected complaint from the table, and for displaying OpenAI's response to how a customer service department could help the customer with their issue.
Do the following:
-
Open the first sheet, Example 1.
-
In the advanced options edit mode, select the yellow Text & image chart titled OpenAI Possible Starting Points.
-
In the properties panel, under Data, click under Expression to open the expression editor.
-
Remove the // from the start of the expression. This uncomments the expression, allowing the expression to be evaluated. The expression should now be:
if( count(distinct [ComplaintText]) = 1, endpoints.ScriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"Interactive_Chat_Completions_GPT_3.5_4_Rows","column":"choices.message.content"}}', 'draft generic starting points for ways a customer can fix an issue reported with '& [ProductDescription]))
-
Exit edit mode.
-
In the table titled Complaints - Reference Table, select a single cell value in the ComplaintText column.
The OpenAI chart now calculates the response to the selected complaint.
Clear the selection and try selecting another complaint. The chart will refresh with OpenAI's response to the new selection.
Example 2
The second sheet, Example 2, contains similar charts as Example 1. The OpenAI object is now configured to provide general impression analysis of the comment that a user selects. This provides general details on how severe the complaint is, and might be helpful in identifying how to prioritize or start addressing the feedback.
Do the following:
-
If you haven't already done so, add the // back to the expression for the OpenAI object in the Example 1 sheet.
-
Open the second sheet, Example 2.
-
In the advanced options edit mode, select the yellow Text & image chart titled OpenAI Initial Impression.
-
In the properties panel, under Data, click under Expression to open the expression editor.
-
Remove the // from the start of the expression. This uncomments the expression, allowing the expression to be evaluated. The expression should now be:
if( count(distinct [ComplaintText]) = 1, endpoints.ScriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"Interactive_Chat_Completions_GPT_3.5_4_Rows","column":"choices.message.content"}}', 'provide a short description of how severe the following customer complaint is: ' & [ComplaintText]))
-
Exit edit mode.
-
In the table titled Complaints - Reference Table, select a single cell value in the ComplaintText column.
The OpenAI chart now calculates the response to the selected complaint.
Clear the selection and try selecting another complaint. The chart will refresh with OpenAI's response to the new selection.