Node Documentation for Bedrock

Below is the documentation for integrating Reconify with Amazon Bedrock via Node NPM 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.

Node Integration

The easiest way to integrate in Node.js is with the NPM module.

Install the NPM module

npm install reconify --save

Import the module

import {reconifyBedrockRuntimeHandler} from 'reconify';

Initialize the module

Initialize the module passing in the keys created above.

const reconify = reconifyBedrockRuntimeHandler({
appKey: process.env.RECONIFY_APP_KEY,
apiKey: process.env.RECONIFY_API_KEY,
})

Add Reconify as a Middleware to the Bedrock Client

After creating the Bedrock Runtime Client, add the Reconify middleware

const client = new BedrockRuntimeClient({
region: "us-west-2"
})

client.middlewareStack.use(reconify.plugin());

This is all that is needed, and the NPM takes care of the rest when you call client.send().

Optional initialization parameters

You can optionally turn on "debug" mode by passing in "debug: true" in the JSON above. This will print debug messages to the console.

You can also disable image tracking, by passing in "trackImages : false" in the JSON.

const reconify = reconifyBedrockRuntimeHandler({
appKey: process.env.RECONIFY_APP_KEY,
apiKey: process.env.RECONIFY_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.

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

The session ID is a simple string.

reconify.setSession('MySessionId');

Example: Amazon Titan Text

import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";
import { reconifyBedrockRuntimeHandler } from 'reconify';

const client = new BedrockRuntimeClient({
  region: "us-west-2",
   credentials: {
     accessKeyId: process.env.AWS_ACCESS_KEY,
     secretAccessKey: process.env.AWS_SECRET_KEY
   }
});

const reconify = reconifyBedrockRuntimeHandler({
  appKey: process.env.RECONIFY_APP_KEY,
  apiKey: process.env.RECONIFY_API_KEY,
});

client.middlewareStack.use(reconify.plugin());

reconify.setUser({
  userId: "12345",
  firstName: "Jane",
  lastName: "Smith"
});

const command = new InvokeModelCommand({
  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\":[] }}"
});

const results = await client.send(command)

Example: Amazon Titan Image

import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";
import { reconifyBedrockRuntimeHandler } from 'reconify';

const client = new BedrockRuntimeClient({
  region: "us-west-2",
   credentials: {
     accessKeyId: process.env.AWS_ACCESS_KEY,
     secretAccessKey: process.env.AWS_SECRET_KEY
   }
});

const reconify = reconifyBedrockRuntimeHandler({
  appKey: process.env.RECONIFY_APP_KEY,
  apiKey: process.env.RECONIFY_API_KEY,
});

client.middlewareStack.use(reconify.plugin());

reconify.setUser({
  userId: "12345",
  firstName: "Jane",
  lastName: "Smith"
});

const command = new InvokeModelCommand({
  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}}"
});

const results = await client.send(command)

Example: AI21 Jurassic

import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";
import { reconifyBedrockRuntimeHandler } from 'reconify';

const client = new BedrockRuntimeClient({
  region: "us-west-2",
   credentials: {
     accessKeyId: process.env.AWS_ACCESS_KEY,
     secretAccessKey: process.env.AWS_SECRET_KEY
   }
});

const reconify = reconifyBedrockRuntimeHandler({
  appKey: process.env.RECONIFY_APP_KEY,
  apiKey: process.env.RECONIFY_API_KEY,
});

client.middlewareStack.use(reconify.plugin());

reconify.setUser({
  userId: "12345",
  firstName: "Jane",
  lastName: "Smith"
});

const command = new InvokeModelCommand({
  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}}"
});

const results = await client.send(command)

Example: Anthropic Claude:

import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";
import { reconifyBedrockRuntimeHandler } from 'reconify';

const client = new BedrockRuntimeClient({
  region: "us-west-2",
   credentials: {
     accessKeyId: process.env.AWS_ACCESS_KEY,
     secretAccessKey: process.env.AWS_SECRET_KEY
   }
});

const reconify = reconifyBedrockRuntimeHandler({
  appKey: process.env.RECONIFY_APP_KEY,
  apiKey: process.env.RECONIFY_API_KEY,
});

client.middlewareStack.use(reconify.plugin());

reconify.setUser({
  userId: "12345",
  firstName: "Jane",
  lastName: "Smith"
});

const command = new InvokeModelCommand({
  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\"}"
});

const results = await client.send(command)

Example: Cohere Command

import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";
import { reconifyBedrockRuntimeHandler } from 'reconify';

const client = new BedrockRuntimeClient({
  region: "us-west-2",
   credentials: {
     accessKeyId: process.env.AWS_ACCESS_KEY,
     secretAccessKey: process.env.AWS_SECRET_KEY
   }
});

const reconify = reconifyBedrockRuntimeHandler({
  appKey: process.env.RECONIFY_APP_KEY,
  apiKey: process.env.RECONIFY_API_KEY,
});

client.middlewareStack.use(reconify.plugin());

reconify.setUser({
  userId: "12345",
  firstName: "Jane",
  lastName: "Smith"
});

const command = new InvokeModelCommand({
  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\"}"
});

const results = await client.send(command)

Example: Meta Llama 2

import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";
import { reconifyBedrockRuntimeHandler } from 'reconify';

const client = new BedrockRuntimeClient({
  region: "us-west-2",
   credentials: {
     accessKeyId: process.env.AWS_ACCESS_KEY,
     secretAccessKey: process.env.AWS_SECRET_KEY
   }
});

const reconify = reconifyBedrockRuntimeHandler({
  appKey: process.env.RECONIFY_APP_KEY,
  apiKey: process.env.RECONIFY_API_KEY,
});

client.middlewareStack.use(reconify.plugin());

reconify.setUser({
  userId: "12345",
  firstName: "Jane",
  lastName: "Smith"
});

const command = new InvokeModelCommand({
  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 }"
});

const results = await client.send(command)

Example: Stability Diffusion

import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";
import { reconifyBedrockRuntimeHandler } from 'reconify';

const client = new BedrockRuntimeClient({
  region: "us-west-2",
   credentials: {
     accessKeyId: process.env.AWS_ACCESS_KEY,
     secretAccessKey: process.env.AWS_SECRET_KEY
   }
});

const reconify = reconifyBedrockRuntimeHandler({
  appKey: process.env.RECONIFY_APP_KEY,
  apiKey: process.env.RECONIFY_API_KEY,
});

client.middlewareStack.use(reconify.plugin());

reconify.setUser({
  userId: "12345",
  firstName: "Jane",
  lastName: "Smith"
});

const command = new InvokeModelCommand({
  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}"
});

const results = await client.send(command)