Cohere SDK Cloud Platform Compatibility

To maximize convenience in building on and switching between Cohere-supported environments, we have developed SDKs that seamlessly support whichever backend you choose. This allows you to start developing your project with one backend while maintaining the flexibility to switch, should the need arise.

Note that the code snippets presented in this document should be more than enough to get you started, but if you end up switching from one environment to another there will be some small changes you need to make to how you import and initialize the SDK.

Supported environments

The table below summarizes the environments in which Cohere models can be deployed. You'll notice it contains many links; the links in the "sdk" column take you to Github pages with more information on Cohere's language-specific SDKs, while all the others take you to relevant sections in this document.

Feature support

The most complete set of features is found on the cohere platform, while each of the cloud platforms support subsets of these features. Please consult the platform-specific documentation for more information about the parameters that they support.

FeatureCohere PlatformBedrockSagemakerAzureOCICohere Toolkit
chat_stream
chat
generate_stream
generate
embed
rerank⬜️⬜️⬜️
classify⬜️⬜️⬜️⬜️
summarize⬜️⬜️⬜️⬜️
tokenize✅ (offline)✅ (offline)✅ (offline)✅ (offline)✅ (offline)
detokenize✅ (offline)✅ (offline)✅ (offline)✅ (offline)✅ (offline)
check_api_key

Snippets

Typescript

Cohere Platform

const { CohereClient } = require('cohere-ai');

const cohere = new CohereClient({
  token: '<<apiKey>>',
});

(async () => {
  const response = await cohere.chat({
    chatHistory: [
      { role: 'USER', message: 'Who discovered gravity?' },
      {
        role: 'CHATBOT',
        message: 'The man who is widely credited with discovering gravity is Sir Isaac Newton',
      },
    ],
    message: 'What year was he born?',
    // perform web search before answering the question. You can also use your own custom connector.
    connectors: [{ id: 'web-search' }],
  });

  console.log(response);
})();

Bedrock

const { BedrockClient } = require('cohere-ai');

const cohere = new BedrockClient({
  awsRegion: "us-east-1",
  awsAccessKey: "...",
  awsSecretKey: "...",
  awsSessionToken: "...",
});

(async () => {
  const response = await cohere.chat({
    model: "cohere.command-r-plus-v1:0",
    chatHistory: [
      { role: 'USER', message: 'Who discovered gravity?' },
      {
        role: 'CHATBOT',
        message: 'The man who is widely credited with discovering gravity is Sir Isaac Newton',
      },
    ],
    message: 'What year was he born?',
  });

  console.log(response);
})();

Sagemaker

const { BedrockClient } = require('cohere-ai');

const cohere = new BedrockClient({
  awsRegion: "us-east-1",
  awsAccessKey: "...",
  awsSecretKey: "...",
  awsSessionToken: "...",
});

(async () => {
  const response = await cohere.chat({
    model: "cohere.command-r-plus-v1:0",
    chatHistory: [
      { role: 'USER', message: 'Who discovered gravity?' },
      {
        role: 'CHATBOT',
        message: 'The man who is widely credited with discovering gravity is Sir Isaac Newton',
      },
    ],
    message: 'What year was he born?',
  });

  console.log(response);
})();

Azure

const { CohereClient } = require('cohere-ai');

const cohere = new CohereClient({
  token: "<azure token>",
  base_url="https://Cohere-command-r-plus-phulf-serverless.eastus2.inference.ai.azure.com/v1",
});

(async () => {
  const response = await cohere.chat({
    chatHistory: [
      { role: 'USER', message: 'Who discovered gravity?' },
      {
        role: 'CHATBOT',
        message: 'The man who is widely credited with discovering gravity is Sir Isaac Newton',
      },
    ],
    message: 'What year was he born?',
  });

  console.log(response);
})();

Python

Cohere Platform

import cohere

co = cohere.Client("<<apiKey>>")

response = co.chat(
    chat_history=[
        {"role": "USER", "message": "Who discovered gravity?"},
        {
            "role": "CHATBOT",
            "message": "The man who is widely credited with discovering gravity is Sir Isaac Newton",
        },
    ],
    message="What year was he born?",
    # perform web search before answering the question. You can also use your own custom connector.
    connectors=[{"id": "web-search"}],
)

print(response)

Bedrock

import cohere

co = cohere.BedrockClient(
    aws_region="us-east-1",
    aws_access_key="...",
    aws_secret_key="...",
    aws_session_token="...",
)

response = co.chat(
  	model="cohere.command-r-plus-v1:0",
    chat_history=[
        {"role": "USER", "message": "Who discovered gravity?"},
        {
            "role": "CHATBOT",
            "message": "The man who is widely credited with discovering gravity is Sir Isaac Newton",
        },
    ],
    message="What year was he born?",
)

print(response)

Sagemaker

import cohere

co = cohere.SagemakerClient(
    aws_region="us-east-1",
    aws_access_key="...",
    aws_secret_key="...",
    aws_session_token="...",
)

response = co.chat(
    model="cohere.command-r-plus-v1:0",
    chat_history=[
        {"role": "USER", "message": "Who discovered gravity?"},
        {
            "role": "CHATBOT",
            "message": "The man who is widely credited with discovering gravity is Sir Isaac Newton",
        },
    ],
    message="What year was he born?",
)

print(response)

Go

Cohere Platform

package main

import (
	"context"
	"log"

	cohere "github.com/cohere-ai/cohere-go/v2"
	client "github.com/cohere-ai/cohere-go/v2/client"
)

func main() {
	co := client.NewClient(client.WithToken("<<apiKey>>"))

	resp, err := co.Chat(
		context.TODO(),
		&cohere.ChatRequest{
			ChatHistory: []*cohere.ChatMessage{
				{
					Role:    cohere.ChatMessageRoleUser,
					Message: "Who discovered gravity?",
				},
				{
					Role:    cohere.ChatMessageRoleChatbot,
					Message: "The man who is widely credited with discovering gravity is Sir Isaac Newton",
				}},
			Message: "What year was he born?",
			Connectors: []*cohere.ChatConnector{
				{Id: "web-search"},
			},
		},
	)

	if err != nil {
		log.Fatal(err)
	}

	log.Printf("%+v", resp)
}

Java

Cohere Platform

package chatpost;

import com.cohere.api.Cohere;
import com.cohere.api.requests.ChatRequest;
import com.cohere.api.types.ChatMessage;
import com.cohere.api.types.ChatMessageRole;
import com.cohere.api.types.NonStreamedChatResponse;
import java.util.List;

public class Default {
    public static void main(String[] args) {
        Cohere cohere = Cohere.builder().token("<<apiKey>>").clientName("snippet").build();

        NonStreamedChatResponse response =
                cohere.chat(
                        ChatRequest.builder()
                                .message("What year was he born?")
                                .chatHistory(
                                        List.of(
                                                ChatMessage.builder()
                                                        .role(ChatMessageRole.USER)
                                                        .message("Who discovered gravity?")
                                                        .build(),
                                                ChatMessage.builder()
                                                        .role(ChatMessageRole.CHATBOT)
                                                        .message(
                                                                "The man who is widely credited"
                                                                    + " with discovering gravity is"
                                                                    + " Sir Isaac Newton")
                                                        .build()))
                                .build());

        System.out.println(response);
    }
}