AI/ML: Text and Chatbots (Comprehend, Translate, Lex)

Part 1 of 3 in the AWS managed AI/ML series.

PartTopic
1 (this page)Comprehend, Translate, Lex
2 — Speech, vision, documentsPolly, Transcribe, Rekognition, Textract
3 — Search and recommendationsKendra, Personalize

Console steps come first. Copy-paste AWS CLI lives in <details> blocks.

Prerequisites

  • Signed-in AWS account in a chosen Region (stick to one Region for the whole exercise).
  • IAM user or role with service access (labs often attach broad managed policies; production uses least privilege).
  • Optional sample text: dir/ai-ml/sample-review.txt.

Verify credentials:

aws sts get-caller-identity
export AWS_REGION=us-east-1

Amazon Comprehend

Use when: you need sentiment, entities, key phrases, or language from short text—no model training required.

Console

  1. Open Amazon Comprehend.
  2. Left menu → Real-time analysis.
  3. Analysis type → Built-in.
  4. Paste text (up to 5,000 characters) or use the sample from sample-review.txt.
  5. Choose Analyze.
  6. In Insights, open Sentiment, Entities, and Key phrases tabs.

Reference: Real-time analysis using built-in models.

AWS CLI (Comprehend)

Dominant language:

aws comprehend detect-dominant-language \
  --region "$AWS_REGION" \
  --text "It is a beautiful day in Seattle."

Sentiment (set --language-code to match the text):

aws comprehend detect-sentiment \
  --region "$AWS_REGION" \
  --language-code en \
  --text "It is a beautiful day in Seattle."

Named entities:

aws comprehend detect-entities \
  --region "$AWS_REGION" \
  --language-code en \
  --text "Amazon Web Services is based in Seattle."

Docs: Using the synchronous API.


Amazon Translate

Use when: you need one string or document snippet translated to another language.

Console

  1. Open Amazon Translate.
  2. Choose Real-time translation (wording may vary by console version).
  3. Enter source text, pick Source language and Target language (or Auto for source where supported).
  4. Read the translated output in the panel.

Reference: Getting started with Amazon Translate.

AWS CLI (Translate)
aws translate translate-text \
  --region "$AWS_REGION" \
  --source-language-code en \
  --target-language-code es \
  --text "Hello, how are you?"

Auto-detect source (Region must support Comprehend-backed autodetect):

aws translate translate-text \
  --region "$AWS_REGION" \
  --source-language-code auto \
  --target-language-code fr \
  --text "Good morning"

Docs: translate-text.

Tip: Comprehend → Translate chain: detect language with Comprehend, then call Translate with that code instead of auto.


Amazon Lex (V2)

Use when: you need a conversational bot (intents, slots) with a test UI before wiring mobile or web clients.

Lex has many CLI steps (lexv2-models create bot, locale, build, version, alias). The console is faster for a first bot.

Console

  1. Open Amazon Lex.
  2. Create bot → Traditional → Create a blank bot.
  3. Name the bot, accept or create the IAM role, set Idle session timeout, choose Next.
  4. Add a language (for example English (US)).
  5. Create at least one Intent with sample Utterances (for example intent OrderFlowers, utterance I want to order flowers).
  6. Build the bot locale, then Test in the built-in test window.

Reference: Creating a bot using the Lex V2 console.

AWS CLI (Lex — test only)

After the bot is Built and you have a bot alias, send text to the runtime API. Replace placeholders from the Lex console (Bot ID, Bot alias ID):

aws lexv2-runtime recognize-text \
  --region "$AWS_REGION" \
  --bot-id BOT_ID \
  --bot-alias-id ALIAS_ID \
  --locale-id en_US \
  --session-id demo-session-001 \
  --text "I want to order flowers"

List bots (find IDs):

aws lexv2-models list-bots --region "$AWS_REGION"

Full bot creation via CLI uses create-bot, create-bot-locale, intent APIs, build-bot-locale, create-bot-version, and create-bot-alias. Start from skeletons:

aws lexv2-models create-bot --generate-cli-skeleton input > create-bot.json

Docs: Lex V2 API, recognize-text.


Troubleshooting

SymptomCheck
Comprehend UnsupportedLanguageExceptionUse a supported language code and matching --language-code on entity/sentiment calls.
Translate autodetect failsUse an explicit --source-language-code or a Region that supports autodetect.
Lex test returns AccessDeniedExceptionBot alias must point to a Built locale; runtime needs lex:RecognizeText on the caller.

Next

Continue with Part 2 — Polly, Transcribe, Rekognition, Textract.