Skip to main content Skip to complementary content

Using OpenAI connections in visualization expressions

In addition to adding OpenAI questions and answers to your app's data model using the load script, you can also perform these operations directly in a chart in your app in analysis mode. Data for a single field is sent back and forth between you and OpenAI.

In many cases, the code used for a load script connection can be re-purposed for use in a chart, with some differences.

If the use case is based on user input, you can use an analytic connection and corresponding server side extension syntax in your chart expression to create interactive charts that visualize data received from the model endpoints.

Considerations for data request volumes

It is recommended to only use OpenAI connections in chart expressions for one or a handful of rows of data. Using chart expressions to send large amounts of data to OpenAI is not recommended.

It is important to be aware of how many rows you are sending with each request. Errors in configuring the connection in a chart expression can lead to unintentionally large requests (for example, identical rows with the same question due to improper use of the data model in a chart). Before you interact with a third-party platform, make sure you're aware of how the fields used in the chart expression calculation, and their place in the data model, affect your financial agreement and billing arrangements with the third party. Higher usage of the external platform could result in higher costs incurred.

Follow these guidelines to moderate and limit the size of your requests:

  • Only use the connection in a Text & image chart expression. Using the OpenAI connection in a Table object is strongly discouraged, especially if you are adding the expression to a table with one or more dimensions already added.

  • If the chart expression is linked to your app's data model, use an if statement in the expression to only calculate the chart when a single selection is made in the app. This drastically reduces the row count in the requests that your chart will send to OpenAI.

  • If you are using the OpenAI connection to send large requests, use the connection in your load script instead of in a chart expression, so that the data is pre-loaded into your data model.

Creating the chart expression

  1. When editing a visualization, click Expression to enter the expression editor.

  2. In the expression editor, enter an expression in the expression field. The expression must be constructed using the server side extension syntax.

Quick examples

Here you can find sample expressions that can be used when getting started with the OpenAI connector. Consider the following details:

  • Only use the expressions with a Text & image object.

  • Consider adding an if statement to your expressions to control when the chart is re-calculated, and to update depending on user selections in the app.

  • For the expressions to work, you must substitute the connectionname value and any field names to match your own connections and apps.

For additional examples, see Tutorial – Using the OpenAI analytics connector in Qlik Cloud.

Chart expression examples for the OpenAI analytics connector

Connector configuration Expression Summary
OpenAI Completions API (GPT-3) - Rows endpoints.ScriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"OpenAI Connection for Rows Completions (GPT-3)","column":"choices.text"}}', 'Translate this text from english to spanish: The sun was rising over the new tomorrow with a tinge of excitement.')

This statement will return the OpenAI response to a single question, which has been entered into the expression.

Warning note

This expression does not use an if statement, and is not advised to be used if tied to a field in your data model. It is only suitable for use in a standalone expression with a single input cell containing a hard-coded value.

OpenAI Completions API (GPT-3) - Rows if(vQuestion <> '', endpoints.scriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"My_OpenAI_Connection_Rows_3","column":"choices.text"}}',vQuestion))

This is a statement which can be used in a Text & image object linked to a Variable input control. The chart is only calculated when input is entered. When the user types input into the object and presses Enter, the chart displays OpenAI's response to their input.

For a walkthrough, see Exercise: Creating a text input box using a variable.

OpenAI Chat Completions API (GPT-3.5, GPT-4) - Rows if(GetSelectedCount(BusinessTopic)=1, endpoints.ScriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"My_OpenAI_Connection_Rows_3", "column":"choices.message.content"}}', 'What are people saying about ' & 'BusinessTopic'), 'Please select a single value for BusinessTopic.') This is a complex if statement in which the chart is only calculated if the user has selected one value for the field BusinessTopic. The chart only updates if the user has made this selection in the app, and if they have not, the chart will display the following alternate message: Please select a single value for BusinessTopic.
OpenAI Chat Completions API (GPT-3.5, GPT-4) - Rows if(vQuestion <> '', endpoints.scriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"My_OpenAI_Connection_Rows_3","column":"choices.message.content"}}',vQuestion))

This is a statement which can be used in a Text & image object linked to a Variable input control. The chart is only calculated when input is entered. When the user types input into the object and presses Enter, the chart displays OpenAI's response to their input.

For a walkthrough, see Exercise: Creating a text input box using a variable.

OpenAI Chat Completions API (GPT-3.5, GPT-4) - Rows if( count(distinct [IssueCategory]) = 1, endpoints.ScriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"My_OpenAI_Connection_Chat_3_5","column":"choices.message.content"}}', 'write me a sample response from a technical support department for a customer complaint about ' & [IssueCategory])) In this example, the chart is only calculated if one distinct value has been selected from the field IssueCategory in the app's data model. A request is sent to OpenAI requesting a sample response from a technical support department about a general type of issue, a list of which is contained in the IssueCategory field. The chart is updated depending on the value selected by the user.

Detailed examples

For full examples of how to create visualization expressions using your OpenAI connection, see Tutorial – Using the OpenAI analytics connector in Qlik Cloud.

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!