Early Access: The content on this website is provided for informational purposes only in connection with pre-General Availability Qlik Products.
All content is subject to change and is provided without warranty.
Skip to main content Skip to complementary content

Using Anthropic (Amazon Bedrock) connections in visualization expressions

In addition to adding Anthropic 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 Anthropic.

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 Anthropic (Amazon Bedrock) connections in chart expressions for one or a handful of rows of data. Using chart expressions to send large amounts of data to Anthropic 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 Anthropic (Amazon Bedrock) 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 Anthropic.

  • If you are using the Anthropic (Amazon Bedrock) 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.

Prompt engineering

When writing the requests you are sending to a generative AI model (often known as prompts), follow the guidelines specific to the platform you are interacting with. For information about prompt engineering and some guidelines on how to structure your prompts for Amazon Bedrock connectors, see Prompt engineering guidelines.

Constructing prompts for the Anthropic - Claude configuration

You can use the Anthropic - Claude configuration of this connector to connect to the following legacy models:

  • Claude 2.1

  • Claude 2

  • Claude Instant 1.2

Prompts written for this configuration require specific syntax in order to return completions successfully. Here are the requirements:

  • Each prompt must be prefixed with the text Human:

  • Each prompt must be suffixed with the text Assistant:

  • The Human: prefix must have two preceding carriage returns. This is accomplished by adding chr(10) & chr(10) to the expression, before the prefix.

  • The Assistant: suffix must have two preceding carriage returns. This is accomplished by adding chr(10) & chr(10) to the expression, before the suffix.

For information about how to structure and format a Claude prompt, see Constructing a prompt.

For examples, see Quick examples.

Constructing prompts for the Anthropic - Claude (Messages API) configuration

The Anthropic - Claude (Messages API) configuration connects to the Messages API. You can use this configuration to access the following Claude models:

  • Claude 3.5 Sonnet

  • Claude 3 Opus

  • Claude 3 Sonnet

  • Claude 3 Haiku

  • Claude 2.1

  • Claude 2

  • Claude Instant 1.2

Prompts for these models do not require any specific syntax. For examples, see Quick examples.

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 Anthropic (Amazon Bedrock) 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.

  • There are different syntax requirements for each configuration of this connector. See Prompt engineering.

Chart expression examples for the Anthropic (Amazon Bedrock) analytics connector

Connector configuration Expression Summary
Anthropic - Claude endpoints.ScriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"Anthropic_(Amazon_Bedrock)","column":"completion"}}', chr(10) & chr(10) & 'Human:' & 'Translate this text from English to Spanish: The sun was rising over the new tomorrow with a tinge of excitement.' & chr(10) & chr(10) & 'Assistant:')

This statement will return the Anthropic 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.

Anthropic - Claude if(vQuestion <> '', endpoints.scriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"Anthropic_(Amazon_Bedrock)","column":"completion"}}', chr(10) & chr(10) & 'Human:' & vQuestion & chr(10) & chr(10) & ' Assistant:'))

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 the model's response to their input.

Note that given the Anthropic prompt requirements, the user must enter the prefix and suffix manually in their input.

For a similar walkthrough with a different connector, see Exercise: Creating a text input box using a variable.

Anthropic - Claude if(count(distinct [BusinessTopic]) = 1, endpoints.ScriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"Anthropic_(Amazon_Bedrock)","column":"completion"}}', chr(10) & chr(10) & 'Human: write me a sample response from a technical support department for a customer complaint about ' & [BusinessTopic] & chr(10) & chr(10) & ' Assistant:')) 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 the model 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.
Anthropic - Claude (Messages API) endpoints.ScriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"Anthropic_(Amazon_Bedrock)","column":"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 Anthropic 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.

Anthropic - Claude (Messages API) if(vQuestion <> '', endpoints.scriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"Anthropic_(Amazon_Bedrock)","column":"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 the model's response to their input.

Note that given the Anthropic prompt requirements, the user must enter the prefix and suffix manually in their input.

For a similar walkthrough with a different connector, see Exercise: Creating a text input box using a variable.

Anthropic - Claude (Messages API) if(count(distinct [IssueCategory]) = 1, endpoints.ScriptAggrStr('{"RequestType":"endpoint", "endpoint":{"connectionname":"Anthropic_(Amazon_Bedrock)","column":"text"}}', '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 the model 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.

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!