Note

The code examples in this section use the Cohere v1 API. The v2 API is not yet supported for cloud deployments and will be coming soon.

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+
  • Embed - English
  • Embed - Multilingual
  • Rerank v3.5

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 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 English v3 model (cohere.embed-english-v3) or Embed Multilingual v3 model (cohere.embed-multilingual-v3) on Amazon Bedrock:

PYTHON
1import cohere
2
3co = cohere.BedrockClient(
4 aws_region="us-east-1",
5 aws_access_key="...",
6 aws_secret_key="...",
7 aws_session_token="...",
8)
9
10# Input parameters for embed. In this example we are embedding hacker news post titles.
11texts = ["Interesting (Non software) books?",
12 "Non-tech books that have helped you grow professionally?",
13 "I sold my company last month for $5m. What do I do with the money?",
14 "How are you getting through (and back from) burning out?",
15 "I made $24k over the last month. Now what?",
16 "What kind of personal financial investment do you do?",
17 "Should I quit the field of software development?"]
18input_type = "clustering"
19truncate = "NONE" # optional
20model_id = "cohere.embed-english-v3" # or "cohere.embed-multilingual-v3"
21
22
23# Invoke the model and print the response
24result = co.embed(
25 model=model_id,
26 input_type=input_type,
27 texts=texts,
28 truncate=truncate) # aws_client.invoke_model(**params)
29
30print(result)

Text Generation

You can use this code to invoke either Command R (cohere.command-r-v1:0), Command R+ (cohere.command-r-plus-v1:0) on Amazon Bedrock:

PYTHON
1import cohere
2
3co = cohere.BedrockClient(
4 aws_region="us-east-1",
5 aws_access_key="...",
6 aws_secret_key="...",
7 aws_session_token="...",
8)
9
10result = co.chat(message="Write a LinkedIn post about starting a career in tech:",
11 model='cohere.command-r-plus-v1:0' # or 'cohere.command-r-v1:0'
12 )
13
14print(result)

Rerank

You can use this code to invoke our latest Rerank models on Bedrock

PYTHON
1import cohere
2
3co = cohere.BedrockClientV2(
4 aws_region="us-west-2", # pick a region where the model is available
5 aws_access_key="...",
6 aws_secret_key="...",
7 aws_session_token="...",
8)
9
10docs = [
11 "Carson City is the capital city of the American state of Nevada.",
12 "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
13 "Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
14 "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",
15 "Capital punishment has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states.",
16]
17
18response = co.rerank(
19 model="cohere.rerank-v3-5:0",
20 query="What is the capital of the United States?",
21 documents=docs,
22 top_n=3,
23)
24
25print(response)