Skip to content

Cohere

Unstable API

0.8.0

@project-lakechain/bedrock-text-processors

TypeScript Icon

The Cohere text processor allows you to leverage machine-learning models provided by Cohere on Amazon Bedrock within your pipelines. Using this construct, you can use prompt engineering techniques to transform text documents, including, text summarization, text translation, information extraction, and more!


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

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

import { S3EventTrigger } from '@project-lakechain/s3-event-trigger';
import { CohereTextProcessor, CohereTextModel } 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');
// Create the S3 event trigger.
const trigger = new S3EventTrigger.Builder()
.withScope(this)
.withIdentifier('Trigger')
.withCacheStorage(cache)
.withBucket(bucket)
.build();
// Transforms input documents using a Cohere model.
const cohere = new CohereTextProcessor.Builder()
.withScope(this)
.withIdentifier('CohereTextProcessor')
.withCacheStorage(cache)
.withSource(source)
.withModel(CohereTextModel.COHERE_COMMAND_TEXT_V14)
.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({
max_tokens: 4096
})
.build();
}
}


You can select the specific Cohere model to use with this middleware using the .withModel API.

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

💁 You can choose amongst the following models — see the Bedrock documentation for more information.

Model Name Model identifier
COHERE_COMMAND_TEXT_V14 cohere.command-text-v14
COHERE_COMMAND_LIGHT_TEXT_V14 cohere.command-light-text-v14
COHERE_COMMAND_R cohere.command-r-v1:0
COHERE_COMMAND_R_PLUS cohere.command-r-plus-v1:0


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 { CohereTextProcessor, CohereTextModel } from '@project-lakechain/bedrock-text-processors';
const cohere = new CohereTextProcessor.Builder()
.withScope(this)
.withIdentifier('CohereTextProcessor')
.withCacheStorage(cache)
.withSource(source)
.withRegion('eu-central-1') // 👈 Alternate region
.withModel(CohereTextModel.COHERE_COMMAND_TEXT_V14)
.withPrompt(prompt)
.build();


You can optionally forward specific parameters to the underlying LLM using the .withModelParameters method. Below is a description of the supported parameters.

💁 See the Bedrock Inference Parameters for more information on the parameters supported by the different models.

Parameter Description Min Max Default
temperature Controls the randomness of the generated text. 0 1 0.3
maxTokens The maximum number of tokens to generate. 1 4096 4096
topP The cumulative probability of the top tokens to sample from. 0.01 0.99 0.75


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




Mime Type Description
text/plain UTF-8 text documents.
text/markdown Markdown documents.
text/csv CSV documents.
text/html HTML documents.
application/x-subrip SubRip subtitles.
text/vtt Web Video Text Tracks (WebVTT) subtitles.
application/json JSON documents.
application/json+scheduler Used by the Scheduler middleware.
Mime Type Description
text/plain UTF-8 text documents.
Type Description
CPU This middleware only supports CPU compute.