Python Documentation for Bedrock

Below is the documentation for integrating Reconify with Amazon Bedrock via a Python PIP module.

We currently support the following foundational models: Amazon Titan, AI21 Jurassic, Anthropic Claude, Cohere Command, Meta Llama 2, Mistral, and Stablity Stable Diffusion.

Get started - Create an account

The first step is to create an account at app.reconify.com.

Generate API and APP Keys

In the Reconify console, add an Application to your account. This will generate both an API_KEY and an APP_KEY which will be used in the code below to send data to Reconify.

Python Integration

The easiest way to get started is to use the PIP module.

Install the module

pip install reconify

Import the module

from reconify import reconifyBedrockRuntimeHandler

Initialize the module

Prior to initializing the Reconify module, make sure to import Amazon's boto3 module and initialize Bedrock Runtime.

import boto3
bedrock = boto3.client('bedrock-runtime')

Configure the instance of Reconify passing the Bedrock Runtime instance along with the Reconify API_KEY and APP_KEY created above.

reconifyBedrockRuntimeHandler.config(bedrock,  
appKey = "YOUR_APP_KEY",
apiKey = "YOUR_API_KEY",
)

This is all that is needed for the basic integration. The module takes care of the rest when you call bedrock.invoke_model().

Response handling

When using the Reconify module, the response body from invoke_model() will be converted from  botocore.response.StreamingBody to JSON and saved in the response as "parsedBody." The StreamingBody stream will then be empty.

Optional initialization parameters

You can optionally turn on "debug" mode by passing in "debug = True" in the method above. This will print debug messages to the console.

You can also disable image tracking, by passing in "trackImages = False" in the method.

reconifyBedrockRuntimeHandler.config(bedrock,  
appKey = "YOUR_APP_KEY",
apiKey = "YOUR_API_KEY",
debug = True,
)

Optional methods

You can optionally pass in a user object or session ID to be used in the analytics reporting. The session ID will be used to group interactions together in the same session transcript.

The user object should include a unique userId, the other fields are optional.

reconifyBedrockRuntimeHandler.setUser ({
  "userId": "123",
  "isAuthenticated": 1,
  "firstName": "Francis",
  "lastName": "Smith",
  "email": "",
  "phone": "",
  "gender": "female"
});

The session ID is a simple string.

reconifyBedrockRuntimeHandler.setSession('MySessionId');

Example: Amazon Titan Text

import boto3
from reconify import reconifyBedrockRuntimeHandler

bedrock = boto3.client('bedrock-runtime')

reconifyBedrockRuntimeHandler.config(bedrock,
 appKey = 'Your_App_Key',
 apiKey = 'Your_Api_Key'
)

reconifyBedrockRuntimeHandler.setUser({
 "userId": "12345",
 "firstName": "Jim",
 "lastName": "Smith"
})

response = bedrock.invoke_model(
 modelId="amazon.titan-text-express-v1",
  contentType="application/json",
  accept="application/json",
 body = "{\"inputText\": \"Tell me a cat joke\", \"textGenerationConfig\":{\"maxTokenCount\": 512, \"temperature\": 0.2, \"topP\":0.9, \"stopSequences\":[] }}"
)

Example: Amazon Titan Image

import boto3
from reconify import reconifyBedrockRuntimeHandler

bedrock = boto3.client('bedrock-runtime')

reconifyBedrockRuntimeHandler.config(bedrock,
 appKey = 'Your_App_Key',
 apiKey = 'Your_Api_Key'
)

reconifyBedrockRuntimeHandler.setUser({
 "userId": "12345",
 "firstName": "Jim",
 "lastName": "Smith"
})

response = bedrock.invoke_model(
 modelId="amazon.titan-image-generator-v1",
  contentType="application/json",
  accept="application/json",
 body="{\"textToImageParams\": {\"text\": \"a tuxedo cat\"}, \"taskType\": \"TEXT_IMAGE\", \"imageGenerationConfig\": {\"cfgScale\": 8, \"seed\": 0, \"quality\": \"standard\", \"width\": 1024, \"height\": 1024, \"numberOfImages\": 1}}"
)

Example: AI21 Jurassic

import boto3
from reconify import reconifyBedrockRuntimeHandler

bedrock = boto3.client('bedrock-runtime')

reconifyBedrockRuntimeHandler.config(bedrock,
 appKey = 'Your_App_Key',
 apiKey = 'Your_Api_Key'
)

reconifyBedrockRuntimeHandler.setUser({
 "userId": "12345",
 "firstName": "Jim",
 "lastName": "Smith"
})

response = bedrock.invoke_model(
 modelId="ai21.j2-mid-v1",
  contentType="application/json",
  accept="application/json",
 body="{\"prompt\":\"Tell a cat joke.\", \"maxTokens\":200, \"temperature\":0.7, \"topP\":1, \"stopSequences\":[], \"countPenalty\":{\"scale\":0} ,\"presencePenalty\":{\"scale\":0}, \"frequencyPenalty\":{\"scale\":0}}"
)

Example: Anthropic Claude

import boto3
from reconify import reconifyBedrockRuntimeHandler

bedrock = boto3.client('bedrock-runtime')

reconifyBedrockRuntimeHandler.config(bedrock,
 appKey = 'Your_App_Key',
 apiKey = 'Your_Api_Key'
)

reconifyBedrockRuntimeHandler.setUser({
 "userId": "12345",
 "firstName": "Jim",
 "lastName": "Smith"
})

response = bedrock.invoke_model(
 modelId="anthropic.claude-instant-v1",
  contentType="application/json",
  accept="application/json",
  body = "{\"prompt\":\"\\n\\nHuman: Tell a cat joke.\\n\\nAssistant:\", \"max_tokens_to_sample\":300, \"temperature\":1, \"top_k\":250, \"top_p\":0.999, \"stop_sequences\":[\"\\n\\nHuman:\"], \"anthropic_version\":\"bedrock-2023-05-31\"}"
)

Example: Cohere Command

import boto3
from reconify import reconifyBedrockRuntimeHandler

bedrock = boto3.client('bedrock-runtime')

reconifyBedrockRuntimeHandler.config(bedrock,
 appKey = 'Your_App_Key',
 apiKey = 'Your_Api_Key'
)

reconifyBedrockRuntimeHandler.setUser({
 "userId": "12345",
 "firstName": "Jim",
 "lastName": "Smith"
})

response = bedrock.invoke_model(
 modelId="cohere.command-text-v14",
  contentType="application/json",
  accept="application/json",
 body="{\"prompt\":\"Tell a cat joke.\", \"max_tokens\":400, \"temperature\":0.75, \"p\":0.01, \"k\":0, \"stop_sequences\":[], \"return_likelihoods\":\"NONE\"}"
)

Example: Meta Llama 2

import boto3
from reconify import reconifyBedrockRuntimeHandler

bedrock = boto3.client('bedrock-runtime')

reconifyBedrockRuntimeHandler.config(bedrock,
 appKey = 'Your_App_Key',
 apiKey = 'Your_Api_Key'
)

reconifyBedrockRuntimeHandler.setUser({
 "userId": "12345",
 "firstName": "Jim",
 "lastName": "Smith"
})

response = bedrock.invoke_model(
 modelId="meta.llama2-13b-chat-v1",
  contentType="application/json",
  accept="application/json",
 body="{\"prompt\": \"Tell me a cat joke\", \"max_gen_len\": 512, \"temperature\": 0.2, \"top_p\":0.9 }"
)

Example: Stability Diffusion

import boto3
from reconify import reconifyBedrockRuntimeHandler

bedrock = boto3.client('bedrock-runtime')

reconifyBedrockRuntimeHandler.config(bedrock,
 appKey = 'Your_App_Key',
 apiKey = 'Your_Api_Key'
)

reconifyBedrockRuntimeHandler.setUser({
 "userId": "12345",
 "firstName": "Jim",
 "lastName": "Smith"
})

response = bedrock.invoke_model(
 modelId="stability.stable-diffusion-xl-v0",
  contentType="application/json",
  accept="application/json",
 body = "{\"text_prompts\":[{\"text\":\"A cat drinking boba tea\"}], \"cfg_scale\":10, \"seed\":0, \"steps\":50}"
)