Skip to content

AI21

Unstable API

0.7.0

@project-lakechain/bedrock-text-processors

TypeScript Icon

Bedrock text processors expose different constructs that leverage the Generative AI text models powered by Amazon Bedrock. Using these constructs, you can use prompt engineering techniques to transform text documents, including, text summarization, text translation, information extraction, and more!

The AI21 text processor is part of the Bedrock text processors and allows you to leverage machine-learning models provided by AI21 Labs within your pipelines.


📝 Text Generation

To start using AI21 models in your pipelines, you import the AI21TextProcessor construct in your CDK stack, and specify the specific text model you want to use.

💁 The below example demonstrates how to use the AI21 text processor to summarize input documents uploaded to an S3 bucket.

import { S3EventTrigger } from '@project-lakechain/s3-event-trigger';
import { AI21TextProcessor, AI21TextModel } from '@project-lakechain/bedrock-text-processors';
import { CacheStorage } from '@project-lakechain/core';
class Stack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string) {
const cache = new CacheStorage(this, 'Cache');
// Monitor the S3 bucket for new documents.
const trigger = new S3EventTrigger.Builder()
.withScope(this)
.withIdentifier('Trigger')
.withCacheStorage(cache)
.withBucket(bucket)
.build();
// Transforms input documents using an AI21 model.
const ai21 = new AI21TextProcessor.Builder()
.withScope(this)
.withIdentifier('AI21TextProcessor')
.withCacheStorage(cache)
.withSource(source)
.withModel(AI21TextModel.AI21_J2_ULTRA_V1)
.withPrompt(`
Give a detailed summary of the text with the following constraints:
- Write the summary in the same language as the original text.
- Keep the original meaning, style, and tone of the text in the summary.
`)
.withModelParameters({
maxTokens: 8191
})
.build();
}
}


🤖 Model Selection

You can select the specific AI21 model to use using the .withModel API.

import { AI21TextProcessor, AI21TextModel } from '@project-lakechain/bedrock-text-processors';
const ai21 = new AI21TextProcessor.Builder()
.withScope(this)
.withIdentifier('AI21TextProcessor')
.withCacheStorage(cache)
.withSource(source)
.withModel(AI21TextModel.AI21_J2_MID_V1) // 👈 Specify a model
.withPrompt(prompt)
.build();

You can use the following models : AI21_J2_ULTRA_V1, and AI21_J2_MID_V1.



🌐 Region Selection

You can specify the AWS region in which you want to invoke Amazon Bedrock using the .withRegion API. This can be helpful if Amazon Bedrock is not yet available in your deployment region.

💁 By default, the middleware will use the current region in which it is deployed.

import { AI21TextProcessor, AI21TextModel } from '@project-lakechain/bedrock-text-processors';
const ai21 = new AI21TextProcessor.Builder()
.withScope(this)
.withIdentifier('AI21TextProcessor')
.withCacheStorage(cache)
.withSource(source)
.withRegion('eu-central-1') // 👈 Alternate region
.withModel(AI21TextModel.AI21_J2_MID_V1)
.withPrompt(prompt)
.build();


⚙️ Model Parameters

You can forward specific parameters to text models using the .withModelParameters method. See the Bedrock Inference Parameters for more information on the parameters supported by the different models.



🏗️ Architecture

This middleware is based on a Lambda compute running on an ARM64 architecture, and integrate with Amazon Bedrock to generate text based on the given prompt and input documents.

Architecture



🏷️ Properties


Supported Inputs
Mime TypeDescription
text/plainUTF-8 text documents.
text/markdownMarkdown documents.
text/csvCSV documents.
text/htmlHTML documents.
application/x-subripSubRip subtitles.
text/vttWeb Video Text Tracks (WebVTT) subtitles.
application/jsonJSON documents.
application/json+schedulerUsed by the Scheduler middleware.
Supported Outputs
Mime TypeDescription
text/plainUTF-8 text documents.
Supported Compute Types
TypeDescription
CPUThis middleware only supports CPU compute.