Amazon Bedrock

In an effort to make our language-model capabilities more widely available, we've partnered with a few major platforms to create hosted versions of our offerings.

Here, you'll learn how to use Amazon Bedrock to deploy both the Cohere Command and the Cohere Embed models on the AWS cloud computing platform. The following models are available on Bedrock:

  • Command R
  • Command R+
  • Command Light
  • Command
  • Embed - English
  • Embed - Multilingual

Prerequisites

Here are the steps you'll need to get set up in advance of running Cohere models on Amazon Bedrock.

  • Subscribe to Cohere's models on Amazon Bedrock. For more details, see here.
  • You'll also need to install the AWS Python SDK and some related tooling. Run:
    • pip install boto3. You can find more details about the boto3 library here.
    • pip install cohere-aws (or pip install --upgrade cohere-aws if you need to upgrade). You can also install from source with python setup.py install.
    • For more details, see this GitHub repo and related notebooks.
  • Finally, you'll have to configure your authentication credentials for AWS. This document has more information.

Embeddings

You can use this code to invoke Cohere's embed model on Amazon Bedrock:

import boto3
import json

# Create the AWS client for the Bedrock runtime with boto3
aws_client = boto3.client(service_name="bedrock-runtime")

# Input parameters for embed. In this example we are embedding hacker news post titles.
texts = ["Interesting (Non software) books?",
         "Non-tech books that have helped you grow professionally?",
         "I sold my company last month for $5m. What do I do with the money?",
         "How are you getting through (and back from) burning out?",
         "I made $24k over the last month. Now what?",
         "What kind of personal financial investment do you do?",
         "Should I quit the field of software development?"]
input_type = "clustering"
truncate = "NONE" # optional
model_id = "cohere.embed-english-v3" # or "cohere.embed-multilingual-v3"

# Create the JSON payload for the request
json_params = {
        'texts': texts,
        'truncate': truncate, 
        "input_type": input_type
    }
json_body = json.dumps(json_params)
params = {'body': json_body, 'modelId': model_id,}

# Invoke the model and print the response
result = aws_client.invoke_model(**params)
response = json.loads(result['body'].read().decode())
print(response)

Text Generation

You can use this code to invoke Cohere's Command models on Amazon Bedrock:

import boto3
import json

# Create the AWS client for the Bedrock runtime with boto3
aws_client = boto3.client(service_name="bedrock-runtime")

# Create the JSON payload for the request
json_params = {'prompt': "Write a LinkedIn post about starting a career in tech:"}
params = {'body': json.dumps(json_params),'modelId': 'cohere.command-text-v14',}

# Invoke the model and print the response
result = aws_client.invoke_model(**params)
response = json.loads(result['body'].read().decode())
print(response)